feat: Add rename symbol support

This commit is contained in:
simrat39
2021-04-22 15:02:48 -07:00
parent 2448b647d4
commit 4395434980
2 changed files with 41 additions and 0 deletions

View File

@@ -136,6 +136,10 @@ local function setup_keymaps(bufnr)
vim.api.nvim_buf_set_keymap(bufnr, "n", "<C-space>",
":lua require('symbols-outline.hover').show_hover()<Cr>",
{})
-- rename symbol
vim.api.nvim_buf_set_keymap(bufnr, "n", "r",
":lua require('symbols-outline.rename').rename()<Cr>",
{})
-- close outline when escape is pressed
vim.api.nvim_buf_set_keymap(bufnr, "n", "<Esc>", ":bw!<Cr>", {})
end

View File

@@ -0,0 +1,37 @@
local vim = vim
local state = require('symbols-outline').state
local M = {}
local function get_rename_params(node, winnr)
local bufnr = vim.api.nvim_win_get_buf(winnr)
local fn = "file://" .. vim.api.nvim_buf_get_name(bufnr)
return {
textDocument = {uri = fn},
position = {line = node.line, character = node.character},
bufnr = bufnr
}
end
function M.rename()
local current_line = vim.api.nvim_win_get_cursor(state.outline_win)[1]
local node = state.flattened_outline_items[current_line]
local params = get_rename_params(node, state.code_win)
local new_name = vim.fn.input("New Name: ", node.name)
if not new_name or new_name == "" or new_name == node.name then return end
params.newName = new_name
vim.lsp.buf_request(params.bufnr, "textDocument/rename", params,
function(_, _, result)
if result ~= nil then
vim.lsp.util.apply_workspace_edit(result)
end
end)
end
return M