nix-community.nixvim/modules/commands.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

83 lines
2.6 KiB
Nix
Raw Permalink Normal View History

{
lib,
helpers,
config,
...
}:
let
2024-08-30 14:37:41 -05:00
commandAttributes = lib.types.submodule {
options = {
2024-08-30 14:37:41 -05:00
command = lib.mkOption {
2024-09-27 08:07:20 +01:00
type = with lib.types; either str rawLua;
2024-02-11 12:51:34 +00:00
description = "The command to run.";
};
nargs =
helpers.mkNullOrOption
2024-08-30 14:37:41 -05:00
(lib.types.enum [
2024-05-21 12:37:43 +01:00
0
1
"*"
"?"
"+"
])
''
The number of arguments to expect, see :h command-nargs.
'';
2024-09-27 08:07:20 +01:00
complete = helpers.mkNullOrOption (with lib.types; either str rawLua) ''
Tab-completion behaviour, see :h command-complete.
'';
range =
helpers.mkNullOrOption
(
2024-08-30 14:37:41 -05:00
with lib.types;
oneOf [
bool
int
(enum [ "%" ])
2024-05-05 19:39:35 +02:00
]
)
''
Whether the command accepts a range, see :h command-range.
'';
2024-08-30 14:37:41 -05:00
count = helpers.mkNullOrOption (with lib.types; either bool int) ''
Whether the command accepts a count, see :h command-range.
'';
2024-08-30 14:37:41 -05:00
addr = helpers.mkNullOrOption lib.types.str ''
Whether special characters relate to other things, see :h command-addr.
'';
2024-02-11 12:51:34 +00:00
bang = helpers.defaultNullOpts.mkBool false "Whether this command can take a bang (!).";
bar = helpers.defaultNullOpts.mkBool false "Whether this command can be followed by a \"|\" and another command.";
register = helpers.defaultNullOpts.mkBool false "The first argument to the command can be an optional register.";
keepscript = helpers.defaultNullOpts.mkBool false "Do not use the location of where the user command was defined for verbose messages, use the location of where the command was invoked.";
force = helpers.defaultNullOpts.mkBool false "Overwrite an existing user command.";
desc = helpers.defaultNullOpts.mkStr "" "A description of the command.";
# TODO: command-preview, need to grab a function here.
};
};
in
{
2024-08-30 14:37:41 -05:00
options.userCommands = lib.mkOption {
type = lib.types.attrsOf commandAttributes;
default = { };
2024-02-11 12:51:34 +00:00
description = "A list of user commands to add to the configuration.";
};
config =
let
cleanupCommand = _: cmd: {
inherit (cmd) command;
2024-08-30 14:37:41 -05:00
options = lib.filterAttrs (name: _: name != "command") cmd;
};
in
2024-08-30 14:37:41 -05:00
lib.mkIf (config.userCommands != { }) {
extraConfigLua = helpers.wrapDo ''
local cmds = ${lib.nixvim.toLuaObject (lib.mapAttrs cleanupCommand config.userCommands)};
for name,cmd in pairs(cmds) do
vim.api.nvim_create_user_command(name, cmd.command, cmd.options or {})
end
'';
};
}