gitsigns: use nullable options (#148)

* helpers: Correctly print bool values as true/false

* helpers: Add helpers to make nullable enums with default values

* gitsigns: Refactor to use nullable options
This commit is contained in:
traxys 2023-01-25 00:55:51 +01:00 committed by GitHub
parent 66b1b099cf
commit b9a5c9c2b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 133 additions and 171 deletions

View file

@ -86,8 +86,10 @@ rec {
); );
mkInt = default: mkNullable lib.types.int (toString default); mkInt = default: mkNullable lib.types.int (toString default);
mkBool = default: mkNullable lib.types.bool (toString default); mkBool = default: mkNullable lib.types.bool (if default then "true" else "false");
mkStr = default: mkNullable lib.types.str ''"${default}"''; mkStr = default: mkNullable lib.types.str ''"${default}"'';
mkEnum = enum: default: mkNullable (lib.types.enum enum) ''"${default}"'';
mkEnumFirstDefault = enum: mkEnum enum (head enum);
}; };
mkPlugin = { config, lib, ... }: { name mkPlugin = { config, lib, ... }: { name

View file

@ -1,33 +1,26 @@
{ config { config
, lib , lib
, pkgs , pkgs
, helpers
, ... , ...
}: } @ args:
with lib; let with lib; let
signOptions = defaults: helpers = import ../helpers.nix args;
with types; { signOptions = defaults: {
hl = mkOption { hl =
type = str; helpers.defaultNullOpts.mkStr defaults.hl
description = "Specifies the highlight group to use for the sign"; "Specifies the highlight group to use for the sign";
default = defaults.hl; text =
}; helpers.defaultNullOpts.mkStr defaults.text
text = mkOption { "Specifies the character to use for the sign";
type = str; numhl =
description = "Specifies the character to use for the sign"; helpers.defaultNullOpts.mkStr defaults.numhl
default = defaults.text; "Specifies the highlight group to use for the number column";
}; linehl =
numhl = mkOption { helpers.defaultNullOpts.mkStr defaults.linehl
type = str; "Specifies the highlight group to use for the line";
description = "Specifies the highlight group to use for the number column"; showCount =
default = defaults.numhl; helpers.defaultNullOpts.mkBool false
}; "showing count of hunk, e.g. number of deleted lines";
linehl = mkOption {
type = str;
description = "Specifies the highlight group to use for the line";
default = defaults.linehl;
};
showCount = mkEnableOption "showing count of hunk, e.g. number of deleted lines";
}; };
signSetupOptions = values: { signSetupOptions = values: {
inherit (values) hl text numhl linehl; inherit (values) hl text numhl linehl;
@ -135,52 +128,42 @@ in
}; };
watchGitDir = { watchGitDir = {
enable = mkOption { enable =
type = types.bool; helpers.defaultNullOpts.mkBool true
default = true; "Whether the watcher is enabled";
description = "Whether the watcher is enabled"; interval =
helpers.defaultNullOpts.mkInt 1000
"Interval the watcher waits between polls of the gitdir in milliseconds";
followFiles =
helpers.defaultNullOpts.mkBool true
"If a file is moved with `git mv`, switch the buffer to the new location";
}; };
interval = mkOption { signPriority =
type = types.int; helpers.defaultNullOpts.mkInt 6
default = 1000; "Priority to use for signs";
description = "Interval the watcher waits between polls of the gitdir in milliseconds"; signcolumn =
}; helpers.defaultNullOpts.mkBool true
followFiles = mkOption { ''
type = types.bool;
default = true;
description = "If a file is moved with `git mv`, switch the buffer to the new location";
};
};
signPriority = mkOption {
type = types.int;
default = 6;
description = "Priority to use for signs";
};
signcolumn = mkOption {
type = types.bool;
default = true;
description = ''
Enable/disable symbols in the sign column. Enable/disable symbols in the sign column.
When enabled the highlights defined in `signs.*.hl` and symbols defined When enabled the highlights defined in `signs.*.hl` and symbols defined
in `signs.*.text` are used. in `signs.*.text` are used.
''; '';
}; numhl = helpers.defaultNullOpts.mkBool false ''
numhl = mkEnableOption ''
line number highlights. line number highlights.
When enabled the highlights defined in `signs.*.numhl` are used. If When enabled the highlights defined in `signs.*.numhl` are used. If
the highlight group does not exist, then it is automatically defined the highlight group does not exist, then it is automatically defined
and linked to the corresponding highlight group in `signs.*.hl`. and linked to the corresponding highlight group in `signs.*.hl`.
''; '';
linehl = mkEnableOption '' linehl = helpers.defaultNullOpts.mkBool false ''
line highlights. line highlights.
When enabled the highlights defined in `signs.*.linehl` are used. If When enabled the highlights defined in `signs.*.linehl` are used. If
the highlight group does not exist, then it is automatically defined the highlight group does not exist, then it is automatically defined
and linked to the corresponding highlight group in `signs.*.hl`. and linked to the corresponding highlight group in `signs.*.hl`.
''; '';
showDeleted = mkEnableOption '' showDeleted = helpers.defaultNullOpts.mkBool false ''
showing the old version of hunks inline in the buffer (via virtual lines). showing the old version of hunks inline in the buffer (via virtual lines).
Note: Virtual lines currently use the highlight `GitSignsDeleteVirtLn`. Note: Virtual lines currently use the highlight `GitSignsDeleteVirtLn`.
@ -189,26 +172,18 @@ in
let let
diffOptModule = { diffOptModule = {
options = { options = {
algorithm = mkOption { algorithm =
type = types.enum [ "myers" "minimal" "patience" "histogram" ]; helpers.defaultNullOpts.mkEnumFirstDefault [ "myers" "minimal" "patience" "histogram" ]
default = "myers"; "Diff algorithm to use";
description = "Diff algorithm to use"; internal =
}; helpers.defaultNullOpts.mkBool false
internal = mkOption { "Use Neovim's built in xdiff library for running diffs";
type = types.bool; indentHeuristic =
default = false; helpers.defaultNullOpts.mkBool false
description = "Use Neovim's built in xdiff library for running diffs"; "Use the indent heuristic for the internal diff library.";
}; vertical =
indentHeuristic = mkOption { helpers.defaultNullOpts.mkBool true
type = types.bool; "Start diff mode with vertical splits";
default = false;
description = "Use the indent heuristic for the internal diff library.";
};
vertical = mkOption {
type = types.bool;
default = true;
description = "Start diff mode with vertical splits";
};
linematch = mkOption { linematch = mkOption {
type = types.nullOr types.int; type = types.nullOr types.int;
default = null; default = null;
@ -230,9 +205,9 @@ in
default = null; default = null;
description = "The object/revision to diff against. Default to 'index'"; description = "The object/revision to diff against. Default to 'index'";
}; };
countChars = mkOption { countChars =
type = types.attrsOf types.str; helpers.defaultNullOpts.mkNullable (types.attrsOf types.str) ''
default = { {
"1" = "1"; "1" = "1";
"2" = "2"; "2" = "2";
"3" = "3"; "3" = "3";
@ -243,8 +218,8 @@ in
"8" = "8"; "8" = "8";
"9" = "9"; "9" = "9";
"+" = ">"; "+" = ">";
}; }
description = '' '' ''
The count characters used when `signs.*.show_count` is enabled. The The count characters used when `signs.*.show_count` is enabled. The
`+` entry is used as a fallback. With the default, any count outside `+` entry is used as a fallback. With the default, any count outside
of 1-9 uses the `>` character in the sign. of 1-9 uses the `>` character in the sign.
@ -253,11 +228,9 @@ in
to specify unicode characters for the counts instead of 1-9. to specify unicode characters for the counts instead of 1-9.
to define characters to be used for counts greater than 9. to define characters to be used for counts greater than 9.
''; '';
}; statusFormatter = helpers.defaultNullOpts.mkNullable luaFunction ''
statusFormatter = mkOption { {
type = luaFunction; function = \'\'
default = {
function = ''
function(status) function(status)
local added, changed, removed = status.added, status.changed, status.removed local added, changed, removed = status.added, status.changed, status.removed
local status_txt = {} local status_txt = {}
@ -266,86 +239,68 @@ in
if removed and removed > 0 then table.insert(status_txt, '-'..removed) end if removed and removed > 0 then table.insert(status_txt, '-'..removed) end
return table.concat(status_txt, ' ') return table.concat(status_txt, ' ')
end end
''; \'\';
}; }
description = "Function used to format `b:gitsigns_status`"; '' "Function used to format `b:gitsigns_status`";
}; maxFileLength =
maxFileLength = mkOption { helpers.defaultNullOpts.mkInt 40000
type = types.int; "Max file length (in lines) to attach to";
default = 40000; previewConfig =
description = "Max file length (in lines) to attach to"; helpers.defaultNullOpts.mkNullable (types.attrsOf types.anything) ''
}; {
previewConfig = mkOption {
type = types.attrsOf types.anything;
default = {
border = "single"; border = "single";
style = "minimal"; style = "minimal";
relative = "cursor"; relative = "cursor";
row = 0; row = 0;
col = 1; col = 1;
}; }
description = '' '' ''
Option overrides for the Gitsigns preview window. Option overrides for the Gitsigns preview window.
Table is passed directly to `nvim_open_win`. Table is passed directly to `nvim_open_win`.
''; '';
}; attachToUntracked =
attachToUntracked = mkOption { helpers.defaultNullOpts.mkBool true
type = types.bool; "Attach to untracked files.";
default = true; updateDebounce =
description = "Attach to untracked files."; helpers.defaultNullOpts.mkInt 100
}; "Debounce time for updates (in milliseconds).";
updateDebounce = mkOption { currentLineBlame = helpers.defaultNullOpts.mkBool false ''
type = types.number;
default = 100;
description = "Debounce time for updates (in milliseconds).";
};
currentLineBlame = mkEnableOption ''
Adds an unobtrusive and customisable blame annotation at the end of the current line. Adds an unobtrusive and customisable blame annotation at the end of the current line.
''; '';
currentLineBlameOpts = { currentLineBlameOpts = {
virtText = mkOption { virtText =
type = types.bool; helpers.defaultNullOpts.mkBool true
default = true; "Whether to show a virtual text blame annotation";
description = "Whether to show a virtual text blame annotation"; virtTextPos =
}; helpers.defaultNullOpts.mkEnumFirstDefault [ "eol" "overlay" "right_align" ]
virtTextPos = mkOption { "Blame annotation position";
type = types.enum [ "eol" "overlay" "right_align" ]; delay =
default = "eol"; helpers.defaultNullOpts.mkInt 1000
description = "Blame annotation position"; "Sets the delay (in milliseconds) before blame virtual text is displayed";
}; ignoreWhitespace =
delay = mkOption { helpers.defaultNullOpts.mkBool false
type = types.int; "Ignore whitespace when running blame";
default = 1000; virtTextPriority =
description = "Sets the delay (in milliseconds) before blame virtual text is displayed"; helpers.defaultNullOpts.mkInt 100
}; "Priority of virtual text";
ignoreWhitespace = mkEnableOption "Ignore whitespace when running blame";
virtTextPriority = mkOption {
type = types.int;
default = 100;
description = "Priority of virtual text";
};
}; };
currentLineBlameFormatter = { currentLineBlameFormatter = {
normal = mkOption { normal =
type = types.either types.str luaFunction; helpers.defaultNullOpts.mkNullable (types.either types.str luaFunction)
default = " <author>, <author_time> - <summary>"; ''" <author>, <author_time> - <summary>"'' ''
description = ''
String or function used to format the virtual text of String or function used to format the virtual text of
|gitsigns-config-current_line_blame|. |gitsigns-config-current_line_blame|.
See |gitsigns-config-current_line_blame_formatter| for more details. See |gitsigns-config-current_line_blame_formatter| for more details.
''; '';
};
nonCommitted = mkOption { nonCommitted =
type = types.either types.str luaFunction; helpers.defaultNullOpts.mkNullable (types.either types.str luaFunction)
default = " <author>"; ''" <author>"'' ''
description = ''
String or function used to format the virtual text of String or function used to format the virtual text of
|gitsigns-config-current_line_blame| for lines that aren't committed. |gitsigns-config-current_line_blame| for lines that aren't committed.
''; '';
}; };
};
trouble = mkOption { trouble = mkOption {
type = types.nullOr types.bool; type = types.nullOr types.bool;
default = null; default = null;
@ -354,12 +309,12 @@ in
window. window.
''; '';
}; };
yadm.enable = mkEnableOption "YADM support"; yadm.enable = helpers.defaultNullOpts.mkBool false "YADM support";
wordDiff = mkEnableOption '' wordDiff = helpers.defaultNullOpts.mkBool false ''
Highlight intra-line word differences in the buffer. Highlight intra-line word differences in the buffer.
Requires `config.diff_opts.internal = true`. Requires `config.diff_opts.internal = true`.
''; '';
debugMode = mkEnableOption '' debugMode = helpers.defaultNullOpts.mkBool false ''
Enables debug logging and makes the following functions available: `dump_cache`, Enables debug logging and makes the following functions available: `dump_cache`,
`debug_messages`, `clear_debug`. `debug_messages`, `clear_debug`.
''; '';
@ -370,13 +325,13 @@ in
cfg = config.plugins.gitsigns; cfg = config.plugins.gitsigns;
in in
mkIf cfg.enable { mkIf cfg.enable {
extraPlugins = with pkgs.vimPlugins; [ extraPlugins = [ cfg.package ];
cfg.package
];
extraConfigLua = extraConfigLua =
let let
luaFnOrStrToObj = val: luaFnOrStrToObj = val:
if builtins.isString val if val == null
then null
else if builtins.isString val
then val then val
else { __raw = val.function; }; else { __raw = val.function; };
setupOptions = { setupOptions = {
@ -403,7 +358,8 @@ in
let let
isStrInt = s: (builtins.match "[0-9]+" s) != null; isStrInt = s: (builtins.match "[0-9]+" s) != null;
in in
{ if cfg.countChars != null
then {
__raw = __raw =
"{" "{"
+ (concatStringsSep "," ( + (concatStringsSep "," (
@ -417,8 +373,12 @@ in
cfg.countChars cfg.countChars
)) ))
+ "}"; + "}";
}; }
status_formatter = { __raw = cfg.statusFormatter.function; }; else null;
status_formatter =
if cfg.statusFormatter != null
then { __raw = cfg.statusFormatter.function; }
else null;
max_file_length = cfg.maxFileLength; max_file_length = cfg.maxFileLength;
preview_config = cfg.previewConfig; preview_config = cfg.previewConfig;
attach_to_untracked = cfg.attachToUntracked; attach_to_untracked = cfg.attachToUntracked;