Skip to content

Commit 3d88184

Browse files
committed
plugins.lsp: alias onAttach to new lsp.onAttach
This simplifies the impl by doing global on-attach logic in a `LspAttach` autocmd instead of adding lua lines to each server's individual `on_attach` callback. This is effectively a `mkAliasOptionModule` alias, other than the alias only being applied when `plugins.lsp.enable`.
1 parent c26f5c2 commit 3d88184

File tree

5 files changed

+51
-50
lines changed

5 files changed

+51
-50
lines changed

plugins/by-name/rust-tools/default.nix

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ in
173173
server = {
174174
inherit (cfg.server) standalone;
175175
settings.rust-analyzer = lib.filterAttrs (n: v: n != "standalone") cfg.server;
176-
on_attach = lib.nixvim.mkRaw "__lspOnAttach";
177176
};
178177
} // cfg.extraOptions;
179178
in

plugins/by-name/rustaceanvim/default.nix

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -53,43 +53,30 @@ lib.nixvim.plugins.mkNeovimPlugin {
5353

5454
callSetup = false;
5555
hasLuaConfig = false;
56-
extraConfig =
57-
cfg:
58-
mkMerge [
59-
{
60-
globals.rustaceanvim = cfg.settings;
56+
extraConfig = cfg: {
57+
globals.rustaceanvim = cfg.settings;
6158

62-
assertions = lib.nixvim.mkAssertions "plugins.rustaceanvim" {
63-
assertion = cfg.enable -> !config.plugins.lsp.servers.rust_analyzer.enable;
64-
message = ''
65-
Both `plugins.rustaceanvim.enable` and `plugins.lsp.servers.rust_analyzer.enable` are true.
66-
Disable one of them otherwise you will have multiple clients attached to each buffer.
67-
'';
68-
};
59+
assertions = lib.nixvim.mkAssertions "plugins.rustaceanvim" {
60+
assertion = cfg.enable -> !config.plugins.lsp.servers.rust_analyzer.enable;
61+
message = ''
62+
Both `plugins.rustaceanvim.enable` and `plugins.lsp.servers.rust_analyzer.enable` are true.
63+
Disable one of them otherwise you will have multiple clients attached to each buffer.
64+
'';
65+
};
6966

70-
# TODO: remove after 24.11
71-
warnings = lib.nixvim.mkWarnings "plugins.rustaceanvim" {
72-
when = hasAttrByPath [
73-
"settings"
74-
"server"
75-
"settings"
76-
] cfg;
77-
message = ''
78-
The `settings.server.settings' option has been renamed to `settings.server.default_settings'.
67+
# TODO: remove after 24.11
68+
warnings = lib.nixvim.mkWarnings "plugins.rustaceanvim" {
69+
when = hasAttrByPath [
70+
"settings"
71+
"server"
72+
"settings"
73+
] cfg;
74+
message = ''
75+
The `settings.server.settings' option has been renamed to `settings.server.default_settings'.
7976
80-
Note that if you supplied an attrset and not a function you need to set this attr set in:
81-
`settings.server.default_settings.rust-analyzer'.
82-
'';
83-
};
84-
}
85-
# If nvim-lspconfig is enabled:
86-
(mkIf config.plugins.lsp.enable {
87-
# Use the same `on_attach` callback as for the other LSP servers
88-
plugins.rustaceanvim.settings.server.on_attach = mkDefault ''
89-
function(client, bufnr)
90-
return _M.lspOnAttach(client, bufnr)
91-
end
92-
'';
93-
})
94-
];
77+
Note that if you supplied an attrset and not a function you need to set this attr set in:
78+
`settings.server.default_settings.rust-analyzer'.
79+
'';
80+
};
81+
};
9582
}

plugins/by-name/rustaceanvim/settings-options.nix

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,7 @@ with lib;
234234
```
235235
'';
236236

237-
on_attach = helpers.mkNullOrLuaFn ''
238-
Function to call on attach.
239-
If `plugins.lsp` is enabled, it defaults to the Nixvim global `__lspOnAttach` function.
240-
Otherwise it defaults to `null`.
241-
'';
237+
on_attach = defaultNullOpts.mkLuaFn null "Function to call when rustaceanvim attaches to a buffer.";
242238

243239
cmd = helpers.mkNullOrStrLuaFnOr (with types; listOf str) ''
244240
Command and arguments for starting rust-analyzer.

plugins/by-name/typescript-tools/default.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ lib.nixvim.plugins.mkNeovimPlugin {
1919
];
2020

2121
settingsOptions = {
22-
on_attach = defaultNullOpts.mkLuaFn "__lspOnAttach" "Lua code to run when tsserver attaches to a buffer.";
22+
on_attach = defaultNullOpts.mkLuaFn null "Function to call when tsserver attaches to a buffer.";
2323

2424
handlers = lib.mkOption {
2525
type = with lib.types; nullOr (attrsOf strLuaFn);

plugins/lsp/default.nix

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{ lib, ... }:
1+
{ lib, config, ... }:
22
let
33
inherit (lib) mkOption types;
44
in
@@ -114,6 +114,9 @@ lib.nixvim.plugins.mkNeovimPlugin {
114114
type = types.lines;
115115
description = "A lua function to be run when a new LSP buffer is attached. The argument `client` and `bufnr` is provided.";
116116
default = "";
117+
# When `plugins.lsp` is enabled, definitions are aliased to `lsp.onAttach`; so read that final value here.
118+
# The other half of this two-way alias is below in `extraConfig`.
119+
apply = value: if config.plugins.lsp.enable then config.lsp.onAttach else value;
117120
};
118121

119122
capabilities = mkOption {
@@ -141,7 +144,7 @@ lib.nixvim.plugins.mkNeovimPlugin {
141144
};
142145
};
143146

144-
extraConfig = cfg: {
147+
extraConfig = cfg: opts: {
145148
keymapsOnEvents.LspAttach =
146149
let
147150
mkMaps =
@@ -173,6 +176,26 @@ lib.nixvim.plugins.mkNeovimPlugin {
173176
++ mkMaps "vim.lsp.buf." "Lsp buf" cfg.keymaps.lspBuf
174177
++ cfg.keymaps.extra;
175178

179+
# Alias onAttach definitions to the new impl in the top-level lsp module
180+
#
181+
# NOTE: While `mkDerivedConfig` creates an alias based on the final `value` and `highestPrio`,
182+
# `mkAliasAndWrapDefinitions` and `mkAliasAndWrapDefsWithPriority` propagates the un-merged
183+
# `definitions`.
184+
#
185+
# This assumes both options have compatible merge functions, but it allows override and order
186+
# priorities to be merged correctly.
187+
#
188+
# E.g:
189+
# lsp.onAttach = mkAfter "world";
190+
# plugins.lsp.onAttach = mkBefore "hello"
191+
# ⇒
192+
# hello
193+
# world
194+
#
195+
# This is equivalent to `mkAliasOptionModule`, except predicated on `plugins.lsp.enable`.
196+
# The other half of this two-way alias is above in the option's `apply` function.
197+
lsp.onAttach = lib.modules.mkAliasAndWrapDefsWithPriority lib.id opts.onAttach;
198+
176199
plugins.lsp.luaConfig.content =
177200
let
178201
runWrappers =
@@ -188,10 +211,7 @@ lib.nixvim.plugins.mkNeovimPlugin {
188211
${lib.optionalString cfg.inlayHints "vim.lsp.inlay_hint.enable(true)"}
189212
190213
local __lspServers = ${lib.nixvim.toLuaObject cfg.enabledServers}
191-
-- Adding lspOnAttach function to nixvim module lua table so other plugins can hook into it.
192-
_M.lspOnAttach = function(client, bufnr)
193-
${cfg.onAttach}
194-
end
214+
195215
local __lspCapabilities = function()
196216
capabilities = vim.lsp.protocol.make_client_capabilities()
197217
@@ -201,7 +221,6 @@ lib.nixvim.plugins.mkNeovimPlugin {
201221
end
202222
203223
local __setup = ${runWrappers cfg.setupWrappers "{
204-
on_attach = _M.lspOnAttach,
205224
capabilities = __lspCapabilities(),
206225
}"}
207226

0 commit comments

Comments
 (0)