@@ -2511,18 +2511,23 @@ async fn make_format_callback(
2511
2511
) -> anyhow:: Result < job:: Callback > {
2512
2512
let format = format. await ?;
2513
2513
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 ( ) ;
2525
2528
}
2529
+ } else {
2530
+ log:: info!( "discarded formatting changes because the document changed" ) ;
2526
2531
}
2527
2532
} ) ;
2528
2533
Ok ( call)
0 commit comments