Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pairwise drag dragging beyond form boundaries #79

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lua/nvim-paredit/treesitter/pairs.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local ts_utils = require("nvim-paredit.treesitter.utils")
local ts_forms = require("nvim-paredit.treesitter.forms")

local M = {}

Expand All @@ -9,6 +10,10 @@ local M = {}
-- matched nodes.
function M.find_pairwise_nodes(target_node, opts)
local root_node = ts_utils.find_local_root(target_node)
local enclosing_form = ts_forms.find_nearest_form(target_node, opts)
if not enclosing_form then
return
end

local bufnr = vim.api.nvim_get_current_buf()
local lang = vim.treesitter.language.get_lang(vim.bo.filetype)
Expand All @@ -27,9 +32,11 @@ function M.find_pairwise_nodes(target_node, opts)
for id, node in captures do
if query.captures[id] == "pair" then
if not ts_utils.node_is_comment(node, opts) then
table.insert(pairwise_nodes, node)
if node:equal(target_node) then
found = true
if node:parent():equal(enclosing_form) then
table.insert(pairwise_nodes, node)
if node:equal(target_node) then
found = true
end
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions tests/nvim-paredit/pair_drag_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ describe("pair dragging ::", function()
})
end)

it("should stop dragging at pair boundaries", function()
prepare_buffer({
"{:entity {|:a 1 :b 2}}",
})
paredit.drag_element_backwards({
dragging = {
auto_drag_pairs = true,
},
})
expect({
"{:entity {|:a 1 :b 2}}",
})
end)

it("should detect various types", function()
expect_all(function()
paredit.drag_element_forwards({ dragging = { auto_drag_pairs = true } })
Expand Down
Loading