From 660e0e44b2b6329c1d0af788d624dd5765c2acc6 Mon Sep 17 00:00:00 2001 From: Omnikar Date: Tue, 12 Apr 2022 03:52:54 -0400 Subject: Add `:write!` to create nonexistent subdirectories (#1839) * Make `:write` create nonexistent subdirectories Prompting as to whether this should take place remains a TODO. * Move subdirectory creation to new `w!` command--- helix-term/src/commands/typed.rs | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'helix-term') diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 8f74adb6..d158388f 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -190,7 +190,11 @@ fn buffer_previous( Ok(()) } -fn write_impl(cx: &mut compositor::Context, path: Option<&Cow>) -> anyhow::Result<()> { +fn write_impl( + cx: &mut compositor::Context, + path: Option<&Cow>, + force: bool, +) -> anyhow::Result<()> { let jobs = &mut cx.jobs; let doc = doc_mut!(cx.editor); @@ -212,7 +216,7 @@ fn write_impl(cx: &mut compositor::Context, path: Option<&Cow>) -> anyhow:: jobs.callback(callback); shared }); - let future = doc.format_and_save(fmt); + let future = doc.format_and_save(fmt, force); cx.jobs.add(Job::new(future).wait_before_exiting()); if path.is_some() { @@ -228,7 +232,15 @@ fn write( args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { - write_impl(cx, args.first()) + write_impl(cx, args.first(), false) +} + +fn force_write( + cx: &mut compositor::Context, + args: &[Cow], + _event: PromptEvent, +) -> anyhow::Result<()> { + write_impl(cx, args.first(), true) } fn new_file( @@ -381,7 +393,7 @@ fn write_quit( args: &[Cow], event: PromptEvent, ) -> anyhow::Result<()> { - write_impl(cx, args.first())?; + write_impl(cx, args.first(), false)?; quit(cx, &[], event) } @@ -390,7 +402,7 @@ fn force_write_quit( args: &[Cow], event: PromptEvent, ) -> anyhow::Result<()> { - write_impl(cx, args.first())?; + write_impl(cx, args.first(), true)?; force_quit(cx, &[], event) } @@ -447,7 +459,7 @@ fn write_all_impl( jobs.callback(callback); shared }); - let future = doc.format_and_save(fmt); + let future = doc.format_and_save(fmt, force); jobs.add(Job::new(future).wait_before_exiting()); } @@ -1140,6 +1152,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ fun: write, completer: Some(completers::filename), }, + TypableCommand { + name: "write!", + aliases: &["w!"], + doc: "Write changes to disk forcefully (creating necessary subdirectories). Accepts an optional path (:write some/path.txt)", + fun: force_write, + completer: Some(completers::filename), + }, TypableCommand { name: "new", aliases: &["n"], -- cgit v1.2.3-70-g09d2