Skip to content

Commit

Permalink
feat(commands): add clear_registers command
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge committed Feb 4, 2023
1 parent b2e83f8 commit 799aebb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,4 @@
| `:pipe` | Pipe each selection to the shell command. |
| `:pipe-to` | Pipe each selection to the shell command, ignoring output. |
| `:run-shell-command`, `:sh` | Run a shell command |
| `:clear-register` | Clear given register. If no argument is provided, clear all registers. |
8 changes: 8 additions & 0 deletions helix-core/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,12 @@ impl Registers {
pub fn inner(&self) -> &HashMap<char, Register> {
&self.inner
}

pub fn clear(&mut self) {
self.inner.clear();
}

pub fn remove(&mut self, name: char) -> Option<Register> {
self.inner.remove(&name)
}
}
35 changes: 35 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1952,6 +1952,34 @@ fn run_shell_command(
Ok(())
}

fn clear_register(
cx: &mut compositor::Context,
args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}
ensure!(args.len() <= 1, ":clear-register takes at most 1 argument");
if args.is_empty() {
cx.editor.registers.clear();
cx.editor.set_status("All registers cleared");
return Ok(());
}

ensure!(args[0].len() == 1, format!("Invalid register {}", args[0]));
let register = args[0].chars().next().unwrap_or_default();
match cx.editor.registers.remove(register) {
Some(_) => cx
.editor
.set_status(format!("Register {} cleared", register)),
None => cx
.editor
.set_error(format!("Register {} not found", register)),
}
Ok(())
}

pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
TypableCommand {
name: "quit",
Expand Down Expand Up @@ -2474,6 +2502,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: run_shell_command,
completer: Some(completers::filename),
},
TypableCommand {
name: "clear-register",
aliases: &[],
doc: "Clear given register. If no argument is provided, clear all registers.",
fun: clear_register,
completer: None,
},
];

pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableCommand>> =
Expand Down

0 comments on commit 799aebb

Please sign in to comment.