nvrim/cmds/
user_cmds.rs

1//! User command registration helpers.
2//!
3//! Defines a small set of ergonomic commands (copy all, highlight listing, lazy maintenance, select all, fkr generator)
4//! and registers them with Nvim while notifying on failures.
5
6use nvim_oxi::api::StringOrFunction;
7use nvim_oxi::api::opts::CreateCommandOpts;
8use nvim_oxi::api::opts::CreateCommandOptsBuilder;
9use nvim_oxi::api::types::CommandArgs;
10
11const USER_CMDS: [(&str, &str, &str); 6] = [
12    ("CopyAll", "Copy all", ":%y+"),
13    ("Highlights", "FzfHighlights", ":FzfLua highlights"),
14    ("LazyProfile", "Lazy profile", ":Lazy profile"),
15    ("LazyUpdate", "Lazy update", ":Lazy update"),
16    ("SelectAll", "Select all", "normal! ggVG"),
17    (
18        "Fkr",
19        "Gen string with fkr",
20        ":lua require('nvrim').fkr.insert_string()",
21    ),
22];
23
24/// Creates user commands (including `Fkr` for random string generation).
25pub fn create() {
26    for (cmd_name, desc, cmd) in USER_CMDS {
27        create_user_cmd(cmd_name, cmd, &CreateCommandOptsBuilder::default().desc(desc).build());
28    }
29}
30
31/// Registers a single user command with Nvim.
32fn create_user_cmd<Cmd>(name: &str, command: Cmd, opts: &CreateCommandOpts)
33where
34    Cmd: StringOrFunction<CommandArgs, ()>,
35{
36    if let Err(err) = nvim_oxi::api::create_user_command(name, command, opts) {
37        ytil_noxi::notify::error(format!(
38            "error creating user command | name={name:?} opts={opts:#?} error={err:#?}"
39        ));
40    }
41}