Browse Source

nixpkgs/neovim: only use LSP format if a client is attached

Thomas Dy 3 months ago
parent
commit
357e17b02d
1 changed files with 13 additions and 2 deletions
  1. 13 2
      .config/nixpkgs/neovim/plugins.lua

+ 13 - 2
.config/nixpkgs/neovim/plugins.lua

@@ -277,8 +277,19 @@ local format_on_save = require("format-on-save")
 local formatters = require("format-on-save.formatters")
 local vim_notify = require("format-on-save.error-notifiers.vim-notify")
 
+-- only format with LSP if there are clients attached
+local lsp_formatter = formatters.custom({
+  format = function()
+    local bufnr = vim.api.nvim_get_current_buf()
+    local clients = vim.lsp.get_active_clients({ bufnr = bufnr })
+    if #clients > 0 then
+      vim.lsp.buf.format({ timeout_ms = 4000, bufnr = bufnr })
+    end
+  end
+})
+
 local js = {
-  formatters.lsp,
+  lsp_formatter,
   formatters.if_file_exists({
     pattern = {
       "dprint.json",
@@ -305,7 +316,7 @@ format_on_save.setup({
 
   fallback_formatter = {
     formatters.remove_trailing_whitespace,
-    formatters.lsp,
+    lsp_formatter,
   },
 })