Skip to content

Commit

Permalink
Fix barf specs on nightly
Browse files Browse the repository at this point in the history
This fixes some inconsistencies with the cursor_behaviour logic during
barf operations that was revealed by the neovim nightly release.

Closes #56
  • Loading branch information
julienvincent committed Mar 10, 2024
1 parent d681ab8 commit 4d93f88
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 31 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: Tests

on:
push:
branches:
- master
pull_request:
workflow_dispatch:

Expand All @@ -11,7 +13,7 @@ jobs:
runs-on: ubuntu-20.04
strategy:
matrix:
channel: [stable] #, nightly]
channel: [stable, nightly]
steps:
- uses: actions/checkout@v2

Expand Down
47 changes: 31 additions & 16 deletions lua/nvim-paredit/api/barfing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
28 changes: 14 additions & 14 deletions tests/nvim-paredit/barf_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -122,7 +122,7 @@ describe("barfing", function()
before_content = "('())",
before_cursor = { 1, 3 },
after_content = "()'()",
after_cursor = { 1, 4 },
after_cursor = { 1, 1 },
},
})
end)
Expand Down Expand Up @@ -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)
Expand All @@ -230,7 +230,7 @@ describe("barfing", function()
paredit.barf_backwards()
expect({
content = { "", ";; comment", "a()" },
cursor = { 1, 0 },
cursor = { 3, 1 },
})

prepare_buffer({
Expand All @@ -240,7 +240,7 @@ describe("barfing", function()
paredit.barf_backwards()
expect({
content = {"a ;; comment", "()"},
cursor = { 1, 1 },
cursor = { 2, 0 },
})
end)

Expand Down Expand Up @@ -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",
Expand All @@ -289,7 +289,7 @@ describe("barfing", function()
before_content = "('())",
before_cursor = { 1, 3 },
after_content = "'()()",
after_cursor = { 1, 2 },
after_cursor = { 1, 4 },
},
})
end)
Expand Down

0 comments on commit 4d93f88

Please sign in to comment.