Skip to content

Commit fee6321

Browse files
committed
Fix pairwise drag dragging beyond form boundaries
The pairwise drag implementation would collect all nodes marker as a pair within the local node tree. This means that pairs found and other levels in the tree were considered for dragging and would allow eroneously dragging pairs outside of form boundaries. This fixes the issue by first filtering the matches nodes by those contained within the current form.
1 parent 350ca86 commit fee6321

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lua/nvim-paredit/treesitter/pairs.lua

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local ts_utils = require("nvim-paredit.treesitter.utils")
2+
local ts_forms = require("nvim-paredit.treesitter.forms")
23

34
local M = {}
45

@@ -9,6 +10,10 @@ local M = {}
910
-- matched nodes.
1011
function M.find_pairwise_nodes(target_node, opts)
1112
local root_node = ts_utils.find_local_root(target_node)
13+
local enclosing_form = ts_forms.find_nearest_form(target_node, opts)
14+
if not enclosing_form then
15+
return
16+
end
1217

1318
local bufnr = vim.api.nvim_get_current_buf()
1419
local lang = vim.treesitter.language.get_lang(vim.bo.filetype)
@@ -27,9 +32,11 @@ function M.find_pairwise_nodes(target_node, opts)
2732
for id, node in captures do
2833
if query.captures[id] == "pair" then
2934
if not ts_utils.node_is_comment(node, opts) then
30-
table.insert(pairwise_nodes, node)
31-
if node:equal(target_node) then
32-
found = true
35+
if node:parent():equal(enclosing_form) then
36+
table.insert(pairwise_nodes, node)
37+
if node:equal(target_node) then
38+
found = true
39+
end
3340
end
3441
end
3542
end

tests/nvim-paredit/pair_drag_spec.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ describe("pair dragging ::", function()
4747
})
4848
end)
4949

50+
it("should stop dragging at pair boundaries", function()
51+
prepare_buffer({
52+
"{:entity {|:a 1 :b 2}}",
53+
})
54+
paredit.drag_element_backwards({
55+
dragging = {
56+
auto_drag_pairs = true,
57+
},
58+
})
59+
expect({
60+
"{:entity {|:a 1 :b 2}}",
61+
})
62+
end)
63+
5064
it("should detect various types", function()
5165
expect_all(function()
5266
paredit.drag_element_forwards({ dragging = { auto_drag_pairs = true } })

0 commit comments

Comments
 (0)