From 99a9647b78e847d9aabd54c8f182379bd2a57b1a Mon Sep 17 00:00:00 2001 From: Smit Barmase Date: Mon, 7 Apr 2025 21:15:30 +0530 Subject: [PATCH] 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 --- crates/editor/src/editor.rs | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 4f73b3e2d61ce850bd9e8c8fd59e2b96171927d1..1d64ea5a44c114026567979a3338b596dcbf62f2 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -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, ) { let current_scroll_position = self.scroll_position(cx); - let lines = EditorSettings::get_global(cx).expand_excerpt_lines; + let lines_to_expand = EditorSettings::get_global(cx).expand_excerpt_lines; + let mut should_scroll_up = false; + + if direction == ExpandExcerptDirection::Down { + 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, direction, cx) + buffer.expand_excerpts([excerpt], lines_to_expand, direction, cx) }); - if direction == ExpandExcerptDirection::Down { - let new_scroll_position = current_scroll_position + gpui::Point::new(0.0, lines as f32); + + 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); } }