feat(lsp): remove custom rename function
This commit is contained in:
+1
-1
@@ -35,7 +35,7 @@ function M:init(server, bufnr)
|
|||||||
{ mode = { "n", "i" }, lhs = "<C-k>", rhs = vim.lsp.buf.hover },
|
{ mode = { "n", "i" }, lhs = "<C-k>", rhs = vim.lsp.buf.hover },
|
||||||
{ mode = { "n", "i" }, lhs = "<C-j>", rhs = vim.lsp.buf.signature_help },
|
{ mode = { "n", "i" }, lhs = "<C-j>", rhs = vim.lsp.buf.signature_help },
|
||||||
{ mode = { "n", "i" }, lhs = "<C-h>", rhs = vim.lsp.buf.document_highlight },
|
{ mode = { "n", "i" }, lhs = "<C-h>", rhs = vim.lsp.buf.document_highlight },
|
||||||
{ mode = { "n" }, lhs = "<leader>lr", rhs = server.ca_rename },
|
{ mode = { "n" }, lhs = "<leader>lr", rhs = vim.lsp.buf.rename },
|
||||||
{ mode = { "n" }, lhs = "<leader>la", rhs = vim.lsp.buf.code_action },
|
{ mode = { "n" }, lhs = "<leader>la", rhs = vim.lsp.buf.code_action },
|
||||||
{ mode = { "n", "x" }, lhs = "<leader>lf", rhs = vim.lsp.buf.format },
|
{ mode = { "n", "x" }, lhs = "<leader>lf", rhs = vim.lsp.buf.format },
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -98,84 +98,6 @@ function M.validate(name, config)
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Rename Code Action
|
|
||||||
function M.ca_rename()
|
|
||||||
local ts_utils = utils.try_require("nvim-treesitter.ts_utils")
|
|
||||||
if not ts_utils then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local identifier_types = {
|
|
||||||
"IDENTIFIER",
|
|
||||||
"identifier",
|
|
||||||
"variable_name",
|
|
||||||
"word",
|
|
||||||
}
|
|
||||||
local node = ts_utils.get_node_at_cursor()
|
|
||||||
if not node or not vim.list_contains(identifier_types, node:type()) then
|
|
||||||
utils.info("Only identifiers may be renamed")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
vim.lsp.buf.document_highlight()
|
|
||||||
|
|
||||||
local old = vim.fn.expand("<cword>")
|
|
||||||
local buf = vim.api.nvim_create_buf(false, true)
|
|
||||||
local min_width = 10
|
|
||||||
local max_width = 50
|
|
||||||
local default_width = math.min(max_width, math.max(min_width, vim.str_utfindex(old) + 1))
|
|
||||||
local row, col, _, _ = node:range()
|
|
||||||
local win = vim.api.nvim_open_win(buf, true, {
|
|
||||||
relative = "win",
|
|
||||||
anchor = "NW",
|
|
||||||
width = default_width,
|
|
||||||
height = 1,
|
|
||||||
bufpos = { row, col - 1 },
|
|
||||||
focusable = true,
|
|
||||||
zindex = 50,
|
|
||||||
style = "minimal",
|
|
||||||
border = "rounded",
|
|
||||||
title = "Rename",
|
|
||||||
title_pos = "center",
|
|
||||||
})
|
|
||||||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { old })
|
|
||||||
|
|
||||||
vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI", "TextChangedP" }, {
|
|
||||||
buffer = buf,
|
|
||||||
callback = function()
|
|
||||||
local win_width = vim.api.nvim_win_get_width(win)
|
|
||||||
local content = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
|
|
||||||
if #content > 0 then
|
|
||||||
local cwidth = vim.str_utfindex(content[1] or "") + 1
|
|
||||||
local new_width = math.min(max_width, math.max(min_width, cwidth))
|
|
||||||
if new_width ~= win_width then
|
|
||||||
vim.api.nvim_win_set_width(win, new_width)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
|
|
||||||
vim.keymap.set({ "n", "i", "x" }, "<cr>", function()
|
|
||||||
local content = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
|
|
||||||
vim.api.nvim_win_close(win, true)
|
|
||||||
vim.cmd.stopinsert()
|
|
||||||
if #content > 0 then
|
|
||||||
local new_name = content[1]
|
|
||||||
vim.lsp.buf.rename(new_name)
|
|
||||||
end
|
|
||||||
end, { buffer = buf })
|
|
||||||
vim.keymap.set({ "n", "i", "x" }, "<C-c>", function()
|
|
||||||
vim.api.nvim_win_close(win, true)
|
|
||||||
vim.cmd.stopinsert()
|
|
||||||
end, { buffer = buf })
|
|
||||||
vim.keymap.set({ "n", "x" }, "<esc>", function()
|
|
||||||
vim.api.nvim_win_close(win, true)
|
|
||||||
end, { buffer = buf })
|
|
||||||
vim.keymap.set({ "n", "x" }, "q", function()
|
|
||||||
vim.api.nvim_win_close(win, true)
|
|
||||||
end, { buffer = buf })
|
|
||||||
|
|
||||||
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("^v$<C-g>", true, false, true), "n", true)
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Called when language server attaches
|
--- Called when language server attaches
|
||||||
---@param client vim.lsp.Client
|
---@param client vim.lsp.Client
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
|
|||||||
Reference in New Issue
Block a user