From 20578280969ace1cd8103ed939162f60e7d400b6 Mon Sep 17 00:00:00 2001 From: Gaetan Lepage Date: Fri, 12 May 2023 11:01:10 +0200 Subject: [PATCH] plugins/lsp-format: add plugin + test --- plugins/default.nix | 1 + plugins/lsp/helpers.nix | 2 +- plugins/lsp/lsp-format.nix | 117 ++++++++++++++++++ tests/test-sources/plugins/lsp/lsp-format.nix | 37 ++++++ 4 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 plugins/lsp/lsp-format.nix create mode 100644 tests/test-sources/plugins/lsp/lsp-format.nix diff --git a/plugins/default.nix b/plugins/default.nix index db30268b..49462a9f 100644 --- a/plugins/default.nix +++ b/plugins/default.nix @@ -48,6 +48,7 @@ ./lsp ./lsp/inc-rename.nix ./lsp/lspsaga.nix + ./lsp/lsp-format.nix ./lsp/lsp-lines.nix ./lsp/nvim-lightbulb.nix ./lsp/trouble.nix diff --git a/plugins/lsp/helpers.nix b/plugins/lsp/helpers.nix index f61fffb3..33d82581 100644 --- a/plugins/lsp/helpers.nix +++ b/plugins/lsp/helpers.nix @@ -68,7 +68,7 @@ }; function = mkOption { - type = types.str; + type = types.lines; description = '' Body of the on_attach function. The argument `client` and `bufnr` is provided. diff --git a/plugins/lsp/lsp-format.nix b/plugins/lsp/lsp-format.nix new file mode 100644 index 00000000..a02f3770 --- /dev/null +++ b/plugins/lsp/lsp-format.nix @@ -0,0 +1,117 @@ +{ + pkgs, + lib, + config, + ... +}: +with lib; let + cfg = config.plugins.lsp-format; + helpers = import ../helpers.nix {inherit lib;}; +in { + options.plugins.lsp-format = + helpers.extraOptionsOptions + // { + enable = mkEnableOption "lsp-format.nvim"; + + package = helpers.mkPackageOption "lsp-format.nvim" pkgs.vimPlugins.lsp-format-nvim; + + setup = mkOption { + type = with types; + attrsOf + (submodule { + # Allow the user to provide other options + freeformType = types.attrs; + + options = { + exclude = + helpers.mkNullOrOption (listOf str) + "List of client names to exclude from formatting."; + + order = + helpers.mkNullOrOption (listOf str) + '' + List of client names. Formatting is requested from clients in the following + order: first all clients that are not in the `order` table, then the remaining + clients in the order as they occur in the `order` table. + (same logic as |vim.lsp.buf.formatting_seq_sync()|). + ''; + + sync = helpers.defaultNullOpts.mkBool false '' + Whether to turn on synchronous formatting. + The editor will block until formatting is done. + ''; + + force = helpers.defaultNullOpts.mkBool false '' + If true, the format result will always be written to the buffer, even if the + buffer changed. + ''; + }; + }); + description = "The setup option maps |filetypes| to format options."; + example = { + gopls = { + exclude = ["gopls"]; + order = ["gopls" "efm"]; + sync = true; + force = true; + }; + }; + default = {}; + }; + + lspServersToEnable = mkOption { + type = with types; either (enum ["none" "all"]) (listOf str); + default = "all"; + description = '' + Choose the LSP servers for which lsp-format should be enabled. + + Possible values: + - "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. + - list of LS names: Manually choose the servers by name + ''; + example = [ + "efm" + "gopls" + ]; + }; + }; + + config = let + setupOptions = cfg.setup // cfg.extraOptions; + in + mkIf cfg.enable { + warnings = mkIf (!config.plugins.lsp.enable) [ + "You have enabled `plugins.lsp-format` but have `plugins.lsp` disabled." + ]; + + extraPlugins = [cfg.package]; + + plugins.lsp = { + onAttach = mkIf (cfg.lspServersToEnable == "all") '' + require("lsp-format").on_attach(client) + ''; + + servers = + if (isList cfg.lspServersToEnable) + then + genAttrs + cfg.lspServersToEnable + ( + serverName: { + onAttach.function = '' + require("lsp-format").on_attach(client) + ''; + } + ) + else {}; + }; + + extraConfigLua = '' + require("lsp-format").setup(${helpers.toLuaObject setupOptions}) + ''; + }; +} diff --git a/tests/test-sources/plugins/lsp/lsp-format.nix b/tests/test-sources/plugins/lsp/lsp-format.nix new file mode 100644 index 00000000..4ce81d4d --- /dev/null +++ b/tests/test-sources/plugins/lsp/lsp-format.nix @@ -0,0 +1,37 @@ +{ + empty = { + plugins.lsp.enable = true; + plugins.lsp-format.enable = true; + }; + + example = { + plugins = { + lsp = { + enable = true; + + servers.gopls = { + enable = true; + onAttach.function = '' + x = 12 + ''; + }; + }; + + lsp-format = { + enable = true; + + setup = { + gopls = { + exclude = ["gopls"]; + order = ["gopls" "efm"]; + sync = true; + force = true; + + # Test the ability to provide extra options for each filetype + someRandomOption = 42; + }; + }; + }; + }; + }; +}