Skip to content

Commit

Permalink
Pilfer ts_util's swap_nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronik committed Dec 18, 2024
1 parent 4982b24 commit a2fffb5
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions lua/treewalker/ops.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ end
function M.jump(row, node)
vim.cmd("normal! m'") -- Add originating node to jump list
vim.api.nvim_win_set_cursor(0, { row, 0 })
vim.cmd("normal! ^") -- Jump to start of line
vim.cmd("normal! ^") -- Jump to start of line
if require("treewalker").opts.highlight then
node = nodes.get_highest_coincident(node)
local range = nodes.range(node)
Expand All @@ -63,18 +63,35 @@ function M.jump(row, node)
end
end

-- https://github.com/nvim-treesitter/nvim-treesitter/blob/981ca7e353da6ea69eaafe4348fda5e800f9e1d8/lua/nvim-treesitter/ts_utils.lua#L388
-- (ts_utils.swap_nodes)
---@param rows1 [integer, integer] -- [start row, end row]
---@param rows2 [integer, integer] -- [start row, end row]
function M.swap(rows1, rows2)
local s1, e1, s2, e2 = rows1[1], rows1[2], rows2[1], rows2[2]
local text1 = lines.get_lines(s1, e1)
local text2 = lines.get_lines(s2, e2)

-- Get lines from both the source and the destination positions
local lines1 = lines.get_lines(s1, e1)
local lines2 = lines.get_lines(s2, e2)
---@type lsp.Range
local range1 = {
start = { line = s1 - 1, character = 0 },
["end"] = { line = e1 - 1, character = 0 } -- end is reserved
}

-- Set the lines in the swapped positions
lines.set_lines(s1, e1, lines2)
lines.set_lines(s2, e2, lines1)
---@type lsp.Range
local range2 = {
start = { line = s2 - 1, character = 0 },
["end"] = { line = e2 - 1, character = 0 }
}

-- https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textEdit
---@type lsp.TextEdit
local edit1 = { range = range1, newText = table.concat(text2, "\n") }
---@type lsp.TextEdit
local edit2 = { range = range2, newText = table.concat(text1, "\n") }

local bufnr = vim.api.nvim_get_current_buf()
vim.lsp.util.apply_text_edits({ edit1, edit2 }, bufnr, "utf-8")
end

return M

0 comments on commit a2fffb5

Please sign in to comment.