mirror of
https://github.com/nix-community/nixvim.git
synced 2025-06-20 16:15:43 +02:00
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:
parent
391e4fd093
commit
a45b5f372f
2 changed files with 49 additions and 0 deletions
|
@ -20,6 +20,7 @@ in
|
||||||
imports = [
|
imports = [
|
||||||
./servers
|
./servers
|
||||||
./keymaps.nix
|
./keymaps.nix
|
||||||
|
./on-attach.nix
|
||||||
];
|
];
|
||||||
|
|
||||||
config = {
|
config = {
|
||||||
|
|
48
modules/lsp/on-attach.nix
Normal file
48
modules/lsp/on-attach.nix
Normal 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";
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue