Skip to content

Commit

Permalink
plugins/lsp: fix enabledServers.extraOptions type merging
Browse files Browse the repository at this point in the history
Use `attrsOf` instead of `attrs` to ensure recursive merging, otherwise
things like `extraOptions.settings = lib.mkIf` will not be unwrapped by
the module system.
  • Loading branch information
MattSturgeon committed Nov 26, 2024
1 parent a1c352a commit 8b19d15
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 3 deletions.
6 changes: 3 additions & 3 deletions plugins/lsp/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ in
};

diagnostic = mkOption {
type = with types; attrsOf (either str attrs);
type = with types; attrsOf (either str (attrsOf anything));
description = "Mappings for `vim.diagnostic.<action>` functions to be added when an LSP is attached.";
example = {
"<leader>k" = "goto_prev";
Expand All @@ -39,7 +39,7 @@ in
};

lspBuf = mkOption {
type = with types; attrsOf (either str attrs);
type = with types; attrsOf (either str (attrsOf anything));
description = "Mappings for `vim.lsp.buf.<action>` functions to be added when an LSP it attached.";
example = {
"gd" = "definition";
Expand Down Expand Up @@ -106,7 +106,7 @@ in
};

extraOptions = mkOption {
type = attrs;
type = attrsOf anything;
description = "Extra options for the server";
};
};
Expand Down
110 changes: 110 additions & 0 deletions tests/test-sources/plugins/lsp/_lsp.nix
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,114 @@
}
];
};

settings-merge =
{ config, lib, ... }:
{
test.runNvim = false;

plugins = {
lsp = {
enable = true;
servers.nil_ls = {
enable = true;
settings.formatting.command = lib.mkForce [
"real"
"example"
];
};
enabledServers = lib.mkAfter [
{
name = "second";
extraOptions.settings = lib.mkIf false {
should.be = "missing";
};
}
{
name = "third";
extraOptions.settings = lib.mkIf true {
should.be = "present";
};
}
];
};
};

assertions =
let
toLua = lib.nixvim.lua.toLua' {
removeNullAttrValues = true;
removeEmptyAttrValues = true;
removeEmptyListEntries = false;
removeNullListEntries = false;
multiline = true;
};

print = lib.generators.toPretty {
multiline = true;
};

serverCount = builtins.length config.plugins.lsp.enabledServers;
expectedCount = 3;

nilServer = builtins.head config.plugins.lsp.enabledServers;
nilSettings = toLua nilServer.extraOptions.settings;
expectedNilSettings = toLua {
nil.formatting.command = [
"real"
"example"
];
};

secondServer = builtins.elemAt config.plugins.lsp.enabledServers 1;
expectedSecondServer = {
name = "second";
capabilities = null;
extraOptions = { };
};

thirdServer = builtins.elemAt config.plugins.lsp.enabledServers 2;
expectedThirdServer = {
name = "third";
capabilities = null;
extraOptions.settings.should.be = "present";
};
in
[
{
assertion = serverCount == expectedCount;
message = "Expected ${toString expectedCount} enabled LSP server!";
}
{
assertion = nilSettings == expectedNilSettings;
message = ''
nil's `extraOptions.settings` does not match expected value.
Expected: ${expectedNilSettings}
Actual: ${nilSettings}
'';
}
{
assertion = secondServer == expectedSecondServer;
message = ''
`secondServer` does not match expected value.
Expected: ${print expectedSecondServer}
Actual: ${print secondServer}
'';
}
{
assertion = secondServer == expectedSecondServer;
message = ''
`thirdServer` does not match expected value.
Expected: ${print expectedThirdServer}
Actual: ${print thirdServer}
'';
}
];
};
}

0 comments on commit 8b19d15

Please sign in to comment.