mirror of
https://github.com/LazyVim/LazyVim.git
synced 2025-06-22 00:49:03 +02:00
feat(lsp): added leader-cR to rename the current file and to lsp rename operations
This commit is contained in:
parent
4bbeb37a18
commit
b949dba489
2 changed files with 38 additions and 1 deletions
|
@ -25,6 +25,9 @@ function M.get()
|
||||||
{ "<leader>ca", vim.lsp.buf.code_action, desc = "Code Action", mode = { "n", "v" }, has = "codeAction" },
|
{ "<leader>ca", vim.lsp.buf.code_action, desc = "Code Action", mode = { "n", "v" }, has = "codeAction" },
|
||||||
{ "<leader>cc", vim.lsp.codelens.run, desc = "Run Codelens", mode = { "n", "v" }, has = "codeLens" },
|
{ "<leader>cc", vim.lsp.codelens.run, desc = "Run Codelens", mode = { "n", "v" }, has = "codeLens" },
|
||||||
{ "<leader>cC", vim.lsp.codelens.refresh, desc = "Refresh & Display Codelens", mode = { "n" }, has = "codeLens" },
|
{ "<leader>cC", vim.lsp.codelens.refresh, desc = "Refresh & Display Codelens", mode = { "n" }, has = "codeLens" },
|
||||||
|
{ "<leader>cC", vim.lsp.codelens.refresh, desc = "Refresh & Display Codelens", mode = { "n" }, has = "codeLens" },
|
||||||
|
{ "<leader>cR", LazyVim.lsp.rename_file, desc = "Rename File", mode ={"n"}, has = "workspace/didRenameFiles"},
|
||||||
|
{ "<leader>cR", LazyVim.lsp.rename_file, desc = "Rename File", mode ={"n"}, has = "workspace/willRenameFiles"},
|
||||||
{
|
{
|
||||||
"<leader>cA",
|
"<leader>cA",
|
||||||
function()
|
function()
|
||||||
|
|
|
@ -117,9 +117,36 @@ function M.on_supports_method(method, fn)
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M.rename_file()
|
||||||
|
local buf = vim.api.nvim_get_current_buf()
|
||||||
|
local old = assert(LazyVim.root.realpath(vim.api.nvim_buf_get_name(buf)))
|
||||||
|
local root = assert(LazyVim.root.realpath(LazyVim.root.get({ normalize = true })))
|
||||||
|
assert(old:find(root, 1, true) == 1, "File not in project root")
|
||||||
|
|
||||||
|
local extra = old:sub(#root + 2)
|
||||||
|
|
||||||
|
vim.ui.input({
|
||||||
|
prompt = "New File Name: ",
|
||||||
|
default = extra,
|
||||||
|
}, function(new)
|
||||||
|
if not new or new == "" or new == extra then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
new = LazyVim.norm(root .. "/" .. new)
|
||||||
|
vim.fn.mkdir(vim.fs.dirname(new), "p")
|
||||||
|
M.on_rename(old, new, function()
|
||||||
|
vim.fn.rename(old, new)
|
||||||
|
vim.cmd.edit(new)
|
||||||
|
vim.api.nvim_buf_delete(buf, { force = true })
|
||||||
|
vim.fn.delete(old)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
---@param from string
|
---@param from string
|
||||||
---@param to string
|
---@param to string
|
||||||
function M.on_rename(from, to)
|
---@param rename? fun()
|
||||||
|
function M.on_rename(from, to, rename)
|
||||||
local changes = { files = { {
|
local changes = { files = { {
|
||||||
oldUri = vim.uri_from_fname(from),
|
oldUri = vim.uri_from_fname(from),
|
||||||
newUri = vim.uri_from_fname(to),
|
newUri = vim.uri_from_fname(to),
|
||||||
|
@ -133,6 +160,13 @@ function M.on_rename(from, to)
|
||||||
vim.lsp.util.apply_workspace_edit(resp.result, client.offset_encoding)
|
vim.lsp.util.apply_workspace_edit(resp.result, client.offset_encoding)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if rename then
|
||||||
|
rename()
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, client in ipairs(clients) do
|
||||||
if client.supports_method("workspace/didRenameFiles") then
|
if client.supports_method("workspace/didRenameFiles") then
|
||||||
client.notify("workspace/didRenameFiles", changes)
|
client.notify("workspace/didRenameFiles", changes)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue