mirror of
https://github.com/LazyVim/LazyVim.git
synced 2025-07-16 12:24:36 +02:00
feat(snacks): use rename
This commit is contained in:
parent
f3f50b77ec
commit
45acfaacb5
4 changed files with 3 additions and 60 deletions
|
@ -105,7 +105,7 @@ return {
|
|||
},
|
||||
config = function(_, opts)
|
||||
local function on_move(data)
|
||||
LazyVim.lsp.on_rename(data.source, data.destination)
|
||||
Snacks.rename.on_rename(data.source, data.destination)
|
||||
end
|
||||
|
||||
local events = require("neo-tree.events")
|
||||
|
|
|
@ -104,7 +104,7 @@ return {
|
|||
vim.api.nvim_create_autocmd("User", {
|
||||
pattern = "MiniFilesActionRename",
|
||||
callback = function(event)
|
||||
LazyVim.lsp.on_rename(event.data.from, event.data.to)
|
||||
Snacks.rename.on_rename(event.data.from, event.data.to)
|
||||
end,
|
||||
})
|
||||
end,
|
||||
|
|
|
@ -25,7 +25,7 @@ function M.get()
|
|||
{ "<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.refresh, desc = "Refresh & Display Codelens", mode = { "n" }, has = "codeLens" },
|
||||
{ "<leader>cR", LazyVim.lsp.rename_file, desc = "Rename File", mode ={"n"}, has = { "workspace/didRenameFiles", "workspace/willRenameFiles" } },
|
||||
{ "<leader>cR", function() Snacks.rename() end, desc = "Rename File", mode ={"n"}, has = { "workspace/didRenameFiles", "workspace/willRenameFiles" } },
|
||||
{ "<leader>cr", vim.lsp.buf.rename, desc = "Rename", has = "rename" },
|
||||
{ "<leader>cA", LazyVim.lsp.action.source, desc = "Source Action", has = "codeAction" },
|
||||
{ "]]", function() Snacks.words.jump(vim.v.count1) end, has = "documentHighlight",
|
||||
|
|
|
@ -118,63 +118,6 @@ function M.on_supports_method(method, fn)
|
|||
})
|
||||
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,
|
||||
completion = "file",
|
||||
}, 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 to string
|
||||
---@param rename? fun()
|
||||
function M.on_rename(from, to, rename)
|
||||
local changes = { files = { {
|
||||
oldUri = vim.uri_from_fname(from),
|
||||
newUri = vim.uri_from_fname(to),
|
||||
} } }
|
||||
|
||||
local clients = M.get_clients()
|
||||
for _, client in ipairs(clients) do
|
||||
if client.supports_method("workspace/willRenameFiles") then
|
||||
local resp = client.request_sync("workspace/willRenameFiles", changes, 1000, 0)
|
||||
if resp and resp.result ~= nil then
|
||||
vim.lsp.util.apply_workspace_edit(resp.result, client.offset_encoding)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if rename then
|
||||
rename()
|
||||
end
|
||||
|
||||
for _, client in ipairs(clients) do
|
||||
if client.supports_method("workspace/didRenameFiles") then
|
||||
client.notify("workspace/didRenameFiles", changes)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---@return _.lspconfig.options
|
||||
function M.get_config(server)
|
||||
local configs = require("lspconfig.configs")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue