Skip to content

Commit

Permalink
feat(lsp): apply changes to related files when a file is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
mikavilpas committed Apr 20, 2024
1 parent 43ed7dc commit e824eb2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lua/yazi/event_handling.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local utils = require('yazi.utils')
local plenaryIterators = require('plenary.iterators')
local lsp_delete = require('yazi.lsp.delete')

local M = {}

Expand All @@ -24,6 +25,7 @@ function M.process_delete_event(event, remaining_events)
break
else
vim.api.nvim_buf_delete(buffer.bufnr, { force = false })
lsp_delete.file_deleted(buffer.path.filename)
end
end
end
Expand Down
57 changes: 57 additions & 0 deletions lua/yazi/lsp/delete.lua
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
14 changes: 14 additions & 0 deletions tests/yazi/lsp_delete_spec.lua
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)

0 comments on commit e824eb2

Please sign in to comment.