mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-19 03:40:04 -04:00
Fix #206: Use lsp.util.apply_text_edits for smart_rename
This commit is contained in:
parent
23a16ab605
commit
16af10999c
2 changed files with 14 additions and 7 deletions
|
|
@ -35,15 +35,14 @@ function M.smart_rename(bufnr)
|
||||||
table.insert(nodes_to_rename, definition)
|
table.insert(nodes_to_rename, definition)
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, node in ipairs(nodes_to_rename) do
|
local edits = {}
|
||||||
local start_row, start_col, _, end_col = node:range()
|
|
||||||
local line = api.nvim_buf_get_lines(bufnr, start_row, start_row + 1, false)[1]
|
|
||||||
|
|
||||||
if line then
|
for _, node in ipairs(nodes_to_rename) do
|
||||||
local new_line = line:sub(1, start_col) .. new_name .. line:sub(end_col + 1, -1)
|
local lsp_range = ts_utils.node_to_lsp_range(node)
|
||||||
api.nvim_buf_set_lines(bufnr, start_row, start_row + 1, false, { new_line })
|
local text_edit = { range = lsp_range, newText = new_name }
|
||||||
end
|
table.insert(edits, text_edit)
|
||||||
end
|
end
|
||||||
|
vim.lsp.util.apply_text_edits(edits, bufnr)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.attach(bufnr)
|
function M.attach(bufnr)
|
||||||
|
|
|
||||||
|
|
@ -186,4 +186,12 @@ function M.is_in_node_range(node, line, col)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M.node_to_lsp_range(node)
|
||||||
|
local start_line, start_col, end_line, end_col = node:range()
|
||||||
|
local rtn = {}
|
||||||
|
rtn.start = { line = start_line, character = start_col }
|
||||||
|
rtn['end'] = { line = end_line, character = end_col }
|
||||||
|
return rtn
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue