2021-02-01 15:54:53 +00:00
|
|
|
{
|
2023-11-06 15:04:08 +01:00
|
|
|
lib,
|
|
|
|
helpers,
|
2023-02-20 11:42:13 +01:00
|
|
|
config,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
2024-05-05 19:39:35 +02:00
|
|
|
with lib;
|
|
|
|
let
|
2023-02-20 11:42:13 +01:00
|
|
|
cfg = config.plugins.lsp;
|
2024-05-05 19:39:35 +02:00
|
|
|
in
|
|
|
|
{
|
2024-06-03 17:19:31 +01:00
|
|
|
imports = [ ./language-servers ];
|
2021-02-01 15:54:53 +00:00
|
|
|
|
|
|
|
options = {
|
2022-09-18 11:19:23 +01:00
|
|
|
plugins.lsp = {
|
2023-01-22 03:32:08 +00:00
|
|
|
enable = mkEnableOption "neovim's built-in LSP";
|
2021-02-01 15:54:53 +00:00
|
|
|
|
2023-03-14 15:22:56 +01:00
|
|
|
keymaps = {
|
|
|
|
silent = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
description = "Whether nvim-lsp keymaps should be silent";
|
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
diagnostic = mkOption {
|
2023-07-17 15:37:00 +02:00
|
|
|
type = with types; attrsOf (either str attrs);
|
2024-04-03 13:11:18 +01:00
|
|
|
description = "Mappings for `vim.diagnostic.<action>` functions to be added when an LSP is attached.";
|
2023-03-14 15:22:56 +01:00
|
|
|
example = {
|
|
|
|
"<leader>k" = "goto_prev";
|
|
|
|
"<leader>j" = "goto_next";
|
|
|
|
};
|
2024-05-05 19:39:35 +02:00
|
|
|
default = { };
|
2023-03-14 15:22:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
lspBuf = mkOption {
|
2023-07-17 15:37:00 +02:00
|
|
|
type = with types; attrsOf (either str attrs);
|
2024-04-03 13:11:18 +01:00
|
|
|
description = "Mappings for `vim.lsp.buf.<action>` functions to be added when an LSP it attached.";
|
2023-03-14 15:22:56 +01:00
|
|
|
example = {
|
|
|
|
"gd" = "definition";
|
|
|
|
"gD" = "references";
|
|
|
|
"gt" = "type_definition";
|
|
|
|
"gi" = "implementation";
|
|
|
|
"K" = "hover";
|
|
|
|
};
|
2024-05-05 19:39:35 +02:00
|
|
|
default = { };
|
2023-03-14 15:22:56 +01:00
|
|
|
};
|
2024-03-25 21:39:21 +00:00
|
|
|
|
|
|
|
extra = mkOption {
|
2024-08-17 22:40:14 +01:00
|
|
|
type = with types; listOf helpers.keymaps.deprecatedMapOptionSubmodule;
|
|
|
|
apply = map helpers.keymaps.removeDeprecatedMapAttrs;
|
2024-04-03 13:11:18 +01:00
|
|
|
description = ''
|
|
|
|
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.
|
|
|
|
'';
|
2024-03-25 21:39:21 +00:00
|
|
|
example = [
|
|
|
|
{
|
|
|
|
key = "<leader>lx";
|
|
|
|
action = "<CMD>LspStop<Enter>";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
key = "<leader>ls";
|
|
|
|
action = "<CMD>LspStart<Enter>";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
key = "<leader>lr";
|
|
|
|
action = "<CMD>LspRestart<Enter>";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
key = "gd";
|
2024-05-27 14:03:11 +02:00
|
|
|
action.__raw = "require('telescope.builtin').lsp_definitions()";
|
2024-03-25 21:39:21 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
key = "K";
|
|
|
|
action = "<CMD>Lspsaga hover_doc<Enter>";
|
|
|
|
}
|
|
|
|
];
|
2024-05-05 19:39:35 +02:00
|
|
|
default = [ ];
|
2024-03-25 21:39:21 +00:00
|
|
|
};
|
2023-03-14 15:22:56 +01:00
|
|
|
};
|
|
|
|
|
2021-02-01 15:54:53 +00:00
|
|
|
enabledServers = mkOption {
|
2024-05-05 19:39:35 +02:00
|
|
|
type =
|
|
|
|
with types;
|
2023-02-20 11:42:13 +01:00
|
|
|
listOf (oneOf [
|
|
|
|
str
|
|
|
|
(submodule {
|
|
|
|
options = {
|
|
|
|
name = mkOption {
|
|
|
|
type = str;
|
|
|
|
description = "The server's name";
|
|
|
|
};
|
2021-02-01 15:54:53 +00:00
|
|
|
|
2024-03-06 12:50:14 -08:00
|
|
|
capabilities = mkOption {
|
|
|
|
type = types.nullOr (types.attrsOf types.bool);
|
|
|
|
description = "Control resolved capabilities for the language server.";
|
|
|
|
default = null;
|
|
|
|
example = {
|
|
|
|
documentFormattingProvider = false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-02-20 11:42:13 +01:00
|
|
|
extraOptions = mkOption {
|
|
|
|
type = attrs;
|
|
|
|
description = "Extra options for the server";
|
|
|
|
};
|
2022-09-18 11:19:23 +01:00
|
|
|
};
|
2023-02-20 11:42:13 +01:00
|
|
|
})
|
|
|
|
]);
|
2021-02-01 15:54:53 +00:00
|
|
|
description = "A list of enabled LSP servers. Don't use this directly.";
|
2024-05-05 19:39:35 +02:00
|
|
|
default = [ ];
|
2023-11-23 16:36:41 +01:00
|
|
|
internal = true;
|
|
|
|
visible = false;
|
2021-02-01 15:54:53 +00:00
|
|
|
};
|
|
|
|
|
2024-06-09 09:23:05 +01:00
|
|
|
inlayHints = mkOption {
|
|
|
|
description = ''
|
|
|
|
Whether to enable LSP inlay-hints.
|
|
|
|
Only affects language servers with inlay-hints support.
|
2024-03-23 17:03:15 +00:00
|
|
|
|
2024-06-09 09:23:05 +01:00
|
|
|
See [`:h lsp-inlay_hint`](https://neovim.io/doc/user/lsp.html#lsp-inlay_hint).
|
|
|
|
'';
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
example = true;
|
|
|
|
};
|
2024-03-23 17:03:15 +00:00
|
|
|
|
2021-02-01 15:54:53 +00:00
|
|
|
onAttach = mkOption {
|
|
|
|
type = types.lines;
|
2022-12-05 09:49:37 +07:00
|
|
|
description = "A lua function to be run when a new LSP buffer is attached. The argument `client` and `bufnr` is provided.";
|
2021-02-01 15:54:53 +00:00
|
|
|
default = "";
|
|
|
|
};
|
2021-11-23 15:39:57 +01:00
|
|
|
|
2023-02-20 17:32:05 +01:00
|
|
|
capabilities = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
description = "Lua code that modifies inplace the `capabilities` table.";
|
|
|
|
default = "";
|
|
|
|
};
|
|
|
|
|
2022-01-09 23:00:19 +00:00
|
|
|
setupWrappers = mkOption {
|
|
|
|
type = with types; listOf (functionTo str);
|
|
|
|
description = "Code to be run to wrap the setup args. Takes in an argument containing the previous results, and returns a new string of code.";
|
2024-05-05 19:39:35 +02:00
|
|
|
default = [ ];
|
2022-01-09 23:00:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
preConfig = mkOption {
|
2021-11-23 15:39:57 +01:00
|
|
|
type = types.lines;
|
2022-01-09 23:00:19 +00:00
|
|
|
description = "Code to be run before loading the LSP. Useful for requiring plugins";
|
2021-11-23 15:39:57 +01:00
|
|
|
default = "";
|
|
|
|
};
|
2023-01-21 19:52:56 +01:00
|
|
|
|
|
|
|
postConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
description = "Code to be run after loading the LSP. This is an internal option";
|
|
|
|
default = "";
|
|
|
|
};
|
2021-02-01 15:54:53 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-05-05 19:39:35 +02:00
|
|
|
config =
|
|
|
|
let
|
|
|
|
runWrappers =
|
|
|
|
wrappers: s: if wrappers == [ ] then s else (head wrappers) (runWrappers (tail wrappers) s);
|
|
|
|
updateCapabilities =
|
|
|
|
let
|
|
|
|
servers = builtins.filter (
|
|
|
|
server: server.capabilities != null && server.capabilities != { }
|
|
|
|
) cfg.enabledServers;
|
|
|
|
in
|
|
|
|
lib.concatMapStringsSep "\n" (
|
|
|
|
server:
|
|
|
|
let
|
|
|
|
updates = lib.concatMapStringsSep "\n" (name: ''
|
2024-03-06 12:50:14 -08:00
|
|
|
client.server_capabilities.${name} = ${helpers.toLuaObject server.capabilities.${name}}
|
2024-05-05 19:39:35 +02:00
|
|
|
'') (builtins.attrNames server.capabilities);
|
|
|
|
in
|
|
|
|
''
|
|
|
|
if client.name == "${server.name}" then
|
|
|
|
${updates}
|
|
|
|
end
|
|
|
|
''
|
|
|
|
) servers;
|
|
|
|
in
|
2022-09-18 11:19:23 +01:00
|
|
|
mkIf cfg.enable {
|
2024-05-05 19:39:35 +02:00
|
|
|
extraPlugins = [ pkgs.vimPlugins.nvim-lspconfig ];
|
2021-02-01 15:54:53 +00:00
|
|
|
|
2024-05-05 19:39:35 +02:00
|
|
|
keymapsOnEvents.LspAttach =
|
|
|
|
let
|
|
|
|
mkMaps =
|
|
|
|
prefix:
|
|
|
|
mapAttrsToList (
|
|
|
|
key: action:
|
|
|
|
let
|
|
|
|
actionStr = if isString action then action else action.action;
|
|
|
|
actionProps = if isString action then { } else filterAttrs (n: v: n != "action") action;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
mode = "n";
|
|
|
|
inherit key;
|
|
|
|
action = helpers.mkRaw (prefix + actionStr);
|
2023-09-15 14:35:13 +02:00
|
|
|
|
2024-05-05 19:39:35 +02:00
|
|
|
options = {
|
2024-03-26 13:38:45 +00:00
|
|
|
inherit (cfg.keymaps) silent;
|
2024-05-05 19:39:35 +02:00
|
|
|
} // actionProps;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
in
|
2024-03-26 13:38:45 +00:00
|
|
|
(mkMaps "vim.diagnostic." cfg.keymaps.diagnostic)
|
|
|
|
++ (mkMaps "vim.lsp.buf." cfg.keymaps.lspBuf)
|
|
|
|
++ cfg.keymaps.extra;
|
2023-03-14 15:22:56 +01:00
|
|
|
|
2024-03-23 17:03:15 +00:00
|
|
|
# Enable inlay-hints
|
|
|
|
plugins.lsp.onAttach = mkIf cfg.inlayHints ''
|
|
|
|
-- LSP Inlay Hints {{{
|
|
|
|
if client.server_capabilities.inlayHintProvider and vim.lsp.inlay_hint then
|
2024-06-12 11:52:49 -04:00
|
|
|
vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })
|
2024-03-23 17:03:15 +00:00
|
|
|
end
|
|
|
|
-- }}}
|
|
|
|
'';
|
|
|
|
|
2021-02-01 15:54:53 +00:00
|
|
|
# Enable all LSP servers
|
|
|
|
extraConfigLua = ''
|
|
|
|
-- LSP {{{
|
2022-01-09 23:00:19 +00:00
|
|
|
do
|
|
|
|
${cfg.preConfig}
|
2023-02-20 17:32:05 +01:00
|
|
|
|
2022-01-09 23:00:19 +00:00
|
|
|
local __lspServers = ${helpers.toLuaObject cfg.enabledServers}
|
2022-12-05 09:49:37 +07:00
|
|
|
local __lspOnAttach = function(client, bufnr)
|
2024-03-06 12:50:14 -08:00
|
|
|
${updateCapabilities}
|
|
|
|
|
2022-01-09 23:00:19 +00:00
|
|
|
${cfg.onAttach}
|
|
|
|
end
|
2023-02-20 17:32:05 +01:00
|
|
|
local __lspCapabilities = function()
|
|
|
|
capabilities = vim.lsp.protocol.make_client_capabilities()
|
|
|
|
|
|
|
|
${cfg.capabilities}
|
|
|
|
|
|
|
|
return capabilities
|
|
|
|
end
|
2022-01-09 23:00:19 +00:00
|
|
|
|
|
|
|
local __setup = ${runWrappers cfg.setupWrappers "{
|
2023-02-20 17:32:05 +01:00
|
|
|
on_attach = __lspOnAttach,
|
|
|
|
capabilities = __lspCapabilities()
|
2022-01-09 23:00:19 +00:00
|
|
|
}"}
|
2021-02-01 15:54:53 +00:00
|
|
|
|
2022-01-09 23:00:19 +00:00
|
|
|
for i,server in ipairs(__lspServers) do
|
|
|
|
if type(server) == "string" then
|
|
|
|
require('lspconfig')[server].setup(__setup)
|
|
|
|
else
|
|
|
|
local options = ${runWrappers cfg.setupWrappers "server.extraOptions"}
|
2022-10-22 22:15:03 +01:00
|
|
|
|
|
|
|
if options == nil then
|
|
|
|
options = __setup
|
|
|
|
else
|
|
|
|
options = vim.tbl_extend("keep", options, __setup)
|
|
|
|
end
|
|
|
|
|
2022-01-09 23:00:19 +00:00
|
|
|
require('lspconfig')[server.name].setup(options)
|
|
|
|
end
|
2021-02-01 15:54:53 +00:00
|
|
|
end
|
2023-01-21 19:52:56 +01:00
|
|
|
|
|
|
|
${cfg.postConfig}
|
2021-02-01 15:54:53 +00:00
|
|
|
end
|
|
|
|
-- }}}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
}
|