2024-05-14 15:44:12 +02:00
|
|
|
#########
|
|
|
|
# mkLsp #
|
|
|
|
#########
|
|
|
|
#
|
|
|
|
# Helper function that returns the Nix module `plugins.lsp.servers.<name>`.
|
|
|
|
#
|
|
|
|
{
|
|
|
|
name,
|
|
|
|
description ? "Enable ${name}.",
|
|
|
|
serverName ? name,
|
2024-10-10 19:43:40 +01:00
|
|
|
package ? null,
|
2024-09-01 09:43:53 +01:00
|
|
|
url ? null,
|
2025-01-24 23:03:18 +00:00
|
|
|
cmd ? null,
|
|
|
|
cmdText ? throw "cmdText is required when cmd is a function",
|
2024-05-14 15:44:12 +02:00
|
|
|
settings ? (cfg: cfg),
|
|
|
|
settingsOptions ? { },
|
|
|
|
extraConfig ? cfg: { },
|
2024-05-14 16:18:57 +02:00
|
|
|
extraOptions ? { },
|
2024-05-14 15:44:12 +02:00
|
|
|
...
|
2024-09-01 09:43:53 +01:00
|
|
|
}@args:
|
2024-05-14 15:44:12 +02:00
|
|
|
# returns a module
|
|
|
|
{
|
|
|
|
pkgs,
|
|
|
|
config,
|
2024-09-01 09:43:53 +01:00
|
|
|
options,
|
2024-05-14 15:44:12 +02:00
|
|
|
lib,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
let
|
|
|
|
cfg = config.plugins.lsp.servers.${name};
|
2024-11-19 13:55:33 +00:00
|
|
|
opts = options.plugins.lsp.servers.${name};
|
2024-11-19 11:36:31 +01:00
|
|
|
|
|
|
|
enabled = config.plugins.lsp.enable && cfg.enable;
|
2024-12-17 15:48:55 -03:00
|
|
|
|
|
|
|
inherit (lib) mkOption types;
|
2024-05-14 15:44:12 +02:00
|
|
|
in
|
|
|
|
{
|
2024-06-07 21:33:16 +01:00
|
|
|
meta.nixvimInfo = {
|
|
|
|
# TODO: description
|
2025-01-30 22:53:55 +01:00
|
|
|
# The package default can throw when the server is unpackaged, so use tryEval
|
|
|
|
url = args.url or (builtins.tryEval opts.package.default).value.meta.homepage or null;
|
2024-06-07 21:33:16 +01:00
|
|
|
path = [
|
|
|
|
"plugins"
|
|
|
|
"lsp"
|
|
|
|
"servers"
|
|
|
|
name
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2024-05-14 15:44:12 +02:00
|
|
|
options = {
|
|
|
|
plugins.lsp.servers.${name} = {
|
2024-12-17 15:48:55 -03:00
|
|
|
enable = lib.mkEnableOption description;
|
2024-05-14 15:44:12 +02:00
|
|
|
|
2024-09-01 09:43:53 +01:00
|
|
|
package =
|
2025-01-30 22:53:55 +01:00
|
|
|
lib.nixvim.mkMaybeUnpackagedOption "plugins.lsp.servers.${name}.package" pkgs name
|
|
|
|
package;
|
2024-05-14 15:44:12 +02:00
|
|
|
|
2025-06-08 23:30:08 +01:00
|
|
|
packageFallback = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
When enabled, the language server package will be added to the end of the `PATH` _(suffix)_ instead of the beginning _(prefix)_.
|
|
|
|
|
|
|
|
This can be useful if you want local versions of the language server (e.g. from a devshell) to override the nixvim version.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-05-14 15:44:12 +02:00
|
|
|
cmd = mkOption {
|
|
|
|
type = with types; nullOr (listOf str);
|
2024-10-05 15:55:49 +02:00
|
|
|
default =
|
2024-10-10 19:43:40 +01:00
|
|
|
# TODO: do we really only want the default `cmd` when `package` is non-null?
|
2024-11-19 13:55:33 +00:00
|
|
|
if !(opts.package.isDefined or false) then
|
2024-10-10 19:43:40 +01:00
|
|
|
null
|
|
|
|
else if cfg.package == null then
|
|
|
|
null
|
|
|
|
else if builtins.isFunction cmd then
|
|
|
|
cmd cfg
|
|
|
|
else
|
|
|
|
cmd;
|
2025-01-24 23:03:18 +00:00
|
|
|
defaultText = lib.literalMD ''
|
|
|
|
null when `package` is null, otherwise ${
|
|
|
|
if args ? cmdText || builtins.isFunction cmd then
|
|
|
|
let
|
|
|
|
literal = lib.options.renderOptionValue cmdText;
|
|
|
|
inherit (literal) text;
|
|
|
|
in
|
|
|
|
if literal._type == "literalMD" then
|
|
|
|
text
|
|
|
|
else if lib.hasInfix "\n" text || lib.hasInfix "``" text then
|
|
|
|
"\n\n```\n${text}\n```"
|
|
|
|
else
|
|
|
|
"`` ${text} ``"
|
|
|
|
else if cmd == null then
|
|
|
|
"null"
|
|
|
|
else
|
|
|
|
"`[ ${lib.concatMapStringsSep " " builtins.toJSON cmd} ]`"
|
|
|
|
}
|
|
|
|
'';
|
2024-06-09 23:04:27 +02:00
|
|
|
description = ''
|
|
|
|
A list where each entry corresponds to the blankspace delimited part of the command that
|
|
|
|
launches the server.
|
|
|
|
|
|
|
|
The first entry is the binary used to run the language server.
|
|
|
|
Additional entries are passed as arguments.
|
|
|
|
'';
|
2024-05-14 15:44:12 +02:00
|
|
|
};
|
|
|
|
|
2024-12-17 15:39:14 -03:00
|
|
|
filetypes = lib.nixvim.mkNullOrOption (types.listOf types.str) ''
|
2024-05-14 15:44:12 +02:00
|
|
|
Set of filetypes for which to attempt to resolve {root_dir}.
|
|
|
|
May be empty, or server may specify a default value.
|
|
|
|
'';
|
|
|
|
|
2024-12-17 15:39:14 -03:00
|
|
|
autostart = lib.nixvim.defaultNullOpts.mkBool true ''
|
2024-05-14 15:44:12 +02:00
|
|
|
Controls if the `FileType` autocommand that launches a language server is created.
|
|
|
|
If `false`, allows for deferring language servers until manually launched with
|
|
|
|
`:LspStart` (|lspconfig-commands|).
|
|
|
|
'';
|
|
|
|
|
2025-04-25 16:00:16 +01:00
|
|
|
rootMarkers = lib.nixvim.defaultNullOpts.mkListOf types.str null ''
|
|
|
|
A list of files that mark the root of the project/workspace.
|
|
|
|
|
|
|
|
Vim's LSP will try to share the same language server instance for all
|
|
|
|
buffers matching `filetypes` within the same project.
|
|
|
|
|
|
|
|
A new server instance is only spawned when opening a buffer with a
|
|
|
|
different project root.
|
|
|
|
|
|
|
|
See `:h lsp-config` and `:h vim.fs.root()`.
|
2024-05-14 15:44:12 +02:00
|
|
|
'';
|
|
|
|
|
2024-12-17 15:39:14 -03:00
|
|
|
onAttach = lib.nixvim.mkCompositeOption "Server specific on_attach behavior." {
|
2024-05-14 15:44:12 +02:00
|
|
|
override = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Override the global `plugins.lsp.onAttach` function.";
|
|
|
|
};
|
|
|
|
|
|
|
|
function = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
description = ''
|
|
|
|
Body of the on_attach function.
|
|
|
|
The argument `client` and `bufnr` is provided.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-12-17 15:39:14 -03:00
|
|
|
settings = lib.nixvim.mkSettingsOption {
|
2024-05-14 15:44:12 +02:00
|
|
|
description = "The settings for this LSP.";
|
|
|
|
options = settingsOptions;
|
|
|
|
};
|
|
|
|
|
|
|
|
extraOptions = mkOption {
|
|
|
|
default = { };
|
|
|
|
type = types.attrsOf types.anything;
|
|
|
|
description = "Extra options for the ${name} language server.";
|
|
|
|
};
|
2024-05-14 16:18:57 +02:00
|
|
|
} // extraOptions;
|
2024-05-14 15:44:12 +02:00
|
|
|
};
|
|
|
|
|
2024-12-17 15:48:55 -03:00
|
|
|
config = lib.mkIf enabled {
|
2025-06-08 23:30:08 +01:00
|
|
|
extraPackages = lib.optional (!cfg.packageFallback) cfg.package;
|
|
|
|
extraPackagesAfter = lib.optional cfg.packageFallback cfg.package;
|
2024-05-14 15:44:12 +02:00
|
|
|
|
|
|
|
plugins.lsp.enabledServers = [
|
|
|
|
{
|
|
|
|
name = serverName;
|
|
|
|
extraOptions = {
|
|
|
|
inherit (cfg) cmd filetypes autostart;
|
2025-04-25 16:00:16 +01:00
|
|
|
root_markers = cfg.rootMarkers;
|
2024-12-17 15:39:14 -03:00
|
|
|
on_attach = lib.nixvim.ifNonNull' cfg.onAttach (
|
|
|
|
lib.nixvim.mkRaw ''
|
2024-05-14 15:44:12 +02:00
|
|
|
function(client, bufnr)
|
2024-12-17 15:48:55 -03:00
|
|
|
${lib.optionalString (!cfg.onAttach.override) config.plugins.lsp.onAttach}
|
2024-05-14 15:44:12 +02:00
|
|
|
${cfg.onAttach.function}
|
|
|
|
end
|
|
|
|
''
|
|
|
|
);
|
2024-12-18 22:12:51 +00:00
|
|
|
settings = lib.nixvim.plugins.utils.applyExtraConfig {
|
2024-11-19 13:55:33 +00:00
|
|
|
extraConfig = settings;
|
|
|
|
cfg = cfg.settings;
|
|
|
|
opts = opts.settings;
|
|
|
|
enabled = true;
|
|
|
|
};
|
2024-05-14 15:44:12 +02:00
|
|
|
} // cfg.extraOptions;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
2024-06-02 18:58:06 +01:00
|
|
|
|
2024-05-14 15:44:12 +02:00
|
|
|
imports =
|
|
|
|
let
|
2024-06-02 18:58:06 +01:00
|
|
|
basePath = [
|
2024-05-14 15:44:12 +02:00
|
|
|
"plugins"
|
|
|
|
"lsp"
|
|
|
|
"servers"
|
|
|
|
];
|
2024-06-02 18:58:06 +01:00
|
|
|
basePluginPath = basePath ++ [ name ];
|
2024-12-17 15:48:55 -03:00
|
|
|
basePluginPathString = builtins.concatStringsSep "." basePluginPath;
|
2024-05-14 15:44:12 +02:00
|
|
|
in
|
|
|
|
[
|
2024-12-17 15:48:55 -03:00
|
|
|
(lib.mkRemovedOptionModule (
|
2024-05-14 15:44:12 +02:00
|
|
|
basePluginPath ++ [ "extraSettings" ]
|
|
|
|
) "You can use `${basePluginPathString}.extraOptions.settings` instead.")
|
2025-04-25 16:00:16 +01:00
|
|
|
(lib.mkRemovedOptionModule (basePluginPath ++ [ "rootDir" ]) ''
|
|
|
|
|
|
|
|
nvim-lspconfig has switched from its own `root_dir` implementation to using neovim's built-in LSP API.
|
|
|
|
|
|
|
|
In most cases you can use `${opts.rootMarkers}` instead. It should be a list of files that mark the root of the project.
|
|
|
|
In more complex cases you can still use `${opts.extraOptions}.root_dir`.
|
|
|
|
'')
|
2024-06-02 18:58:06 +01:00
|
|
|
]
|
2024-11-19 13:55:33 +00:00
|
|
|
++ lib.optional (args ? extraConfig) (
|
2024-12-18 22:12:51 +00:00
|
|
|
lib.nixvim.plugins.utils.applyExtraConfig {
|
2024-11-19 13:55:33 +00:00
|
|
|
inherit
|
|
|
|
extraConfig
|
|
|
|
cfg
|
|
|
|
opts
|
|
|
|
enabled
|
|
|
|
;
|
|
|
|
}
|
|
|
|
)
|
2024-06-02 18:58:06 +01:00
|
|
|
# Add an alias (with warning) for the lspconfig server name, if different to `name`.
|
|
|
|
# Note: users may use lspconfig's docs to guess the `plugins.lsp.servers.*` name
|
2024-12-17 15:48:55 -03:00
|
|
|
++ (lib.optional (name != serverName) (
|
|
|
|
lib.mkRenamedOptionModule (basePath ++ [ serverName ]) basePluginPath
|
2024-06-02 18:58:06 +01:00
|
|
|
));
|
2024-05-14 15:44:12 +02:00
|
|
|
}
|