scroll.rs

  1use crate::Vim;
  2use editor::{
  3    DisplayPoint, Editor, EditorSettings, SelectionEffects,
  4    display_map::{DisplayRow, ToDisplayPoint},
  5    scroll::ScrollAmount,
  6};
  7use gpui::{Context, Window, actions};
  8use language::Bias;
  9use settings::Settings;
 10use text::SelectionGoal;
 11
 12actions!(
 13    vim,
 14    [
 15        /// Scrolls up by one line.
 16        LineUp,
 17        /// Scrolls down by one line.
 18        LineDown,
 19        /// Scrolls right by one column.
 20        ColumnRight,
 21        /// Scrolls left by one column.
 22        ColumnLeft,
 23        /// Scrolls up by half a page.
 24        ScrollUp,
 25        /// Scrolls down by half a page.
 26        ScrollDown,
 27        /// Scrolls up by one page.
 28        PageUp,
 29        /// Scrolls down by one page.
 30        PageDown,
 31        /// Scrolls right by half a page's width.
 32        HalfPageRight,
 33        /// Scrolls left by half a page's width.
 34        HalfPageLeft,
 35    ]
 36);
 37
 38pub fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
 39    Vim::action(editor, cx, |vim, _: &LineDown, window, cx| {
 40        vim.scroll(false, window, cx, |c| ScrollAmount::Line(c.unwrap_or(1.)))
 41    });
 42    Vim::action(editor, cx, |vim, _: &LineUp, window, cx| {
 43        vim.scroll(false, window, cx, |c| ScrollAmount::Line(-c.unwrap_or(1.)))
 44    });
 45    Vim::action(editor, cx, |vim, _: &ColumnRight, window, cx| {
 46        vim.scroll(false, window, cx, |c| ScrollAmount::Column(c.unwrap_or(1.)))
 47    });
 48    Vim::action(editor, cx, |vim, _: &ColumnLeft, window, cx| {
 49        vim.scroll(false, window, cx, |c| {
 50            ScrollAmount::Column(-c.unwrap_or(1.))
 51        })
 52    });
 53    Vim::action(editor, cx, |vim, _: &PageDown, window, cx| {
 54        vim.scroll(false, window, cx, |c| ScrollAmount::Page(c.unwrap_or(1.)))
 55    });
 56    Vim::action(editor, cx, |vim, _: &PageUp, window, cx| {
 57        vim.scroll(false, window, cx, |c| ScrollAmount::Page(-c.unwrap_or(1.)))
 58    });
 59    Vim::action(editor, cx, |vim, _: &HalfPageRight, window, cx| {
 60        vim.scroll(false, window, cx, |c| {
 61            ScrollAmount::PageWidth(c.unwrap_or(0.5))
 62        })
 63    });
 64    Vim::action(editor, cx, |vim, _: &HalfPageLeft, window, cx| {
 65        vim.scroll(false, window, cx, |c| {
 66            ScrollAmount::PageWidth(-c.unwrap_or(0.5))
 67        })
 68    });
 69    Vim::action(editor, cx, |vim, _: &ScrollDown, window, cx| {
 70        vim.scroll(true, window, cx, |c| {
 71            if let Some(c) = c {
 72                ScrollAmount::Line(c)
 73            } else {
 74                ScrollAmount::Page(0.5)
 75            }
 76        })
 77    });
 78    Vim::action(editor, cx, |vim, _: &ScrollUp, window, cx| {
 79        vim.scroll(true, window, cx, |c| {
 80            if let Some(c) = c {
 81                ScrollAmount::Line(-c)
 82            } else {
 83                ScrollAmount::Page(-0.5)
 84            }
 85        })
 86    });
 87}
 88
 89impl Vim {
 90    fn scroll(
 91        &mut self,
 92        move_cursor: bool,
 93        window: &mut Window,
 94        cx: &mut Context<Self>,
 95        by: fn(c: Option<f32>) -> ScrollAmount,
 96    ) {
 97        let amount = by(Vim::take_count(cx).map(|c| c as f32));
 98        Vim::take_forced_motion(cx);
 99        self.exit_temporary_normal(window, cx);
100        self.update_editor(window, cx, |_, editor, window, cx| {
101            scroll_editor(editor, move_cursor, &amount, window, cx)
102        });
103    }
104}
105
106fn scroll_editor(
107    editor: &mut Editor,
108    preserve_cursor_position: bool,
109    amount: &ScrollAmount,
110    window: &mut Window,
111    cx: &mut Context<Editor>,
112) {
113    let should_move_cursor = editor.newest_selection_on_screen(cx).is_eq();
114    let old_top_anchor = editor.scroll_manager.anchor().anchor;
115
116    if editor.scroll_hover(amount, window, cx) {
117        return;
118    }
119
120    let full_page_up = amount.is_full_page() && amount.direction().is_upwards();
121    let amount = match (amount.is_full_page(), editor.visible_line_count()) {
122        (true, Some(visible_line_count)) => {
123            if amount.direction().is_upwards() {
124                ScrollAmount::Line(amount.lines(visible_line_count) + 1.0)
125            } else {
126                ScrollAmount::Line(amount.lines(visible_line_count) - 1.0)
127            }
128        }
129        _ => amount.clone(),
130    };
131
132    editor.scroll_screen(&amount, window, cx);
133    if !should_move_cursor {
134        return;
135    }
136
137    let Some(visible_line_count) = editor.visible_line_count() else {
138        return;
139    };
140
141    let Some(visible_column_count) = editor.visible_column_count() else {
142        return;
143    };
144
145    let top_anchor = editor.scroll_manager.anchor().anchor;
146    let vertical_scroll_margin = EditorSettings::get_global(cx).vertical_scroll_margin;
147
148    editor.change_selections(
149        SelectionEffects::no_scroll().nav_history(false),
150        window,
151        cx,
152        |s| {
153            s.move_with(|map, selection| {
154                // TODO: Improve the logic and function calls below to be dependent on
155                // the `amount`. If the amount is vertical, we don't care about
156                // columns, while if it's horizontal, we don't care about rows,
157                // so we don't need to calculate both and deal with logic for
158                // both.
159                let mut head = selection.head();
160                let top = top_anchor.to_display_point(map);
161                let max_point = map.max_point();
162                let starting_column = head.column();
163
164                let vertical_scroll_margin =
165                    (vertical_scroll_margin as u32).min(visible_line_count as u32 / 2);
166
167                if preserve_cursor_position {
168                    let old_top = old_top_anchor.to_display_point(map);
169                    let new_row = if old_top.row() == top.row() {
170                        DisplayRow(
171                            head.row()
172                                .0
173                                .saturating_add_signed(amount.lines(visible_line_count) as i32),
174                        )
175                    } else {
176                        DisplayRow(top.row().0 + selection.head().row().0 - old_top.row().0)
177                    };
178                    head = map.clip_point(DisplayPoint::new(new_row, head.column()), Bias::Left)
179                }
180
181                let min_row = if top.row().0 == 0 {
182                    DisplayRow(0)
183                } else {
184                    DisplayRow(top.row().0 + vertical_scroll_margin)
185                };
186
187                let max_visible_row = top.row().0.saturating_add(
188                    (visible_line_count as u32).saturating_sub(1 + vertical_scroll_margin),
189                );
190                // scroll off the end.
191                let max_row = if top.row().0 + visible_line_count as u32 >= max_point.row().0 {
192                    max_point.row()
193                } else {
194                    DisplayRow(
195                        (top.row().0 + visible_line_count as u32)
196                            .saturating_sub(1 + vertical_scroll_margin),
197                    )
198                };
199
200                let new_row = if full_page_up {
201                    // Special-casing ctrl-b/page-up, which is special-cased by Vim, it seems
202                    // to always put the cursor on the last line of the page, even if the cursor
203                    // was before that.
204                    DisplayRow(max_visible_row)
205                } else if head.row() < min_row {
206                    min_row
207                } else if head.row() > max_row {
208                    max_row
209                } else {
210                    head.row()
211                };
212
213                // The minimum column position that the cursor position can be
214                // at is either the scroll manager's anchor column, which is the
215                // left-most column in the visible area, or the scroll manager's
216                // old anchor column, in case the cursor position is being
217                // preserved. This is necessary for motions like `ctrl-d` in
218                // case there's not enough content to scroll half page down, in
219                // which case the scroll manager's anchor column will be the
220                // maximum column for the current line, so the minimum column
221                // would end up being the same as the maximum column.
222                let min_column = match preserve_cursor_position {
223                    true => old_top_anchor.to_display_point(map).column(),
224                    false => top.column(),
225                };
226
227                // As for the maximum column position, that should be either the
228                // right-most column in the visible area, which we can easily
229                // calculate by adding the visible column count to the minimum
230                // column position, or the right-most column in the current
231                // line, seeing as the cursor might be in a short line, in which
232                // case we don't want to go past its last column.
233                let max_row_column = map.line_len(new_row);
234                let max_column = match min_column + visible_column_count as u32 {
235                    max_column if max_column >= max_row_column => max_row_column,
236                    max_column => max_column,
237                };
238
239                // Ensure that the cursor's column stays within the visible
240                // area, otherwise clip it at either the left or right edge of
241                // the visible area.
242                let new_column = match (min_column, max_column) {
243                    (min_column, _) if starting_column < min_column => min_column,
244                    (_, max_column) if starting_column > max_column => max_column,
245                    _ => starting_column,
246                };
247
248                let new_head = map.clip_point(DisplayPoint::new(new_row, new_column), Bias::Left);
249                let goal = match amount {
250                    ScrollAmount::Column(_) | ScrollAmount::PageWidth(_) => SelectionGoal::None,
251                    _ => selection.goal,
252                };
253
254                if selection.is_empty() {
255                    selection.collapse_to(new_head, goal)
256                } else {
257                    selection.set_head(new_head, goal)
258                };
259            })
260        },
261    );
262}
263
264#[cfg(test)]
265mod test {
266    use crate::{
267        state::Mode,
268        test::{NeovimBackedTestContext, VimTestContext},
269    };
270    use editor::{EditorSettings, ScrollBeyondLastLine};
271    use gpui::{AppContext as _, point, px, size};
272    use indoc::indoc;
273    use language::Point;
274    use settings::SettingsStore;
275
276    pub fn sample_text(rows: usize, cols: usize, start_char: char) -> String {
277        let mut text = String::new();
278        for row in 0..rows {
279            let c: char = (start_char as u32 + row as u32) as u8 as char;
280            let mut line = c.to_string().repeat(cols);
281            if row < rows - 1 {
282                line.push('\n');
283            }
284            text += &line;
285        }
286        text
287    }
288
289    #[gpui::test]
290    async fn test_scroll(cx: &mut gpui::TestAppContext) {
291        let mut cx = VimTestContext::new(cx, true).await;
292
293        let (line_height, visible_line_count) = cx.editor(|editor, window, _cx| {
294            (
295                editor
296                    .style()
297                    .unwrap()
298                    .text
299                    .line_height_in_pixels(window.rem_size()),
300                editor.visible_line_count().unwrap(),
301            )
302        });
303
304        let window = cx.window;
305        let margin = cx
306            .update_window(window, |_, window, _cx| {
307                window.viewport_size().height - line_height * visible_line_count
308            })
309            .unwrap();
310        cx.simulate_window_resize(
311            cx.window,
312            size(px(1000.), margin + 8. * line_height - px(1.0)),
313        );
314
315        cx.set_state(
316            indoc!(
317                "ˇone
318                two
319                three
320                four
321                five
322                six
323                seven
324                eight
325                nine
326                ten
327                eleven
328                twelve
329            "
330            ),
331            Mode::Normal,
332        );
333
334        cx.update_editor(|editor, window, cx| {
335            assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 0.))
336        });
337        cx.simulate_keystrokes("ctrl-e");
338        cx.update_editor(|editor, window, cx| {
339            assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 1.))
340        });
341        cx.simulate_keystrokes("2 ctrl-e");
342        cx.update_editor(|editor, window, cx| {
343            assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 3.))
344        });
345        cx.simulate_keystrokes("ctrl-y");
346        cx.update_editor(|editor, window, cx| {
347            assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 2.))
348        });
349
350        // does not select in normal mode
351        cx.simulate_keystrokes("g g");
352        cx.update_editor(|editor, window, cx| {
353            assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 0.))
354        });
355        cx.simulate_keystrokes("ctrl-d");
356        cx.update_editor(|editor, window, cx| {
357            assert_eq!(
358                editor.snapshot(window, cx).scroll_position(),
359                point(0., 3.0)
360            );
361            assert_eq!(
362                editor.selections.newest(cx).range(),
363                Point::new(6, 0)..Point::new(6, 0)
364            )
365        });
366
367        // does select in visual mode
368        cx.simulate_keystrokes("g g");
369        cx.update_editor(|editor, window, cx| {
370            assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 0.))
371        });
372        cx.simulate_keystrokes("v ctrl-d");
373        cx.update_editor(|editor, window, cx| {
374            assert_eq!(
375                editor.snapshot(window, cx).scroll_position(),
376                point(0., 3.0)
377            );
378            assert_eq!(
379                editor.selections.newest(cx).range(),
380                Point::new(0, 0)..Point::new(6, 1)
381            )
382        });
383    }
384
385    #[gpui::test]
386    async fn test_ctrl_d_u(cx: &mut gpui::TestAppContext) {
387        let mut cx = NeovimBackedTestContext::new(cx).await;
388
389        cx.set_scroll_height(10).await;
390
391        let content = "ˇ".to_owned() + &sample_text(26, 2, 'a');
392        cx.set_shared_state(&content).await;
393
394        // skip over the scrolloff at the top
395        // test ctrl-d
396        cx.simulate_shared_keystrokes("4 j ctrl-d").await;
397        cx.shared_state().await.assert_matches();
398        cx.simulate_shared_keystrokes("ctrl-d").await;
399        cx.shared_state().await.assert_matches();
400        cx.simulate_shared_keystrokes("g g ctrl-d").await;
401        cx.shared_state().await.assert_matches();
402
403        // test ctrl-u
404        cx.simulate_shared_keystrokes("ctrl-u").await;
405        cx.shared_state().await.assert_matches();
406        cx.simulate_shared_keystrokes("ctrl-d ctrl-d 4 j ctrl-u ctrl-u")
407            .await;
408        cx.shared_state().await.assert_matches();
409
410        // test returning to top
411        cx.simulate_shared_keystrokes("g g ctrl-d ctrl-u ctrl-u")
412            .await;
413        cx.shared_state().await.assert_matches();
414    }
415
416    #[gpui::test]
417    async fn test_ctrl_f_b(cx: &mut gpui::TestAppContext) {
418        let mut cx = NeovimBackedTestContext::new(cx).await;
419
420        let visible_lines = 10;
421        cx.set_scroll_height(visible_lines).await;
422
423        // First test without vertical scroll margin
424        cx.neovim.set_option(&format!("scrolloff={}", 0)).await;
425        cx.update_global(|store: &mut SettingsStore, cx| {
426            store.update_user_settings::<EditorSettings>(cx, |s| {
427                s.vertical_scroll_margin = Some(0.0)
428            });
429        });
430
431        let content = "ˇ".to_owned() + &sample_text(26, 2, 'a');
432        cx.set_shared_state(&content).await;
433
434        // scroll down: ctrl-f
435        cx.simulate_shared_keystrokes("ctrl-f").await;
436        cx.shared_state().await.assert_matches();
437
438        cx.simulate_shared_keystrokes("ctrl-f").await;
439        cx.shared_state().await.assert_matches();
440
441        // scroll up: ctrl-b
442        cx.simulate_shared_keystrokes("ctrl-b").await;
443        cx.shared_state().await.assert_matches();
444
445        cx.simulate_shared_keystrokes("ctrl-b").await;
446        cx.shared_state().await.assert_matches();
447
448        // Now go back to start of file, and test with vertical scroll margin
449        cx.simulate_shared_keystrokes("g g").await;
450        cx.shared_state().await.assert_matches();
451
452        cx.neovim.set_option(&format!("scrolloff={}", 3)).await;
453        cx.update_global(|store: &mut SettingsStore, cx| {
454            store.update_user_settings::<EditorSettings>(cx, |s| {
455                s.vertical_scroll_margin = Some(3.0)
456            });
457        });
458
459        // scroll down: ctrl-f
460        cx.simulate_shared_keystrokes("ctrl-f").await;
461        cx.shared_state().await.assert_matches();
462
463        cx.simulate_shared_keystrokes("ctrl-f").await;
464        cx.shared_state().await.assert_matches();
465
466        // scroll up: ctrl-b
467        cx.simulate_shared_keystrokes("ctrl-b").await;
468        cx.shared_state().await.assert_matches();
469
470        cx.simulate_shared_keystrokes("ctrl-b").await;
471        cx.shared_state().await.assert_matches();
472    }
473
474    #[gpui::test]
475    async fn test_scroll_beyond_last_line(cx: &mut gpui::TestAppContext) {
476        let mut cx = NeovimBackedTestContext::new(cx).await;
477
478        cx.set_scroll_height(10).await;
479
480        let content = "ˇ".to_owned() + &sample_text(26, 2, 'a');
481        cx.set_shared_state(&content).await;
482
483        cx.update_global(|store: &mut SettingsStore, cx| {
484            store.update_user_settings::<EditorSettings>(cx, |s| {
485                s.scroll_beyond_last_line = Some(ScrollBeyondLastLine::Off);
486                // s.vertical_scroll_margin = Some(0.);
487            });
488        });
489
490        // ctrl-d can reach the end and the cursor stays in the first column
491        cx.simulate_shared_keystrokes("shift-g k").await;
492        cx.shared_state().await.assert_matches();
493        cx.simulate_shared_keystrokes("ctrl-d").await;
494        cx.shared_state().await.assert_matches();
495
496        // ctrl-u from the last line
497        cx.simulate_shared_keystrokes("shift-g").await;
498        cx.shared_state().await.assert_matches();
499        cx.simulate_shared_keystrokes("ctrl-u").await;
500        cx.shared_state().await.assert_matches();
501    }
502
503    #[gpui::test]
504    async fn test_ctrl_y_e(cx: &mut gpui::TestAppContext) {
505        let mut cx = NeovimBackedTestContext::new(cx).await;
506
507        cx.set_scroll_height(10).await;
508
509        let content = "ˇ".to_owned() + &sample_text(26, 2, 'a');
510        cx.set_shared_state(&content).await;
511
512        for _ in 0..8 {
513            cx.simulate_shared_keystrokes("ctrl-e").await;
514            cx.shared_state().await.assert_matches();
515        }
516
517        for _ in 0..8 {
518            cx.simulate_shared_keystrokes("ctrl-y").await;
519            cx.shared_state().await.assert_matches();
520        }
521    }
522
523    #[gpui::test]
524    async fn test_scroll_jumps(cx: &mut gpui::TestAppContext) {
525        let mut cx = NeovimBackedTestContext::new(cx).await;
526
527        cx.set_scroll_height(20).await;
528
529        let content = "ˇ".to_owned() + &sample_text(52, 2, 'a');
530        cx.set_shared_state(&content).await;
531
532        cx.simulate_shared_keystrokes("shift-g g g").await;
533        cx.simulate_shared_keystrokes("ctrl-d ctrl-d ctrl-o").await;
534        cx.shared_state().await.assert_matches();
535        cx.simulate_shared_keystrokes("ctrl-o").await;
536        cx.shared_state().await.assert_matches();
537    }
538
539    #[gpui::test]
540    async fn test_horizontal_scroll(cx: &mut gpui::TestAppContext) {
541        let mut cx = NeovimBackedTestContext::new(cx).await;
542
543        cx.set_scroll_height(20).await;
544        cx.set_shared_wrap(12).await;
545        cx.set_neovim_option("nowrap").await;
546
547        let content = "ˇ01234567890123456789";
548        cx.set_shared_state(&content).await;
549
550        cx.simulate_shared_keystrokes("z shift-l").await;
551        cx.shared_state().await.assert_eq("012345ˇ67890123456789");
552
553        // At this point, `z h` should not move the cursor as it should still be
554        // visible within the 12 column width.
555        cx.simulate_shared_keystrokes("z h").await;
556        cx.shared_state().await.assert_eq("012345ˇ67890123456789");
557
558        let content = "ˇ01234567890123456789";
559        cx.set_shared_state(&content).await;
560
561        cx.simulate_shared_keystrokes("z l").await;
562        cx.shared_state().await.assert_eq("0ˇ1234567890123456789");
563    }
564}