simplify and remove unnecessary stuff

This commit is contained in:
sjcobb 2025-05-22 14:48:14 +01:00
commit 7ae81ca51b
3 changed files with 27 additions and 94 deletions

View file

@ -1,13 +1,9 @@
{lib}: let {lib}: let
inherit (builtins) isString getAttr; inherit (builtins) isString getAttr;
inherit (lib.options) mkOption; inherit (lib.options) mkOption;
inherit (lib.strings) concatStringsSep; inherit (lib.types) listOf bool str submodule attrsOf anything either nullOr;
inherit (lib.types) listOf bool str submodule attrsOf anything either nullOr oneOf enum;
inherit (lib.attrsets) attrNames;
inherit (lib.nvim.attrsets) mapListToAttrs; inherit (lib.nvim.attrsets) mapListToAttrs;
inherit (lib.nvim.types) luaInline; inherit (lib.nvim.types) luaInline;
inherit (lib.lists) isList;
inherit (lib) genAttrs recursiveUpdate;
in { in {
# TODO: remove # TODO: remove
diagnosticsToLua = { diagnosticsToLua = {
@ -38,18 +34,6 @@ in {
description = "Turn on ${desc} for enabled languages by default"; 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 { lspOptions = submodule {
freeformType = attrsOf anything; freeformType = attrsOf anything;
options = { 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);
} }

View file

@ -18,36 +18,12 @@
lspConfigurations = lspConfigurations =
mapAttrsToList ( mapAttrsToList (
# TODO: Determine the best thing to do about merging in lspconfig name: value: ''
name: value: vim.lsp.config["${name}"] = ${toLuaObject value}
/* ''
lua
*/
''vim.lsp.config["${name}"] = ${toLuaObject value}''
) )
cfg.servers; 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; enabledServers = filterAttrs (_: u: u.enable) cfg.servers;
in { in {
options = { options = {
@ -105,18 +81,13 @@ in {
} }
(mkIf (cfg.servers != {}) { (mkIf (cfg.servers != {}) {
vim.luaConfigRC.lsp-servers = vim.luaConfigRC.lsp-servers = entryAnywhere ''
entryAnywhere -- Individual LSP configurations managed by nvf.
# Or entryAfter ["lspconfig"] if we go with the second approach ${concatLines lspConfigurations}
/*
lua
*/
''
${concatLines lspConfigurations}
-- Enable configured LSPs explicitly -- Enable configured LSPs explicitly
vim.lsp.enable(${toLuaObject (filter (name: name != "*") (attrNames enabledServers))}); vim.lsp.enable(${toLuaObject (filter (name: name != "*") (attrNames enabledServers))})
''; '';
}) })
]; ];
} }

View file

@ -6,15 +6,14 @@
}: let }: let
inherit (builtins) attrNames; inherit (builtins) attrNames;
inherit (lib) concatStringsSep; inherit (lib) concatStringsSep;
inherit (lib.generators) mkLuaInline;
inherit (lib.meta) getExe; inherit (lib.meta) getExe;
inherit (lib.options) mkEnableOption mkOption; inherit (lib.options) mkEnableOption mkOption;
inherit (lib.modules) mkIf mkMerge; inherit (lib.modules) mkIf mkMerge;
inherit (lib.lists) isList; inherit (lib.lists) isList;
inherit (lib.types) enum package; inherit (lib.types) enum package listOf;
inherit (lib.nvim.types) mkGrammarOption diagnostics; inherit (lib.nvim.types) mkGrammarOption diagnostics;
inherit (lib.nvim.attrsets) mapListToAttrs;
inherit (lib.nvim.lua) expToLua; inherit (lib.nvim.lua) expToLua;
inherit (lib.nvim.languages) resolveLspOptions mkLspOption;
cfg = config.vim.languages.nix; cfg = config.vim.languages.nix;
@ -23,18 +22,18 @@
then expToLua package then expToLua package
else ''{"${package}/bin/${defaultCmd}"}''; else ''{"${package}/bin/${defaultCmd}"}'';
formattingCmd = lib.mkIf (cfg.format.enable && cfg.lsp.enable) { formattingCmd = mkIf (cfg.format.enable && cfg.lsp.enable) {
formatting = lib.mkMerge [ formatting = mkMerge [
(lib.mkIf (cfg.format.type == "alejandra") { (mkIf (cfg.format.type == "alejandra") {
command = ["${cfg.format.package}/bin/alejandra" "--quiet"]; command = ["${cfg.format.package}/bin/alejandra" "--quiet"];
}) })
(lib.mkIf (cfg.format.type == "nixfmt") { (mkIf (cfg.format.type == "nixfmt") {
command = ["${cfg.format.package}/bin/nixfmt"]; command = ["${cfg.format.package}/bin/nixfmt"];
}) })
]; ];
}; };
defaultServers = ["nil_ls"] defaultServers = ["nil_ls"];
servers = { servers = {
nil_ls = { nil_ls = {
enable = true; enable = true;
@ -101,9 +100,11 @@ in {
lsp = { lsp = {
enable = mkEnableOption "Nix LSP support" // {default = config.vim.lsp.enable;}; enable = mkEnableOption "Nix LSP support" // {default = config.vim.lsp.enable;};
servers = mkLspOption { servers = mkOption {
inherit servers; description = "Nix LSP server to use";
type = listOf (enum (attrNames servers));
default = defaultServers; default = defaultServers;
example = ["nixd"];
}; };
}; };
@ -153,14 +154,14 @@ in {
}) })
(mkIf cfg.lsp.enable { (mkIf cfg.lsp.enable {
# TODO: Map this to include lspconfig stuff so that we can do vim.lsp.servers =
vim.lsp.servers = resolveLspOptions { mapListToAttrs (n: {
inherit servers; name = n;
selected = cfg.lsp.servers; 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) { (mkIf (cfg.format.enable && !cfg.lsp.enable) {
vim.formatter.conform-nvim = { vim.formatter.conform-nvim = {
enable = true; enable = true;