kickstart.nvim
kickstart.nvim copied to clipboard
How do I add language specific LSP keymaps?
How do I add language specific LSP keymaps? Similarly to how LazyVim does it here.
Adding the keys field the server setup doesn't work. I'm guessing LazyVim does some magic to load them.
How/where can I add this functionality?
Adding the
keysfield the server setup doesn't work. I'm guessing LazyVim does some magic to load them.
Can you show what you tried?
Yes of course, inside lsp config function:
local servers = {
...
pyright = {},
ruff = {
keys = {
{
"<leader>co",
function()
vim.lsp.buf.code_action({
apply = true,
context = { only = { "source.organizeImports" }, diagnostics = {} },
})
end,
desc = "Organize Imports",
},
},
},
...
}
When opening a python file, ruff is loaded but the key doesn't register.
That syntax appears to be a LazyVim specific, and is handled by:
LazyVim/lua/lazyvim/plugins/lsp/keymaps.lua:
local maps = opts.servers[client.name] and opts.servers[client.name].keys or {}
Using kickstart you could try this:
diff --git a/init.lua b/init.lua
index 88658ef..6f61c5e 100644
--- a/init.lua
+++ b/init.lua
@@ -513,6 +513,16 @@ require('lazy').setup({
--
-- When you move your cursor, the highlights will be cleared (the second autocommand).
local client = vim.lsp.get_client_by_id(event.data.client_id)
+
+ if client.config.name == 'riff' then
+ map('<leader>co', function()
+ vim.lsp.buf.code_action {
+ apply = true,
+ context = { only = { 'source.organizeImports' }, diagnostics = {} },
+ }
+ end, 'Organize Imports')
+ end
+
if client and client.server_capabilities.documentHighlightProvider then
local highlight_augroup = vim.api.nvim_create_augroup('kickstart-lsp-highlight', { clear = false })
vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
Thanks @dam9000, that worked perfectly. Also client.name works. I don't know if it is any different.
I was hoping to use the servers.<lang>.keys table to avoid defining the keys separately to the server config.
I thought that something like servers[client.config.name].keys would work, but vim.print(servers[client.config.name].keys) prints nil. Do you know if I'm doing something wrong?
Update: I think it's working, I'll confirm soon.
Update: Got it!
-- Language specific keymaps
local keymaps = servers[client.name].keys
if keymaps then
for _, keymap in pairs(keymaps) do
map(keymap[1], keymap[2], keymap[3])
end
end
Note: for this to work, local servers = {..} needs to be moved above the LspAttach autocmd.
:clap: