Skip to content

Commit 6d8d83b

Browse files
author
Jorge
committed
feat(commands): add clear-register typable command
1 parent ef221ab commit 6d8d83b

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

book/src/generated/typable-cmd.md

+1
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,4 @@
7474
| `:pipe` | Pipe each selection to the shell command. |
7575
| `:pipe-to` | Pipe each selection to the shell command, ignoring output. |
7676
| `:run-shell-command`, `:sh` | Run a shell command |
77+
| `:clear-register` | Clear given register. If no argument is provided, clear all registers. |

helix-core/src/register.rs

+8
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,12 @@ impl Registers {
7878
pub fn inner(&self) -> &HashMap<char, Register> {
7979
&self.inner
8080
}
81+
82+
pub fn clear(&mut self) {
83+
self.inner.clear();
84+
}
85+
86+
pub fn remove(&mut self, name: char) -> Option<Register> {
87+
self.inner.remove(&name)
88+
}
8189
}

helix-term/src/commands/typed.rs

+39
Original file line numberDiff line numberDiff line change
@@ -1953,6 +1953,38 @@ fn run_shell_command(
19531953
Ok(())
19541954
}
19551955

1956+
fn clear_register(
1957+
cx: &mut compositor::Context,
1958+
args: &[Cow<str>],
1959+
event: PromptEvent,
1960+
) -> anyhow::Result<()> {
1961+
if event != PromptEvent::Validate {
1962+
return Ok(());
1963+
}
1964+
1965+
ensure!(args.len() <= 1, ":clear-register takes at most 1 argument");
1966+
if args.is_empty() {
1967+
cx.editor.registers.clear();
1968+
cx.editor.set_status("All registers cleared");
1969+
return Ok(());
1970+
}
1971+
1972+
ensure!(
1973+
args[0].chars().count() == 1,
1974+
format!("Invalid register {}", args[0])
1975+
);
1976+
let register = args[0].chars().next().unwrap_or_default();
1977+
match cx.editor.registers.remove(register) {
1978+
Some(_) => cx
1979+
.editor
1980+
.set_status(format!("Register {} cleared", register)),
1981+
None => cx
1982+
.editor
1983+
.set_error(format!("Register {} not found", register)),
1984+
}
1985+
Ok(())
1986+
}
1987+
19561988
pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
19571989
TypableCommand {
19581990
name: "quit",
@@ -2475,6 +2507,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
24752507
fun: run_shell_command,
24762508
completer: Some(completers::filename),
24772509
},
2510+
TypableCommand {
2511+
name: "clear-register",
2512+
aliases: &[],
2513+
doc: "Clear given register. If no argument is provided, clear all registers.",
2514+
fun: clear_register,
2515+
completer: None,
2516+
},
24782517
];
24792518

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

0 commit comments

Comments
 (0)