1use std::{cmp, f32};
2
3use gpui::{px, Pixels, ViewContext};
4use language::Point;
5
6use crate::{display_map::ToDisplayPoint, Editor, EditorMode, LineWithInvisibles};
7
8#[derive(PartialEq, Eq, Clone, Copy)]
9pub enum Autoscroll {
10 Next,
11 Strategy(AutoscrollStrategy),
12}
13
14impl Autoscroll {
15 /// scrolls the minimal amount to (try) and fit all cursors onscreen
16 pub fn fit() -> Self {
17 Self::Strategy(AutoscrollStrategy::Fit)
18 }
19
20 /// scrolls the minimal amount to fit the newest cursor
21 pub fn newest() -> Self {
22 Self::Strategy(AutoscrollStrategy::Newest)
23 }
24
25 /// scrolls so the newest cursor is vertically centered
26 pub fn center() -> Self {
27 Self::Strategy(AutoscrollStrategy::Center)
28 }
29
30 /// scrolls so the neweset cursor is near the top
31 /// (offset by vertical_scroll_margin)
32 pub fn focused() -> Self {
33 Self::Strategy(AutoscrollStrategy::Focused)
34 }
35}
36
37#[derive(PartialEq, Eq, Default, Clone, Copy)]
38pub enum AutoscrollStrategy {
39 Fit,
40 Newest,
41 #[default]
42 Center,
43 Focused,
44 Top,
45 Bottom,
46}
47
48impl AutoscrollStrategy {
49 fn next(&self) -> Self {
50 match self {
51 AutoscrollStrategy::Center => AutoscrollStrategy::Top,
52 AutoscrollStrategy::Top => AutoscrollStrategy::Bottom,
53 _ => AutoscrollStrategy::Center,
54 }
55 }
56}
57
58impl Editor {
59 pub fn autoscroll_vertically(
60 &mut self,
61 viewport_height: Pixels,
62 line_height: Pixels,
63 cx: &mut ViewContext<Editor>,
64 ) -> bool {
65 let visible_lines = viewport_height / line_height;
66 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
67 let mut scroll_position = self.scroll_manager.scroll_position(&display_map);
68 let max_scroll_top = if matches!(self.mode, EditorMode::AutoHeight { .. }) {
69 (display_map.max_point().row() as f32 - visible_lines + 1.).max(0.)
70 } else {
71 display_map.max_point().row() as f32
72 };
73 if scroll_position.y > max_scroll_top {
74 scroll_position.y = max_scroll_top;
75 self.set_scroll_position(scroll_position, cx);
76 }
77
78 let Some((autoscroll, local)) = self.scroll_manager.autoscroll_request.take() else {
79 return false;
80 };
81
82 let mut target_top;
83 let mut target_bottom;
84 if let Some(first_highlighted_row) = &self.highlighted_display_rows(cx).first_entry() {
85 target_top = *first_highlighted_row.key() as f32;
86 target_bottom = target_top + 1.;
87 } else {
88 let selections = self.selections.all::<Point>(cx);
89 target_top = selections
90 .first()
91 .unwrap()
92 .head()
93 .to_display_point(&display_map)
94 .row() as f32;
95 target_bottom = selections
96 .last()
97 .unwrap()
98 .head()
99 .to_display_point(&display_map)
100 .row() as f32
101 + 1.0;
102
103 // If the selections can't all fit on screen, scroll to the newest.
104 if autoscroll == Autoscroll::newest()
105 || autoscroll == Autoscroll::fit() && target_bottom - target_top > visible_lines
106 {
107 let newest_selection_top = selections
108 .iter()
109 .max_by_key(|s| s.id)
110 .unwrap()
111 .head()
112 .to_display_point(&display_map)
113 .row() as f32;
114 target_top = newest_selection_top;
115 target_bottom = newest_selection_top + 1.;
116 }
117 }
118
119 let margin = if matches!(self.mode, EditorMode::AutoHeight { .. }) {
120 0.
121 } else {
122 ((visible_lines - (target_bottom - target_top)) / 2.0).floor()
123 };
124
125 let strategy = match autoscroll {
126 Autoscroll::Strategy(strategy) => strategy,
127 Autoscroll::Next => {
128 let last_autoscroll = &self.scroll_manager.last_autoscroll;
129 if let Some(last_autoscroll) = last_autoscroll {
130 if self.scroll_manager.anchor.offset == last_autoscroll.0
131 && target_top == last_autoscroll.1
132 && target_bottom == last_autoscroll.2
133 {
134 last_autoscroll.3.next()
135 } else {
136 AutoscrollStrategy::default()
137 }
138 } else {
139 AutoscrollStrategy::default()
140 }
141 }
142 };
143
144 match strategy {
145 AutoscrollStrategy::Fit | AutoscrollStrategy::Newest => {
146 let margin = margin.min(self.scroll_manager.vertical_scroll_margin);
147 let target_top = (target_top - margin).max(0.0);
148 let target_bottom = target_bottom + margin;
149 let start_row = scroll_position.y;
150 let end_row = start_row + visible_lines;
151
152 let needs_scroll_up = target_top < start_row;
153 let needs_scroll_down = target_bottom >= end_row;
154
155 if needs_scroll_up && !needs_scroll_down {
156 scroll_position.y = target_top;
157 self.set_scroll_position_internal(scroll_position, local, true, cx);
158 }
159 if !needs_scroll_up && needs_scroll_down {
160 scroll_position.y = target_bottom - visible_lines;
161 self.set_scroll_position_internal(scroll_position, local, true, cx);
162 }
163 }
164 AutoscrollStrategy::Center => {
165 scroll_position.y = (target_top - margin).max(0.0);
166 self.set_scroll_position_internal(scroll_position, local, true, cx);
167 }
168 AutoscrollStrategy::Focused => {
169 scroll_position.y =
170 (target_top - self.scroll_manager.vertical_scroll_margin).max(0.0);
171 self.set_scroll_position_internal(scroll_position, local, true, cx);
172 }
173 AutoscrollStrategy::Top => {
174 scroll_position.y = (target_top).max(0.0);
175 self.set_scroll_position_internal(scroll_position, local, true, cx);
176 }
177 AutoscrollStrategy::Bottom => {
178 scroll_position.y = (target_bottom - visible_lines).max(0.0);
179 self.set_scroll_position_internal(scroll_position, local, true, cx);
180 }
181 }
182
183 self.scroll_manager.last_autoscroll = Some((
184 self.scroll_manager.anchor.offset,
185 target_top,
186 target_bottom,
187 strategy,
188 ));
189
190 true
191 }
192
193 pub(crate) fn autoscroll_horizontally(
194 &mut self,
195 start_row: u32,
196 viewport_width: Pixels,
197 scroll_width: Pixels,
198 max_glyph_width: Pixels,
199 layouts: &[LineWithInvisibles],
200 cx: &mut ViewContext<Self>,
201 ) -> bool {
202 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
203 let selections = self.selections.all::<Point>(cx);
204
205 let mut target_left;
206 let mut target_right;
207
208 if self.highlighted_rows.is_empty() {
209 target_left = px(f32::INFINITY);
210 target_right = px(0.);
211 for selection in selections {
212 let head = selection.head().to_display_point(&display_map);
213 if head.row() >= start_row && head.row() < start_row + layouts.len() as u32 {
214 let start_column = head.column().saturating_sub(3);
215 let end_column = cmp::min(display_map.line_len(head.row()), head.column() + 3);
216 target_left = target_left.min(
217 layouts[(head.row() - start_row) as usize]
218 .line
219 .x_for_index(start_column as usize),
220 );
221 target_right = target_right.max(
222 layouts[(head.row() - start_row) as usize]
223 .line
224 .x_for_index(end_column as usize)
225 + max_glyph_width,
226 );
227 }
228 }
229 } else {
230 target_left = px(0.);
231 target_right = px(0.);
232 }
233
234 target_right = target_right.min(scroll_width);
235
236 if target_right - target_left > viewport_width {
237 return false;
238 }
239
240 let scroll_left = self.scroll_manager.anchor.offset.x * max_glyph_width;
241 let scroll_right = scroll_left + viewport_width;
242
243 if target_left < scroll_left {
244 self.scroll_manager.anchor.offset.x = target_left / max_glyph_width;
245 true
246 } else if target_right > scroll_right {
247 self.scroll_manager.anchor.offset.x = (target_right - viewport_width) / max_glyph_width;
248 true
249 } else {
250 false
251 }
252 }
253
254 pub fn request_autoscroll(&mut self, autoscroll: Autoscroll, cx: &mut ViewContext<Self>) {
255 self.scroll_manager.autoscroll_request = Some((autoscroll, true));
256 cx.notify();
257 }
258
259 pub(crate) fn request_autoscroll_remotely(
260 &mut self,
261 autoscroll: Autoscroll,
262 cx: &mut ViewContext<Self>,
263 ) {
264 self.scroll_manager.autoscroll_request = Some((autoscroll, false));
265 cx.notify();
266 }
267}