From 53084257189483605182aaf388696ace02f5d208 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Mon, 5 May 2025 22:36:48 +0100 Subject: [PATCH] modules/lsp/servers: move to dedicated file/dir Move the code related to the `lsp.servers` option into a dedicated module, cleaning up `modules/lsp/default.nix`. --- modules/lsp/default.nix | 178 ++----------------- modules/lsp/servers/default.nix | 163 +++++++++++++++++ modules/lsp/{ => servers}/global-server.nix | 0 modules/lsp/{ => servers}/server-renames.nix | 0 modules/lsp/{ => servers}/server.nix | 0 5 files changed, 175 insertions(+), 166 deletions(-) create mode 100644 modules/lsp/servers/default.nix rename modules/lsp/{ => servers}/global-server.nix (100%) rename modules/lsp/{ => servers}/server-renames.nix (100%) rename modules/lsp/{ => servers}/server.nix (100%) diff --git a/modules/lsp/default.nix b/modules/lsp/default.nix index c8a35fb6..af4c8e9f 100644 --- a/modules/lsp/default.nix +++ b/modules/lsp/default.nix @@ -1,64 +1,6 @@ -{ - lib, - config, - options, - pkgs, - ... -}: +{ lib, config, ... }: let - inherit (lib) types; - inherit (lib.nixvim) toLuaObject; - cfg = config.lsp; - - # Import `server.nix` and apply args - # For convenience, we set a default here for args.pkgs - mkServerModule = args: lib.modules.importApply ./server.nix ({ inherit pkgs; } // args); - - # Create a submodule type from `server.nix` - # Used as the type for both the freeform `lsp.servers.` - # and the explicitly declared `lsp.servers.*` options - mkServerType = args: types.submodule (mkServerModule args); - - # Create a server option - # Used below for the `lsp.servers.*` options - mkServerOption = - name: args: - let - homepage = lib.pipe options.lsp.servers [ - # Get suboptions of `lsp.servers` - (opt: opt.type.getSubOptions opt.loc) - # Get suboptions of `lsp.servers.` - (opts: opts.${name}.type.getSubOptions opts.${name}.loc) - # Get package option's homepage - (opts: opts.package.default.meta.homepage or null) - ]; - - # If there's a known homepage for this language server, - # we'll link to it in the option description - nameLink = if homepage == null then name else "[${name}](${homepage})"; - in - lib.mkOption { - type = mkServerType args; - description = '' - The ${nameLink} language server. - ''; - default = { }; - }; - - # Combine `packages` and `customCmd` sets from `lsp-packages.nix` - # We use this set to generate the package-option defaults - serverPackages = - let - inherit (import ../../plugins/lsp/lsp-packages.nix) - packages - customCmd - ; - in - builtins.mapAttrs (name: v: { - inherit name; - package = v.package or v; - }) (packages // customCmd); in { options.lsp = { @@ -73,118 +15,22 @@ in inlayHints = { enable = lib.mkEnableOption "inlay hints globally"; }; - - servers = lib.mkOption { - type = types.submodule [ - { - freeformType = types.attrsOf (mkServerType { }); - } - { - options = builtins.mapAttrs mkServerOption serverPackages; - } - { - # `*` is effectively a meta server, where shared config & defaults can be set. - # It shouldn't have options like `activate` or `package` which relate to "real" servers. - # Therefore, we use a bespoke `global-server.nix`, which is inspired by the full `server.nix` module. - options."*" = lib.mkOption { - description = '' - Global configuration applied to all language servers. - ''; - type = types.submodule ./global-server.nix; - default = { }; - }; - } - ]; - - description = '' - LSP servers to enable and/or configure. - - This option is implemented using neovim's `vim.lsp` lua API. - - You may also want to use [nvim-lspconfig] to install _default configs_ for many language servers. - This can be installed using [`${options.plugins.lspconfig.enable}`][`plugins.lspconfig`]. - - [nvim-lspconfig]: ${options.plugins.lspconfig.package.default.meta.homepage} - [`plugins.lspconfig`]: ../../plugins/lspconfig/index.md - ''; - default = { }; - example = { - "*".settings = { - root_markers = [ ".git" ]; - capabilities.textDocument.semanticTokens = { - multilineTokenSupport = true; - }; - }; - luals.enable = true; - clangd = { - enable = true; - settings = { - cmd = [ - "clangd" - "--background-index" - ]; - root_markers = [ - "compile_commands.json" - "compile_flags.txt" - ]; - filetypes = [ - "c" - "cpp" - ]; - }; - }; - }; - }; }; imports = [ + ./servers ./keymaps.nix ]; - config = - let - enabledServers = lib.pipe cfg.servers [ - builtins.attrValues - (builtins.filter (server: server.enable)) - ]; + config = { + lsp.luaConfig.content = lib.mkIf cfg.inlayHints.enable "vim.lsp.inlay_hint.enable(true)"; - # Collect per-server warnings - serverWarnings = lib.pipe cfg.servers [ - builtins.attrValues - (builtins.catAttrs "warnings") - builtins.concatLists - ]; - in - { - extraPackages = builtins.catAttrs "package" enabledServers; - - lsp.luaConfig.content = - let - mkServerConfig = - server: - let - luaName = toLuaObject server.name; - luaSettings = toLuaObject server.settings; - in - [ - (lib.mkIf (server.settings != { }) "vim.lsp.config(${luaName}, ${luaSettings})") - (lib.mkIf (server.activate or false) "vim.lsp.enable(${luaName})") - ]; - in - lib.mkMerge ( - lib.optional cfg.inlayHints.enable "vim.lsp.inlay_hint.enable(true)" - ++ builtins.concatMap mkServerConfig enabledServers - ); - - extraConfigLua = lib.mkIf (cfg.luaConfig.content != "") '' - -- LSP {{{ - do - ${cfg.luaConfig.content} - end - -- }}} - ''; - - # Propagate per-server warnings - warnings = lib.mkIf (serverWarnings != [ ]) serverWarnings; - }; + extraConfigLua = lib.mkIf (cfg.luaConfig.content != "") '' + -- LSP {{{ + do + ${cfg.luaConfig.content} + end + -- }}} + ''; + }; } diff --git a/modules/lsp/servers/default.nix b/modules/lsp/servers/default.nix new file mode 100644 index 00000000..e540f26b --- /dev/null +++ b/modules/lsp/servers/default.nix @@ -0,0 +1,163 @@ +{ + lib, + config, + options, + pkgs, + ... +}: +let + inherit (lib) types; + inherit (lib.nixvim) toLuaObject; + + cfg = config.lsp; + + # Import `server.nix` and apply args + # For convenience, we set a default here for args.pkgs + mkServerModule = args: lib.modules.importApply ./server.nix ({ inherit pkgs; } // args); + + # Create a submodule type from `server.nix` + # Used as the type for both the freeform `lsp.servers.` + # and the explicitly declared `lsp.servers.*` options + mkServerType = args: types.submodule (mkServerModule args); + + # Create a server option + # Used below for the `lsp.servers.*` options + mkServerOption = + name: args: + let + homepage = lib.pipe options.lsp.servers [ + # Get suboptions of `lsp.servers` + (opt: opt.type.getSubOptions opt.loc) + # Get suboptions of `lsp.servers.` + (opts: opts.${name}.type.getSubOptions opts.${name}.loc) + # Get package option's homepage + (opts: opts.package.default.meta.homepage or null) + ]; + + # If there's a known homepage for this language server, + # we'll link to it in the option description + nameLink = if homepage == null then name else "[${name}](${homepage})"; + in + lib.mkOption { + type = mkServerType args; + description = '' + The ${nameLink} language server. + ''; + default = { }; + }; + + # Combine `packages` and `customCmd` sets from `lsp-packages.nix` + # We use this set to generate the package-option defaults + serverPackages = + let + inherit (import ../../../plugins/lsp/lsp-packages.nix) + packages + customCmd + ; + in + builtins.mapAttrs (name: v: { + inherit name; + package = v.package or v; + }) (packages // customCmd); +in +{ + options.lsp = { + servers = lib.mkOption { + type = types.submodule [ + { + freeformType = types.attrsOf (mkServerType { }); + } + { + options = builtins.mapAttrs mkServerOption serverPackages; + } + { + # `*` is effectively a meta server, where shared config & defaults can be set. + # It shouldn't have options like `activate` or `package` which relate to "real" servers. + # Therefore, we use a bespoke `global-server.nix`, which is inspired by the full `server.nix` module. + options."*" = lib.mkOption { + description = '' + Global configuration applied to all language servers. + ''; + type = types.submodule ./global-server.nix; + default = { }; + }; + } + ]; + + description = '' + LSP servers to enable and/or configure. + + This option is implemented using neovim's `vim.lsp` lua API. + + You may also want to use [nvim-lspconfig] to install _default configs_ for many language servers. + This can be installed using [`${options.plugins.lspconfig.enable}`][`plugins.lspconfig`]. + + [nvim-lspconfig]: ${options.plugins.lspconfig.package.default.meta.homepage} + [`plugins.lspconfig`]: ../../plugins/lspconfig/index.md + ''; + default = { }; + example = { + "*".settings = { + root_markers = [ ".git" ]; + capabilities.textDocument.semanticTokens = { + multilineTokenSupport = true; + }; + }; + luals.enable = true; + clangd = { + enable = true; + settings = { + cmd = [ + "clangd" + "--background-index" + ]; + root_markers = [ + "compile_commands.json" + "compile_flags.txt" + ]; + filetypes = [ + "c" + "cpp" + ]; + }; + }; + }; + }; + }; + + config = + let + enabledServers = lib.pipe cfg.servers [ + builtins.attrValues + (builtins.filter (server: server.enable)) + ]; + + # Collect per-server warnings + serverWarnings = lib.pipe cfg.servers [ + builtins.attrValues + (builtins.catAttrs "warnings") + builtins.concatLists + ]; + in + { + extraPackages = builtins.catAttrs "package" enabledServers; + + lsp.luaConfig.content = + let + mkServerConfig = + server: + let + luaName = toLuaObject server.name; + luaSettings = toLuaObject server.settings; + in + [ + (lib.mkIf (server.settings != { }) "vim.lsp.config(${luaName}, ${luaSettings})") + (lib.mkIf (server.activate or false) "vim.lsp.enable(${luaName})") + ]; + in + lib.mkMerge (builtins.concatMap mkServerConfig enabledServers); + + # Propagate per-server warnings + warnings = lib.mkIf (serverWarnings != [ ]) serverWarnings; + }; +} diff --git a/modules/lsp/global-server.nix b/modules/lsp/servers/global-server.nix similarity index 100% rename from modules/lsp/global-server.nix rename to modules/lsp/servers/global-server.nix diff --git a/modules/lsp/server-renames.nix b/modules/lsp/servers/server-renames.nix similarity index 100% rename from modules/lsp/server-renames.nix rename to modules/lsp/servers/server-renames.nix diff --git a/modules/lsp/server.nix b/modules/lsp/servers/server.nix similarity index 100% rename from modules/lsp/server.nix rename to modules/lsp/servers/server.nix