Scroll to follow expanding part of editor::SelectLargerSyntaxNode (#27295)
When the selection grows both ways, the new code prioritizes the top part instead of bottom one, this is usually more helpful considering that most programming language grammars tend to define tokens right before large delimited blocks, and rarely after (because humans and parsers read from top to bottom). Also, revert selection when convenient, so you have more control over what you're selecting, looking at the selection `head` is commonly more convenient than at the `tail`. Release Notes: - Improve scrolling of `editor::SelectLargerSyntaxNode` for better visibility.
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use super::Axis;
|
||||
use crate::{
|
||||
Autoscroll, Bias, Editor, EditorMode, NextScreen, NextScrollCursorCenterTopBottom,
|
||||
ScrollAnchor, ScrollCursorBottom, ScrollCursorCenter, ScrollCursorCenterTopBottom,
|
||||
ScrollCursorTop, SCROLL_CENTER_TOP_BOTTOM_DEBOUNCE_TIMEOUT,
|
||||
display_map::DisplayRow, Autoscroll, Editor, EditorMode, NextScreen,
|
||||
NextScrollCursorCenterTopBottom, ScrollCursorBottom, ScrollCursorCenter,
|
||||
ScrollCursorCenterTopBottom, ScrollCursorTop, SCROLL_CENTER_TOP_BOTTOM_DEBOUNCE_TIMEOUT,
|
||||
};
|
||||
use gpui::{Context, Point, Window};
|
||||
|
||||
@@ -40,41 +40,17 @@ impl Editor {
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
let snapshot = self.snapshot(window, cx).display_snapshot;
|
||||
let visible_rows = if let Some(visible_rows) = self.visible_line_count() {
|
||||
visible_rows as u32
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
|
||||
let scroll_margin_rows = self.vertical_scroll_margin() as u32;
|
||||
let mut new_screen_top = self.selections.newest_display(cx).head();
|
||||
*new_screen_top.column_mut() = 0;
|
||||
match self.next_scroll_position {
|
||||
NextScrollCursorCenterTopBottom::Center => {
|
||||
*new_screen_top.row_mut() = new_screen_top.row().0.saturating_sub(visible_rows / 2);
|
||||
self.scroll_cursor_center(&Default::default(), window, cx);
|
||||
}
|
||||
NextScrollCursorCenterTopBottom::Top => {
|
||||
*new_screen_top.row_mut() =
|
||||
new_screen_top.row().0.saturating_sub(scroll_margin_rows);
|
||||
self.scroll_cursor_top(&Default::default(), window, cx);
|
||||
}
|
||||
NextScrollCursorCenterTopBottom::Bottom => {
|
||||
*new_screen_top.row_mut() = new_screen_top
|
||||
.row()
|
||||
.0
|
||||
.saturating_sub(visible_rows.saturating_sub(scroll_margin_rows));
|
||||
self.scroll_cursor_bottom(&Default::default(), window, cx);
|
||||
}
|
||||
}
|
||||
self.set_scroll_anchor(
|
||||
ScrollAnchor {
|
||||
anchor: snapshot
|
||||
.buffer_snapshot
|
||||
.anchor_before(new_screen_top.to_offset(&snapshot, Bias::Left)),
|
||||
offset: Default::default(),
|
||||
},
|
||||
window,
|
||||
cx,
|
||||
);
|
||||
|
||||
self.next_scroll_position = self.next_scroll_position.next();
|
||||
self._scroll_cursor_center_top_bottom_task = cx.spawn(async move |editor, cx| {
|
||||
@@ -95,23 +71,10 @@ impl Editor {
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Editor>,
|
||||
) {
|
||||
let snapshot = self.snapshot(window, cx).display_snapshot;
|
||||
let scroll_margin_rows = self.vertical_scroll_margin() as u32;
|
||||
|
||||
let mut new_screen_top = self.selections.newest_display(cx).head();
|
||||
*new_screen_top.row_mut() = new_screen_top.row().0.saturating_sub(scroll_margin_rows);
|
||||
*new_screen_top.column_mut() = 0;
|
||||
let new_screen_top = new_screen_top.to_offset(&snapshot, Bias::Left);
|
||||
let new_anchor = snapshot.buffer_snapshot.anchor_before(new_screen_top);
|
||||
|
||||
self.set_scroll_anchor(
|
||||
ScrollAnchor {
|
||||
anchor: new_anchor,
|
||||
offset: Default::default(),
|
||||
},
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
let new_screen_top = self.selections.newest_display(cx).head().row().0;
|
||||
let new_screen_top = new_screen_top.saturating_sub(scroll_margin_rows);
|
||||
self.set_scroll_top_row(DisplayRow(new_screen_top), window, cx);
|
||||
}
|
||||
|
||||
pub fn scroll_cursor_center(
|
||||
@@ -120,27 +83,12 @@ impl Editor {
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Editor>,
|
||||
) {
|
||||
let snapshot = self.snapshot(window, cx).display_snapshot;
|
||||
let visible_rows = if let Some(visible_rows) = self.visible_line_count() {
|
||||
visible_rows as u32
|
||||
} else {
|
||||
let Some(visible_rows) = self.visible_line_count().map(|count| count as u32) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let mut new_screen_top = self.selections.newest_display(cx).head();
|
||||
*new_screen_top.row_mut() = new_screen_top.row().0.saturating_sub(visible_rows / 2);
|
||||
*new_screen_top.column_mut() = 0;
|
||||
let new_screen_top = new_screen_top.to_offset(&snapshot, Bias::Left);
|
||||
let new_anchor = snapshot.buffer_snapshot.anchor_before(new_screen_top);
|
||||
|
||||
self.set_scroll_anchor(
|
||||
ScrollAnchor {
|
||||
anchor: new_anchor,
|
||||
offset: Default::default(),
|
||||
},
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
let new_screen_top = self.selections.newest_display(cx).head().row().0;
|
||||
let new_screen_top = new_screen_top.saturating_sub(visible_rows / 2);
|
||||
self.set_scroll_top_row(DisplayRow(new_screen_top), window, cx);
|
||||
}
|
||||
|
||||
pub fn scroll_cursor_bottom(
|
||||
@@ -149,30 +97,13 @@ impl Editor {
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Editor>,
|
||||
) {
|
||||
let snapshot = self.snapshot(window, cx).display_snapshot;
|
||||
let scroll_margin_rows = self.vertical_scroll_margin() as u32;
|
||||
let visible_rows = if let Some(visible_rows) = self.visible_line_count() {
|
||||
visible_rows as u32
|
||||
} else {
|
||||
let Some(visible_rows) = self.visible_line_count().map(|count| count as u32) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let mut new_screen_top = self.selections.newest_display(cx).head();
|
||||
*new_screen_top.row_mut() = new_screen_top
|
||||
.row()
|
||||
.0
|
||||
.saturating_sub(visible_rows.saturating_sub(scroll_margin_rows));
|
||||
*new_screen_top.column_mut() = 0;
|
||||
let new_screen_top = new_screen_top.to_offset(&snapshot, Bias::Left);
|
||||
let new_anchor = snapshot.buffer_snapshot.anchor_before(new_screen_top);
|
||||
|
||||
self.set_scroll_anchor(
|
||||
ScrollAnchor {
|
||||
anchor: new_anchor,
|
||||
offset: Default::default(),
|
||||
},
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
let new_screen_top = self.selections.newest_display(cx).head().row().0;
|
||||
let new_screen_top =
|
||||
new_screen_top.saturating_sub(visible_rows.saturating_sub(scroll_margin_rows));
|
||||
self.set_scroll_top_row(DisplayRow(new_screen_top), window, cx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,16 @@ impl Autoscroll {
|
||||
Self::Strategy(AutoscrollStrategy::TopRelative(n))
|
||||
}
|
||||
|
||||
/// Scrolls so that the newest cursor is at the top.
|
||||
pub fn top() -> Self {
|
||||
Self::Strategy(AutoscrollStrategy::Top)
|
||||
}
|
||||
|
||||
/// Scrolls so that the newest cursor is roughly an n-th line from the bottom.
|
||||
pub fn bottom_relative(n: usize) -> Self {
|
||||
Self::Strategy(AutoscrollStrategy::BottomRelative(n))
|
||||
}
|
||||
|
||||
/// Scrolls so that the newest cursor is at the bottom.
|
||||
pub fn bottom() -> Self {
|
||||
Self::Strategy(AutoscrollStrategy::Bottom)
|
||||
@@ -54,6 +64,7 @@ pub enum AutoscrollStrategy {
|
||||
Top,
|
||||
Bottom,
|
||||
TopRelative(usize),
|
||||
BottomRelative(usize),
|
||||
}
|
||||
|
||||
impl AutoscrollStrategy {
|
||||
@@ -213,6 +224,10 @@ impl Editor {
|
||||
scroll_position.y = target_top - lines as f32;
|
||||
self.set_scroll_position_internal(scroll_position, local, true, window, cx);
|
||||
}
|
||||
AutoscrollStrategy::BottomRelative(lines) => {
|
||||
scroll_position.y = target_bottom + lines as f32;
|
||||
self.set_scroll_position_internal(scroll_position, local, true, window, cx);
|
||||
}
|
||||
}
|
||||
|
||||
self.scroll_manager.last_autoscroll = Some((
|
||||
|
||||
Reference in New Issue
Block a user