Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Avoid auto-scrolling the editor when inserting/removing headers

Antonio Scandurra1aa177468809
diff --git a/crates/ai/src/assistant.rs b/crates/ai/src/assistant.rs
index 39309fd3610..e733f4d4771 100644
--- a/crates/ai/src/assistant.rs
+++ b/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);
});
}
diff --git a/crates/diagnostics/src/diagnostics.rs b/crates/diagnostics/src/diagnostics.rs
index 12fcc1395bf..5350e53d6ae 100644
--- a/crates/diagnostics/src/diagnostics.rs
+++ b/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,
);
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs
index 4fef94a9bbe..6e06c901f0a 100644
--- a/crates/editor/src/editor.rs
+++ b/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 {
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs
index d256a0424d4..e2b876f4b76 100644
--- a/crates/editor/src/editor_tests.rs
+++ b/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| {
diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs
index e1b925d4eab..d6f9a2e9068 100644
--- a/crates/editor/src/element.rs
+++ b/crates/editor/src/element.rs
@@ -2883,6 +2883,7 @@ mod tests {
position: Anchor::min(),
render: Arc::new(|_| Empty::new().into_any()),
}],
+ None,
cx,
);