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