Skip to content

Commit

Permalink
fix: LSP renaming did not work in some cases
Browse files Browse the repository at this point in the history
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 <#80>
  • Loading branch information
mikavilpas committed Jul 21, 2024
1 parent bf57797 commit d1ceb03
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 107 deletions.
1 change: 1 addition & 0 deletions .github/workflows/typecheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ jobs:
libraries:
# space separated
https://github.com/nvim-lua/plenary.nvim
https://github.com/antosha417/nvim-lsp-file-operations
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand Down
6 changes: 6 additions & 0 deletions lazy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lua/yazi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,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
Expand Down
11 changes: 6 additions & 5 deletions lua/yazi/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 7 additions & 50 deletions lua/yazi/lsp/delete.lua
Original file line number Diff line number Diff line change
@@ -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
59 changes: 7 additions & 52 deletions lua/yazi/lsp/rename.lua
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions yazi.nvim-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ source = {
url = 'git+https://github.com/mikavilpas/yazi.nvim',
}
dependencies = {
'lua >= 5.1',
-- Add runtime dependencies here
-- e.g. "plenary.nvim",
'plenary.nvim',

-- 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',
Expand Down

0 comments on commit d1ceb03

Please sign in to comment.