-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lsp): apply changes to related files when a file is deleted
- Loading branch information
1 parent
43ed7dc
commit e824eb2
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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 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 | ||
|
||
-- Send a notification to LSP servers, letting them know that yazi just deleted some files | ||
---@param path string | ||
function M.file_deleted(path) | ||
notify_file_was_deleted(path) | ||
notify_delete_complete(path) | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
local luassert = require('luassert') | ||
local lsp_delete = require('yazi.lsp.delete') | ||
|
||
describe('renaming files with LSP support', function() | ||
-- It's pretty hard to test this as it would require a running LSP server. | ||
-- There are some examples of this, e.g. | ||
-- https://github.com/pmizio/typescript-tools.nvim/blob/master/tests/editor_spec.lua | ||
-- | ||
-- Maybe more testing could be added later. | ||
it('does nothing if no LSP is running', function() | ||
local result = lsp_delete.file_deleted('test-file.txt') | ||
luassert.is_nil(result) | ||
end) | ||
end) |