Skip to content

Commit 24b3e14

Browse files
v4nderstruckthe-mikedavis
authored andcommitted
Ensure cursor in view after format (helix-editor#4047)
Co-authored-by: Michael Davis <[email protected]>
1 parent abc6771 commit 24b3e14

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

helix-term/src/commands.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,18 +2511,23 @@ async fn make_format_callback(
25112511
) -> anyhow::Result<job::Callback> {
25122512
let format = format.await?;
25132513
let call: job::Callback = Box::new(move |editor, _compositor| {
2514-
let view_id = view!(editor).id;
2515-
if let Some(doc) = editor.document_mut(doc_id) {
2516-
if doc.version() == doc_version {
2517-
doc.apply(&format, view_id);
2518-
doc.append_changes_to_history(view_id);
2519-
doc.detect_indent_and_line_ending();
2520-
if let Modified::SetUnmodified = modified {
2521-
doc.reset_modified();
2522-
}
2523-
} else {
2524-
log::info!("discarded formatting changes because the document changed");
2514+
if !editor.documents.contains_key(&doc_id) {
2515+
return;
2516+
}
2517+
2518+
let scrolloff = editor.config().scrolloff;
2519+
let doc = doc_mut!(editor, &doc_id);
2520+
let view = view_mut!(editor);
2521+
if doc.version() == doc_version {
2522+
doc.apply(&format, view.id);
2523+
doc.append_changes_to_history(view.id);
2524+
doc.detect_indent_and_line_ending();
2525+
view.ensure_cursor_in_view(doc, scrolloff);
2526+
if let Modified::SetUnmodified = modified {
2527+
doc.reset_modified();
25252528
}
2529+
} else {
2530+
log::info!("discarded formatting changes because the document changed");
25262531
}
25272532
});
25282533
Ok(call)

0 commit comments

Comments
 (0)