diff options
Diffstat (limited to 'helix-term/src/commands')
-rw-r--r-- | helix-term/src/commands/typed.rs | 35 |
1 files changed, 29 insertions, 6 deletions
diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 394c0733..7ea4c801 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -271,7 +271,7 @@ fn write_impl( ) -> anyhow::Result<()> { let editor_auto_fmt = cx.editor.config().auto_format; let jobs = &mut cx.jobs; - let doc = doc_mut!(cx.editor); + let (view, doc) = current!(cx.editor); let path = path.map(AsRef::as_ref); let fmt = if editor_auto_fmt { @@ -279,6 +279,7 @@ fn write_impl( let callback = make_format_callback( doc.id(), doc.version(), + view.id, fmt, Some((path.map(Into::into), force)), ); @@ -344,9 +345,9 @@ fn format( return Ok(()); } - let doc = doc!(cx.editor); + let (view, doc) = current!(cx.editor); if let Some(format) = doc.format() { - let callback = make_format_callback(doc.id(), doc.version(), format, None); + let callback = make_format_callback(doc.id(), doc.version(), view.id, format, None); cx.jobs.callback(callback); } @@ -568,12 +569,13 @@ pub fn write_all_impl( let mut errors: Vec<&'static str> = Vec::new(); let auto_format = cx.editor.config().auto_format; let jobs = &mut cx.jobs; + let current_view = view!(cx.editor); // save all documents let saves: Vec<_> = cx .editor .documents - .values() + .values_mut() .filter_map(|doc| { if !doc.is_modified() { return None; @@ -585,10 +587,30 @@ pub fn write_all_impl( return None; } + // Look for a view to apply the formatting change to. If the document + // is in the current view, just use that. Otherwise, since we don't + // have any other metric available for better selection, just pick + // the first view arbitrarily so that we still commit the document + // state for undos. If somehow we have a document that has not been + // initialized with any view, initialize it with the current view. + let target_view = if doc.selections().contains_key(¤t_view.id) { + current_view.id + } else if let Some(view) = doc.selections().keys().next() { + *view + } else { + doc.ensure_view_init(current_view.id); + current_view.id + }; + let fmt = if auto_format { doc.auto_format().map(|fmt| { - let callback = - make_format_callback(doc.id(), doc.version(), fmt, Some((None, force))); + let callback = make_format_callback( + doc.id(), + doc.version(), + target_view, + fmt, + Some((None, force)), + ); jobs.add(Job::with_callback(callback).wait_before_exiting()); }) } else { @@ -598,6 +620,7 @@ pub fn write_all_impl( if fmt.is_none() { return Some(doc.id()); } + None }) .collect(); |