on master help sign in
feat(commands): add clear-register typable command (#5695)

Co-authored-by: Jorge <chorcheus@tutanota.com>

commit204d1eba4b1bb09d2e861986d6b6e8b868d16afe authorjorge <20152130+chorcheus@users.noreply.github.com> dateThu 27 Apr 2023, 21:57 (4 years ago) parent9cdc6b2
3 files changed+48
book/src/generated/typable-cmd.md+1 helix-core/src/register.rs+8 helix-term/src/commands/typed.rs+39
book/src/generated/typable-cmd.md+1
@@ -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
@@ -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
@@ -2167,6 +2167,38 @@ fn reset_diff_change(
21672167 Ok(())
21682168}
21692169
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+
21702202pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
21712203 TypableCommand {
21722204 name: "quit",
@@ -2720,6 +2752,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
27202752 fun: reset_diff_change,
27212753 signature: CommandSignature::none(),
27222754 },
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+ },
27232762 ];
27242763
27252764pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableCommand>> =