modules/lsp: add onAttach option

Similar to `plugins.lsp.onAttach`, implement a "global" equivalent to
the per-server `on_attach` callback.

This is implemented using a `LspAttach` autoCmd.
This commit is contained in:
Matt Sturgeon 2025-05-03 23:30:31 +01:00
parent 391e4fd093
commit a45b5f372f
2 changed files with 49 additions and 0 deletions

View file

@ -20,6 +20,7 @@ in
imports = [
./servers
./keymaps.nix
./on-attach.nix
];
config = {

48
modules/lsp/on-attach.nix Normal file
View file

@ -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";
}
];
};
}