From 45acfaacb57cedc28c390d2f94f3053c8fbc1f8e Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sun, 3 Nov 2024 15:44:23 +0100 Subject: [PATCH] feat(snacks): use rename --- lua/lazyvim/plugins/editor.lua | 2 +- .../plugins/extras/editor/mini-files.lua | 2 +- lua/lazyvim/plugins/lsp/keymaps.lua | 2 +- lua/lazyvim/util/lsp.lua | 57 ------------------- 4 files changed, 3 insertions(+), 60 deletions(-) diff --git a/lua/lazyvim/plugins/editor.lua b/lua/lazyvim/plugins/editor.lua index a57d9aeb..9f5988c6 100644 --- a/lua/lazyvim/plugins/editor.lua +++ b/lua/lazyvim/plugins/editor.lua @@ -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") diff --git a/lua/lazyvim/plugins/extras/editor/mini-files.lua b/lua/lazyvim/plugins/extras/editor/mini-files.lua index 461e6dca..86353eb5 100644 --- a/lua/lazyvim/plugins/extras/editor/mini-files.lua +++ b/lua/lazyvim/plugins/extras/editor/mini-files.lua @@ -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, diff --git a/lua/lazyvim/plugins/lsp/keymaps.lua b/lua/lazyvim/plugins/lsp/keymaps.lua index 00fa0c64..f590b6bf 100644 --- a/lua/lazyvim/plugins/lsp/keymaps.lua +++ b/lua/lazyvim/plugins/lsp/keymaps.lua @@ -25,7 +25,7 @@ function M.get() { "ca", vim.lsp.buf.code_action, desc = "Code Action", mode = { "n", "v" }, has = "codeAction" }, { "cc", vim.lsp.codelens.run, desc = "Run Codelens", mode = { "n", "v" }, has = "codeLens" }, { "cC", vim.lsp.codelens.refresh, desc = "Refresh & Display Codelens", mode = { "n" }, has = "codeLens" }, - { "cR", LazyVim.lsp.rename_file, desc = "Rename File", mode ={"n"}, has = { "workspace/didRenameFiles", "workspace/willRenameFiles" } }, + { "cR", function() Snacks.rename() end, desc = "Rename File", mode ={"n"}, has = { "workspace/didRenameFiles", "workspace/willRenameFiles" } }, { "cr", vim.lsp.buf.rename, desc = "Rename", has = "rename" }, { "cA", LazyVim.lsp.action.source, desc = "Source Action", has = "codeAction" }, { "]]", function() Snacks.words.jump(vim.v.count1) end, has = "documentHighlight", diff --git a/lua/lazyvim/util/lsp.lua b/lua/lazyvim/util/lsp.lua index eb10b02f..e868dcf0 100644 --- a/lua/lazyvim/util/lsp.lua +++ b/lua/lazyvim/util/lsp.lua @@ -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")