From 092163bd8fc31e382ee57f4f5909762699a36bcd Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 1 Oct 2022 19:52:04 +0200 Subject: [PATCH] Add command to delete the file of the current buffer --- README.md | 1 + helix-term/src/commands/typed.rs | 31 +++++++++++++++++++++++++++++++ helix-view/src/document.rs | 15 +++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/README.md b/README.md index b939caac..f6a52cfd 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ And others I forgot about... - Added an auto highlight for files in the tree explorer when jumping through opened buffers - Changed some default settings (enabling bufferline, indent guides, the embedded explorer, cursor modes etc.) - Added a `--show-explorer` cli flag to open the file explorer on startup (useful for embedded explorer mode) +- Added a `delete` (aliases `rm`, `del`) command to delete the file associated with the current buffer - - - diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 3ee5324d..f2063b0f 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -232,6 +232,30 @@ fn buffer_previous( Ok(()) } +fn delete( + cx: &mut compositor::Context, + _args: &[Cow], + event: PromptEvent, +) -> anyhow::Result<()> { + if event != PromptEvent::Validate { + return Ok(()); + } + + let doc = doc_mut!(cx.editor); + + if doc.path().is_none() { + bail!("cannot delete a buffer with no associated file on the disk"); + } + + let future = doc.delete(); + cx.jobs.add(Job::new(future)); + + let doc_id = view!(cx.editor).doc; + cx.editor.close_document(doc_id, true)?; + + Ok(()) +} + fn write_impl( cx: &mut compositor::Context, path: Option<&Cow>, @@ -1658,6 +1682,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ fun: new_file, completer: Some(completers::filename), }, + TypableCommand { + name: "delete", + aliases: &["remove", "rm", "del"], + doc: "Deletes the file associated with the current buffer", + fun: delete, + completer: None, + }, TypableCommand { name: "format", aliases: &["fmt"], diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 6dfa26bc..9eb89931 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -491,6 +491,21 @@ impl Document { Some(fut.boxed()) } + /// 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(()) + } + } + pub fn save(&mut self, force: bool) -> impl Future> { self.save_impl::>(None, force) }