editor: Fix excerpt down scroll behavior to only scroll when there are enough lines (#28231)

Follow up for https://github.com/zed-industries/zed/pull/27058

Improves excerpt down button to only scroll when there exists lines more
than equal to `expand_excerpt_lines`. This prevents weird shift at end
of the file.

Before:


https://github.com/user-attachments/assets/244a3bd6-d813-4cc8-9dcb-3addba2b652f

After:


https://github.com/user-attachments/assets/a9a9ba62-a454-4b56-9c8a-d8e6931b270b


Release Notes:

- N/A
This commit is contained in:
Smit Barmase 2025-04-07 21:15:30 +05:30 committed by GitHub
parent fa90b3a986
commit 99a9647b78
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -183,7 +183,7 @@ use std::{
};
pub use sum_tree::Bias;
use sum_tree::TreeMap;
use text::{BufferId, OffsetUtf16, Rope};
use text::{BufferId, FromAnchor, OffsetUtf16, Rope};
use theme::{
ActiveTheme, PlayerColor, StatusColors, SyntaxTheme, ThemeColors, ThemeSettings,
observe_buffer_font_size_adjustment,
@ -12718,12 +12718,33 @@ impl Editor {
cx: &mut Context<Self>,
) {
let current_scroll_position = self.scroll_position(cx);
let lines = EditorSettings::get_global(cx).expand_excerpt_lines;
self.buffer.update(cx, |buffer, cx| {
buffer.expand_excerpts([excerpt], lines, direction, cx)
});
let lines_to_expand = EditorSettings::get_global(cx).expand_excerpt_lines;
let mut should_scroll_up = false;
if direction == ExpandExcerptDirection::Down {
let new_scroll_position = current_scroll_position + gpui::Point::new(0.0, lines as f32);
let multi_buffer = self.buffer.read(cx);
let snapshot = multi_buffer.snapshot(cx);
if let Some(buffer_id) = snapshot.buffer_id_for_excerpt(excerpt) {
if let Some(buffer) = multi_buffer.buffer(buffer_id) {
if let Some(excerpt_range) = snapshot.buffer_range_for_excerpt(excerpt) {
let buffer_snapshot = buffer.read(cx).snapshot();
let excerpt_end_row =
Point::from_anchor(&excerpt_range.end, &buffer_snapshot).row;
let last_row = buffer_snapshot.max_point().row;
let lines_below = last_row.saturating_sub(excerpt_end_row);
should_scroll_up = lines_below >= lines_to_expand;
}
}
}
}
self.buffer.update(cx, |buffer, cx| {
buffer.expand_excerpts([excerpt], lines_to_expand, direction, cx)
});
if should_scroll_up {
let new_scroll_position =
current_scroll_position + gpui::Point::new(0.0, lines_to_expand as f32);
self.set_scroll_position(new_scroll_position, window, cx);
}
}