@@ -271,7 +271,7 @@ fn write_impl(
271) -> anyhow::Result<()> {
271) -> anyhow::Result<()> {
272 let editor_auto_fmt = cx.editor.config().auto_format;
272 let editor_auto_fmt = cx.editor.config().auto_format;
273 let jobs = &mut cx.jobs;
273 let jobs = &mut cx.jobs;
274− let doc = doc_mut!(cx.editor);
274+ let (view, doc) = current!(cx.editor);
275 let path = path.map(AsRef::as_ref);
275 let path = path.map(AsRef::as_ref);
277 let fmt = if editor_auto_fmt {
277 let fmt = if editor_auto_fmt {
@@ -279,6 +279,7 @@ fn write_impl(
279 let callback = make_format_callback(
279 let callback = make_format_callback(
280 doc.id(),
280 doc.id(),
281 doc.version(),
281 doc.version(),
283 Some((path.map(Into::into), force)),
284 Some((path.map(Into::into), force)),
@@ -344,9 +345,9 @@ fn format(
344 return Ok(());
345 return Ok(());
347− let doc = doc!(cx.editor);
348+ let (view, doc) = current!(cx.editor);
348 if let Some(format) = doc.format() {
349 if let Some(format) = doc.format() {
349− let callback = make_format_callback(doc.id(), doc.version(), format, None);
350+ let callback = make_format_callback(doc.id(), doc.version(), view.id, format, None);
350 cx.jobs.callback(callback);
351 cx.jobs.callback(callback);
@@ -568,12 +569,13 @@ pub fn write_all_impl(
568 let mut errors: Vec<&'static str> = Vec::new();
569 let mut errors: Vec<&'static str> = Vec::new();
569 let auto_format = cx.editor.config().auto_format;
570 let auto_format = cx.editor.config().auto_format;
570 let jobs = &mut cx.jobs;
571 let jobs = &mut cx.jobs;
572+ let current_view = view!(cx.editor);
572 // save all documents
574 // save all documents
573 let saves: Vec<_> = cx
575 let saves: Vec<_> = cx
575 .documents
577 .documents
576− .values()
578+ .values_mut()
577 .filter_map(|doc| {
579 .filter_map(|doc| {
578 if !doc.is_modified() {
580 if !doc.is_modified() {
579 return None;
581 return None;
@@ -585,10 +587,30 @@ pub fn write_all_impl(
585 return None;
587 return None;
590+ // Look for a view to apply the formatting change to. If the document
591+ // is in the current view, just use that. Otherwise, since we don't
592+ // have any other metric available for better selection, just pick
593+ // the first view arbitrarily so that we still commit the document
594+ // state for undos. If somehow we have a document that has not been
595+ // initialized with any view, initialize it with the current view.
596+ let target_view = if doc.selections().contains_key(¤t_view.id) {
598+ } else if let Some(view) = doc.selections().keys().next() {
601+ doc.ensure_view_init(current_view.id);
588 let fmt = if auto_format {
605 let fmt = if auto_format {
589 doc.auto_format().map(|fmt| {
606 doc.auto_format().map(|fmt| {
590− let callback =
607+ let callback = make_format_callback(
591− make_format_callback(doc.id(), doc.version(), fmt, Some((None, force)));
612+ Some((None, force)),
592 jobs.add(Job::with_callback(callback).wait_before_exiting());
614 jobs.add(Job::with_callback(callback).wait_before_exiting());
@@ -598,6 +620,7 @@ pub fn write_all_impl(
598 if fmt.is_none() {
620 if fmt.is_none() {
599 return Some(doc.id());
621 return Some(doc.id());
603 .collect();
626 .collect();