on master help sign in
Add a bunch of aliases (#797)

* add a bunch of aliases * apply code review from archseer

commit7e958e1834f555724bb73db0091d66365abd63a3 authorIrevoire <irevoire@protonmail.ch> dateSun 3 Oct 2021, 04:41 (5 years ago) parent0af8928
1 file changed+44−47
helix-term/src/commands.rs+44−47
helix-term/src/commands.rs+44−47
@@ -1492,7 +1492,7 @@ mod cmd {
14921492 #[derive(Clone)]
14931493 pub struct TypableCommand {
14941494 pub name: &'static str,
1495− pub alias: Option<&'static str>,
1495+ pub aliases: &'static [&'static str],
14961496 pub doc: &'static str,
14971497 // params, flags, helper, completer
14981498 pub fun: fn(&mut compositor::Context, &[&str], PromptEvent) -> anyhow::Result<()>,
@@ -2092,252 +2092,252 @@ mod cmd {
20922092 pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
20932093 TypableCommand {
20942094 name: "quit",
2095− alias: Some("q"),
2095+ aliases: &["q"],
20962096 doc: "Close the current view.",
20972097 fun: quit,
20982098 completer: None,
20992099 },
21002100 TypableCommand {
21012101 name: "quit!",
2102− alias: Some("q!"),
2102+ aliases: &["q!"],
21032103 doc: "Close the current view.",
21042104 fun: force_quit,
21052105 completer: None,
21062106 },
21072107 TypableCommand {
21082108 name: "open",
2109− alias: Some("o"),
2109+ aliases: &["o"],
21102110 doc: "Open a file from disk into the current view.",
21112111 fun: open,
21122112 completer: Some(completers::filename),
21132113 },
21142114 TypableCommand {
21152115 name: "write",
2116− alias: Some("w"),
2116+ aliases: &["w"],
21172117 doc: "Write changes to disk. Accepts an optional path (:write some/path.txt)",
21182118 fun: write,
21192119 completer: Some(completers::filename),
21202120 },
21212121 TypableCommand {
21222122 name: "new",
2123− alias: Some("n"),
2123+ aliases: &["n"],
21242124 doc: "Create a new scratch buffer.",
21252125 fun: new_file,
21262126 completer: Some(completers::filename),
21272127 },
21282128 TypableCommand {
21292129 name: "format",
2130− alias: Some("fmt"),
2130+ aliases: &["fmt"],
21312131 doc: "Format the file using a formatter.",
21322132 fun: format,
21332133 completer: None,
21342134 },
21352135 TypableCommand {
21362136 name: "indent-style",
2137− alias: None,
2137+ aliases: &[],
21382138 doc: "Set the indentation style for editing. ('t' for tabs or 1-8 for number of spaces.)",
21392139 fun: set_indent_style,
21402140 completer: None,
21412141 },
21422142 TypableCommand {
21432143 name: "line-ending",
2144− alias: None,
2144+ aliases: &[],
21452145 doc: "Set the document's default line ending. Options: crlf, lf, cr, ff, nel.",
21462146 fun: set_line_ending,
21472147 completer: None,
21482148 },
21492149 TypableCommand {
21502150 name: "earlier",
2151− alias: Some("ear"),
2151+ aliases: &["ear"],
21522152 doc: "Jump back to an earlier point in edit history. Accepts a number of steps or a time span.",
21532153 fun: earlier,
21542154 completer: None,
21552155 },
21562156 TypableCommand {
21572157 name: "later",
2158− alias: Some("lat"),
2158+ aliases: &["lat"],
21592159 doc: "Jump to a later point in edit history. Accepts a number of steps or a time span.",
21602160 fun: later,
21612161 completer: None,
21622162 },
21632163 TypableCommand {
21642164 name: "write-quit",
2165− alias: Some("wq"),
2165+ aliases: &["wq", "x"],
21662166 doc: "Writes changes to disk and closes the current view. Accepts an optional path (:wq some/path.txt)",
21672167 fun: write_quit,
21682168 completer: Some(completers::filename),
21692169 },
21702170 TypableCommand {
21712171 name: "write-quit!",
2172− alias: Some("wq!"),
2172+ aliases: &["wq!", "x!"],
21732173 doc: "Writes changes to disk and closes the current view forcefully. Accepts an optional path (:wq! some/path.txt)",
21742174 fun: force_write_quit,
21752175 completer: Some(completers::filename),
21762176 },
21772177 TypableCommand {
21782178 name: "write-all",
2179− alias: Some("wa"),
2179+ aliases: &["wa"],
21802180 doc: "Writes changes from all views to disk.",
21812181 fun: write_all,
21822182 completer: None,
21832183 },
21842184 TypableCommand {
21852185 name: "write-quit-all",
2186− alias: Some("wqa"),
2186+ aliases: &["wqa", "xa"],
21872187 doc: "Writes changes from all views to disk and close all views.",
21882188 fun: write_all_quit,
21892189 completer: None,
21902190 },
21912191 TypableCommand {
21922192 name: "write-quit-all!",
2193− alias: Some("wqa!"),
2193+ aliases: &["wqa!", "xa!"],
21942194 doc: "Writes changes from all views to disk and close all views forcefully (ignoring unsaved changes).",
21952195 fun: force_write_all_quit,
21962196 completer: None,
21972197 },
21982198 TypableCommand {
21992199 name: "quit-all",
2200− alias: Some("qa"),
2200+ aliases: &["qa"],
22012201 doc: "Close all views.",
22022202 fun: quit_all,
22032203 completer: None,
22042204 },
22052205 TypableCommand {
22062206 name: "quit-all!",
2207− alias: Some("qa!"),
2207+ aliases: &["qa!"],
22082208 doc: "Close all views forcefully (ignoring unsaved changes).",
22092209 fun: force_quit_all,
22102210 completer: None,
22112211 },
22122212 TypableCommand {
22132213 name: "theme",
2214− alias: None,
2214+ aliases: &[],
22152215 doc: "Change the theme of current view. Requires theme name as argument (:theme <name>)",
22162216 fun: theme,
22172217 completer: Some(completers::theme),
22182218 },
22192219 TypableCommand {
22202220 name: "clipboard-yank",
2221− alias: None,
2221+ aliases: &[],
22222222 doc: "Yank main selection into system clipboard.",
22232223 fun: yank_main_selection_to_clipboard,
22242224 completer: None,
22252225 },
22262226 TypableCommand {
22272227 name: "clipboard-yank-join",
2228− alias: None,
2228+ aliases: &[],
22292229 doc: "Yank joined selections into system clipboard. A separator can be provided as first argument. Default value is newline.", // FIXME: current UI can't display long doc.
22302230 fun: yank_joined_to_clipboard,
22312231 completer: None,
22322232 },
22332233 TypableCommand {
22342234 name: "primary-clipboard-yank",
2235− alias: None,
2235+ aliases: &[],
22362236 doc: "Yank main selection into system primary clipboard.",
22372237 fun: yank_main_selection_to_primary_clipboard,
22382238 completer: None,
22392239 },
22402240 TypableCommand {
22412241 name: "primary-clipboard-yank-join",
2242− alias: None,
2242+ aliases: &[],
22432243 doc: "Yank joined selections into system primary clipboard. A separator can be provided as first argument. Default value is newline.", // FIXME: current UI can't display long doc.
22442244 fun: yank_joined_to_primary_clipboard,
22452245 completer: None,
22462246 },
22472247 TypableCommand {
22482248 name: "clipboard-paste-after",
2249− alias: None,
2249+ aliases: &[],
22502250 doc: "Paste system clipboard after selections.",
22512251 fun: paste_clipboard_after,
22522252 completer: None,
22532253 },
22542254 TypableCommand {
22552255 name: "clipboard-paste-before",
2256− alias: None,
2256+ aliases: &[],
22572257 doc: "Paste system clipboard before selections.",
22582258 fun: paste_clipboard_before,
22592259 completer: None,
22602260 },
22612261 TypableCommand {
22622262 name: "clipboard-paste-replace",
2263− alias: None,
2263+ aliases: &[],
22642264 doc: "Replace selections with content of system clipboard.",
22652265 fun: replace_selections_with_clipboard,
22662266 completer: None,
22672267 },
22682268 TypableCommand {
22692269 name: "primary-clipboard-paste-after",
2270− alias: None,
2270+ aliases: &[],
22712271 doc: "Paste primary clipboard after selections.",
22722272 fun: paste_primary_clipboard_after,
22732273 completer: None,
22742274 },
22752275 TypableCommand {
22762276 name: "primary-clipboard-paste-before",
2277− alias: None,
2277+ aliases: &[],
22782278 doc: "Paste primary clipboard before selections.",
22792279 fun: paste_primary_clipboard_before,
22802280 completer: None,
22812281 },
22822282 TypableCommand {
22832283 name: "primary-clipboard-paste-replace",
2284− alias: None,
2284+ aliases: &[],
22852285 doc: "Replace selections with content of system primary clipboard.",
22862286 fun: replace_selections_with_primary_clipboard,
22872287 completer: None,
22882288 },
22892289 TypableCommand {
22902290 name: "show-clipboard-provider",
2291− alias: None,
2291+ aliases: &[],
22922292 doc: "Show clipboard provider name in status bar.",
22932293 fun: show_clipboard_provider,
22942294 completer: None,
22952295 },
22962296 TypableCommand {
22972297 name: "change-current-directory",
2298− alias: Some("cd"),
2298+ aliases: &["cd"],
22992299 doc: "Change the current working directory (:cd <dir>).",
23002300 fun: change_current_directory,
23012301 completer: Some(completers::directory),
23022302 },
23032303 TypableCommand {
23042304 name: "show-directory",
2305− alias: Some("pwd"),
2305+ aliases: &["pwd"],
23062306 doc: "Show the current working directory.",
23072307 fun: show_current_directory,
23082308 completer: None,
23092309 },
23102310 TypableCommand {
23112311 name: "encoding",
2312− alias: None,
2312+ aliases: &[],
23132313 doc: "Set encoding based on `https://encoding.spec.whatwg.org`",
23142314 fun: set_encoding,
23152315 completer: None,
23162316 },
23172317 TypableCommand {
23182318 name: "reload",
2319− alias: None,
2319+ aliases: &[],
23202320 doc: "Discard changes and reload from the source file.",
23212321 fun: reload,
23222322 completer: None,
23232323 },
23242324 TypableCommand {
23252325 name: "tree-sitter-scopes",
2326− alias: None,
2326+ aliases: &[],
23272327 doc: "Display tree sitter scopes, primarily for theming and development.",
23282328 fun: tree_sitter_scopes,
23292329 completer: None,
23302330 },
23312331 TypableCommand {
23322332 name: "vsplit",
2333− alias: Some("vs"),
2333+ aliases: &["vs"],
23342334 doc: "Open the file in a vertical split.",
23352335 fun: vsplit,
23362336 completer: Some(completers::filename),
23372337 },
23382338 TypableCommand {
23392339 name: "hsplit",
2340− alias: Some("sp"),
2340+ aliases: &["hs", "sp"],
23412341 doc: "Open the file in a horizontal split.",
23422342 fun: hsplit,
23432343 completer: Some(completers::filename),
@@ -2345,16 +2345,13 @@ mod cmd {
23452345 ];
23462346
23472347 pub static COMMANDS: Lazy<HashMap<&'static str, &'static TypableCommand>> = Lazy::new(|| {
2348− let mut map = HashMap::new();
2349−
2350− for cmd in TYPABLE_COMMAND_LIST {
2351− map.insert(cmd.name, cmd);
2352− if let Some(alias) = cmd.alias {
2353− map.insert(alias, cmd);
2354− }
2355− }
2356−
2357− map
2348+ TYPABLE_COMMAND_LIST
2349+ .into_iter()
2350+ .flat_map(|cmd| {
2351+ std::iter::once((cmd.name, cmd))
2352+ .chain(cmd.aliases.into_iter().map(move |&alias| (alias, cmd)))
2353+ })
2354+ .collect()
23582355 });
23592356}
23602357