kickstart.nvim icon indicating copy to clipboard operation
kickstart.nvim copied to clipboard

How do I add language specific LSP keymaps?

Open dotfrag opened this issue 1 year ago • 6 comments

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?

dotfrag avatar May 11 '24 13:05 dotfrag

Adding the keys field the server setup doesn't work. I'm guessing LazyVim does some magic to load them.

Can you show what you tried?

jglueckstein avatar May 11 '24 15:05 jglueckstein

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.

dotfrag avatar May 11 '24 17:05 dotfrag

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 {}

dam9000 avatar May 11 '24 20:05 dam9000

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' }, {

dam9000 avatar May 11 '24 20:05 dam9000

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.

dotfrag avatar May 12 '24 11:05 dotfrag

:clap:

dam9000 avatar May 12 '24 21:05 dam9000