Avoid auto-scrolling the editor when inserting/removing headers

Antonio Scandurra created

Change summary

crates/ai/src/assistant.rs            |  4 ++--
crates/diagnostics/src/diagnostics.rs |  3 ++-
crates/editor/src/editor.rs           | 27 +++++++++++++++++++++++----
crates/editor/src/editor_tests.rs     |  1 +
crates/editor/src/element.rs          |  1 +
5 files changed, 29 insertions(+), 7 deletions(-)

Detailed changes

crates/ai/src/assistant.rs 🔗

@@ -1055,8 +1055,8 @@ impl AssistantEditor {
                 })
                 .collect::<Vec<_>>();
 
-            editor.remove_blocks(old_blocks, cx);
-            let ids = editor.insert_blocks(new_blocks, cx);
+            editor.remove_blocks(old_blocks, None, cx);
+            let ids = editor.insert_blocks(new_blocks, None, cx);
             self.blocks = HashSet::from_iter(ids);
         });
     }

crates/diagnostics/src/diagnostics.rs 🔗

@@ -430,7 +430,7 @@ impl ProjectDiagnosticsEditor {
         });
 
         self.editor.update(cx, |editor, cx| {
-            editor.remove_blocks(blocks_to_remove, cx);
+            editor.remove_blocks(blocks_to_remove, None, cx);
             let block_ids = editor.insert_blocks(
                 blocks_to_add.into_iter().map(|block| {
                     let (excerpt_id, text_anchor) = block.position;
@@ -442,6 +442,7 @@ impl ProjectDiagnosticsEditor {
                         disposition: block.disposition,
                     }
                 }),
+                Some(Autoscroll::fit()),
                 cx,
             );
 

crates/editor/src/editor.rs 🔗

@@ -6268,6 +6268,7 @@ impl Editor {
                             }),
                             disposition: BlockDisposition::Below,
                         }],
+                        Some(Autoscroll::fit()),
                         cx,
                     )[0];
                     this.pending_rename = Some(RenameState {
@@ -6334,7 +6335,11 @@ impl Editor {
         cx: &mut ViewContext<Self>,
     ) -> Option<RenameState> {
         let rename = self.pending_rename.take()?;
-        self.remove_blocks([rename.block_id].into_iter().collect(), cx);
+        self.remove_blocks(
+            [rename.block_id].into_iter().collect(),
+            Some(Autoscroll::fit()),
+            cx,
+        );
         self.clear_text_highlights::<Rename>(cx);
         self.show_local_selections = true;
 
@@ -6720,29 +6725,43 @@ impl Editor {
     pub fn insert_blocks(
         &mut self,
         blocks: impl IntoIterator<Item = BlockProperties<Anchor>>,
+        autoscroll: Option<Autoscroll>,
         cx: &mut ViewContext<Self>,
     ) -> Vec<BlockId> {
         let blocks = self
             .display_map
             .update(cx, |display_map, cx| display_map.insert_blocks(blocks, cx));
-        self.request_autoscroll(Autoscroll::fit(), cx);
+        if let Some(autoscroll) = autoscroll {
+            self.request_autoscroll(autoscroll, cx);
+        }
         blocks
     }
 
     pub fn replace_blocks(
         &mut self,
         blocks: HashMap<BlockId, RenderBlock>,
+        autoscroll: Option<Autoscroll>,
         cx: &mut ViewContext<Self>,
     ) {
         self.display_map
             .update(cx, |display_map, _| display_map.replace_blocks(blocks));
-        self.request_autoscroll(Autoscroll::fit(), cx);
+        if let Some(autoscroll) = autoscroll {
+            self.request_autoscroll(autoscroll, cx);
+        }
     }
 
-    pub fn remove_blocks(&mut self, block_ids: HashSet<BlockId>, cx: &mut ViewContext<Self>) {
+    pub fn remove_blocks(
+        &mut self,
+        block_ids: HashSet<BlockId>,
+        autoscroll: Option<Autoscroll>,
+        cx: &mut ViewContext<Self>,
+    ) {
         self.display_map.update(cx, |display_map, cx| {
             display_map.remove_blocks(block_ids, cx)
         });
+        if let Some(autoscroll) = autoscroll {
+            self.request_autoscroll(autoscroll, cx);
+        }
     }
 
     pub fn longest_row(&self, cx: &mut AppContext) -> u32 {

crates/editor/src/editor_tests.rs 🔗

@@ -2495,6 +2495,7 @@ fn test_move_line_up_down_with_blocks(cx: &mut TestAppContext) {
                 height: 1,
                 render: Arc::new(|_| Empty::new().into_any()),
             }],
+            Some(Autoscroll::fit()),
             cx,
         );
         editor.change_selections(None, cx, |s| {

crates/editor/src/element.rs 🔗

@@ -2883,6 +2883,7 @@ mod tests {
                     position: Anchor::min(),
                     render: Arc::new(|_| Empty::new().into_any()),
                 }],
+                None,
                 cx,
             );