1use crate::Vim;
2use editor::{
3 DisplayPoint, Editor, EditorSettings,
4 display_map::{DisplayRow, ToDisplayPoint},
5 scroll::ScrollAmount,
6};
7use gpui::{Context, Window, actions};
8use language::Bias;
9use settings::Settings;
10
11actions!(
12 vim,
13 [LineUp, LineDown, ScrollUp, ScrollDown, PageUp, PageDown]
14);
15
16pub fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
17 Vim::action(editor, cx, |vim, _: &LineDown, window, cx| {
18 vim.scroll(false, window, cx, |c| ScrollAmount::Line(c.unwrap_or(1.)))
19 });
20 Vim::action(editor, cx, |vim, _: &LineUp, window, cx| {
21 vim.scroll(false, window, cx, |c| ScrollAmount::Line(-c.unwrap_or(1.)))
22 });
23 Vim::action(editor, cx, |vim, _: &PageDown, window, cx| {
24 vim.scroll(false, window, cx, |c| ScrollAmount::Page(c.unwrap_or(1.)))
25 });
26 Vim::action(editor, cx, |vim, _: &PageUp, window, cx| {
27 vim.scroll(false, window, cx, |c| ScrollAmount::Page(-c.unwrap_or(1.)))
28 });
29 Vim::action(editor, cx, |vim, _: &ScrollDown, window, cx| {
30 vim.scroll(true, window, cx, |c| {
31 if let Some(c) = c {
32 ScrollAmount::Line(c)
33 } else {
34 ScrollAmount::Page(0.5)
35 }
36 })
37 });
38 Vim::action(editor, cx, |vim, _: &ScrollUp, window, cx| {
39 vim.scroll(true, window, cx, |c| {
40 if let Some(c) = c {
41 ScrollAmount::Line(-c)
42 } else {
43 ScrollAmount::Page(-0.5)
44 }
45 })
46 });
47}
48
49impl Vim {
50 fn scroll(
51 &mut self,
52 move_cursor: bool,
53 window: &mut Window,
54 cx: &mut Context<Self>,
55 by: fn(c: Option<f32>) -> ScrollAmount,
56 ) {
57 let amount = by(Vim::take_count(cx).map(|c| c as f32));
58 Vim::take_forced_motion(cx);
59 self.exit_temporary_normal(window, cx);
60 self.update_editor(window, cx, |_, editor, window, cx| {
61 scroll_editor(editor, move_cursor, &amount, window, cx)
62 });
63 }
64}
65
66fn scroll_editor(
67 editor: &mut Editor,
68 preserve_cursor_position: bool,
69 amount: &ScrollAmount,
70 window: &mut Window,
71 cx: &mut Context<Editor>,
72) {
73 let should_move_cursor = editor.newest_selection_on_screen(cx).is_eq();
74 let old_top_anchor = editor.scroll_manager.anchor().anchor;
75
76 if editor.scroll_hover(amount, window, cx) {
77 return;
78 }
79
80 let full_page_up = amount.is_full_page() && amount.direction().is_upwards();
81 let amount = match (amount.is_full_page(), editor.visible_line_count()) {
82 (true, Some(visible_line_count)) => {
83 if amount.direction().is_upwards() {
84 ScrollAmount::Line(amount.lines(visible_line_count) + 1.0)
85 } else {
86 ScrollAmount::Line(amount.lines(visible_line_count) - 1.0)
87 }
88 }
89 _ => amount.clone(),
90 };
91
92 editor.scroll_screen(&amount, window, cx);
93 if !should_move_cursor {
94 return;
95 }
96
97 let Some(visible_line_count) = editor.visible_line_count() else {
98 return;
99 };
100
101 let top_anchor = editor.scroll_manager.anchor().anchor;
102 let vertical_scroll_margin = EditorSettings::get_global(cx).vertical_scroll_margin;
103
104 editor.change_selections(None, window, cx, |s| {
105 s.move_with(|map, selection| {
106 let mut head = selection.head();
107 let top = top_anchor.to_display_point(map);
108 let starting_column = head.column();
109
110 let vertical_scroll_margin =
111 (vertical_scroll_margin as u32).min(visible_line_count as u32 / 2);
112
113 if preserve_cursor_position {
114 let old_top = old_top_anchor.to_display_point(map);
115 let new_row = if old_top.row() == top.row() {
116 DisplayRow(
117 head.row()
118 .0
119 .saturating_add_signed(amount.lines(visible_line_count) as i32),
120 )
121 } else {
122 DisplayRow(top.row().0 + selection.head().row().0 - old_top.row().0)
123 };
124 head = map.clip_point(DisplayPoint::new(new_row, head.column()), Bias::Left)
125 }
126
127 let min_row = if top.row().0 == 0 {
128 DisplayRow(0)
129 } else {
130 DisplayRow(top.row().0 + vertical_scroll_margin)
131 };
132
133 let max_visible_row = top.row().0.saturating_add(
134 (visible_line_count as u32).saturating_sub(1 + vertical_scroll_margin),
135 );
136 // scroll off the end.
137 let max_row = if top.row().0 + visible_line_count as u32 >= map.max_point().row().0 {
138 map.max_point().row()
139 } else {
140 DisplayRow(
141 (top.row().0 + visible_line_count as u32)
142 .saturating_sub(1 + vertical_scroll_margin),
143 )
144 };
145
146 let new_row = if full_page_up {
147 // Special-casing ctrl-b/page-up, which is special-cased by Vim, it seems
148 // to always put the cursor on the last line of the page, even if the cursor
149 // was before that.
150 DisplayRow(max_visible_row)
151 } else if head.row() < min_row {
152 min_row
153 } else if head.row() > max_row {
154 max_row
155 } else {
156 head.row()
157 };
158 let new_head = map.clip_point(DisplayPoint::new(new_row, starting_column), Bias::Left);
159
160 if selection.is_empty() {
161 selection.collapse_to(new_head, selection.goal)
162 } else {
163 selection.set_head(new_head, selection.goal)
164 };
165 })
166 });
167}
168
169#[cfg(test)]
170mod test {
171 use crate::{
172 state::Mode,
173 test::{NeovimBackedTestContext, VimTestContext},
174 };
175 use editor::{EditorSettings, ScrollBeyondLastLine};
176 use gpui::{AppContext as _, point, px, size};
177 use indoc::indoc;
178 use language::Point;
179 use settings::SettingsStore;
180
181 pub fn sample_text(rows: usize, cols: usize, start_char: char) -> String {
182 let mut text = String::new();
183 for row in 0..rows {
184 let c: char = (start_char as u32 + row as u32) as u8 as char;
185 let mut line = c.to_string().repeat(cols);
186 if row < rows - 1 {
187 line.push('\n');
188 }
189 text += &line;
190 }
191 text
192 }
193
194 #[gpui::test]
195 async fn test_scroll(cx: &mut gpui::TestAppContext) {
196 let mut cx = VimTestContext::new(cx, true).await;
197
198 let (line_height, visible_line_count) = cx.editor(|editor, window, _cx| {
199 (
200 editor
201 .style()
202 .unwrap()
203 .text
204 .line_height_in_pixels(window.rem_size()),
205 editor.visible_line_count().unwrap(),
206 )
207 });
208
209 let window = cx.window;
210 let margin = cx
211 .update_window(window, |_, window, _cx| {
212 window.viewport_size().height - line_height * visible_line_count
213 })
214 .unwrap();
215 cx.simulate_window_resize(
216 cx.window,
217 size(px(1000.), margin + 8. * line_height - px(1.0)),
218 );
219
220 cx.set_state(
221 indoc!(
222 "ˇone
223 two
224 three
225 four
226 five
227 six
228 seven
229 eight
230 nine
231 ten
232 eleven
233 twelve
234 "
235 ),
236 Mode::Normal,
237 );
238
239 cx.update_editor(|editor, window, cx| {
240 assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 0.))
241 });
242 cx.simulate_keystrokes("ctrl-e");
243 cx.update_editor(|editor, window, cx| {
244 assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 1.))
245 });
246 cx.simulate_keystrokes("2 ctrl-e");
247 cx.update_editor(|editor, window, cx| {
248 assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 3.))
249 });
250 cx.simulate_keystrokes("ctrl-y");
251 cx.update_editor(|editor, window, cx| {
252 assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 2.))
253 });
254
255 // does not select in normal mode
256 cx.simulate_keystrokes("g g");
257 cx.update_editor(|editor, window, cx| {
258 assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 0.))
259 });
260 cx.simulate_keystrokes("ctrl-d");
261 cx.update_editor(|editor, window, cx| {
262 assert_eq!(
263 editor.snapshot(window, cx).scroll_position(),
264 point(0., 3.0)
265 );
266 assert_eq!(
267 editor.selections.newest(cx).range(),
268 Point::new(6, 0)..Point::new(6, 0)
269 )
270 });
271
272 // does select in visual mode
273 cx.simulate_keystrokes("g g");
274 cx.update_editor(|editor, window, cx| {
275 assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 0.))
276 });
277 cx.simulate_keystrokes("v ctrl-d");
278 cx.update_editor(|editor, window, cx| {
279 assert_eq!(
280 editor.snapshot(window, cx).scroll_position(),
281 point(0., 3.0)
282 );
283 assert_eq!(
284 editor.selections.newest(cx).range(),
285 Point::new(0, 0)..Point::new(6, 1)
286 )
287 });
288 }
289
290 #[gpui::test]
291 async fn test_ctrl_d_u(cx: &mut gpui::TestAppContext) {
292 let mut cx = NeovimBackedTestContext::new(cx).await;
293
294 cx.set_scroll_height(10).await;
295
296 let content = "ˇ".to_owned() + &sample_text(26, 2, 'a');
297 cx.set_shared_state(&content).await;
298
299 // skip over the scrolloff at the top
300 // test ctrl-d
301 cx.simulate_shared_keystrokes("4 j ctrl-d").await;
302 cx.shared_state().await.assert_matches();
303 cx.simulate_shared_keystrokes("ctrl-d").await;
304 cx.shared_state().await.assert_matches();
305 cx.simulate_shared_keystrokes("g g ctrl-d").await;
306 cx.shared_state().await.assert_matches();
307
308 // test ctrl-u
309 cx.simulate_shared_keystrokes("ctrl-u").await;
310 cx.shared_state().await.assert_matches();
311 cx.simulate_shared_keystrokes("ctrl-d ctrl-d 4 j ctrl-u ctrl-u")
312 .await;
313 cx.shared_state().await.assert_matches();
314
315 // test returning to top
316 cx.simulate_shared_keystrokes("g g ctrl-d ctrl-u ctrl-u")
317 .await;
318 cx.shared_state().await.assert_matches();
319 }
320
321 #[gpui::test]
322 async fn test_ctrl_f_b(cx: &mut gpui::TestAppContext) {
323 let mut cx = NeovimBackedTestContext::new(cx).await;
324
325 let visible_lines = 10;
326 cx.set_scroll_height(visible_lines).await;
327
328 // First test without vertical scroll margin
329 cx.neovim.set_option(&format!("scrolloff={}", 0)).await;
330 cx.update_global(|store: &mut SettingsStore, cx| {
331 store.update_user_settings::<EditorSettings>(cx, |s| {
332 s.vertical_scroll_margin = Some(0.0)
333 });
334 });
335
336 let content = "ˇ".to_owned() + &sample_text(26, 2, 'a');
337 cx.set_shared_state(&content).await;
338
339 // scroll down: ctrl-f
340 cx.simulate_shared_keystrokes("ctrl-f").await;
341 cx.shared_state().await.assert_matches();
342
343 cx.simulate_shared_keystrokes("ctrl-f").await;
344 cx.shared_state().await.assert_matches();
345
346 // scroll up: ctrl-b
347 cx.simulate_shared_keystrokes("ctrl-b").await;
348 cx.shared_state().await.assert_matches();
349
350 cx.simulate_shared_keystrokes("ctrl-b").await;
351 cx.shared_state().await.assert_matches();
352
353 // Now go back to start of file, and test with vertical scroll margin
354 cx.simulate_shared_keystrokes("g g").await;
355 cx.shared_state().await.assert_matches();
356
357 cx.neovim.set_option(&format!("scrolloff={}", 3)).await;
358 cx.update_global(|store: &mut SettingsStore, cx| {
359 store.update_user_settings::<EditorSettings>(cx, |s| {
360 s.vertical_scroll_margin = Some(3.0)
361 });
362 });
363
364 // scroll down: ctrl-f
365 cx.simulate_shared_keystrokes("ctrl-f").await;
366 cx.shared_state().await.assert_matches();
367
368 cx.simulate_shared_keystrokes("ctrl-f").await;
369 cx.shared_state().await.assert_matches();
370
371 // scroll up: ctrl-b
372 cx.simulate_shared_keystrokes("ctrl-b").await;
373 cx.shared_state().await.assert_matches();
374
375 cx.simulate_shared_keystrokes("ctrl-b").await;
376 cx.shared_state().await.assert_matches();
377 }
378
379 #[gpui::test]
380 async fn test_scroll_beyond_last_line(cx: &mut gpui::TestAppContext) {
381 let mut cx = NeovimBackedTestContext::new(cx).await;
382
383 cx.set_scroll_height(10).await;
384
385 let content = "ˇ".to_owned() + &sample_text(26, 2, 'a');
386 cx.set_shared_state(&content).await;
387
388 cx.update_global(|store: &mut SettingsStore, cx| {
389 store.update_user_settings::<EditorSettings>(cx, |s| {
390 s.scroll_beyond_last_line = Some(ScrollBeyondLastLine::Off);
391 // s.vertical_scroll_margin = Some(0.);
392 });
393 });
394
395 // ctrl-d can reach the end and the cursor stays in the first column
396 cx.simulate_shared_keystrokes("shift-g k").await;
397 cx.shared_state().await.assert_matches();
398 cx.simulate_shared_keystrokes("ctrl-d").await;
399 cx.shared_state().await.assert_matches();
400
401 // ctrl-u from the last line
402 cx.simulate_shared_keystrokes("shift-g").await;
403 cx.shared_state().await.assert_matches();
404 cx.simulate_shared_keystrokes("ctrl-u").await;
405 cx.shared_state().await.assert_matches();
406 }
407
408 #[gpui::test]
409 async fn test_ctrl_y_e(cx: &mut gpui::TestAppContext) {
410 let mut cx = NeovimBackedTestContext::new(cx).await;
411
412 cx.set_scroll_height(10).await;
413
414 let content = "ˇ".to_owned() + &sample_text(26, 2, 'a');
415 cx.set_shared_state(&content).await;
416
417 for _ in 0..8 {
418 cx.simulate_shared_keystrokes("ctrl-e").await;
419 cx.shared_state().await.assert_matches();
420 }
421
422 for _ in 0..8 {
423 cx.simulate_shared_keystrokes("ctrl-y").await;
424 cx.shared_state().await.assert_matches();
425 }
426 }
427}