Skip to content

Commit 739da25

Browse files
authored
fix(swap): preserve swapped nodes' range metadata (#550)
1 parent 95933e7 commit 739da25

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

lua/nvim-treesitter/textobjects/shared.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ function M.next_textobject(node, query_string, query_group, same_parent, overlap
332332

333333
local next_node = queries.find_best_match(bufnr, query_string, query_group, filter_function, scoring_function)
334334

335-
return next_node and next_node.node
335+
return next_node and next_node.node, next_node.metadata
336336
end
337337

338338
function M.previous_textobject(node, query_string, query_group, same_parent, overlapping_range_ok, bufnr)
@@ -367,7 +367,7 @@ function M.previous_textobject(node, query_string, query_group, same_parent, ove
367367

368368
local previous_node = queries.find_best_match(bufnr, query_string, query_group, filter_function, scoring_function)
369369

370-
return previous_node and previous_node.node
370+
return previous_node and previous_node.node, previous_node.metadata
371371
end
372372

373373
return M

lua/nvim-treesitter/textobjects/swap.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ local function swap_textobject(query_strings_regex, query_group, direction)
2727
local same_parent = true
2828
for _ = 1, math.abs(direction), step do
2929
local forward = direction > 0
30-
local adjacent =
30+
local adjacent, metadata =
3131
shared.get_adjacent(forward, node, query_string, query_group, same_parent, overlapping_range_ok, bufnr)
32-
ts_utils.swap_nodes(textobject_range, adjacent, bufnr, "yes, set cursor!")
32+
ts_utils.swap_nodes(textobject_range, metadata and metadata.range or adjacent, bufnr, "yes, set cursor!")
3333
end
3434
end
3535

0 commit comments

Comments
 (0)