From 7ae81ca51b45b20be295cfcdd78ce6ff9531389d Mon Sep 17 00:00:00 2001 From: sjcobb Date: Thu, 22 May 2025 14:48:14 +0100 Subject: [PATCH 1/2] simplify and remove unnecessary stuff --- lib/languages.nix | 41 +-------------------------- modules/neovim/init/lsp.nix | 47 ++++++------------------------- modules/plugins/languages/nix.nix | 33 +++++++++++----------- 3 files changed, 27 insertions(+), 94 deletions(-) diff --git a/lib/languages.nix b/lib/languages.nix index 556c38cf..c4074144 100644 --- a/lib/languages.nix +++ b/lib/languages.nix @@ -1,13 +1,9 @@ {lib}: let inherit (builtins) isString getAttr; inherit (lib.options) mkOption; - inherit (lib.strings) concatStringsSep; - inherit (lib.types) listOf bool str submodule attrsOf anything either nullOr oneOf enum; - inherit (lib.attrsets) attrNames; + inherit (lib.types) listOf bool str submodule attrsOf anything either nullOr; inherit (lib.nvim.attrsets) mapListToAttrs; inherit (lib.nvim.types) luaInline; - inherit (lib.lists) isList; - inherit (lib) genAttrs recursiveUpdate; in { # TODO: remove diagnosticsToLua = { @@ -38,18 +34,6 @@ in { description = "Turn on ${desc} for enabled languages by default"; }; - # resolveLspOptions - # servers: AttrsOf lspOptions - # selected: AttrsOf lspOptions | List of string keys from servers - # Returns: AttrsOf lspOptions - resolveLspOptions = { - servers, - selected, - }: - if isList selected - then genAttrs selected (name: servers.${name}) - else selected; - lspOptions = submodule { freeformType = attrsOf anything; options = { @@ -93,27 +77,4 @@ in { }; }; }; - - mkLspOption = {servers, ...} @ args: let - serverNames = attrNames servers; - defaultAttrs = { - type = oneOf [ - (attrsOf lib.nvim.languages.lspOptions) - (listOf (enum serverNames)) - ]; - description = '' - Either a full set of selected LSP options as an attribute set, - or a list of server names from: ${concatStringsSep ", " serverNames}. - ''; - default = {}; - example = { - clangd = { - filetypes = ["c"]; - root_markers = ["CMakeLists.txt"]; - }; - }; - }; - cleanedArgs = removeAttrs args ["servers"]; - in - mkOption (recursiveUpdate defaultAttrs cleanedArgs); } diff --git a/modules/neovim/init/lsp.nix b/modules/neovim/init/lsp.nix index 1b22388c..b89c3fde 100644 --- a/modules/neovim/init/lsp.nix +++ b/modules/neovim/init/lsp.nix @@ -18,36 +18,12 @@ lspConfigurations = mapAttrsToList ( - # TODO: Determine the best thing to do about merging in lspconfig - name: value: - /* - lua - */ - ''vim.lsp.config["${name}"] = ${toLuaObject value}'' + name: value: '' + vim.lsp.config["${name}"] = ${toLuaObject value} + '' ) cfg.servers; - # Approach 1: - # Create function perhaps called mkLspConfig - # mkLspConfig servers; - # that expands to something like - # vim.lsp.servers = cfg.lsp.servers...... - # vim.luaConfigRC.lspconfigMerge = entryAfter ["lsp-servers"] ''vim.lsp.config["${name}"] = vim.tbl_deep_extend("force", lspconfig.${name}, vim.lsp.config["${name}"])'' - - - # Approach 2: - # lspConfigurations = - # mapAttrsToList ( - # name: value: ''vim.lsp.config["${name}"] = vim.tbl_deep_extend("force", lspconfig.${name}, ${toLuaObject value})'' - # ) - # (filterAttrs (n: _: n != "*") cfg.servers); - # Then also need to configure global * settings - # globalConfiguration = - # mapAttrsToList ( - # name: value: ''vim.lsp.config["${name}"] = ${toLuaObject value}'' - # ) - # (filterAttrs (n: _: n == "*") cfg.servers); - enabledServers = filterAttrs (_: u: u.enable) cfg.servers; in { options = { @@ -105,18 +81,13 @@ in { } (mkIf (cfg.servers != {}) { - vim.luaConfigRC.lsp-servers = - entryAnywhere - # Or entryAfter ["lspconfig"] if we go with the second approach - /* - lua - */ - '' - ${concatLines lspConfigurations} + vim.luaConfigRC.lsp-servers = entryAnywhere '' + -- Individual LSP configurations managed by nvf. + ${concatLines lspConfigurations} - -- Enable configured LSPs explicitly - vim.lsp.enable(${toLuaObject (filter (name: name != "*") (attrNames enabledServers))}); - ''; + -- Enable configured LSPs explicitly + vim.lsp.enable(${toLuaObject (filter (name: name != "*") (attrNames enabledServers))}) + ''; }) ]; } diff --git a/modules/plugins/languages/nix.nix b/modules/plugins/languages/nix.nix index 39f2a76b..9edf5637 100644 --- a/modules/plugins/languages/nix.nix +++ b/modules/plugins/languages/nix.nix @@ -6,15 +6,14 @@ }: let inherit (builtins) attrNames; inherit (lib) concatStringsSep; - inherit (lib.generators) mkLuaInline; inherit (lib.meta) getExe; inherit (lib.options) mkEnableOption mkOption; inherit (lib.modules) mkIf mkMerge; inherit (lib.lists) isList; - inherit (lib.types) enum package; + inherit (lib.types) enum package listOf; inherit (lib.nvim.types) mkGrammarOption diagnostics; + inherit (lib.nvim.attrsets) mapListToAttrs; inherit (lib.nvim.lua) expToLua; - inherit (lib.nvim.languages) resolveLspOptions mkLspOption; cfg = config.vim.languages.nix; @@ -23,18 +22,18 @@ then expToLua package else ''{"${package}/bin/${defaultCmd}"}''; - formattingCmd = lib.mkIf (cfg.format.enable && cfg.lsp.enable) { - formatting = lib.mkMerge [ - (lib.mkIf (cfg.format.type == "alejandra") { + formattingCmd = mkIf (cfg.format.enable && cfg.lsp.enable) { + formatting = mkMerge [ + (mkIf (cfg.format.type == "alejandra") { command = ["${cfg.format.package}/bin/alejandra" "--quiet"]; }) - (lib.mkIf (cfg.format.type == "nixfmt") { + (mkIf (cfg.format.type == "nixfmt") { command = ["${cfg.format.package}/bin/nixfmt"]; }) ]; }; - defaultServers = ["nil_ls"] + defaultServers = ["nil_ls"]; servers = { nil_ls = { enable = true; @@ -101,9 +100,11 @@ in { lsp = { enable = mkEnableOption "Nix LSP support" // {default = config.vim.lsp.enable;}; - servers = mkLspOption { - inherit servers; + servers = mkOption { + description = "Nix LSP server to use"; + type = listOf (enum (attrNames servers)); default = defaultServers; + example = ["nixd"]; }; }; @@ -153,14 +154,14 @@ in { }) (mkIf cfg.lsp.enable { - # TODO: Map this to include lspconfig stuff so that we can do - vim.lsp.servers = resolveLspOptions { - inherit servers; - selected = cfg.lsp.servers; - }; + vim.lsp.servers = + mapListToAttrs (n: { + name = n; + value = servers.${n}; + }) + cfg.lsp.servers; }) - # TODO: Figure out what do here. This is not necessarily correct as other lsps might not have formatting by default (mkIf (cfg.format.enable && !cfg.lsp.enable) { vim.formatter.conform-nvim = { enable = true; From 96f3524a4ddd8229da0c61cfef8f701259fcbe51 Mon Sep 17 00:00:00 2001 From: sjcobb Date: Thu, 22 May 2025 16:11:38 +0100 Subject: [PATCH 2/2] add markdown as well --- modules/neovim/init/lsp.nix | 6 ++-- modules/plugins/languages/markdown.nix | 42 +++++++++----------------- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/modules/neovim/init/lsp.nix b/modules/neovim/init/lsp.nix index b89c3fde..712f88f9 100644 --- a/modules/neovim/init/lsp.nix +++ b/modules/neovim/init/lsp.nix @@ -11,7 +11,7 @@ inherit (lib.attrsets) mapAttrsToList attrNames filterAttrs; inherit (lib.generators) mkLuaInline; inherit (lib.nvim.languages) lspOptions; - inherit (lib.nvim.dag) entryAnywhere; + inherit (lib.nvim.dag) entryAfter; inherit (lib.nvim.lua) toLuaObject; cfg = config.vim.lsp; @@ -81,7 +81,9 @@ in { } (mkIf (cfg.servers != {}) { - vim.luaConfigRC.lsp-servers = entryAnywhere '' + # Enable lspconfig in order to merge in the predefined opts + vim.lsp.lspconfig.enable = true; + vim.luaConfigRC.lsp-servers = entryAfter ["lspconfig"] '' -- Individual LSP configurations managed by nvf. ${concatLines lspConfigurations} diff --git a/modules/plugins/languages/markdown.nix b/modules/plugins/languages/markdown.nix index 23848835..32c16240 100644 --- a/modules/plugins/languages/markdown.nix +++ b/modules/plugins/languages/markdown.nix @@ -9,27 +9,18 @@ inherit (lib.modules) mkIf mkMerge; inherit (lib.options) mkEnableOption mkOption; inherit (lib.lists) isList; - inherit (lib.types) bool enum either package listOf str nullOr; - inherit (lib.nvim.lua) expToLua toLuaObject; + inherit (lib.types) bool enum package listOf str nullOr; + inherit (lib.nvim.lua) toLuaObject; inherit (lib.nvim.types) diagnostics mkGrammarOption mkPluginSetupOption; inherit (lib.nvim.dag) entryAnywhere; + inherit (lib.nvim.attrsets) mapListToAttrs; cfg = config.vim.languages.markdown; - defaultServer = "marksman"; + defaultServers = ["marksman"]; servers = { marksman = { - package = pkgs.marksman; - lspConfig = '' - lspconfig.marksman.setup{ - capabilities = capabilities; - on_attach = default_on_attach; - cmd = ${ - if isList cfg.lsp.package - then expToLua cfg.lsp.package - else ''{"${cfg.lsp.package}/bin/marksman", "server"}'' - }, - } - ''; + enable = true; + cmd = ["${pkgs.marksman}/bin/marksman" "server"]; }; }; @@ -69,17 +60,10 @@ in { lsp = { enable = mkEnableOption "Enable Markdown LSP support" // {default = config.vim.lsp.enable;}; - server = mkOption { - type = enum (attrNames servers); - default = defaultServer; + servers = mkOption { description = "Markdown LSP server to use"; - }; - - package = mkOption { - type = either package (listOf str); - default = servers.${cfg.lsp.server}.package; - example = ''[lib.getExe pkgs.jdt-language-server " - data " " ~/.cache/jdtls/workspace "]''; - description = "Markdown LSP server package, or the command to run as a list of strings"; + type = listOf (enum (attrNames servers)); + default = defaultServers; }; }; @@ -161,8 +145,12 @@ in { }) (mkIf cfg.lsp.enable { - vim.lsp.lspconfig.enable = true; - vim.lsp.lspconfig.sources.markdown-lsp = servers.${cfg.lsp.server}.lspConfig; + vim.lsp.servers = + mapListToAttrs (n: { + name = n; + value = servers.${n}; + }) + cfg.lsp.servers; }) (mkIf cfg.format.enable {