Skip to content

Commit 5450ad7

Browse files
committed
modules/lsp/server: declare package defaults
Convert the `attrsOf (servers.nix)` option to a freeform submodule. Declare a `servers.nix` option for each lsp server listed in `lsp-packages.nix` that has a known nixpkgs package.
1 parent cd3cbb1 commit 5450ad7

File tree

3 files changed

+89
-27
lines changed

3 files changed

+89
-27
lines changed

modules/lsp/default.nix

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,57 @@
22
lib,
33
config,
44
options,
5+
pkgs,
56
...
67
}:
78
let
89
inherit (lib) types;
910
inherit (lib.nixvim) toLuaObject;
1011

1112
cfg = config.lsp;
13+
14+
mkServerModule = args: lib.modules.importApply ./server.nix ({ inherit pkgs; } // args);
15+
16+
mkServerOption =
17+
name:
18+
{
19+
package ? null,
20+
# lsp-packages.nix tracks some custom commands, however this is currently not used by `server.nix`
21+
cmd ? null,
22+
}:
23+
let
24+
homepage = lib.pipe options.lsp.servers [
25+
# Get suboptions of `lsp.servers`
26+
(opt: opt.type.getSubOptions opt.loc)
27+
# Get suboptions of `lsp.servers.<name>`
28+
(opts: opts.${name}.type.getSubOptions opts.${name}.loc)
29+
# Get package option's homepage
30+
(opts: opts.package.default.meta.homepage or null)
31+
];
32+
33+
nameLink = if homepage == null then name else "[${name}](${homepage})";
34+
in
35+
lib.mkOption {
36+
type = types.submodule (mkServerModule {
37+
inherit name package;
38+
});
39+
description = ''
40+
The ${nameLink} language server.
41+
'';
42+
default = { };
43+
};
44+
45+
# Combine `packages` and `customCmd` sets from `lsp-packages.nix`
46+
# We use this set to generate the package-option defaults
47+
serverPackages =
48+
let
49+
inherit (import ../../plugins/lsp/lsp-packages.nix)
50+
packages
51+
customCmd
52+
;
53+
normalisedPackages = builtins.mapAttrs (_: package: { inherit package; }) packages;
54+
in
55+
normalisedPackages // customCmd;
1256
in
1357
{
1458
options.lsp = {
@@ -25,7 +69,11 @@ in
2569
};
2670

2771
servers = lib.mkOption {
28-
type = types.attrsOf (types.submodule ./server.nix);
72+
type = types.submodule {
73+
freeformType = types.attrsOf (types.submodule (mkServerModule { }));
74+
options = builtins.mapAttrs mkServerOption serverPackages;
75+
};
76+
2977
description = ''
3078
LSP servers to enable and/or configure.
3179
@@ -35,7 +83,7 @@ in
3583
This can be installed using [`${options.plugins.lspconfig.enable}`][`plugins.lspconfig`].
3684
3785
[nvim-lspconfig]: ${options.plugins.lspconfig.package.default.meta.homepage}
38-
[`plugins.lspconfig`]: ../plugins/lspconfig/index.md
86+
[`plugins.lspconfig`]: ../../plugins/lspconfig/index.md
3987
'';
4088
default = { };
4189
example = {

modules/lsp/server.nix

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1+
# Usage: lib.importApply ./server.nix { /*args*/ }
2+
{
3+
name ? "the language server",
4+
package ? null,
5+
config ? null,
6+
pkgs ? { },
7+
}:
8+
let
9+
# module arg `name` shadows `name`
10+
displayName = name;
11+
in
112
{ lib, name, ... }:
213
let
314
inherit (lib) types;
415
in
516
{
617
options = {
7-
enable = lib.mkEnableOption "the language server";
18+
enable = lib.mkEnableOption displayName;
819

920
name = lib.mkOption {
1021
type = types.maybeRaw types.str;
1122
description = ''
12-
The name of the language server, supplied to functions like `vim.lsp.enable()`.
23+
The name to use for ${displayName}.
24+
Supplied to functions like `vim.lsp.enable()`.
1325
'';
1426
default = name;
1527
defaultText = lib.literalMD "the attribute name";
@@ -18,42 +30,44 @@ in
1830
activate = lib.mkOption {
1931
type = types.bool;
2032
description = ''
21-
Whether to call `vim.lsp.enable()` for this server.
33+
Whether to call `vim.lsp.enable()` for ${displayName}.
2234
'';
2335
default = true;
2436
example = false;
2537
};
2638

27-
package = lib.mkOption {
28-
type = with types; nullOr package;
29-
default = null;
30-
description = ''
31-
Package to use for this language server.
39+
package = lib.mkPackageOption pkgs (package.name or (lib.strings.removePrefix "the " displayName)) {
40+
nullable = true;
41+
default = package.default or package;
42+
example = package.example or null;
43+
extraDescription = ''
44+
${package.extraDescription or ""}
3245
33-
Alternatively, the language server should be available on your `$PATH`.
46+
Alternatively, ${displayName} should be installed on your `$PATH`.
3447
'';
3548
};
3649

3750
config = lib.mkOption {
3851
type = with types; attrsOf anything;
3952
description = ''
40-
Configurations for each language server.
53+
Configurations for ${displayName}. ${config.extraDescription or ""}
4154
'';
4255
default = { };
43-
example = {
44-
cmd = [
45-
"clangd"
46-
"--background-index"
47-
];
48-
root_markers = [
49-
"compile_commands.json"
50-
"compile_flags.txt"
51-
];
52-
filetypes = [
53-
"c"
54-
"cpp"
55-
];
56-
};
56+
example =
57+
config.example or {
58+
cmd = [
59+
"clangd"
60+
"--background-index"
61+
];
62+
root_markers = [
63+
"compile_commands.json"
64+
"compile_flags.txt"
65+
];
66+
filetypes = [
67+
"c"
68+
"cpp"
69+
];
70+
};
5771
};
5872
};
5973
}

plugins/by-name/lspconfig/default.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ lib.nixvim.plugins.mkNeovimPlugin {
3535
> setup that relate to neovim's builtin LSP and are now being moved to the
3636
> new [`lsp`] module.
3737
38-
[`lsp`]: ../../lsp/servers.md
38+
[`lsp`]: ../../lsp/servers/index.md
3939
[`plugins.lsp`]: ../lsp/index.md
4040
[nvim-lspconfig]: ${opts.package.default.meta.homepage}
4141
'';

0 commit comments

Comments
 (0)