diff options
author | Mathias Fussenegger <f.mathias@zignar.net> | 2021-03-18 11:32:33 +0100 |
---|---|---|
committer | Mathias Fussenegger <f.mathias@zignar.net> | 2021-03-18 19:53:43 +0100 |
commit | 84213b5b9adfaaf133771432ca468cea8faa4cc9 (patch) | |
tree | 1c949c17cf99f1b4e144ad8ae9aab33d122a9137 | |
parent | 191afb42be90c3f39f445c8340632e732e6710f8 (diff) | |
download | rneovim-84213b5b9adfaaf133771432ca468cea8faa4cc9.tar.gz rneovim-84213b5b9adfaaf133771432ca468cea8faa4cc9.tar.bz2 rneovim-84213b5b9adfaaf133771432ca468cea8faa4cc9.zip |
lsp: Add support for delete workspaceEdit resource operation
-rw-r--r-- | runtime/lua/vim/lsp/protocol.lua | 2 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/util.lua | 24 | ||||
-rw-r--r-- | test/functional/plugin/lsp_spec.lua | 39 |
3 files changed, 63 insertions, 2 deletions
diff --git a/runtime/lua/vim/lsp/protocol.lua b/runtime/lua/vim/lsp/protocol.lua index 085ca7b4a1..7e43eb84de 100644 --- a/runtime/lua/vim/lsp/protocol.lua +++ b/runtime/lua/vim/lsp/protocol.lua @@ -750,7 +750,7 @@ function protocol.make_client_capabilities() workspaceFolders = true; applyEdit = true; workspaceEdit = { - resourceOperations = {'rename', 'create',}, + resourceOperations = {'rename', 'create', 'delete',}, }; }; callHierarchy = { diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua index 82fd77ea90..6945d0f1ec 100644 --- a/runtime/lua/vim/lsp/util.lua +++ b/runtime/lua/vim/lsp/util.lua @@ -647,6 +647,27 @@ local function create_file(change) end +local function delete_file(change) + local opts = change.options or {} + local fname = vim.uri_to_fname(change.uri) + local stat = vim.loop.fs_stat(fname) + if opts.ignoreIfNotExists and not stat then + return + end + assert(stat, "Cannot delete not existing file or folder " .. fname) + local flags + if stat and stat.type == 'directory' then + flags = opts.recursive and 'rf' or 'd' + else + flags = '' + end + local bufnr = vim.fn.bufadd(fname) + local result = tonumber(vim.fn.delete(fname, flags)) + assert(result == 0, 'Could not delete file: ' .. fname .. ', stat: ' .. vim.inspect(stat)) + api.nvim_buf_delete(bufnr, { force = true }) +end + + --- Applies a `WorkspaceEdit`. --- --@param workspace_edit (table) `WorkspaceEdit` @@ -662,8 +683,9 @@ function M.apply_workspace_edit(workspace_edit) ) elseif change.kind == 'create' then create_file(change) + elseif change.kind == 'delete' then + delete_file(change) elseif change.kind then - -- TODO(ashkan) handle DeleteFile error(string.format("Unsupported change: %q", vim.inspect(change))) else M.apply_text_document_edit(change, idx) diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index eb8f5e8824..6d3af115fa 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -1319,6 +1319,45 @@ describe('LSP', function() eq(true, exec_lua('return vim.loop.fs_stat(...) ~= nil', tmpfile)) eq('', read_file(tmpfile)) end) + it('DeleteFile delete file and buffer', function() + local tmpfile = helpers.tmpname() + write_file(tmpfile, 'Be gone') + local uri = exec_lua([[ + local fname = select(1, ...) + local bufnr = vim.fn.bufadd(fname) + vim.fn.bufload(bufnr) + return vim.uri_from_fname(fname) + ]], tmpfile) + local edit = { + documentChanges = { + { + kind = 'delete', + uri = uri, + } + } + } + eq(true, pcall(exec_lua, 'vim.lsp.util.apply_workspace_edit(...)', edit)) + eq(false, exec_lua('return vim.loop.fs_stat(...) ~= nil', tmpfile)) + eq(false, exec_lua('return vim.api.nvim_buf_is_loaded(vim.fn.bufadd(...))', tmpfile)) + end) + it('DeleteFile fails if file does not exist and ignoreIfNotExists is false', function() + local tmpfile = helpers.tmpname() + os.remove(tmpfile) + local uri = exec_lua('return vim.uri_from_fname(...)', tmpfile) + local edit = { + documentChanges = { + { + kind = 'delete', + uri = uri, + options = { + ignoreIfNotExists = false, + } + } + } + } + eq(false, pcall(exec_lua, 'vim.lsp.util.apply_workspace_edit(...)', edit)) + eq(false, exec_lua('return vim.loop.fs_stat(...) ~= nil', tmpfile)) + end) end) describe('completion_list_to_complete_items', function() |