Skip to content

Commit b5adc21

Browse files
author
Jorge
committed
feat(commands): add clear-register typable command
1 parent 9c98043 commit b5adc21

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
@@ -1952,6 +1952,38 @@ 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+
1964+
ensure!(args.len() <= 1, ":clear-register takes at most 1 argument");
1965+
if args.is_empty() {
1966+
cx.editor.registers.clear();
1967+
cx.editor.set_status("All registers cleared");
1968+
return Ok(());
1969+
}
1970+
1971+
ensure!(
1972+
args[0].chars().count() == 1,
1973+
format!("Invalid register {}", args[0])
1974+
);
1975+
let register = args[0].chars().next().unwrap_or_default();
1976+
match cx.editor.registers.remove(register) {
1977+
Some(_) => cx
1978+
.editor
1979+
.set_status(format!("Register {} cleared", register)),
1980+
None => cx
1981+
.editor
1982+
.set_error(format!("Register {} not found", register)),
1983+
}
1984+
Ok(())
1985+
}
1986+
19551987
pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
19561988
TypableCommand {
19571989
name: "quit",
@@ -2474,6 +2506,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
24742506
fun: run_shell_command,
24752507
completer: Some(completers::filename),
24762508
},
2509+
TypableCommand {
2510+
name: "clear-register",
2511+
aliases: &[],
2512+
doc: "Clear given register. If no argument is provided, clear all registers.",
2513+
fun: clear_register,
2514+
completer: None,
2515+
},
24772516
];
24782517

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

0 commit comments

Comments
 (0)