Skip to content

Commit d015911

Browse files
author
Jorge
committed
feat(commands): add clear-register typable command
1 parent e856906 commit d015911

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
@@ -78,3 +78,4 @@
7878
| `:pipe-to` | Pipe each selection to the shell command, ignoring output. |
7979
| `:run-shell-command`, `:sh` | Run a shell command |
8080
| `:reset-diff-change`, `:diffget`, `:diffg` | Reset the diff change at the cursor position. |
81+
| `: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
@@ -2165,6 +2165,38 @@ fn reset_diff_change(
21652165
Ok(())
21662166
}
21672167

2168+
fn clear_register(
2169+
cx: &mut compositor::Context,
2170+
args: &[Cow<str>],
2171+
event: PromptEvent,
2172+
) -> anyhow::Result<()> {
2173+
if event != PromptEvent::Validate {
2174+
return Ok(());
2175+
}
2176+
2177+
ensure!(args.len() <= 1, ":clear-register takes at most 1 argument");
2178+
if args.is_empty() {
2179+
cx.editor.registers.clear();
2180+
cx.editor.set_status("All registers cleared");
2181+
return Ok(());
2182+
}
2183+
2184+
ensure!(
2185+
args[0].chars().count() == 1,
2186+
format!("Invalid register {}", args[0])
2187+
);
2188+
let register = args[0].chars().next().unwrap_or_default();
2189+
match cx.editor.registers.remove(register) {
2190+
Some(_) => cx
2191+
.editor
2192+
.set_status(format!("Register {} cleared", register)),
2193+
None => cx
2194+
.editor
2195+
.set_error(format!("Register {} not found", register)),
2196+
}
2197+
Ok(())
2198+
}
2199+
21682200
pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
21692201
TypableCommand {
21702202
name: "quit",
@@ -2718,6 +2750,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
27182750
fun: reset_diff_change,
27192751
signature: CommandSignature::none(),
27202752
},
2753+
TypableCommand {
2754+
name: "clear-register",
2755+
aliases: &[],
2756+
doc: "Clear given register. If no argument is provided, clear all registers.",
2757+
fun: clear_register,
2758+
signature: CommandSignature::none(),
2759+
},
27212760
];
27222761

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

0 commit comments

Comments
 (0)