From 1768791402ae4112c08f71c077f65d035d672126 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sun, 16 Feb 2025 12:48:40 +0300 Subject: [PATCH 1/7] lib/languages: add `lspOptions` submodule type for freeform LSP config --- lib/languages.nix | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/languages.nix b/lib/languages.nix index a202ff14..19171cc8 100644 --- a/lib/languages.nix +++ b/lib/languages.nix @@ -1,9 +1,10 @@ -# From home-manager: https://github.com/nix-community/home-manager/blob/master/modules/lib/booleans.nix {lib}: let inherit (builtins) isString getAttr; inherit (lib.options) mkOption; - inherit (lib.types) bool; + inherit (lib.types) listOf bool str submodule attrsOf anything; + inherit (lib.generators) mkLuaInline; inherit (lib.nvim.attrsets) mapListToAttrs; + inherit (lib.nvim.types) luaInline; in { diagnosticsToLua = { lang, @@ -32,4 +33,31 @@ in { type = bool; description = "Turn on ${desc} for enabled languages by default"; }; + + lspOptions = submodule { + freeformType = attrsOf anything; + options = { + capabilities = mkOption { + type = luaInline; + default = mkLuaInline "capabilities"; + description = "LSP capabilitiess to pass to lspconfig"; + }; + + on_attach = mkOption { + type = luaInline; + default = mkLuaInline "default_on_attach"; + description = "Function to execute when an LSP server attaches to a buffer"; + }; + + filetypes = mkOption { + type = listOf str; + description = "Filetypes to auto-attach LSP in"; + }; + + cmd = mkOption { + type = listOf str; + description = "Command used to start the LSP server"; + }; + }; + }; } From 159a0339024a3b3e34fa97f46eacb3d41cdbaf34 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sat, 5 Apr 2025 22:47:10 +0300 Subject: [PATCH 2/7] lib/languages: add `root_marker` to `lspOptions` type --- lib/languages.nix | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/languages.nix b/lib/languages.nix index 19171cc8..d7e30394 100644 --- a/lib/languages.nix +++ b/lib/languages.nix @@ -58,6 +58,14 @@ in { type = listOf str; description = "Command used to start the LSP server"; }; + + root_markers = mkOption { + type = listOf str; + description = '' + "root markers" used to determine the root directory of the workspace, and + the filetypes associated with this LSP server. + ''; + }; }; }; } From 35c432db3b76e898fec0f6e25f9e410e0807d9c4 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sat, 5 Apr 2025 22:47:25 +0300 Subject: [PATCH 3/7] neovim/lsp: init --- modules/neovim/init/default.nix | 1 + modules/neovim/init/lsp.nix | 44 +++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 modules/neovim/init/lsp.nix diff --git a/modules/neovim/init/default.nix b/modules/neovim/init/default.nix index 0e7a4c6b..7db6f2ef 100644 --- a/modules/neovim/init/default.nix +++ b/modules/neovim/init/default.nix @@ -5,6 +5,7 @@ ./debug.nix ./diagnostics.nix ./highlight.nix + ./lsp.nix ./spellcheck.nix ]; } diff --git a/modules/neovim/init/lsp.nix b/modules/neovim/init/lsp.nix new file mode 100644 index 00000000..5dfd7a74 --- /dev/null +++ b/modules/neovim/init/lsp.nix @@ -0,0 +1,44 @@ +{ + config, + lib, + ... +}: let + inherit (lib.modules) mkIf; + inherit (lib.options) mkOption; + inherit (lib.types) attrsOf; + inherit (lib.strings) concatLines; + inherit (lib.attrsets) mapAttrsToList attrNames filterAttrs; + inherit (lib.nvim.languages) lspOptions; + inherit (lib.nvim.dag) entryAnywhere; + inherit (lib.nvim.lua) toLuaObject; + + cfg = config.vim.lsp; + + lspConfigurations = + mapAttrsToList ( + name: value: '' + vim.lsp.config["${name}"] = ${toLuaObject value} + '' + ) + cfg.servers; + + enabledServers = filterAttrs (_: u: u.enable) cfg.servers; +in { + options = { + vim.lsp.servers = mkOption { + type = attrsOf lspOptions; + default = {}; + description = ""; + }; + }; + + config = mkIf (cfg.servers != {}) { + vim.luaConfigRC.lsp-servers = entryAnywhere '' + -- Individual LSP configurations managed by nvf. + ${(concatLines lspConfigurations)} + + -- Enable configured LSPs explicitly + vim.lsp.enable(${toLuaObject (attrNames enabledServers)}) + ''; + }; +} From 52fb95ea6b93f37f40dc710d4b6a9df8685051ba Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sat, 5 Apr 2025 23:17:18 +0300 Subject: [PATCH 4/7] lib/languages: add explicit enable to LSP submodule --- lib/languages.nix | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/languages.nix b/lib/languages.nix index d7e30394..c4074144 100644 --- a/lib/languages.nix +++ b/lib/languages.nix @@ -1,11 +1,11 @@ {lib}: let inherit (builtins) isString getAttr; inherit (lib.options) mkOption; - inherit (lib.types) listOf bool str submodule attrsOf anything; - inherit (lib.generators) mkLuaInline; + inherit (lib.types) listOf bool str submodule attrsOf anything either nullOr; inherit (lib.nvim.attrsets) mapListToAttrs; inherit (lib.nvim.types) luaInline; in { + # TODO: remove diagnosticsToLua = { lang, config, @@ -37,30 +37,39 @@ in { lspOptions = submodule { freeformType = attrsOf anything; options = { + enable = mkOption { + type = bool; + default = true; + description = "Whether to enable this LSP server."; + }; + capabilities = mkOption { - type = luaInline; - default = mkLuaInline "capabilities"; + type = nullOr (either luaInline (attrsOf anything)); + default = null; description = "LSP capabilitiess to pass to lspconfig"; }; on_attach = mkOption { - type = luaInline; - default = mkLuaInline "default_on_attach"; + type = nullOr luaInline; + default = null; description = "Function to execute when an LSP server attaches to a buffer"; }; filetypes = mkOption { - type = listOf str; + type = nullOr (listOf str); + default = null; description = "Filetypes to auto-attach LSP in"; }; cmd = mkOption { - type = listOf str; + type = nullOr (listOf str); + default = null; description = "Command used to start the LSP server"; }; root_markers = mkOption { - type = listOf str; + type = nullOr (listOf str); + default = null; description = '' "root markers" used to determine the root directory of the workspace, and the filetypes associated with this LSP server. From ea9075a07f285f31bd22e56c2182a0b9d52c961a Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Sun, 6 Apr 2025 01:18:39 +0200 Subject: [PATCH 5/7] lsp: add default fallback config --- modules/neovim/init/lsp.nix | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/modules/neovim/init/lsp.nix b/modules/neovim/init/lsp.nix index 5dfd7a74..6d3478c9 100644 --- a/modules/neovim/init/lsp.nix +++ b/modules/neovim/init/lsp.nix @@ -3,11 +3,13 @@ lib, ... }: let - inherit (lib.modules) mkIf; + inherit (builtins) filter; + inherit (lib.modules) mkIf mkMerge mkDefault; inherit (lib.options) mkOption; inherit (lib.types) attrsOf; inherit (lib.strings) concatLines; inherit (lib.attrsets) mapAttrsToList attrNames filterAttrs; + inherit (lib.generators) mkLuaInline; inherit (lib.nvim.languages) lspOptions; inherit (lib.nvim.dag) entryAnywhere; inherit (lib.nvim.lua) toLuaObject; @@ -32,13 +34,22 @@ in { }; }; - config = mkIf (cfg.servers != {}) { - vim.luaConfigRC.lsp-servers = entryAnywhere '' - -- Individual LSP configurations managed by nvf. - ${(concatLines lspConfigurations)} + config = mkMerge [ + { + vim.lsp.servers."*" = { + capabilities = mkDefault (mkLuaInline "capabilities"); + on_attach = mkDefault (mkLuaInline "default_on_attach"); + }; + } - -- Enable configured LSPs explicitly - vim.lsp.enable(${toLuaObject (attrNames enabledServers)}) - ''; - }; + (mkIf (cfg.servers != {}) { + vim.luaConfigRC.lsp-servers = entryAnywhere '' + -- Individual LSP configurations managed by nvf. + ${(concatLines lspConfigurations)} + + -- Enable configured LSPs explicitly + vim.lsp.enable(${toLuaObject (filter (name: name != "*") (attrNames enabledServers))}) + ''; + }) + ]; } From 9bd2a6dcb4d8f6f7231aa86f075447255aea812a Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sun, 27 Apr 2025 06:21:13 +0300 Subject: [PATCH 6/7] neovim/lsp: add LSP example; add description to servers option --- modules/neovim/init/lsp.nix | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/modules/neovim/init/lsp.nix b/modules/neovim/init/lsp.nix index 6d3478c9..faef0f93 100644 --- a/modules/neovim/init/lsp.nix +++ b/modules/neovim/init/lsp.nix @@ -30,7 +30,34 @@ in { vim.lsp.servers = mkOption { type = attrsOf lspOptions; default = {}; - description = ""; + example = '' + { + "*" = { + root_markers = [".git"]; + capabilities = { + textDocument = { + semanticTokens = { + multilineTokenSupport = true; + }; + }; + }; + }; + + "clangd" = { + filetypes = ["c"]; + }; + } + ''; + description = '' + LSP configurations that will be managed using `vim.lsp.config()` and + related utilities added in Neovim 0.11. LSPs defined here will be + added to the resulting {file}`init.lua` using `vim.lsp.config` and + enabled through `vim.lsp.enable` below the configuration table. + + You may review the generated configuration by running {command}`nvf-print-config` + in a shell. Please see {command}`:help lsp-config` for more details + on the underlying API. + ''; }; }; @@ -45,7 +72,7 @@ in { (mkIf (cfg.servers != {}) { vim.luaConfigRC.lsp-servers = entryAnywhere '' -- Individual LSP configurations managed by nvf. - ${(concatLines lspConfigurations)} + ${concatLines lspConfigurations} -- Enable configured LSPs explicitly vim.lsp.enable(${toLuaObject (filter (name: name != "*") (attrNames enabledServers))}) From c30f07fcd67a636e685259769bbb1ba18bbb8483 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sun, 27 Apr 2025 06:25:19 +0300 Subject: [PATCH 7/7] languages: set `enableLSP` to true while `vim.lsp` is enabled --- modules/plugins/languages/default.nix | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/modules/plugins/languages/default.nix b/modules/plugins/languages/default.nix index c3312135..b889e60a 100644 --- a/modules/plugins/languages/default.nix +++ b/modules/plugins/languages/default.nix @@ -1,4 +1,8 @@ -{lib, ...}: let +{ + config, + lib, + ... +}: let inherit (lib.nvim.languages) mkEnable; in { imports = [ @@ -47,7 +51,11 @@ in { ]; options.vim.languages = { - enableLSP = mkEnable "LSP"; + # LSPs are now built into Neovim, and we should enable them by default + # if `vim.lsp.enable` is true. + enableLSP = mkEnable "LSP" // {default = config.vim.lsp.enable;}; + + # Those are still managed by plugins, and should be enabled here. enableDAP = mkEnable "Debug Adapter"; enableTreesitter = mkEnable "Treesitter"; enableFormat = mkEnable "Formatting";