Skip to content

Commit

Permalink
feat: expand aliasing support on commands
Browse files Browse the repository at this point in the history
This allows seperate commands to be collapsed into a single one and then
have their aliases to map to flag operations.
  • Loading branch information
RoloEdits committed Dec 22, 2024
1 parent 138a11b commit 0aa159b
Show file tree
Hide file tree
Showing 8 changed files with 735 additions and 552 deletions.
22 changes: 3 additions & 19 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,19 @@
| `:buffer-close-all!`, `:bca!`, `:bcloseall!` | force close all buffers ignoring unsaved changes without quitting. |
| `:buffer-next`, `:bn`, `:bnext` | goto next buffer. |
| `:buffer-previous`, `:bp`, `:bprev` | goto previous buffer. |
| `:write`, `:w` | write changes to disk |
| `:write!`, `:w!` | force write changes to disk creating necessary subdirectories. |
| `:write-buffer-close`, `:wbc` | write changes to disk and closes the buffer |
| `:write-buffer-close!`, `:wbc!` | force write changes to disk creating necessary subdirectories and closes the buffer |
| `:write`, `:w`, `:u`, `:x`, `:wq`, `:x!`, `:wq!`, `:w!`, `:wa`, `:wa!`, `:waq`, `:wqa`, `:xa`, `:waq!`, `:wqa!`, `:xa!`, `:wbc`, `:wbc!` | write changes to disk |
| `:new`, `:n` | create a new scratch buffer. |
| `:format`, `:fmt` | format the file using an external formatter or language server. |
| `:indent-style` | set the indentation style for editing. ('t' for tabs or 1-16 for number of spaces.) |
| `:line-ending` | Set the document's default line ending. Options: crlf, lf. |
| `:earlier`, `:ear` | jump back to an earlier point in edit history. |
| `:later`, `:lat` | jump to a later point in edit history. |
| `:write-quit`, `:wq`, `:x` | write changes to disk and close the current view. |
| `:write-quit!`, `:wq!`, `:x!` | write changes to disk and close the current view forcefully. |
| `:write-all`, `:wa` | write changes from all buffers to disk. |
| `:write-all!`, `:wa!` | forcefully write changes from all buffers to disk creating necessary subdirectories. |
| `:write-quit-all`, `:wqa`, `:xa` | write changes from all buffers to disk and close all views. |
| `:write-quit-all!`, `:wqa!`, `:xa!` | write changes from all buffers to disk and close all views forcefully (ignoring unsaved changes). |
| `:quit-all`, `:qa` | close all views. |
| `:quit-all!`, `:qa!` | force close all views ignoring unsaved changes. |
| `:cquit`, `:cq` | quit with exit code (default 1) |
| `:cquit!`, `:cq!` | force quit with exit code (default 1) ignoring unsaved changes. |
| `:theme` | change the editor theme (show current theme if no name specified). |
| `:yank-join` | yank joined selections. a separator can be provided as first argument. default value is newline. |
| `:yank` | yank selection to clipboard. |
| `:clipboard-yank` | yank main selection into system clipboard. |
| `:clipboard-yank-join` | yank joined selections into system clipboard. a separator can be provided as first argument. default value is newline. |
| `:primary-clipboard-yank` | yank main selection into system primary clipboard. |
| `:primary-clipboard-yank-join` | yank joined selections into system primary clipboard. a separator can be provided as first argument. default value is newline. |
| `:yank`, `:y`, `:yj`, `:yd` | yank selection to clipboard. |
| `:clipboard-paste-after` | paste system clipboard after selections. |
| `:clipboard-paste-before` | paste system clipboard before selections. |
| `:clipboard-paste-replace` | replace selections with content of system clipboard. |
Expand All @@ -51,7 +37,6 @@
| `:character-info`, `:char` | get info about the character under the primary cursor. |
| `:reload`, `:rl` | discard changes and reload from the source file. |
| `:reload-all`, `:rla` | discard changes and reload all documents from the source files. |
| `:update`, `:u` | write changes only if the file has been modified. |
| `:lsp-workspace-command` | open workspace command picker |
| `:lsp-restart` | restarts the language servers used by the current doc |
| `:lsp-stop` | stops the language servers that are used by the current doc |
Expand All @@ -70,7 +55,7 @@
| `:set-option`, `:set` | set a config option at runtime. for example to disable smart case search, use `:set search.smart-case false`. |
| `:toggle-option`, `:toggle` | toggle a boolean config option at runtime. for example to toggle smart case search, use `:toggle search.smart-case`. |
| `:get-option`, `:get` | get the current value of a config option. |
| `:sort` | sort ranges in selection. |
| `:sort`, `:rsort` | sort ranges in selection. |
| `:reflow` | hard-wrap the current selection of lines to a given width. |
| `:tree-sitter-subtree`, `:ts-subtree` | display the smallest tree-sitter subtree that spans the primary selection, primarily for debugging queries. |
| `:config-reload` | refresh user config. |
Expand All @@ -86,5 +71,4 @@
| `:clear-register` | clear given register. |
| `:redraw` | clear and re-render the whole UI |
| `:move`, `:mv` | move the current buffer and its corresponding file to a different path |
| `:yank-diagnostic` | yank diagnostic(s) under primary cursor to register, or clipboard by default |
| `:read`, `:r` | load a file into buffer |
1 change: 1 addition & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub(crate) mod alias;
pub(crate) mod dap;
pub(crate) mod flag;
pub(crate) mod lsp;
Expand Down
77 changes: 77 additions & 0 deletions helix-term/src/commands/alias.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// TODO: check if there could be overlap for the space mode keymap and docs. Might need to make doc Option
// so that if there is overlap then it can be backwards compatible, as long as ":waq" = [":write --all", ":quit"] works.
// [commands]
// ":waq" = { commands = [":write --all", ":quit"], doc = "write all buffers to disk and quit out of helix" }

