Avoid auto-scrolling the editor when inserting/removing headers

This commit is contained in:
Antonio Scandurra 2023-06-14 10:49:23 +02:00
parent f8b9417406
commit 1aa1774688
5 changed files with 29 additions and 7 deletions

View file

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

View file

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

View file

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

View file

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

View file

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