Skip to content

Commit 1ff30f8

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

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-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

+35
Original file line numberDiff line numberDiff line change
@@ -1952,6 +1952,34 @@ fn run_shell_command(
19521952
Ok(())
19531953
}
19541954

1955+
fn clear_register(
1956+
cx: &mut compositor::Context,
1957+
args: &[Cow<str>],
1958+
event: PromptEvent,
1959+
) -> anyhow::Result<()> {
1960+
if event != PromptEvent::Validate {
1961+
return Ok(());
1962+
}
1963+
ensure!(args.len() <= 1, ":clear-register takes at most 1 argument");
1964+
if args.is_empty() {
1965+
cx.editor.registers.clear();
1966+
cx.editor.set_status("All registers cleared");
1967+
return Ok(());
1968+
}
1969+
1970+
ensure!(args[0].len() == 1, format!("Invalid register {}", args[0]));
1971+
let register = args[0].chars().next().unwrap_or_default();
1972+
match cx.editor.registers.remove(register) {
1973+
Some(_) => cx
1974+
.editor
1975+
.set_status(format!("Register {} cleared", register)),
1976+
None => cx
1977+
.editor
1978+
.set_error(format!("Register {} not found", register)),
1979+
}
1980+
Ok(())
1981+
}
1982+
19551983
pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
19561984
TypableCommand {
19571985
name: "quit",
@@ -2474,6 +2502,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
24742502
fun: run_shell_command,
24752503
completer: Some(completers::filename),
24762504
},
2505+
TypableCommand {
2506+
name: "clear-register",
2507+
aliases: &[],
2508+
doc: "Clear given register. If no argument is provided, clear all registers.",
2509+
fun: clear_register,
2510+
completer: None,
2511+
},
24772512
];
24782513

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

0 commit comments

Comments
 (0)