mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-18 03:10:04 -04:00
Refine M.swap_nodes
This commit is contained in:
parent
9dcb96b439
commit
34551231d2
2 changed files with 9 additions and 9 deletions
|
|
@ -69,10 +69,10 @@ M.do_node_movement = function(kind, move_node)
|
||||||
node_start_to_vim(destination_node)
|
node_start_to_vim(destination_node)
|
||||||
if move_node then
|
if move_node then
|
||||||
if kind ~= M.NodeMovementKind.down then
|
if kind ~= M.NodeMovementKind.down then
|
||||||
local new_destination_range = utils.swap_nodes(buf, current_node, destination_node)
|
local _, dst_range = utils.swap_nodes(buf, current_node, destination_node)
|
||||||
local root = parsers.get_parser():parse():root()
|
local root = parsers.get_parser():parse():root()
|
||||||
if new_destination_range then
|
if dst_range then
|
||||||
local new_destination_node = utils.node_from_lsp_range(root, new_destination_range)
|
local new_destination_node = utils.node_from_lsp_range(root, dst_range)
|
||||||
M.current_node[buf] = new_destination_node or current_node
|
M.current_node[buf] = new_destination_node or current_node
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -130,7 +130,7 @@ function M.replace_node_text(buf, node, replacement_lines)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
--- Swaps the contents of two nodes returning new range of destination (LSP range)
|
--- Swaps the contents of two nodes returning new ranges (LSP ranges)
|
||||||
-- @param buf buffer number
|
-- @param buf buffer number
|
||||||
-- @param source first node
|
-- @param source first node
|
||||||
-- @param destination second node
|
-- @param destination second node
|
||||||
|
|
@ -148,17 +148,17 @@ function M.swap_nodes(buf, source, destination)
|
||||||
local source_text = M.get_node_text(source)
|
local source_text = M.get_node_text(source)
|
||||||
local destination_text = M.get_node_text(destination)
|
local destination_text = M.get_node_text(destination)
|
||||||
|
|
||||||
local dst_range
|
local src_range, dst_range
|
||||||
|
|
||||||
if dst_end <= src_start then
|
if dst_end <= src_start then
|
||||||
M.replace_node_text(buf, source, destination_text)
|
src_range = M.replace_node_text(buf, source, destination_text)
|
||||||
dst_range = M.replace_node_text(buf, destination, source_text)
|
dst_range = M.replace_node_text(buf, destination, source_text)
|
||||||
return
|
return
|
||||||
elseif src_end <= dst_start then
|
elseif src_end <= dst_start then
|
||||||
dst_range = M.replace_node_text(buf, destination, source_text)
|
dst_range = M.replace_node_text(buf, destination, source_text)
|
||||||
M.replace_node_text(buf, source, destination_text)
|
src_range = M.replace_node_text(buf, source, destination_text)
|
||||||
end
|
end
|
||||||
return dst_range
|
return src_range, dst_range
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue