plugins/rust-tools: refactoring + tests (#258)

This commit is contained in:
Gaétan Lepage 2023-03-15 12:06:00 +01:00 committed by GitHub
parent c3821e9924
commit 86f4067159
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 215 additions and 190 deletions

View file

@ -5,45 +5,12 @@
... ...
}: }:
with lib; let with lib; let
cfg = config.plugins.rust-tools;
helpers = import ../helpers.nix {inherit lib;}; helpers = import ../helpers.nix {inherit lib;};
in { in {
options.plugins.rust-tools = let options.plugins.rust-tools =
mkNullableOptionWithDefault = { helpers.extraOptionsOptions
type, // {
description,
default,
}:
mkOption {
type = types.nullOr type;
default = null;
description = ''
${description}
default: `${default}`
'';
};
mkNullableBoolDefault = default: description:
mkNullableOptionWithDefault {
inherit description;
type = types.bool;
default = toString default;
};
mkNullableStrDefault = default: description:
mkNullableOptionWithDefault {
inherit description;
type = types.str;
default = ''"${default}"'';
};
mkNullableIntDefault = default: description:
mkNullableOptionWithDefault {
inherit description;
type = types.int;
default = toString default;
};
in {
enable = mkEnableOption "rust tools plugins"; enable = mkEnableOption "rust tools plugins";
package = helpers.mkPackageOption "rust-tools" pkgs.vimPlugins.rust-tools-nvim; package = helpers.mkPackageOption "rust-tools" pkgs.vimPlugins.rust-tools-nvim;
serverPackage = mkOption { serverPackage = mkOption {
@ -52,170 +19,176 @@ in {
description = "Package to use for rust-analyzer"; description = "Package to use for rust-analyzer";
}; };
executor = mkNullableOptionWithDefault { executor =
type = types.enum ["termopen" "quickfix"]; helpers.defaultNullOpts.mkEnumFirstDefault ["termopen" "quickfix"]
default = ''"termopen"''; "how to execute terminal commands";
description = "how to execute terminal commands";
};
onIntialized = mkOption { onInitialized =
type = types.nullOr types.str; helpers.defaultNullOpts.mkStr "null"
default = null; ''
description = ''
Callback to execute once rust-analyzer is done initializing the workspace Callback to execute once rust-analyzer is done initializing the workspace
The callback receives one parameter indicating the `health` of the server: The callback receives one parameter indicating the `health` of the server:
"ok" | "warning" | "error" "ok" | "warning" | "error"
''; '';
};
reloadWorkspaceFromCargoToml = mkNullableBoolDefault true '' reloadWorkspaceFromCargoToml = helpers.defaultNullOpts.mkBool true ''
Automatically call RustReloadWorkspace when writing to a Cargo.toml file. Automatically call RustReloadWorkspace when writing to a Cargo.toml file.
''; '';
inlayHints = { inlayHints = helpers.mkCompositeOption "inlay hints" {
auto = mkNullableBoolDefault true "automatically set inlay hints (type hints)"; auto = helpers.defaultNullOpts.mkBool true "automatically set inlay hints (type hints)";
onlyCurrentLine = mkNullableBoolDefault false "Only show for current line"; onlyCurrentLine = helpers.defaultNullOpts.mkBool false "Only show for current line";
showParameterHints = showParameterHints =
mkNullableBoolDefault true 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 = mkNullableStrDefault "<- " "prefix for parameter hints"; parameterHintsPrefix =
otherHintsPrefix = mkNullableStrDefault "=> " "prefix for all the other hints (type, chaining)"; helpers.defaultNullOpts.mkStr "<- "
"prefix for parameter hints";
otherHintsPrefix =
helpers.defaultNullOpts.mkStr "=> "
"prefix for all the other hints (type, chaining)";
maxLenAlign = maxLenAlign =
mkNullableBoolDefault false 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 = mkNullableIntDefault 1 "padding from the left if max_len_align is true"; maxLenAlignPadding =
helpers.defaultNullOpts.mkInt 1
"padding from the left if max_len_align is true";
rightAlign = mkNullableBoolDefault false "whether to align to the extreme right or not"; rightAlign =
rightAlignPadding = mkNullableIntDefault 7 "padding from the right if right_align is true"; helpers.defaultNullOpts.mkBool false
"whether to align to the extreme right or not";
highlight = mkNullableStrDefault "Comment" "The color of the hints"; rightAlignPadding =
helpers.defaultNullOpts.mkInt 7
"padding from the right if right_align is true";
highlight = helpers.defaultNullOpts.mkStr "Comment" "The color of the hints";
}; };
hoverActions = { hoverActions = helpers.mkCompositeOption "hover actions" {
border = mkOption { border =
type = types.nullOr types.anything; helpers.defaultNullOpts.mkNullable (types.listOf (types.listOf types.str))
default = null; ''
description = '' [
the border that is used for the hover window. see vim.api.nvim_open_win() [ "" "FloatBorder" ]
''; [ "" "FloatBorder" ]
[ "" "FloatBorder" ]
[ "" "FloatBorder" ]
[ "" "FloatBorder" ]
[ "" "FloatBorder" ]
[ "" "FloatBorder" ]
[ "" "FloatBorder" ]
]
''
"the border that is used for the hover window. see vim.api.nvim_open_win()";
maxWidth =
helpers.defaultNullOpts.mkNullable types.int "null"
"Maximal width of the hover window. null means no max.";
maxHeight =
helpers.defaultNullOpts.mkNullable types.int "null"
"Maximal height of the hover window. null means no max.";
autoFocus =
helpers.defaultNullOpts.mkBool false
"whether the hover action window gets automatically focused";
}; };
maxWidth = mkOption { crateGraph = helpers.mkCompositeOption "create graph" {
type = types.nullOr types.int; backend = helpers.defaultNullOpts.mkStr "x11" ''
default = null;
description = "Maximal width of the hover window. Nil means no max.";
};
maxHeight = mkOption {
type = types.nullOr types.int;
default = null;
description = "Maximal height of the hover window. Nil means no max.";
};
autoFocus = mkNullableBoolDefault false "whether the hover action window gets automatically focused";
};
crateGraph = {
backend = mkNullableStrDefault "x11" ''
Backend used for displaying the graph Backend used for displaying the graph
see: https://graphviz.org/docs/outputs/ see: https://graphviz.org/docs/outputs/
''; '';
output = mkOption { output =
type = types.nullOr types.str; helpers.defaultNullOpts.mkStr "null"
default = null; "where to store the output, nil for no output stored";
description = "where to store the output, nil for no output stored";
};
full = mkNullableBoolDefault true '' full = helpers.defaultNullOpts.mkBool true ''
true for all crates.io and external crates, false only the local crates true for all crates.io and external crates, false only the local crates
''; '';
enabledGraphvizBackends = mkOption { enabledGraphvizBackends =
type = types.nullOr (types.listOf types.str); helpers.defaultNullOpts.mkNullable (types.listOf types.str) "null"
default = null; ''
description = ''
List of backends found on: https://graphviz.org/docs/outputs/ List of backends found on: https://graphviz.org/docs/outputs/
Is used for input validation and autocompletion Is used for input validation and autocompletion
''; '';
}; };
};
server = server =
helpers.mkCompositeOption "server"
{ {
standalone = mkNullableBoolDefault true '' standalone = helpers.defaultNullOpts.mkBool true ''
standalone file support standalone file support
setting it to false may improve startup time setting it to false may improve startup time
''; '';
} }
// (import ../nvim-lsp/language-servers/rust-analyzer-config.nix lib); // (import ../nvim-lsp/language-servers/rust-analyzer-config.nix lib);
}; };
config = let config = mkIf cfg.enable {
cfg = config.plugins.rust-tools;
in
mkIf cfg.enable {
extraPlugins = with pkgs.vimPlugins; [nvim-lspconfig cfg.package]; extraPlugins = with pkgs.vimPlugins; [nvim-lspconfig cfg.package];
extraPackages = [cfg.serverPackage]; extraPackages = [cfg.serverPackage];
plugins.lsp.postConfig = let plugins.lsp.postConfig = let
setupOptions = { options =
{
tools = { tools = {
executor = executor =
if cfg.executor != null helpers.ifNonNull' cfg.executor
then {__raw = ''require("rust-tools.executors").${cfg.executor}'';} (helpers.mkRaw "require(${rust-tools.executors}).${cfg.executor}");
else null;
on_initialized = on_initialized =
if cfg.onIntialized != null helpers.ifNonNull' cfg.onInitialized
then {__raw = cfg.onIntialized;} (helpers.mkRaw cfg.onInitialized);
else null;
reload_workspace_from_cargo_toml = cfg.reloadWorkspaceFromCargoToml; reload_workspace_from_cargo_toml = cfg.reloadWorkspaceFromCargoToml;
inlay_hints = let inlay_hints = with cfg.inlayHints;
cfgIH = cfg.inlayHints; helpers.ifNonNull' cfg.inlayHints {
in { inherit auto;
auto = cfgIH.auto; only_current_line = onlyCurrentLine;
only_current_line = cfgIH.onlyCurrentLine; show_parameter_hints = showParameterHints;
show_parameter_hints = cfgIH.showParameterHints; parameter_hints_prefix = parameterHintsPrefix;
parameter_hints_prefix = cfgIH.parameterHintsPrefix; other_hints_prefix = otherHintsPrefix;
other_hints_prefix = cfgIH.otherHintsPrefix; max_len_align = maxLenAlign;
max_len_align = cfgIH.maxLenAlign; max_len_align_padding = maxLenAlignPadding;
max_len_align_padding = cfgIH.maxLenAlignPadding; right_align = rightAlign;
right_align = cfgIH.rightAlign; right_align_padding = rightAlignPadding;
right_align_padding = cfgIH.rightAlignPadding; inherit highlight;
highlight = cfgIH.highlight;
}; };
hover_actions = let hover_actions = with cfg.hoverActions;
cfgHA = cfg.hoverActions; helpers.ifNonNull' cfg.hoverActions
in { {
border = cfgHA.border; inherit border;
max_width = cfgHA.maxWidth; max_width = maxWidth;
max_height = cfgHA.maxHeight; max_height = maxHeight;
auto_focus = cfgHA.autoFocus; auto_focus = autoFocus;
}; };
crate_graph = let crate_graph = with cfg.crateGraph;
cfgCG = cfg.crateGraph; helpers.ifNonNull' cfg.crateGraph {
in { inherit backend output full;
backend = cfgCG.backend; enabled_graphviz_backends = enabledGraphvizBackends;
output = cfgCG.output;
full = cfgCG.full;
enabled_graphviz_backends = cfgCG.enabledGraphvizBackends;
}; };
}; };
server = { server = with cfg.server;
standalone = cfg.server.standalone; helpers.ifNonNull' cfg.server {
inherit standalone;
settings.rust-analyzer = lib.filterAttrs (n: v: n != "standalone") cfg.server; settings.rust-analyzer = lib.filterAttrs (n: v: n != "standalone") cfg.server;
on_attach = {__raw = "__lspOnAttach";}; on_attach = helpers.mkRaw "__lspOnAttach";
};
}; };
}
// cfg.extraOptions;
in '' in ''
require('rust-tools').setup(${helpers.toLuaObject setupOptions}) require('rust-tools').setup(${helpers.toLuaObject options})
''; '';
}; };
} }

View file

@ -0,0 +1,52 @@
{
# Empty configuration
empty = {
plugins.rust-tools.enable = true;
};
# All the upstream default options of rust-tools
defaults = {
plugins.rust-tools = {
enable = true;
executor = "termopen";
onInitialized = null;
reloadWorkspaceFromCargoToml = true;
inlayHints = {
auto = true;
onlyCurrentLine = false;
showParameterHints = true;
parameterHintsPrefix = "<- ";
otherHintsPrefix = "=> ";
maxLenAlign = false;
maxLenAlignPadding = 1;
rightAlign = false;
rightAlignPadding = 7;
highlight = "Comment";
};
hoverActions = {
border = [
["" "FloatBorder"]
["" "FloatBorder"]
["" "FloatBorder"]
["" "FloatBorder"]
["" "FloatBorder"]
["" "FloatBorder"]
["" "FloatBorder"]
["" "FloatBorder"]
];
maxWidth = null;
maxHeight = null;
autoFocus = false;
};
crateGraph = {
backend = "x11";
output = null;
full = true;
enabledGraphvizBackends = null;
};
server = {
standalone = true;
};
};
};
}