From 2f169b172fc16827cc1a2c95865d5c3ab17708f3 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 1 Apr 2023 11:43:56 +0200 Subject: [PATCH] Add `rm` command to delete the current file --- helix-term/src/commands/typed.rs | 31 +++++++++++++++++++++++++++++++ helix-view/src/document.rs | 15 +++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 0255bbea..a8c85303 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -298,6 +298,30 @@ fn force_buffer_close_all( buffer_close_by_ids_impl(cx, &document_ids, true) } +fn delete( + cx: &mut compositor::Context, + _args: &[Cow], + event: PromptEvent, +) -> anyhow::Result<()> { + if event != PromptEvent::Validate { + return Ok(()); + } + + cx.block_try_flush_writes()?; + let doc = doc_mut!(cx.editor); + + if doc.path().is_none() { + bail!("cannot delete a buffer with no associated file on the disk"); + } + + let doc_id = view!(cx.editor).doc; + + let future = doc.delete(); + cx.jobs.add(Job::new(future)); + + buffer_close_by_ids_impl(cx, &[doc_id], true) +} + fn buffer_next( cx: &mut compositor::Context, _args: &[Cow], @@ -2233,6 +2257,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ fun: force_buffer_close_all, signature: CommandSignature::none(), }, + TypableCommand { + name: "delete", + aliases: &["remove", "rm", "del"], + doc: "Deletes the file associated with the current buffer", + fun: delete, + signature: CommandSignature::none(), + }, TypableCommand { name: "buffer-next", aliases: &["bn", "bnext"], diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index eca60026..bcc334ef 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -544,6 +544,21 @@ impl Document { } } + /// Deletes the file associated with this document + pub fn delete(&mut self) -> impl Future> { + let path = self + .path() + .expect("Cannot delete with no path set!") + .clone(); + + async move { + use tokio::fs; + fs::remove_file(path).await?; + + Ok(()) + } + } + /// If supported, returns the changes that should be applied to this document in order /// to format it nicely. // We can't use anyhow::Result here since the output of the future has to be