Skip to content

modules/lsp: add onAttach option #3280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/lsp/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ in
imports = [
./servers
./keymaps.nix
./on-attach.nix
];

config = {
Expand Down
48 changes: 48 additions & 0 deletions modules/lsp/on-attach.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{ lib, config, ... }:
let
cfg = config.lsp;
in
{
options.lsp = {
onAttach = lib.mkOption {
type = lib.types.lines;
description = ''
Lines of lua to be run when a language server is attached.

> [!TIP]
> The variables `client` and `bufnr` are made available in scope.

This is a global equivialent to the per-server `on_attach` callback,
which can be defined via `lsp.servers.<name>.settings.on_attach`.

Unlike the per-server callback, which should be defined as a lua
callback function, this option should be defined as the function body.
'';
default = "";
};
};

config = lib.mkIf (cfg.onAttach != "") {
autoGroups.nixvim_lsp_on_attach.clear = false;

autoCmd = [
{
event = "LspAttach";
group = "nixvim_lsp_on_attach";
callback = lib.nixvim.mkRaw ''
function(args)
do
-- client and bufnr are supplied to the builtin `on_attach` callback,
-- so make them available in scope for our global `onAttach` impl
local client = vim.lsp.get_client_by_id(args.data.client_id)
local bufnr = args.bufnr

${cfg.onAttach}
end
end
'';
desc = "Run LSP onAttach";
}
];
};
}
51 changes: 23 additions & 28 deletions plugins/by-name/lsp-format/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -96,35 +96,30 @@ lib.nixvim.plugins.mkNeovimPlugin {
'';
};

autoGroups.nixvim_lsp_fmt_setup.clear = false;
lsp.onAttach =
let
toLua = lib.nixvim.lua.toLua' {
multiline = false;
};

autoCmd = lib.optionals (cfg.lspServersToEnable != "none") [
{
desc = "set up autoformatting on LSP attach";
event = "LspAttach";
group = "nixvim_lsp_fmt_setup";
callback =
let
enableCondition =
if cfg.lspServersToEnable == "all" then
"true"
else if lib.isList cfg.lspServersToEnable then
# Lua
"vim.list_contains(${lib.nixvim.toLuaObject cfg.lspServersToEnable}, client.name)"
else
throw "Unhandled value for `lspServersToEnable`: ${
lib.generators.toPretty { } cfg.lspServersToEnable
}";
in
lib.nixvim.mkRaw ''
function(args)
local client = assert(vim.lsp.get_client_by_id(args.data.client_id))
if ${enableCondition} then
require("lsp-format").on_attach(client, args.buf)
wrapLua =
lua:
if cfg.lspServersToEnable == "all" then
lua
else if lib.isList cfg.lspServersToEnable then
# lua
''
if vim.list_contains(${toLua cfg.lspServersToEnable}, client.name) then
${lib.removeSuffix "\n" lua}
end
end
'';
}
];
''
else
throw "Unhandled value for `lspServersToEnable`: ${
lib.generators.toPretty { } cfg.lspServersToEnable
}";
in
lib.mkIf (cfg.lspServersToEnable != "none") (wrapLua ''
require("lsp-format").on_attach(client, bufnr)
'');
};
}
15 changes: 15 additions & 0 deletions tests/test-sources/plugins/by-name/lsp-format/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,19 @@
};
};
};

listed-servers = {
plugins = {
lspconfig.enable = true;

lsp-format = {
enable = true;
lspServersToEnable = [
"foo"
"bar"
"baz"
];
};
};
};
}