on master help sign in
Ensure cursor in view after format (#4047)
c958425zensayyy
helix-term/src/commands.rs+16−11
@@ -2504,18 +2504,23 @@ async fn make_format_callback(
2504) -> anyhow::Result<job::Callback> {
2504) -> anyhow::Result<job::Callback> {
2505 let format = format.await?;
2505 let format = format.await?;
2506 let call: job::Callback = Box::new(move |editor, _compositor| {
2506 let call: job::Callback = Box::new(move |editor, _compositor| {
2507− let view_id = view!(editor).id;
2507+ if !editor.documents.contains_key(&doc_id) {
2508+ return;
2509+ }
2510+
2511+ let scrolloff = editor.config().scrolloff;
2508− if let Some(doc) = editor.document_mut(doc_id) {
2512+ let doc = doc_mut!(editor, &doc_id);
2513+ let view = view_mut!(editor);
2509− if doc.version() == doc_version {
2514+ if doc.version() == doc_version {
2510− doc.apply(&format, view_id);
2515+ doc.apply(&format, view.id);
2511− doc.append_changes_to_history(view_id);
2516+ doc.append_changes_to_history(view.id);
2512− doc.detect_indent_and_line_ending();
2517+ doc.detect_indent_and_line_ending();
2518+ view.ensure_cursor_in_view(doc, scrolloff);
2513− if let Modified::SetUnmodified = modified {
2519+ if let Modified::SetUnmodified = modified {
2514− doc.reset_modified();
2520+ doc.reset_modified();
2515− }
2516− } else {
2517− log::info!("discarded formatting changes because the document changed");
2518 }
2521 }
2522+ } else {
2523+ log::info!("discarded formatting changes because the document changed");
2519 }
2524 }
2520 });
2525 });
2521 Ok(call)
2526 Ok(call)