Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #30 Add utility to get client capabilities for LSP server config #32

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 24 additions & 0 deletions lua/lsp-file-operations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string[]> a mapping from modules to events that trigger it

--- helper function to subscribe events to a given module callback
Expand Down Expand Up @@ -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()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@antosha417 I don't do much lua development - so improvements welcome. If you don't like it we can also remove it again. But that would make setup slightly more verbose.

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
antosha417 marked this conversation as resolved.
Show resolved Hide resolved
return result
end

return M