plugins/clangd-extensions: fix options following upstream changes + test

This commit is contained in:
Gaetan Lepage 2023-08-22 10:21:20 +02:00 committed by Gaétan Lepage
parent ebb8e479d2
commit 00016841c9
2 changed files with 267 additions and 144 deletions

View file

@ -3,13 +3,61 @@
pkgs, pkgs,
config, config,
... ...
}: let }:
with lib; let
cfg = config.plugins.clangd-extensions;
helpers = import ../helpers.nix {inherit lib;}; helpers = import ../helpers.nix {inherit lib;};
in
with lib; let basePluginPath = ["plugins" "clangd-extensions"];
borderOpt = helpers.defaultNullOpts.mkBorder "none" "clangd-extensions" ""; borderOpt = helpers.defaultNullOpts.mkBorder "none" "clangd-extensions" "";
in { in {
options.plugins.clangd-extensions = { # All of those warnings were introduced on 08/22/2023.
# TODO: Remove them in ~2 monts (Oct. 2023).
imports =
[
(
mkRemovedOptionModule
(basePluginPath ++ ["server"])
''
To configure the `clangd` language server options, please use
`plugins.lsp.servers.clangd.extraSettings`.
''
)
(
mkRemovedOptionModule (basePluginPath ++ ["extensions" "autoSetHints"]) ""
)
]
++ (
map
(
optionPath:
mkRenamedOptionModule
(basePluginPath ++ ["extensions"] ++ optionPath)
(basePluginPath ++ optionPath)
)
[
["inlayHints" "inline"]
["inlayHints" "onlyCurrentLine"]
["inlayHints" "onlyCurrentLineAutocmd"]
["inlayHints" "showParameterHints"]
["inlayHints" "parameterHintsPrefix"]
["inlayHints" "otherHintsPrefix"]
["inlayHints" "maxLenAlign"]
["inlayHints" "maxLenAlignPadding"]
["inlayHints" "rightAlign"]
["inlayHints" "rightAlignPadding"]
["inlayHints" "highlight"]
["inlayHints" "priority"]
["ast"]
["memoryUsage"]
["symbolInfo"]
]
);
options.plugins.clangd-extensions =
helpers.extraOptionsOptions
// {
enable = mkEnableOption "clangd_extensions, plugin implementing clangd LSP extensions"; enable = mkEnableOption "clangd_extensions, plugin implementing clangd LSP extensions";
package = package =
@ -20,76 +68,77 @@ in
"multiple different client offset_encodings detected for buffer, this is not supported yet" "multiple different client offset_encodings detected for buffer, this is not supported yet"
''; '';
server = {
package = mkOption {
type = types.package;
default = pkgs.clang-tools;
description = "Package to use for clangd";
};
extraOptions = mkOption {
type = types.attrsOf types.anything;
default = {};
description = "Extra options to pass to nvim-lspconfig. You should not need to use this directly";
};
};
extensions = {
autoSetHints = helpers.defaultNullOpts.mkBool true "Automatically set inlay hints (type hints)";
inlayHints = { inlayHints = {
inline = helpers.defaultNullOpts.mkStr ''vim.fn.has("nvim-0.10") == 1'' ''
Show hints inline.
'';
onlyCurrentLine = onlyCurrentLine =
helpers.defaultNullOpts.mkBool false helpers.defaultNullOpts.mkBool false
"Only show inlay hints for the current line"; "Only show inlay hints for the current line";
onlyCurrentLineAutocmd = helpers.defaultNullOpts.mkStr "CursorHold" '' onlyCurrentLineAutocmd = helpers.defaultNullOpts.mkStr "CursorHold" ''
Event which triggers a refersh of the inlay hints. Event which triggers a refersh of the inlay hints.
You can make this "CursorMoved" or "CursorMoved,CursorMovedI" but You can make this "CursorMoved" or "CursorMoved,CursorMovedI" but
not that this may cause higher CPU usage. not that this may cause higher CPU usage.
This option is only respected when only_current_line and This option is only respected when `onlyCurrentLine` is true.
autoSetHints both are true.
''; '';
showParameterHints =
helpers.defaultNullOpts.mkBool true showParameterHints = helpers.defaultNullOpts.mkBool true ''
"whether to show parameter hints with the inlay hints or not"; Whether to show parameter hints with the inlay hints or not.
parameterHintsPrefix = helpers.defaultNullOpts.mkStr "<- " "prefix for parameter hints"; '';
parameterHintsPrefix = helpers.defaultNullOpts.mkStr "<- " "Prefix for parameter hints.";
otherHintsPrefix = otherHintsPrefix =
helpers.defaultNullOpts.mkStr "=> " helpers.defaultNullOpts.mkStr "=> "
"prefix for all the other hints (type, chaining)"; "Prefix for all the other hints (type, chaining).";
maxLenAlign =
helpers.defaultNullOpts.mkBool false maxLenAlign = helpers.defaultNullOpts.mkBool false ''
"whether to align to the length of the longest line in the file"; Whether to align to the length of the longest line in the file.
maxLenAlignPadding = '';
helpers.defaultNullOpts.mkInt 1
"padding from the left if max_len_align is true"; maxLenAlignPadding = helpers.defaultNullOpts.mkInt 1 ''
rightAlign = Padding from the left if max_len_align is true.
helpers.defaultNullOpts.mkBool false '';
"whether to align to the extreme right or not";
rightAlignPadding = rightAlign = helpers.defaultNullOpts.mkBool false ''
helpers.defaultNullOpts.mkInt 7 Whether to align to the extreme right or not.
"padding from the right if right_align is true"; '';
highlight =
helpers.defaultNullOpts.mkStr "Comment" rightAlignPadding = helpers.defaultNullOpts.mkInt 7 ''
"The color of the hints"; Padding from the right if right_align is true.
priority = helpers.defaultNullOpts.mkInt 100 "The highlight group priority for extmark"; '';
highlight = helpers.defaultNullOpts.mkStr "Comment" "The color of the hints.";
priority = helpers.defaultNullOpts.mkInt 100 "The highlight group priority for extmark.";
}; };
ast = { ast = {
roleIcons = { roleIcons =
type = helpers.defaultNullOpts.mkStr "🄣" ""; mapAttrs
declaration = helpers.defaultNullOpts.mkStr "🄓" ""; (name: default: helpers.defaultNullOpts.mkStr default "")
expression = helpers.defaultNullOpts.mkStr "🄔" ""; {
statement = helpers.defaultNullOpts.mkStr ";" ""; type = "🄣";
specifier = helpers.defaultNullOpts.mkStr "🄢" ""; declaration = "🄓";
templateArgument = helpers.defaultNullOpts.mkStr "🆃" ""; expression = "🄔";
statement = ";";
specifier = "🄢";
templateArgument = "🆃";
}; };
kindIcons = { kindIcons =
compound = helpers.defaultNullOpts.mkStr "🄲" ""; mapAttrs
recovery = helpers.defaultNullOpts.mkStr "🅁" ""; (name: default: helpers.defaultNullOpts.mkStr default "")
translationUnit = helpers.defaultNullOpts.mkStr "🅄" ""; {
packExpansion = helpers.defaultNullOpts.mkStr "🄿" ""; compound = "🄲";
templateTypeParm = helpers.defaultNullOpts.mkStr "🅃" ""; recovery = "🅁";
templateTemplateParm = helpers.defaultNullOpts.mkStr "🅃" ""; translationUnit = "🅄";
templateParamObject = helpers.defaultNullOpts.mkStr "🅃" ""; packExpansion = "🄿";
templateTypeParm = "🅃";
templateTemplateParm = "🅃";
templateParamObject = "🅃";
}; };
highlights = { highlights = {
@ -105,72 +154,84 @@ in
border = borderOpt; border = borderOpt;
}; };
}; };
};
config = let config = let
cfg = config.plugins.clangd-extensions; setupOptions = with cfg;
setupOptions = { {
server = cfg.server.extraOptions; inlay_hints = with inlayHints; {
extensions = { inline = helpers.ifNonNull' inline (helpers.mkRaw inline);
inherit (cfg.extensions) autoSetHints; only_current_line = onlyCurrentLine;
inlay_hints = { only_current_line_autocmd = onlyCurrentLineAutocmd;
only_current_line = cfg.extensions.inlayHints.onlyCurrentLine; show_parameter_hints = showParameterHints;
only_current_line_autocmd = cfg.extensions.inlayHints.onlyCurrentLineAutocmd; parameter_hints_prefix = parameterHintsPrefix;
show_parameter_hints = cfg.extensions.inlayHints.showParameterHints; other_hints_prefix = otherHintsPrefix;
parameter_hints_prefix = cfg.extensions.inlayHints.parameterHintsPrefix; max_len_align = maxLenAlign;
other_hints_prefix = cfg.extensions.inlayHints.otherHintsPrefix; max_len_align_padding = maxLenAlignPadding;
max_len_align = cfg.extensions.inlayHints.maxLenAlign; right_align = rightAlign;
max_len_align_padding = cfg.extensions.inlayHints.maxLenAlignPadding; right_align_padding = rightAlignPadding;
right_align = cfg.extensions.inlayHints.rightAlign; inherit
right_align_padding = cfg.extensions.inlayHints.rightAlignPadding; highlight
inherit (cfg.extensions.inlayHints) highlight priority; priority
;
}; };
ast = { ast = with ast; {
role_icons = { role_icons = with roleIcons; {
inherit (cfg.extensions.ast.roleIcons) type declaration expression statement specifier; inherit
"template argument" = cfg.extensions.ast.roleIcons.templateArgument; type
declaration
expression
statement
specifier
;
"template argument" = templateArgument;
}; };
kind_icons = { kind_icons = with kindIcons; {
Compound = cfg.extensions.ast.kindIcons.compound; Compound = compound;
Recovery = cfg.extensions.ast.kindIcons.recovery; Recovery = recovery;
TranslationUnit = cfg.extensions.ast.kindIcons.translationUnit; TranslationUnit = translationUnit;
PackExpansion = cfg.extensions.ast.kindIcons.packExpansion; PackExpansion = packExpansion;
TemplateTypeParm = cfg.extensions.ast.kindIcons.templateTypeParm; TemplateTypeParm = templateTypeParm;
TemplateTemplateParm = cfg.extensions.ast.kindIcons.templateTemplateParm; TemplateTemplateParm = templateTemplateParm;
TemplateParamObject = cfg.extensions.ast.kindIcons.templateParamObject; TemplateParamObject = templateParamObject;
}; };
highlights = { highlights = with highlights; {
inherit (cfg.extensions.ast.highlights) detail; inherit detail;
}; };
}; };
memory_usage = { memory_usage = with memoryUsage; {
inherit (cfg.extensions.memoryUsage) border; inherit border;
};
symbol_info = {
inherit (cfg.extensions.symbolInfo) border;
};
}; };
symbol_info = with symbolInfo; {
inherit border;
}; };
}
// cfg.extraOptions;
in in
mkIf cfg.enable { mkIf cfg.enable {
extraPackages = [cfg.server.package]; warnings = optional (!config.plugins.lsp.enable) ''
extraPlugins = [cfg.package]; You have enabled `clangd-extensions` but not the lsp (`plugins.lsp`).
You should set `plugins.lsp.enable = true` to make use of the clangd-extensions' features.
'';
plugins.clangd-extensions.server.extraOptions = mkIf cfg.enableOffsetEncodingWorkaround { plugins.lsp.servers.clangd.extraOptions = mkIf cfg.enableOffsetEncodingWorkaround {
capabilities = {__raw = "__clangdCaps";}; capabilities = {__raw = "__clangdCaps";};
}; };
plugins.lsp.postConfig = let extraPlugins = [cfg.package];
extraCaps =
if cfg.enableOffsetEncodingWorkaround # Enable the clangd language server
then '' plugins.lsp.servers.clangd.enable = true;
plugins.lsp.preConfig =
optionalString
cfg.enableOffsetEncodingWorkaround
''
local __clangdCaps = vim.lsp.protocol.make_client_capabilities() local __clangdCaps = vim.lsp.protocol.make_client_capabilities()
__clangdCaps.offsetEncoding = { "utf-16" } __clangdCaps.offsetEncoding = { "utf-16" }
'' '';
else "";
in '' plugins.lsp.postConfig = ''
${extraCaps}
require("clangd_extensions").setup(${helpers.toLuaObject setupOptions}) require("clangd_extensions").setup(${helpers.toLuaObject setupOptions})
''; '';
}; };
} }

View file

@ -0,0 +1,62 @@
{
empty = {
plugins = {
lsp.enable = true;
clangd-extensions.enable = true;
};
};
default = {
plugins = {
lsp.enable = true;
clangd-extensions = {
enable = true;
enableOffsetEncodingWorkaround = true;
inlayHints = {
inline = ''vim.fn.has("nvim-0.10") == 1'';
onlyCurrentLine = false;
onlyCurrentLineAutocmd = "CursorHold";
showParameterHints = true;
parameterHintsPrefix = "<- ";
otherHintsPrefix = "=> ";
maxLenAlign = false;
maxLenAlignPadding = 1;
rightAlign = false;
rightAlignPadding = 7;
highlight = "Comment";
priority = 100;
};
ast = {
roleIcons = {
type = "🄣";
declaration = "🄓";
expression = "🄔";
statement = ";";
specifier = "🄢";
templateArgument = "🆃";
};
kindIcons = {
compound = "🄲";
recovery = "🅁";
translationUnit = "🅄";
packExpansion = "🄿";
templateTypeParm = "🅃";
templateTemplateParm = "🅃";
templateParamObject = "🅃";
};
highlights = {
detail = "Comment";
};
};
memoryUsage = {
border = "none";
};
symbolInfo = {
border = "none";
};
};
};
};
}