plugins/lsp: migrate to mkNeovimPlugin

This commit is contained in:
Heitor Augusto 2024-12-17 14:48:03 -03:00 committed by nix-infra-bot
parent 65d082069e
commit 48eeef58e1

View file

@ -1,26 +1,20 @@
{ { lib, ... }:
lib,
helpers,
config,
pkgs,
...
}:
with lib;
let let
cfg = config.plugins.lsp; inherit (lib) mkOption types;
in in
{ lib.nixvim.plugins.mkNeovimPlugin {
name = "lsp";
packPathName = "nvim-lspconfig";
package = "nvim-lspconfig";
callSetup = false;
hasSettings = false;
maintainers = [ lib.maintainers.HeitorAugustoLN ];
imports = [ ./language-servers ]; imports = [ ./language-servers ];
options = { extraOptions = {
plugins.lsp = {
enable = mkEnableOption "neovim's built-in LSP";
package = lib.mkPackageOption pkgs [
"vimPlugins"
"nvim-lspconfig"
] { };
keymaps = { keymaps = {
silent = mkOption { silent = mkOption {
type = types.bool; type = types.bool;
@ -52,8 +46,8 @@ in
}; };
extra = mkOption { extra = mkOption {
type = with types; listOf helpers.keymaps.deprecatedMapOptionSubmodule; type = types.listOf lib.nixvim.keymaps.deprecatedMapOptionSubmodule;
apply = map helpers.keymaps.removeDeprecatedMapAttrs; apply = map lib.nixvim.keymaps.removeDeprecatedMapAttrs;
description = '' description = ''
Extra keymaps to register when an LSP is attached. Extra keymaps to register when an LSP is attached.
This can be used to customise LSP behaviour, for example with "telescope" or the "Lspsaga" plugin, as seen in the examples. This can be used to customise LSP behaviour, for example with "telescope" or the "Lspsaga" plugin, as seen in the examples.
@ -97,7 +91,7 @@ in
}; };
capabilities = mkOption { capabilities = mkOption {
type = types.nullOr (types.attrsOf types.bool); type = nullOr (attrsOf bool);
description = "Control resolved capabilities for the language server."; description = "Control resolved capabilities for the language server.";
default = null; default = null;
example = { example = {
@ -119,15 +113,14 @@ in
}; };
inlayHints = mkOption { inlayHints = mkOption {
type = types.bool;
default = false;
description = '' description = ''
Whether to enable LSP inlay-hints. Whether to enable LSP inlay-hints.
Only affects language servers with inlay-hints support. Only affects language servers with inlay-hints support.
See [`:h lsp-inlay_hint`](https://neovim.io/doc/user/lsp.html#lsp-inlay_hint). See [`:h lsp-inlay_hint`](https://neovim.io/doc/user/lsp.html#lsp-inlay_hint).
''; '';
type = types.bool;
default = false;
example = true;
}; };
onAttach = mkOption { onAttach = mkOption {
@ -160,12 +153,49 @@ in
default = ""; default = "";
}; };
}; };
};
config = extraConfig = cfg: {
keymapsOnEvents.LspAttach =
let
mkMaps =
prefix: descPrefix:
lib.mapAttrsToList (
key: action:
let
actionStr = action.action or action;
actionProps = lib.optionalAttrs (builtins.isAttrs action) (
builtins.removeAttrs action [ "action" ]
);
in
{
inherit key;
mode = "n";
action = lib.nixvim.mkRaw (prefix + actionStr);
options = {
inherit (cfg.keymaps) silent;
desc = "${descPrefix} ${actionStr}";
} // actionProps;
}
);
in
mkMaps "vim.diagnostic." "Lsp diagnostic" cfg.keymaps.diagnostic
++ mkMaps "vim.lsp.buf." "Lsp buf" cfg.keymaps.lspBuf
++ cfg.keymaps.extra;
plugins.lsp.onAttach = lib.mkIf cfg.inlayHints ''
-- LSP Inlay Hints {{{
if client.server_capabilities.inlayHintProvider and vim.lsp.inlay_hint then
vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })
end
-- }}}
'';
plugins.lsp.luaConfig.content =
let let
runWrappers = runWrappers =
wrappers: s: if wrappers == [ ] then s else (head wrappers) (runWrappers (tail wrappers) s); wrappers: s:
if wrappers == [ ] then s else (builtins.head wrappers) (runWrappers (builtins.tail wrappers) s);
updateCapabilities = updateCapabilities =
let let
servers = builtins.filter ( servers = builtins.filter (
@ -186,46 +216,7 @@ in
'' ''
) servers; ) servers;
in in
mkIf cfg.enable { ''
extraPlugins = [ cfg.package ];
keymapsOnEvents.LspAttach =
let
mkMaps =
prefix: descPrefix:
mapAttrsToList (
key: action:
let
actionStr = action.action or action;
actionProps = optionalAttrs (isAttrs action) (removeAttrs action [ "action" ]);
in
{
mode = "n";
inherit key;
action = helpers.mkRaw (prefix + actionStr);
options = {
inherit (cfg.keymaps) silent;
desc = "${descPrefix} ${actionStr}";
} // actionProps;
}
);
in
mkMaps "vim.diagnostic." "Lsp diagnostic" cfg.keymaps.diagnostic
++ mkMaps "vim.lsp.buf." "Lsp buf" cfg.keymaps.lspBuf
++ cfg.keymaps.extra;
# Enable inlay-hints
plugins.lsp.onAttach = mkIf cfg.inlayHints ''
-- LSP Inlay Hints {{{
if client.server_capabilities.inlayHintProvider and vim.lsp.inlay_hint then
vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })
end
-- }}}
'';
# Enable all LSP servers
extraConfigLua = ''
-- LSP {{{ -- LSP {{{
do do
${cfg.preConfig} ${cfg.preConfig}
@ -247,12 +238,12 @@ in
local __setup = ${runWrappers cfg.setupWrappers "{ local __setup = ${runWrappers cfg.setupWrappers "{
on_attach = _M.lspOnAttach, on_attach = _M.lspOnAttach,
capabilities = __lspCapabilities() capabilities = __lspCapabilities(),
}"} }"}
for i, server in ipairs(__lspServers) do for i, server in ipairs(__lspServers) do
if type(server) == "string" then if type(server) == "string" then
require('lspconfig')[server].setup(__setup) require("lspconfig")[server].setup(__setup)
else else
local options = ${runWrappers cfg.setupWrappers "server.extraOptions"} local options = ${runWrappers cfg.setupWrappers "server.extraOptions"}
@ -262,7 +253,7 @@ in
options = vim.tbl_extend("keep", options, __setup) options = vim.tbl_extend("keep", options, __setup)
end end
require('lspconfig')[server.name].setup(options) require("lspconfig")[server.name].setup(options)
end end
end end