mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-05 04:50:03 -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)
|
||||
end
|
||||
|
||||
for _, node in ipairs(nodes_to_rename) do
|
||||
local start_row, start_col, _, end_col = node:range()
|
||||
local line = api.nvim_buf_get_lines(bufnr, start_row, start_row + 1, false)[1]
|
||||
local edits = {}
|
||||
|
||||
if line then
|
||||
local new_line = line:sub(1, start_col) .. new_name .. line:sub(end_col + 1, -1)
|
||||
api.nvim_buf_set_lines(bufnr, start_row, start_row + 1, false, { new_line })
|
||||
end
|
||||
for _, node in ipairs(nodes_to_rename) do
|
||||
local lsp_range = ts_utils.node_to_lsp_range(node)
|
||||
local text_edit = { range = lsp_range, newText = new_name }
|
||||
table.insert(edits, text_edit)
|
||||
end
|
||||
vim.lsp.util.apply_text_edits(edits, bufnr)
|
||||
end
|
||||
|
||||
function M.attach(bufnr)
|
||||
|
|
|
|||
|
|
@ -186,4 +186,12 @@ function M.is_in_node_range(node, line, col)
|
|||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue