diff --git a/lua/nvim-treesitter/ts_utils.lua b/lua/nvim-treesitter/ts_utils.lua index 3d804ccba..bfe6170b1 100644 --- a/lua/nvim-treesitter/ts_utils.lua +++ b/lua/nvim-treesitter/ts_utils.lua @@ -400,12 +400,12 @@ function M.swap_nodes(node_or_range1, node_or_range2, bufnr, cursor_to_second) local line_delta = 0 if range1["end"].line < range2.start.line - or (range1["end"].line == range2.start.line and range1["end"].character < range2.start.character) + or (range1["end"].line == range2.start.line and range1["end"].character <= range2.start.character) then line_delta = #text2 - #text1 end - if range1["end"].line == range2.start.line and range1["end"].character < range2.start.character then + if range1["end"].line == range2.start.line and range1["end"].character <= range2.start.character then if line_delta ~= 0 then --- why? --correction_after_line_change = -range2.start.character diff --git a/tests/unit/ts_utils_spec.lua b/tests/unit/ts_utils_spec.lua index a3b334413..3cc7d4dc8 100644 --- a/tests/unit/ts_utils_spec.lua +++ b/tests/unit/ts_utils_spec.lua @@ -106,3 +106,43 @@ describe("update_selection", function() ) end) end) + +describe("swap_nodes", function() + local function swap(case) + vim.api.nvim_buf_set_lines(0, 0, -1, false, case.lines) + vim.opt.filetype = case.filetype + local a = vim.treesitter.get_node_at_pos(0, case.a[1], case.a[2], {}) + local b = vim.treesitter.get_node_at_pos(0, case.b[1], case.b[2], {}) + tsutils.swap_nodes(a, b, 0, true) + end + + it("works on adjacent nodes", function() + swap { + filetype = "python", + lines = { "print(1)" }, + a = { 0, 0 }, + b = { 0, 5 }, + } + + it("swaps text", function() end) + assert.same(vim.api.nvim_buf_get_lines(0, 0, -1, false), { "(1)print" }) + + it("moves the cursor", function() end) + assert.same(vim.api.nvim_win_get_cursor(0), { 1, 3 }) + end) + + it("works with multiline nodes", function() + swap { + filetype = "lua", + lines = { "x = { [[", "]], [[", ".....]]}" }, + a = { 0, 6 }, + b = { 1, 4 }, + } + + it("swaps text", function() end) + assert.same(vim.api.nvim_buf_get_lines(0, 0, -1, false), { "x = { [[", ".....]], [[", "]]}" }) + + it("moves the cursor", function() end) + assert.same(vim.api.nvim_win_get_cursor(0), { 2, 9 }) + end) +end)