Selaa lähdekoodia

nixpkgs/neovim: redo formatting again

Thomas Dy 8 kuukautta sitten
vanhempi
commit
fae1dd6ab6
2 muutettua tiedostoa jossa 27 lisäystä ja 31 poistoa
  1. 14 1
      .config/nixpkgs/neovim/autocmd.lua
  2. 13 30
      .config/nixpkgs/neovim/lsp.lua

+ 14 - 1
.config/nixpkgs/neovim/autocmd.lua

@@ -32,12 +32,25 @@ vim.api.nvim_create_autocmd('BufLeave', {
   command = 'stopinsert',
 })
 
--- strip trailing whitespace
+-- formatting
 vim.api.nvim_create_autocmd('BufWritePre', {
   callback = function(opts)
     if vim.bo.filetype == 'diff' then
       return
     end
+
+    if not vim.g.no_lsp_format then
+      -- check if can LSP format
+      local clients = vim.lsp.get_active_clients({ bufnr = opts.buf })
+      for _, client in ipairs(clients) do
+        if client.server_capabilities.documentFormattingProvider then
+          vim.lsp.buf.format()
+          return
+        end
+      end
+    end
+
+    -- otherwise strip trailing whitespace
     vim.cmd('%s/\\s\\+$//e')
   end,
 })

+ 13 - 30
.config/nixpkgs/neovim/lsp.lua

@@ -24,16 +24,21 @@ if vim.fn.executable("node_modules/.bin/eslint") == 1 then
     settings = {
       options = options,
     },
-    on_attach = function(client, bufnr)
+    handlers = {
+      ["client/registerCapability"] = function(err, result, ctx, config)
+        -- ignore as we can't handle any of this and it just spams the logs
+        return vim.NIL
+      end
+    },
+    on_init = function(client)
       -- add formatting capability, the language server registers this
       -- dynamically but neovim does not support that yet
       -- https://github.com/microsoft/vscode-eslint/pull/1307
       client.server_capabilities.documentFormattingProvider = true
       client.server_capabilities.documentRangeFormattingProvider = true
-
-      -- mappings should have been attached by typescript and re-attaching can
-      -- overwrite the typescript specific overrides
     end,
+    -- mappings should have been attached by typescript and re-attaching can
+    -- overwrite the typescript specific overrides
   })
 end
 
@@ -67,12 +72,13 @@ else
       flags = {
         debounce_text_changes = 150,
       },
-      on_attach = function(client, bufnr)
+      on_init = function(client)
         -- mark tsserver as not having formatting available as we rely on
-        -- eslint for that
+        -- eslint and dprint for that
         client.server_capabilities.documentFormattingProvider = false
         client.server_capabilities.documentRangeFormattingProvider = false
-
+      end,
+      on_attach = function(client, bufnr)
         on_attach(client, bufnr)
 
         -- override mappings for typescript
@@ -120,29 +126,6 @@ if vim.fn.executable("jdtls") == 1 then
   });
 end
 
--- format on save
-local group = vim.api.nvim_create_augroup('LspFormatting', { clear = false })
-
-vim.api.nvim_create_autocmd('LspAttach', {
-  callback = function(args)
-    local bufnr = args.buf
-    local client = vim.lsp.get_client_by_id(args.data.client_id)
-
-    if client.server_capabilities.documentFormattingProvider then
-      for key, cmd in pairs(vim.api.nvim_get_autocmds({ group = group, buffer = bufnr })) do
-        vim.api.nvim_del_autocmd(cmd.id)
-      end
-      vim.api.nvim_create_autocmd('BufWritePre', {
-        group = group,
-        buffer = bufnr,
-        callback = function()
-          vim.lsp.buf.format()
-        end,
-      })
-    end
-  end,
-})
-
 -- custom LSP servers
 local configs = require('lspconfig.configs')