Add command to delete the file of the current buffer

change-detection
trivernis 2 years ago
parent 0725aa1046
commit 092163bd8f
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -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
- - -

@ -232,6 +232,30 @@ fn buffer_previous(
Ok(())
}
fn delete(
cx: &mut compositor::Context,
_args: &[Cow<str>],
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<str>>,
@ -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"],

@ -491,6 +491,21 @@ impl Document {
Some(fut.boxed())
}
/// Deletes the file associated with this document
pub fn delete(&mut self) -> impl Future<Output = Result<(), anyhow::Error>> {
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<Output = Result<(), anyhow::Error>> {
self.save_impl::<futures_util::future::Ready<_>>(None, force)
}

Loading…
Cancel
Save