diff options
| author | Steven Sojka <steelsojka@users.noreply.github.com> | 2020-08-17 11:39:22 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-17 11:39:22 -0500 |
| commit | b796f0725d913cba0a292bb7b750f0403b07c94a (patch) | |
| tree | 6645909fcf422b0d77f513514ece4a539135aba4 /lua/nvim-treesitter/ts_utils.lua | |
| parent | 0b3cd6c131a5ee9e3377c6b348c2b8995a848967 (diff) | |
| parent | 52168114594d791a3ae6092ab2489758da7b3ae8 (diff) | |
Merge pull request #305 from theHamsta/textobjects-submodules
Textobjects submodules
Diffstat (limited to 'lua/nvim-treesitter/ts_utils.lua')
| -rw-r--r-- | lua/nvim-treesitter/ts_utils.lua | 67 |
1 files changed, 59 insertions, 8 deletions
diff --git a/lua/nvim-treesitter/ts_utils.lua b/lua/nvim-treesitter/ts_utils.lua index 0a5cbc60..52fdd0fc 100644 --- a/lua/nvim-treesitter/ts_utils.lua +++ b/lua/nvim-treesitter/ts_utils.lua @@ -1,6 +1,7 @@ local api = vim.api local parsers = require'nvim-treesitter.parsers' +local utils = require'nvim-treesitter.utils' local M = {} @@ -13,7 +14,7 @@ function M.get_node_text(node, bufnr) if not node then return {} end -- We have to remember that end_col is end-exclusive - local start_row, start_col, end_row, end_col = node:range() + local start_row, start_col, end_row, end_col = M.get_node_range(node) if start_row ~= end_row then local lines = api.nvim_buf_get_lines(bufnr, start_row, end_row+1, false) @@ -131,12 +132,7 @@ end -- Set visual selection to node function M.update_selection(buf, node) - local start_row, start_col, end_row, end_col - if type(node) == 'table' then - start_row, start_col, end_row, end_col = unpack(node) - else - start_row, start_col, end_row, end_col = node:range() - end + local start_row, start_col, end_row, end_col = M.get_node_range(node) if end_row == vim.fn.line('$') then end_col = #vim.fn.getline('$') @@ -187,8 +183,16 @@ function M.is_in_node_range(node, line, col) end end +function M.get_node_range(node_or_range) + if type(node_or_range) == 'table' then + return unpack(node_or_range) + else + return node_or_range:range() + end +end + function M.node_to_lsp_range(node) - local start_line, start_col, end_line, end_col = node:range() + local start_line, start_col, end_line, end_col = M.get_node_range(node) local rtn = {} rtn.start = { line = start_line, character = start_col } rtn['end'] = { line = end_line, character = end_col } @@ -225,4 +229,51 @@ function M.memoize_by_buf_tick(fn, bufnr_fn) end end +function M.swap_nodes(node_or_range1, node_or_range2, bufnr, cursor_to_second) + if not node_or_range1 or not node_or_range2 then return end + local range1 = M.node_to_lsp_range(node_or_range1) + local range2 = M.node_to_lsp_range(node_or_range2) + + local text1 = M.get_node_text(node_or_range1) + local text2 = M.get_node_text(node_or_range2) + + local edit1 = { range = range1, newText = table.concat(text2, '\n') } + local edit2 = { range = range2, newText = table.concat(text1, '\n') } + vim.lsp.util.apply_text_edits({edit1, edit2}, bufnr) + + if cursor_to_second then + utils.set_jump() + + local char_delta = 0 + 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) then + line_delta = #text2 - #text1 + end + + 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 + --text_now_before_range2 = #(text2[#text2]) + --space_between_ranges = range2.start.character - range1["end"].character + --char_delta = correction_after_line_change + text_now_before_range2 + space_between_ranges + --- Equivalent to: + char_delta = #(text2[#text2]) - range1["end"].character + + -- add range1.start.character if last line of range1 (now text2) does not start at 0 + if range1.start.line == range2.start.line + line_delta then + char_delta = char_delta + range1.start.character + end + else + char_delta = #(text2[#text2]) - #(text1[#text1]) + end + end + + api.nvim_win_set_cursor(api.nvim_get_current_win(), + {range2.start.line + 1 + line_delta, + range2.start.character + char_delta}) + end +end + return M |
