neovim-flake/lib/languages.nix

120 lines
3.1 KiB
Nix
Raw Normal View History

2024-03-23 20:14:39 -04:00
{lib}: let
inherit (builtins) isString getAttr;
inherit (lib.options) mkOption;
2025-05-20 15:44:57 +01:00
inherit (lib.strings) concatStringsSep;
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.types) luaInline;
2025-05-20 15:44:57 +01:00
inherit (lib.lists) isList;
inherit (lib) genAttrs recursiveUpdate;
2024-03-23 20:14:39 -04:00
in {
# TODO: remove
diagnosticsToLua = {
lang,
config,
2024-03-23 20:14:39 -04:00
diagnosticsProviders,
}:
mapListToAttrs
(v: let
type =
if isString v
then v
else getAttr v.type;
package =
if isString v
then diagnosticsProviders.${type}.package
else v.package;
in {
name = "${lang}-diagnostics-${type}";
value = diagnosticsProviders.${type}.nullConfig package;
})
config;
2023-09-23 18:36:25 +03:00
mkEnable = desc:
2024-03-23 20:14:39 -04:00
mkOption {
2023-09-23 18:36:25 +03:00
default = false;
2024-12-03 00:52:08 +03:00
type = bool;
description = "Turn on ${desc} for enabled languages by default";
2023-09-23 18:36:25 +03:00
};
2025-05-20 15:44:57 +01:00
# 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 = {
enable = mkOption {
type = bool;
default = true;
description = "Whether to enable this LSP server.";
};
capabilities = mkOption {
type = nullOr (either luaInline (attrsOf anything));
default = null;
description = "LSP capabilitiess to pass to lspconfig";
};
on_attach = mkOption {
type = nullOr luaInline;
default = null;
description = "Function to execute when an LSP server attaches to a buffer";
};
filetypes = mkOption {
type = nullOr (listOf str);
default = null;
description = "Filetypes to auto-attach LSP in";
};
cmd = mkOption {
type = nullOr (listOf str);
default = null;
description = "Command used to start the LSP server";
};
root_markers = mkOption {
type = nullOr (listOf str);
default = null;
description = ''
"root markers" used to determine the root directory of the workspace, and
the filetypes associated with this LSP server.
'';
};
};
};
2025-05-20 15:44:57 +01:00
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);
}