From 3de296f8f60f561fc836d6775e9963dc04620907 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=A4rtl?= Date: Sat, 15 Jun 2024 09:14:50 +0200 Subject: [PATCH] Issue #30 Add utility to get client capabilities for LSP server config --- README.md | 22 ++++++++++++++++++++++ lua/lsp-file-operations.lua | 24 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/README.md b/README.md index 4fa08f1..45fa9d5 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,28 @@ require("lsp-file-operations").setup { timeout_ms = 10000, } ``` +Some LSP servers also expect to be informed about the extended client capabilities. +If you use [nvim-lspconfig](https://github.com/neovim/nvim-lspconfig) you can configure the default client capabilities that will +be sent to all servers like this: + +```lua +local lspconfig = require'lspconfig' + +-- Set global defaults for all servers +lspconfig.util.default_config = vim.tbl_extend( + 'force', + lspconfig.util.default_config, + { + capabilities = vim.tbl_deep_extend( + "force", + vim.lsp.protocol.make_client_capabilities(), + -- returns configured operations if setup() was already called + -- or default operations if not + require'lsp-file-operations'.default_capabilities(), + ) + } +) +``` ## Contributing diff --git a/lua/lsp-file-operations.lua b/lua/lsp-file-operations.lua index 177502b..9e7fc72 100644 --- a/lua/lsp-file-operations.lua +++ b/lua/lsp-file-operations.lua @@ -24,6 +24,15 @@ local modules = { didDeleteFiles = "lsp-file-operations.did-delete", } +local capabilities = { + willRenameFiles = "willRename", + didRenameFiles = "didRename", + willCreateFiles = "willCreate", + didCreateFiles = "didCreate", + willDeleteFiles = "willDelete", + didDeleteFiles = "didDelete", +} + ---@alias HandlerMap table a mapping from modules to events that trigger it --- helper function to subscribe events to a given module callback @@ -108,4 +117,19 @@ M.setup = function(opts) end end +--- The extra client capabilities provided by this plugin. To be merged with +--- vim.lsp.protocol.make_client_capabilities() and sent to the LSP server. +M.default_capabilities = function() + local config = M.config or default_config + local result = { + workspace = { + fileOperations = {} + } + } + for operation, capability in pairs(capabilities) do + result.workspace.fileOperations[capability] = config.operations[operation] + end + return result +end + return M