@@ -35,7 +35,7 @@ use crate::{
3535 ui::{self, Completion, Picker, Popup, Prompt, PromptEvent},
3636};
3737
38−use crate::application::{LspCallbackWrapper, LspCallbacks};
38+use crate::application::{LspCallback, LspCallbackWrapper, LspCallbacks};
3939use futures_util::FutureExt;
4040use std::{fmt, future::Future, path::Display, str::FromStr};
4141
@@ -1145,11 +1145,12 @@ mod cmd {
11451145 }
11461146
11471147 fn write_impl<P: AsRef<Path>>(
1148− view: &View,
1149− doc: &mut Document,
1148+ cx: &mut compositor::Context,
11501149 path: Option<P>,
11511150 ) -> Result<tokio::task::JoinHandle<Result<(), anyhow::Error>>, anyhow::Error> {
11521151 use anyhow::anyhow;
1152+ let callbacks = &mut cx.callbacks;
1153+ let (view, doc) = current!(cx.editor);
11531154
11541155 if let Some(path) = path {
11551156 if let Err(err) = doc.set_path(path.as_ref()) {
@@ -1163,15 +1164,21 @@ mod cmd {
11631164 .language_config()
11641165 .map(|config| config.auto_format)
11651166 .unwrap_or_default();
1166− if autofmt {
1167− doc.format(view.id); // TODO: merge into save
1168− }
1169− Ok(tokio::spawn(doc.save()))
1167+ let fmt = if autofmt {
1168+ doc.format().map(|fmt| {
1169+ let shared = fmt.shared();
1170+ let callback = make_format_callback(doc.id(), doc.version(), true, shared.clone());
1171+ callbacks.push(callback);
1172+ shared
1173+ })
1174+ } else {
1175+ None
1176+ };
1177+ Ok(tokio::spawn(doc.format_and_save(fmt)))
11701178 }
11711179
11721180 fn write(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
1173− let (view, doc) = current!(cx.editor);
1174− if let Err(e) = write_impl(view, doc, args.first()) {
1181+ if let Err(e) = write_impl(cx, args.first()) {
11751182 cx.editor.set_error(e.to_string());
11761183 };
11771184 }
@@ -1181,9 +1188,12 @@ mod cmd {
11811188 }
11821189
11831190 fn format(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
1184− let (view, doc) = current!(cx.editor);
1191+ let (_, doc) = current!(cx.editor);
11851192
1186− doc.format(view.id)
1193+ if let Some(format) = doc.format() {
1194+ let callback = make_format_callback(doc.id(), doc.version(), false, format);
1195+ cx.callbacks.push(callback);
1196+ }
11871197 }
11881198
11891199 fn set_indent_style(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
@@ -1288,8 +1298,7 @@ mod cmd {
12881298 }
12891299
12901300 fn write_quit(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
1291− let (view, doc) = current!(cx.editor);
1292− match write_impl(view, doc, args.first()) {
1301+ match write_impl(cx, args.first()) {
12931302 Ok(handle) => {
12941303 if let Err(e) = helix_lsp::block_on(handle) {
12951304 cx.editor.set_error(e.to_string());
@@ -1305,7 +1314,7 @@ mod cmd {
13051314
13061315 fn force_write_quit(cx: &mut compositor::Context, args: &[&str], event: PromptEvent) {
13071316 let (view, doc) = current!(cx.editor);
1308− match write_impl(view, doc, args.first()) {
1317+ match write_impl(cx, args.first()) {
13091318 Ok(handle) => {
13101319 if let Err(e) = helix_lsp::block_on(handle) {
13111320 cx.editor.set_error(e.to_string());
@@ -1902,6 +1911,35 @@ fn append_to_line(cx: &mut Context) {
19021911 doc.set_selection(view.id, selection);
19031912}
19041913
1914+// Creates an LspCallback that waits for formatting changes to be computed. When they're done,
1915+// it applies them, but only if the doc hasn't changed.
1916+fn make_format_callback(
1917+ doc_id: DocumentId,
1918+ doc_version: i32,
1919+ set_unmodified: bool,
1920+ format: impl Future<Output = helix_lsp::util::LspFormatting> + Send + 'static,
1921+) -> LspCallback {
1922+ Box::pin(async move {
1923+ let format = format.await;
1924+ let call: LspCallbackWrapper =
1925+ Box::new(move |editor: &mut Editor, compositor: &mut Compositor| {
1926+ let view_id = view!(editor).id;
1927+ if let Some(doc) = editor.document_mut(doc_id) {
1928+ if doc.version() == doc_version {
1929+ doc.apply(&Transaction::from(format), view_id);
1930+ doc.append_changes_to_history(view_id);
1931+ if set_unmodified {
1932+ doc.reset_modified();
1933+ }
1934+ } else {
1935+ log::info!("discarded formatting changes because the document changed");
1936+ }
1937+ }
1938+ });
1939+ Ok(call)
1940+ })
1941+}
1942+
19051943enum Open {
19061944 Below,
19071945 Above,