on master help sign in
feat(commands): add clear-register typable command (#5695)
204d1ebjorge
book/src/generated/typable-cmd.md+1
@@ -78,3 +78,4 @@
78| `:pipe-to` | Pipe each selection to the shell command, ignoring output. |
78| `:pipe-to` | Pipe each selection to the shell command, ignoring output. |
79| `:run-shell-command`, `:sh` | Run a shell command |
79| `:run-shell-command`, `:sh` | Run a shell command |
80| `:reset-diff-change`, `:diffget`, `:diffg` | Reset the diff change at the cursor position. |
80| `: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
@@ -78,4 +78,12 @@ impl Registers {
78 pub fn inner(&self) -> &HashMap<char, Register> {
78 pub fn inner(&self) -> &HashMap<char, Register> {
79 &self.inner
79 &self.inner
80 }
80 }
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+ }
81}
89}
helix-term/src/commands/typed.rs+39
@@ -2167,6 +2167,38 @@ fn reset_diff_change(
2167 Ok(())
2167 Ok(())
2168}
2168}
2169
2169
2170+fn clear_register(
2171+ cx: &mut compositor::Context,
2172+ args: &[Cow<str>],
2173+ event: PromptEvent,
2174+) -> anyhow::Result<()> {
2175+ if event != PromptEvent::Validate {
2176+ return Ok(());
2177+ }
2178+
2179+ ensure!(args.len() <= 1, ":clear-register takes at most 1 argument");
2180+ if args.is_empty() {
2181+ cx.editor.registers.clear();
2182+ cx.editor.set_status("All registers cleared");
2183+ return Ok(());
2184+ }
2185+
2186+ ensure!(
2187+ args[0].chars().count() == 1,
2188+ format!("Invalid register {}", args[0])
2189+ );
2190+ let register = args[0].chars().next().unwrap_or_default();
2191+ match cx.editor.registers.remove(register) {
2192+ Some(_) => cx
2193+ .editor
2194+ .set_status(format!("Register {} cleared", register)),
2195+ None => cx
2196+ .editor
2197+ .set_error(format!("Register {} not found", register)),
2198+ }
2199+ Ok(())
2200+}
2201+
2170pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
2202pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
2171 TypableCommand {
2203 TypableCommand {
2172 name: "quit",
2204 name: "quit",
@@ -2720,6 +2752,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
2720 fun: reset_diff_change,
2752 fun: reset_diff_change,
2721 signature: CommandSignature::none(),
2753 signature: CommandSignature::none(),
2722 },
2754 },
2755+ TypableCommand {
2756+ name: "clear-register",
2757+ aliases: &[],
2758+ doc: "Clear given register. If no argument is provided, clear all registers.",
2759+ fun: clear_register,
2760+ signature: CommandSignature::none(),
2761+ },
2723 ];
2762 ];
2724
2763
2725pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableCommand>> =
2764pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableCommand>> =