From 85a8e60a0ca810c6bae21bcf0c4b9e972d433274 Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Sat, 13 Jul 2024 18:16:41 +0300 Subject: [PATCH] fix: LSP renaming did not work in some cases There were some edge cases that caused issues when renaming files with LSP servers. Hopefully fix all of them by using the implementation that's also shared by neotree and nvim-tree: https://github.com/antosha417/nvim-lsp-file-operations Thanks @chaozwn for suggesting the fix! Closes --- .github/workflows/typecheck.yml | 1 + Makefile | 1 + lazy.lua | 6 ++++ lua/yazi.lua | 4 +++ lua/yazi/log.lua | 11 +++--- lua/yazi/lsp/delete.lua | 57 ++++--------------------------- lua/yazi/lsp/rename.lua | 59 ++++----------------------------- yazi.nvim-scm-1.rockspec | 4 +++ 8 files changed, 36 insertions(+), 107 deletions(-) diff --git a/.github/workflows/typecheck.yml b/.github/workflows/typecheck.yml index 768de8b7..46f72d1d 100644 --- a/.github/workflows/typecheck.yml +++ b/.github/workflows/typecheck.yml @@ -20,3 +20,4 @@ jobs: libraries: # space separated https://github.com/nvim-lua/plenary.nvim + https://github.com/antosha417/nvim-lsp-file-operations diff --git a/Makefile b/Makefile index cd63f4af..db322d53 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,7 @@ COLOR_WHITE := \033[1;37m # install development and testing dependencies init: luarocks init --no-gitignore + luarocks build --local --only-deps --dev luarocks install busted 2.2.0-1 @echo "" diff --git a/lazy.lua b/lazy.lua index 693ca583..f863352a 100644 --- a/lazy.lua +++ b/lazy.lua @@ -10,6 +10,12 @@ return { { 'nvim-lua/plenary.nvim', lazy = true }, { 'akinsho/bufferline.nvim', lazy = true }, + { + -- Neovim plugin that adds support for file operations using built-in LSP + -- https://github.com/antosha417/nvim-lsp-file-operations + 'antosha417/nvim-lsp-file-operations', + lazy = true, + }, { 'mikavilpas/yazi.nvim', ---@type YaziConfig diff --git a/lua/yazi.lua b/lua/yazi.lua index 5fc6ce7c..dd19468c 100644 --- a/lua/yazi.lua +++ b/lua/yazi.lua @@ -106,6 +106,10 @@ function M.setup(opts) Log.level = M.config.log_level + pcall(function() + require('lsp-file-operations').setup() + end) + local yazi_augroup = vim.api.nvim_create_augroup('yazi', { clear = true }) if M.config.open_for_directories == true then diff --git a/lua/yazi/log.lua b/lua/yazi/log.lua index f05dbf69..79059649 100644 --- a/lua/yazi/log.lua +++ b/lua/yazi/log.lua @@ -56,13 +56,14 @@ function Log:write_message(level, message) end end +---@param level yazi.LogLevel +function Log:active_for_level(level) + return self.level and self.level ~= log_levels.OFF and self.level <= level +end + ---@param message string function Log:debug(message) - if - self.level - and self.level ~= log_levels.OFF - and self.level <= log_levels.DEBUG - then + if self:active_for_level(log_levels.DEBUG) then self:write_message('DEBUG', message) end end diff --git a/lua/yazi/lsp/delete.lua b/lua/yazi/lsp/delete.lua index b15bc886..bd116234 100644 --- a/lua/yazi/lsp/delete.lua +++ b/lua/yazi/lsp/delete.lua @@ -1,57 +1,14 @@ -local M = {} - ----@param path string -local function notify_file_was_deleted(path) - -- https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_willDeleteFiles - local method = 'workspace/willDeleteFiles' - - local clients = vim.lsp.get_clients({ - method = method, - bufnr = vim.api.nvim_get_current_buf(), - }) - - for _, client in ipairs(clients) do - local resp = client.request_sync(method, { - files = { - { - uri = vim.uri_from_fname(path), - }, - }, - }, 1000, 0) - - if resp and resp.result ~= nil then - vim.lsp.util.apply_workspace_edit(resp.result, client.offset_encoding) - end - end -end - ----@param path string -local function notify_delete_complete(path) - -- https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_didDeleteFiles - local method = 'workspace/didDeleteFiles' +local will_delete = require('lsp-file-operations.will-delete') +local did_delete = require('lsp-file-operations.did-delete') - local clients = vim.lsp.get_clients({ - method = method, - bufnr = vim.api.nvim_get_current_buf(), - }) - - for _, client in ipairs(clients) do - -- NOTE: this returns nothing, so no need to do anything with the response - client.request_sync(method, { - files = { - { - uri = vim.uri_from_fname(path), - }, - }, - }, 1000, 0) - end -end +local M = {} --- Send a notification to LSP servers, letting them know that yazi just deleted some files +-- Send a notification to LSP servers, letting them know that yazi just deleted +-- some files. Execute any changes that the LSP says are needed in other files. ---@param path string function M.file_deleted(path) - notify_file_was_deleted(path) - notify_delete_complete(path) + will_delete.callback({ fname = path }) + did_delete.callback({ fname = path }) end return M diff --git a/lua/yazi/lsp/rename.lua b/lua/yazi/lsp/rename.lua index ac05ade5..b33ba71a 100644 --- a/lua/yazi/lsp/rename.lua +++ b/lua/yazi/lsp/rename.lua @@ -1,60 +1,15 @@ -local M = {} - ----@param from string ----@param to string -local function notify_file_was_renamed(from, to) - -- https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_willRenameFiles - local method = 'workspace/willRenameFiles' - - local clients = vim.lsp.get_clients({ - method = method, - bufnr = vim.api.nvim_get_current_buf(), - }) - - for _, client in ipairs(clients) do - local resp = client.request_sync(method, { - files = { - { - oldUri = vim.uri_from_fname(from), - newUri = vim.uri_from_fname(to), - }, - }, - }, 1000, 0) - - if resp and resp.result ~= nil then - vim.lsp.util.apply_workspace_edit(resp.result, client.offset_encoding) - end - end -end - ----@param from string ----@param to string -local function notify_rename_complete(from, to) - -- https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_didRenameFiles - local method = 'workspace/didRenameFiles' +local will_rename = require('lsp-file-operations.will-rename') +local did_rename = require('lsp-file-operations.did-rename') - local clients = vim.lsp.get_clients({ - method = method, - bufnr = vim.api.nvim_get_current_buf(), - }) - - for _, client in ipairs(clients) do - -- NOTE: this returns nothing, so no need to do anything with the response - client.request_sync(method, { - files = { - oldUri = vim.uri_from_fname(from), - newUri = vim.uri_from_fname(to), - }, - }, 1000, 0) - end -end +local M = {} --- Send a notification to LSP servers, letting them know that yazi just renamed some files +-- Send a notification to LSP servers, letting them know that yazi just renamed +-- some files. Execute any changes that the LSP says are needed in other files. ---@param from string ---@param to string function M.file_renamed(from, to) - notify_file_was_renamed(from, to) - notify_rename_complete(from, to) + will_rename.callback({ old_name = from, new_name = to }) + did_rename.callback({ old_name = from, new_name = to }) end return M diff --git a/yazi.nvim-scm-1.rockspec b/yazi.nvim-scm-1.rockspec index 0bafbbd3..a7fc6e9d 100644 --- a/yazi.nvim-scm-1.rockspec +++ b/yazi.nvim-scm-1.rockspec @@ -6,6 +6,7 @@ source = { url = 'git+https://github.com/mikavilpas/yazi.nvim', } dependencies = { + 'lua >= 5.1', -- Add runtime dependencies here -- e.g. "plenary.nvim", 'plenary.nvim', @@ -13,9 +14,12 @@ dependencies = { -- https://github.com/akinsho/bufferline.nvim -- https://luarocks.org/modules/neorocks/bufferline.nvim 'bufferline.nvim', + + 'nvim-lsp-file-operations >= scm', } test_dependencies = { 'nlua', + 'plenary.nvim', } build = { type = 'builtin',