on master help sign in
Ensure cursor in view after format (#4047)

Co-authored-by: Michael Davis <mcarsondavis@gmail.com>

commitc9584251f321a8540cf530561896b2f48f0b76a2 authorzensayyy <99101223+zensayyy@users.noreply.github.com> dateSat 1 Oct 2022, 16:32 (4 years ago) parentcc257e9
1 file changed+16−11
helix-term/src/commands.rs+16−11
helix-term/src/commands.rs+16−11
@@ -2504,18 +2504,23 @@ async fn make_format_callback(
25042504) -> anyhow::Result<job::Callback> {
25052505 let format = format.await?;
25062506 let call: job::Callback = Box::new(move |editor, _compositor| {
2507− let view_id = view!(editor).id;
2508− if let Some(doc) = editor.document_mut(doc_id) {
2509− if doc.version() == doc_version {
2510− doc.apply(&format, view_id);
2511− doc.append_changes_to_history(view_id);
2512− doc.detect_indent_and_line_ending();
2513− if let Modified::SetUnmodified = modified {
2514− doc.reset_modified();
2515− }
2516− } else {
2517− log::info!("discarded formatting changes because the document changed");
2507+ if !editor.documents.contains_key(&doc_id) {
2508+ return;
2509+ }
2510+
2511+ let scrolloff = editor.config().scrolloff;
2512+ let doc = doc_mut!(editor, &doc_id);
2513+ let view = view_mut!(editor);
2514+ if doc.version() == doc_version {
2515+ doc.apply(&format, view.id);
2516+ doc.append_changes_to_history(view.id);
2517+ doc.detect_indent_and_line_ending();
2518+ view.ensure_cursor_in_view(doc, scrolloff);
2519+ if let Modified::SetUnmodified = modified {
2520+ doc.reset_modified();
25182521 }
2522+ } else {
2523+ log::info!("discarded formatting changes because the document changed");
25192524 }
25202525 });
25212526 Ok(call)