// To be able to display the prompt in a better manner for all typed commands, as well as the aliases. Might need
// a Prompt trait that would have a `prompt(&self) -> String` function This way another function could take
// `T: Prompt` and there could live a `Box<dyn Prompt>`, with a way to iterate over and display them all.

// waq: write all buffers to disk and quit out of helix
//
// aliases:
// :write --all -> :quit

#[derive(Debug, Clone, Copy)]
pub struct Aliases {
pub aliases: &'static [Alias],
}

impl Aliases {
pub const fn empty() -> Self {
Self { aliases: &[] }
}

pub const fn is_empty(&self) -> bool {
self.aliases.is_empty()
}

pub fn get(&self, name: &str) -> Option<&Alias> {
self.aliases.iter().find(|alias| alias.name == name)
}

pub fn iter(&self) -> impl Iterator<Item = &Alias> {
self.aliases.iter()
}
}

#[derive(Debug, Clone, Copy)]
pub struct Alias {
pub name: &'static str,
pub command: Option<&'static str>,
}

impl Alias {
pub fn flags(&self) -> Option<&str> {
if let Some(command) = self.command {
let (_, flags) = command.split_once(' ')?;
return Some(flags);
}
None
}
}

// # Example usage:
// aliases!["wa!" => "write --all --force", "w", "wa" => "write --all"]
#[macro_export]
macro_rules! aliases {
() => {
$crate::commands::alias::Aliases::empty()
};
($($alias:expr $(=> $command:expr)?),* $(,)?) => {{
const ALIASES: &[$crate::commands::alias::Alias] = &[
$(
$crate::commands::alias::Alias {
name: $alias,
command: {
#[allow(unused_mut, unused_assignments)]
let mut command = None;
$(command = Some($command);)?
command
},
}
),*
];
$crate::commands::alias::Aliases { aliases: ALIASES }
}};
}
4 changes: 4 additions & 0 deletions helix-term/src/commands/flag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ impl Flags {
self.0.is_empty()
}

pub fn iter(&self) -> impl Iterator<Item = &Flag> {
self.0.iter()
}

#[inline]
pub fn names(&self) -> impl Iterator<Item = &'static str> {
self.0
Expand Down
Loading

0 comments on commit 0aa159b

Please sign in to comment.