diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 905134d..5ac7ce8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,6 +2,8 @@ name: Tests on: push: + branches: + - master pull_request: workflow_dispatch: @@ -11,7 +13,7 @@ jobs: runs-on: ubuntu-20.04 strategy: matrix: - channel: [stable] #, nightly] + channel: [stable, nightly] steps: - uses: actions/checkout@v2 diff --git a/lua/nvim-paredit/api/barfing.lua b/lua/nvim-paredit/api/barfing.lua index 4d0c177..2eb4435 100644 --- a/lua/nvim-paredit/api/barfing.lua +++ b/lua/nvim-paredit/api/barfing.lua @@ -7,6 +7,28 @@ local langs = require("nvim-paredit.lang") local M = {} +local function position_cursor_according_to_edge(params) + if params.cursor_behaviour == "remain" then + return + end + + local cursor_behaviour = params.cursor_behaviour or "auto" + local current_pos = vim.api.nvim_win_get_cursor(0) + + local cursor_out_of_bounds + if params.reversed then + cursor_out_of_bounds = common.compare_positions(params.edge_pos, current_pos) == 1 + else + cursor_out_of_bounds = common.compare_positions(current_pos, params.edge_pos) == 1 + end + + if cursor_behaviour == "follow" then + vim.api.nvim_win_set_cursor(0, params.edge_pos) + elseif cursor_behaviour == "auto" and cursor_out_of_bounds then + vim.api.nvim_win_set_cursor(0, params.edge_pos) + end +end + function M.barf_forwards(opts) opts = opts or {} @@ -72,14 +94,10 @@ function M.barf_forwards(opts) { text } ) - local cursor_behaviour = opts.cursor_behaviour or config.config.cursor_behaviour - if cursor_behaviour == "auto" or cursor_behaviour == "follow" then - local cursor_pos = vim.api.nvim_win_get_cursor(0) - local cursor_out_of_bounds = common.compare_positions({ cursor_pos[1] - 1, cursor_pos[2] }, end_pos) == 1 - if cursor_behaviour == "follow" or cursor_out_of_bounds then - vim.api.nvim_win_set_cursor(0, { end_pos[1] + 1, end_pos[2] }) - end - end + position_cursor_according_to_edge({ + cursor_behaviour = opts.cursor_behaviour or config.config.cursor_behaviour, + edge_pos = { end_pos[1] + 1, end_pos[2] }, + }) local event = { type = "barf-forwards", @@ -150,14 +168,11 @@ function M.barf_backwards(opts) {} ) - local cursor_behaviour = opts.cursor_behaviour or config.config.cursor_behaviour - if cursor_behaviour == "auto" or cursor_behaviour == "follow" then - local cursor_pos = vim.api.nvim_win_get_cursor(0) - local cursor_out_of_bounds = common.compare_positions(end_pos, { cursor_pos[1] - 1, cursor_pos[2] }) == 1 - if cursor_behaviour == "follow" or cursor_out_of_bounds then - vim.api.nvim_win_set_cursor(0, { end_pos[1] + 1, end_pos[2] }) - end - end + position_cursor_according_to_edge({ + reversed = true, + cursor_behaviour = opts.cursor_behaviour or config.config.cursor_behaviour, + edge_pos = { end_pos[1] + 1, end_pos[2] }, + }) local event = { type = "barf-backwards", diff --git a/tests/nvim-paredit/barf_spec.lua b/tests/nvim-paredit/barf_spec.lua index 392004c..9c58eae 100644 --- a/tests/nvim-paredit/barf_spec.lua +++ b/tests/nvim-paredit/barf_spec.lua @@ -4,11 +4,11 @@ local prepare_buffer = require("tests.nvim-paredit.utils").prepare_buffer local expect_all = require("tests.nvim-paredit.utils").expect_all local expect = require("tests.nvim-paredit.utils").expect -describe("barfing", function() +describe("barfing ::", function() vim.api.nvim_buf_set_option(0, "filetype", "clojure") describe("barfing forwards", function() - it("should barf different form types", function() + it("should barf different form types -", function() expect_all(paredit.barf_forwards, { { "list", @@ -122,7 +122,7 @@ describe("barfing", function() before_content = "('())", before_cursor = { 1, 3 }, after_content = "()'()", - after_cursor = { 1, 4 }, + after_cursor = { 1, 1 }, }, }) end) @@ -175,49 +175,49 @@ describe("barfing", function() end) describe("barfing backwards", function() - it("should barf different form types", function() + it("should barf different form types -", function() expect_all(paredit.barf_backwards, { { "list", before_content = "(a)", before_cursor = { 1, 1 }, after_content = "a()", - after_cursor = { 1, 1 }, + after_cursor = { 1, 2 }, }, { "vector", before_content = "[a]", before_cursor = { 1, 1 }, after_content = "a[]", - after_cursor = { 1, 1 }, + after_cursor = { 1, 2 }, }, { "quoted list", before_content = "`(a)", before_cursor = { 1, 2 }, after_content = "a`()", - after_cursor = { 1, 2 }, + after_cursor = { 1, 3 }, }, { "quoted list", before_content = "'(a)", before_cursor = { 1, 2 }, after_content = "a'()", - after_cursor = { 1, 2 }, + after_cursor = { 1, 3 }, }, { "anon fn", before_content = "#(a)", before_cursor = { 1, 2 }, after_content = "a#()", - after_cursor = { 1, 2 }, + after_cursor = { 1, 3 }, }, { "set", before_content = "#{a}", before_cursor = { 1, 2 }, after_content = "a#{}", - after_cursor = { 1, 2 }, + after_cursor = { 1, 3 }, }, }) end) @@ -230,7 +230,7 @@ describe("barfing", function() paredit.barf_backwards() expect({ content = { "", ";; comment", "a()" }, - cursor = { 1, 0 }, + cursor = { 3, 1 }, }) prepare_buffer({ @@ -240,7 +240,7 @@ describe("barfing", function() paredit.barf_backwards() expect({ content = {"a ;; comment", "()"}, - cursor = { 1, 1 }, + cursor = { 2, 0 }, }) end) @@ -275,7 +275,7 @@ describe("barfing", function() }) end) - it("should recursively barf the next sibling", function() + it("should recursively barf the next sibling in a", function() expect_all(paredit.barf_backwards, { { "double nested list", @@ -289,7 +289,7 @@ describe("barfing", function() before_content = "('())", before_cursor = { 1, 3 }, after_content = "'()()", - after_cursor = { 1, 2 }, + after_cursor = { 1, 4 }, }, }) end)