aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjorge2023-04-27 19:57:05 +0000
committerGitHub2023-04-27 19:57:05 +0000
commit204d1eba4b1bb09d2e861986d6b6e8b868d16afe (patch)
treeaefc7c1bcd504df89143afacfceb549f5a150fc9
parent9cdc6b2e8a0193881297e15641cd801384cd8865 (diff)
feat(commands): add clear-register typable command (#5695)
Co-authored-by: Jorge <chorcheus@tutanota.com>
-rw-r--r--book/src/generated/typable-cmd.md1
-rw-r--r--helix-core/src/register.rs8
-rw-r--r--helix-term/src/commands/typed.rs39
3 files changed, 48 insertions, 0 deletions
diff --git a/book/src/generated/typable-cmd.md b/book/src/generated/typable-cmd.md
index a9d68b75..ae28a9ba 100644
--- a/book/src/generated/typable-cmd.md
+++ b/book/src/generated/typable-cmd.md
@@ -78,3 +78,4 @@
| `:pipe-to` | Pipe each selection to the shell command, ignoring output. |
| `:run-shell-command`, `:sh` | Run a shell command |
| `:reset-diff-change`, `:diffget`, `:diffg` | Reset the diff change at the cursor position. |
+| `:clear-register` | Clear given register. If no argument is provided, clear all registers. |
diff --git a/helix-core/src/register.rs b/helix-core/src/register.rs
index 52eb6e3e..df68a759 100644
--- a/helix-core/src/register.rs
+++ b/helix-core/src/register.rs
@@ -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)
+ }
}
diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs
index ea82dc36..fe92798b 100644
--- a/helix-term/src/commands/typed.rs
+++ b/helix-term/src/commands/typed.rs
@@ -2167,6 +2167,38 @@ fn reset_diff_change(
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].chars().count() == 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",
@@ -2720,6 +2752,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: reset_diff_change,
signature: CommandSignature::none(),
},
+ TypableCommand {
+ name: "clear-register",
+ aliases: &[],
+ doc: "Clear given register. If no argument is provided, clear all registers.",
+ fun: clear_register,
+ signature: CommandSignature::none(),
+ },
];
pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableCommand>> =