Browse Source

nixpkgs/neovim: use LSPAttach autocmd for mappings

Thomas Dy 1 month ago
parent
commit
4acb34602e
2 changed files with 32 additions and 35 deletions
  1. 0 19
      .config/nixpkgs/neovim/lsp.lua
  2. 32 16
      .config/nixpkgs/neovim/mappings.lua

+ 0 - 19
.config/nixpkgs/neovim/lsp.lua

@@ -9,7 +9,6 @@ local nvim_lsp = require('lspconfig')
 
 nvim_lsp.bashls.setup({
   capabilities = capabilities,
-  on_attach = on_attach,
 });
 
 if vim.fn.executable("node_modules/.bin/eslint") == 1 then
@@ -37,8 +36,6 @@ if vim.fn.executable("node_modules/.bin/eslint") == 1 then
       client.server_capabilities.documentFormattingProvider = true
       client.server_capabilities.documentRangeFormattingProvider = true
     end,
-    -- mappings should have been attached by typescript and re-attaching can
-    -- overwrite the typescript specific overrides
   })
 end
 
@@ -52,7 +49,6 @@ end
 if vim.fn.executable("deno") == 1 then
   nvim_lsp.denols.setup({
     capabilities = capabilities,
-    on_attach = on_attach,
   });
 else
   require('typescript').setup({
@@ -78,16 +74,6 @@ else
         client.server_capabilities.documentFormattingProvider = false
         client.server_capabilities.documentRangeFormattingProvider = false
       end,
-      on_attach = function(client, bufnr)
-        on_attach(client, bufnr)
-
-        -- override mappings for typescript
-        local opts = { silent = true, buffer = bufnr }
-        -- exclude import statements from reference search (may have false positives)
-        vim.keymap.set('n', 'gr', '<cmd>Telescope lsp_references default_text=!import\\ <CR>', opts)
-        vim.keymap.set('n', 'gD', '<cmd>Telescope lsp_definitions<CR>', opts)
-        vim.keymap.set('n', 'gd', '<cmd>TypescriptGoToSourceDefinition<CR>', opts)
-      end
     }
   })
 end
@@ -95,14 +81,12 @@ end
 if vim.fn.executable("gopls") == 1 then
   nvim_lsp.gopls.setup({
     capabilities = capabilities,
-    on_attach = on_attach,
   });
 end
 
 if vim.fn.executable("solargraph") == 1 then
   nvim_lsp.solargraph.setup({
     capabilities = capabilities,
-    on_attach = on_attach,
     init_options = {
       formatting = false,
     }
@@ -111,13 +95,11 @@ end
 
 if vim.fn.executable("nil") == 1 then
   nvim_lsp.nil_ls.setup({
-    on_attach = on_attach,
   });
 end
 
 if vim.fn.executable("jdtls") == 1 then
   nvim_lsp.jdtls.setup({
-    on_attach = on_attach,
     handlers = {
       ["$/progress"] = function()
         -- this is quite noisy so just disable it
@@ -142,5 +124,4 @@ end
 
 nvim_lsp.elvish.setup({
   capabilities = capabilities,
-  on_attach = on_attach,
 })

+ 32 - 16
.config/nixpkgs/neovim/mappings.lua

@@ -79,19 +79,35 @@ vim.keymap.set('n', '[d', '<cmd>lua vim.diagnostic.goto_prev()<CR>', opts)
 vim.keymap.set('n', ']d', '<cmd>lua vim.diagnostic.goto_next()<CR>', opts)
 vim.keymap.set('n', '<space>q', '<cmd>lua vim.diagnostic.setqflist({ severity = { min = vim.diagnostic.severity.WARN } })<CR>', opts)
 
-function on_attach(client, bufnr)
-  local opts = { silent = true, buffer = bufnr }
-
-  -- LSP-specific mappings
-  vim.keymap.set('n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<CR>', opts)
-  vim.keymap.set('n', 'gd', '<cmd>Telescope lsp_definitions<CR>', opts)
-  vim.keymap.set('n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>', opts)
-  vim.keymap.set('n', 'gi', '<cmd>Telescope lsp_implementations<CR>', opts)
-  vim.keymap.set({'n', 'i'}, '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
-  vim.keymap.set('n', '<space>D', '<cmd>Telescope lsp_type_definitions<CR>', opts)
-  vim.keymap.set('n', '<space>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
-  vim.keymap.set('n', '<space>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
-  vim.keymap.set('v', '<space>ca', '<cmd>lua vim.lsp.buf.range_code_action()<CR>', opts)
-  vim.keymap.set('n', 'gr', '<cmd>Telescope lsp_references<CR>', opts)
-  vim.keymap.set('n', '<Leader>fs', '<cmd>Telescope lsp_document_symbols<CR>', opts)
-end
+-- LSP-specific
+vim.api.nvim_create_autocmd('LspAttach', {
+  callback = function(args)
+    local client = vim.lsp.get_client_by_id(args.data.client_id)
+
+    if client.name == 'dprint' or client.name == 'eslint' then
+      -- mappings should have been attached by typescript and re-attaching can
+      -- overwrite the typescript specific overrides
+    end
+
+    local opts = { silent = true, buffer = args.buf }
+
+    vim.keymap.set('n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<CR>', opts)
+    vim.keymap.set('n', 'gd', '<cmd>Telescope lsp_definitions<CR>', opts)
+    vim.keymap.set('n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>', opts)
+    vim.keymap.set('n', 'gi', '<cmd>Telescope lsp_implementations<CR>', opts)
+    vim.keymap.set({'n', 'i'}, '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
+    vim.keymap.set('n', '<space>D', '<cmd>Telescope lsp_type_definitions<CR>', opts)
+    vim.keymap.set('n', '<space>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
+    vim.keymap.set('n', '<space>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
+    vim.keymap.set('v', '<space>ca', '<cmd>lua vim.lsp.buf.range_code_action()<CR>', opts)
+    vim.keymap.set('n', 'gr', '<cmd>Telescope lsp_references<CR>', opts)
+    vim.keymap.set('n', '<Leader>fs', '<cmd>Telescope lsp_document_symbols<CR>', opts)
+
+    if client.name == 'tsserver' then
+      -- exclude import statements from reference search (may have false positives)
+      vim.keymap.set('n', 'gr', '<cmd>Telescope lsp_references default_text=!import\\ <CR>', opts)
+      vim.keymap.set('n', 'gD', '<cmd>Telescope lsp_definitions<CR>', opts)
+      vim.keymap.set('n', 'gd', '<cmd>TypescriptGoToSourceDefinition<CR>', opts)
+    end
+  end,
+})