From 64cd675ece86352c8540da765aef72eeba045cf5 Mon Sep 17 00:00:00 2001 From: Jeremy Fleischman Date: Mon, 5 May 2025 11:59:25 -0700 Subject: [PATCH] Port lsp-format to use non-deprecated setup instructions Upstream still documents the now-deprecated `require("lspconfig")..setup` mechanism. I believe the modern alternative is to register a `LspAttach` autocmd. For more context, see: - https://github.com/lukas-reineke/lsp-format.nvim/issues/98 - https://github.com/lukas-reineke/lsp-format.nvim/pull/99 --- plugins/by-name/lsp-format/default.nix | 52 ++++++++++++------- .../plugins/by-name/lsp-format/default.nix | 43 ++++++++++++++- 2 files changed, 73 insertions(+), 22 deletions(-) diff --git a/plugins/by-name/lsp-format/default.nix b/plugins/by-name/lsp-format/default.nix index fe7863d0..e3a4fa1e 100644 --- a/plugins/by-name/lsp-format/default.nix +++ b/plugins/by-name/lsp-format/default.nix @@ -78,8 +78,7 @@ lib.nixvim.plugins.mkNeovimPlugin { - "all" (default): Enable formatting for all language servers - "none": Do not enable formatting on any language server. You might choose this if for some reason you want to manually call - `require("lsp-format").on_attach(client)` in the `onAttach` function of your language - servers. + `require("lsp-format").on_attach(client)`. - list of LS names: Manually choose the servers by name ''; example = [ @@ -91,28 +90,41 @@ lib.nixvim.plugins.mkNeovimPlugin { extraConfig = cfg: { warnings = lib.nixvim.mkWarnings "plugins.lsp-format" { - when = !config.plugins.lsp.enable; + when = !(config.plugins.lsp.enable || config.plugins.lspconfig.enable); message = '' - This plugin requires `plugins.lsp` to be enabled. + This plugin requires either `plugins.lsp` or `plugins.lspconfig` to be enabled. ''; }; - plugins.lsp = { - onAttach = - lib.mkIf (cfg.lspServersToEnable == "all") # Lua - '' - require("lsp-format").on_attach(client) + autoGroups.nixvim_lsp_fmt_setup.clear = false; + + autoCmd = lib.optionals (cfg.lspServersToEnable != "none") [ + { + desc = "set up autoformatting on LSP attach"; + event = "LspAttach"; + group = "nixvim_lsp_fmt_setup"; + callback = + let + enableCondition = + if cfg.lspServersToEnable == "all" then + "true" + else if lib.isList cfg.lspServersToEnable then + # Lua + "vim.list_contains(${lib.nixvim.toLuaObject cfg.lspServersToEnable}, client.name)" + else + throw "Unhandled value for `lspServersToEnable`: ${ + lib.generators.toPretty { } cfg.lspServersToEnable + }"; + in + lib.nixvim.mkRaw '' + function(args) + local client = assert(vim.lsp.get_client_by_id(args.data.client_id)) + if ${enableCondition} then + require("lsp-format").on_attach(client, args.buf) + end + end ''; - - servers = lib.optionalAttrs (lib.isList cfg.lspServersToEnable) ( - lib.genAttrs cfg.lspServersToEnable (serverName: { - onAttach.function = # Lua - '' - require("lsp-format").on_attach(client) - ''; - }) - ); - - }; + } + ]; }; } diff --git a/tests/test-sources/plugins/by-name/lsp-format/default.nix b/tests/test-sources/plugins/by-name/lsp-format/default.nix index 65825ef5..ce829c6b 100644 --- a/tests/test-sources/plugins/by-name/lsp-format/default.nix +++ b/tests/test-sources/plugins/by-name/lsp-format/default.nix @@ -1,10 +1,10 @@ { - empty = { + legacy-empty = { plugins.lsp.enable = true; plugins.lsp-format.enable = true; }; - example = { + legacy-example = { plugins = { lsp = { enable = true; @@ -37,4 +37,43 @@ }; }; }; + + empty = { + plugins.lspconfig.enable = true; + plugins.lsp-format.enable = true; + }; + + none = { + plugins.lspconfig.enable = true; + plugins.lsp-format = { + enable = true; + lspServersToEnable = "none"; + }; + }; + + example = { + plugins = { + lspconfig.enable = true; + lsp.servers.gopls.enable = true; + + lsp-format = { + enable = true; + + settings = { + go = { + exclude = [ "gopls" ]; + order = [ + "gopls" + "efm" + ]; + sync = true; + force = true; + + # Test the ability to provide extra options for each filetype + someRandomOption = 42; + }; + }; + }; + }; + }; }