1use std::cmp;
2
3use gpui::ViewContext;
4use language::Point;
5
6use crate::{display_map::ToDisplayPoint, Editor, EditorMode, LineWithInvisibles};
7
8#[derive(PartialEq, Eq)]
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)]
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: f32,
52 line_height: f32,
53 cx: &mut ViewContext<Editor>,
54 ) -> bool {
55 let visible_lines = 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.set_y(max_scroll_top);
65 self.set_scroll_position(scroll_position, cx);
66 }
67
68 let (autoscroll, local) =
69 if let Some(autoscroll) = self.scroll_manager.autoscroll_request.take() {
70 autoscroll
71 } else {
72 return false;
73 };
74
75 let first_cursor_top;
76 let last_cursor_bottom;
77 if let Some(highlighted_rows) = &self.highlighted_rows {
78 first_cursor_top = highlighted_rows.start as f32;
79 last_cursor_bottom = first_cursor_top + 1.;
80 } else if autoscroll == Autoscroll::newest() {
81 let newest_selection = self.selections.newest::<Point>(cx);
82 first_cursor_top = newest_selection.head().to_display_point(&display_map).row() as f32;
83 last_cursor_bottom = first_cursor_top + 1.;
84 } else {
85 let selections = self.selections.all::<Point>(cx);
86 first_cursor_top = selections
87 .first()
88 .unwrap()
89 .head()
90 .to_display_point(&display_map)
91 .row() as f32;
92 last_cursor_bottom = selections
93 .last()
94 .unwrap()
95 .head()
96 .to_display_point(&display_map)
97 .row() as f32
98 + 1.0;
99 }
100
101 let margin = if matches!(self.mode, EditorMode::AutoHeight { .. }) {
102 0.
103 } else {
104 ((visible_lines - (last_cursor_bottom - first_cursor_top)) / 2.0).floor()
105 };
106 if margin < 0.0 {
107 return false;
108 }
109
110 let strategy = match autoscroll {
111 Autoscroll::Strategy(strategy) => strategy,
112 Autoscroll::Next => {
113 let last_autoscroll = &self.scroll_manager.last_autoscroll;
114 if let Some(last_autoscroll) = last_autoscroll {
115 if self.scroll_manager.anchor.offset == last_autoscroll.0
116 && first_cursor_top == last_autoscroll.1
117 && last_cursor_bottom == last_autoscroll.2
118 {
119 last_autoscroll.3.next()
120 } else {
121 AutoscrollStrategy::default()
122 }
123 } else {
124 AutoscrollStrategy::default()
125 }
126 }
127 };
128
129 match strategy {
130 AutoscrollStrategy::Fit | AutoscrollStrategy::Newest => {
131 let margin = margin.min(self.scroll_manager.vertical_scroll_margin);
132 let target_top = (first_cursor_top - margin).max(0.0);
133 let target_bottom = last_cursor_bottom + margin;
134 let start_row = scroll_position.y();
135 let end_row = start_row + visible_lines;
136
137 if target_top < start_row {
138 scroll_position.set_y(target_top);
139 self.set_scroll_position_internal(scroll_position, local, cx);
140 } else if target_bottom >= end_row {
141 scroll_position.set_y(target_bottom - visible_lines);
142 self.set_scroll_position_internal(scroll_position, local, cx);
143 }
144 }
145 AutoscrollStrategy::Center => {
146 scroll_position.set_y((first_cursor_top - margin).max(0.0));
147 self.set_scroll_position_internal(scroll_position, local, cx);
148 }
149 AutoscrollStrategy::Top => {
150 scroll_position.set_y((first_cursor_top).max(0.0));
151 self.set_scroll_position_internal(scroll_position, local, cx);
152 }
153 AutoscrollStrategy::Bottom => {
154 scroll_position.set_y((last_cursor_bottom - visible_lines).max(0.0));
155 self.set_scroll_position_internal(scroll_position, local, cx);
156 }
157 }
158
159 self.scroll_manager.last_autoscroll = Some((
160 self.scroll_manager.anchor.offset,
161 first_cursor_top,
162 last_cursor_bottom,
163 strategy,
164 ));
165
166 true
167 }
168
169 pub fn autoscroll_horizontally(
170 &mut self,
171 start_row: u32,
172 viewport_width: f32,
173 scroll_width: f32,
174 max_glyph_width: f32,
175 layouts: &[LineWithInvisibles],
176 cx: &mut ViewContext<Self>,
177 ) -> bool {
178 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
179 let selections = self.selections.all::<Point>(cx);
180
181 let mut target_left;
182 let mut target_right;
183
184 if self.highlighted_rows.is_some() {
185 target_left = 0.0_f32;
186 target_right = 0.0_f32;
187 } else {
188 target_left = std::f32::INFINITY;
189 target_right = 0.0_f32;
190 for selection in selections {
191 let head = selection.head().to_display_point(&display_map);
192 if head.row() >= start_row && head.row() < start_row + layouts.len() as u32 {
193 let start_column = head.column().saturating_sub(3);
194 let end_column = cmp::min(display_map.line_len(head.row()), head.column() + 3);
195 target_left = target_left.min(
196 layouts[(head.row() - start_row) as usize]
197 .line
198 .x_for_index(start_column as usize),
199 );
200 target_right = target_right.max(
201 layouts[(head.row() - start_row) as usize]
202 .line
203 .x_for_index(end_column as usize)
204 + max_glyph_width,
205 );
206 }
207 }
208 }
209
210 target_right = target_right.min(scroll_width);
211
212 if target_right - target_left > viewport_width {
213 return false;
214 }
215
216 let scroll_left = self.scroll_manager.anchor.offset.x() * max_glyph_width;
217 let scroll_right = scroll_left + viewport_width;
218
219 if target_left < scroll_left {
220 self.scroll_manager
221 .anchor
222 .offset
223 .set_x(target_left / max_glyph_width);
224 true
225 } else if target_right > scroll_right {
226 self.scroll_manager
227 .anchor
228 .offset
229 .set_x((target_right - viewport_width) / max_glyph_width);
230 true
231 } else {
232 false
233 }
234 }
235
236 pub fn request_autoscroll(&mut self, autoscroll: Autoscroll, cx: &mut ViewContext<Self>) {
237 self.scroll_manager.autoscroll_request = Some((autoscroll, true));
238 cx.notify();
239 }
240
241 pub(crate) fn request_autoscroll_remotely(
242 &mut self,
243 autoscroll: Autoscroll,
244 cx: &mut ViewContext<Self>,
245 ) {
246 self.scroll_manager.autoscroll_request = Some((autoscroll, false));
247 cx.notify();
248 }
249}