From 34551231d2eb00f611954537198dfa633ddfb5c8 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Fri, 8 May 2020 20:24:16 +0200 Subject: [PATCH] Refine M.swap_nodes --- lua/nvim-treesitter/node_movement.lua | 6 +++--- lua/nvim-treesitter/utils.lua | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lua/nvim-treesitter/node_movement.lua b/lua/nvim-treesitter/node_movement.lua index 46e2577d8..30ac9e1c3 100644 --- a/lua/nvim-treesitter/node_movement.lua +++ b/lua/nvim-treesitter/node_movement.lua @@ -69,10 +69,10 @@ M.do_node_movement = function(kind, move_node) node_start_to_vim(destination_node) if move_node 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() - if new_destination_range then - local new_destination_node = utils.node_from_lsp_range(root, new_destination_range) + if dst_range then + local new_destination_node = utils.node_from_lsp_range(root, dst_range) M.current_node[buf] = new_destination_node or current_node end end diff --git a/lua/nvim-treesitter/utils.lua b/lua/nvim-treesitter/utils.lua index 0aa221b7a..debee3257 100644 --- a/lua/nvim-treesitter/utils.lua +++ b/lua/nvim-treesitter/utils.lua @@ -130,7 +130,7 @@ function M.replace_node_text(buf, node, replacement_lines) 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 source first 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 destination_text = M.get_node_text(destination) - local dst_range + local src_range, dst_range 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) - return + return elseif src_end <= dst_start then 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 - return dst_range + return src_range, dst_range end