From bc468178aef56ab4d473a9cc8fa83c588cf91e4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=A9tan=20Lepage?= <33058747+GaetanLepage@users.noreply.github.com> Date: Wed, 19 Apr 2023 14:30:02 +0200 Subject: [PATCH] plugins/nvim-lsp: add per-server onAttach option (#341) --- plugins/_sources/generated.nix | 7 +++-- plugins/nvim-lsp/helpers.nix | 28 +++++++++++++++++++ .../plugins/nvim-lsp/nvim-lsp.nix | 7 ++++- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/plugins/_sources/generated.nix b/plugins/_sources/generated.nix index 40a3bd16..b23a65f8 100644 --- a/plugins/_sources/generated.nix +++ b/plugins/_sources/generated.nix @@ -1,5 +1,8 @@ # This file was generated by nvfetcher, please do not modify it manually. -{ fetchgit, fetchurl, fetchFromGitHub, dockerTools }: { - + fetchgit, + fetchurl, + fetchFromGitHub, + dockerTools, +}: { } diff --git a/plugins/nvim-lsp/helpers.nix b/plugins/nvim-lsp/helpers.nix index 46154477..3f5d9ebe 100644 --- a/plugins/nvim-lsp/helpers.nix +++ b/plugins/nvim-lsp/helpers.nix @@ -57,6 +57,24 @@ `:LspStart` (|lspconfig-commands|). ''; + onAttach = + helpers.mkCompositeOption "Server specific on_attach behavior." + { + override = mkOption { + type = types.bool; + default = false; + description = "Override the global `plugins.lsp.onAttach` function."; + }; + + function = mkOption { + type = types.str; + description = '' + Body of the on_attach function. + The argument `client` and `bufnr` is provided. + ''; + }; + }; + settings = settingsOptions; extraSettings = mkOption { @@ -82,6 +100,16 @@ name = serverName; extraOptions = { inherit (cfg) cmd filetypes autostart; + on_attach = + helpers.ifNonNull' cfg.onAttach + ( + helpers.mkRaw '' + function(client, bufnr) + ${optionalString (!cfg.onAttach.override) config.plugins.lsp.onAttach} + ${cfg.onAttach.function} + end + '' + ); settings = (settings cfg.settings) // cfg.extraSettings; }; } diff --git a/tests/test-sources/plugins/nvim-lsp/nvim-lsp.nix b/tests/test-sources/plugins/nvim-lsp/nvim-lsp.nix index ce51b29d..323b9f49 100644 --- a/tests/test-sources/plugins/nvim-lsp/nvim-lsp.nix +++ b/tests/test-sources/plugins/nvim-lsp/nvim-lsp.nix @@ -25,7 +25,12 @@ servers = { bashls.enable = true; - clangd.enable = true; + clangd = { + enable = true; + onAttach.function = '' + print('The clangd language server has been attached !') + ''; + }; nil_ls.enable = true; rust-analyzer.enable = true; pylsp = {