scroll.rs

  1use crate::{Vim, state::Mode};
  2use editor::{
  3    DisplayPoint, Editor, EditorSettings, SelectionEffects, display_map::DisplayRow,
  4    scroll::ScrollAmount,
  5};
  6use gpui::{Context, Window, actions};
  7use language::Bias;
  8use settings::Settings;
  9use text::SelectionGoal;
 10
 11actions!(
 12    vim,
 13    [
 14        /// Scrolls up by one line.
 15        LineUp,
 16        /// Scrolls down by one line.
 17        LineDown,
 18        /// Scrolls right by one column.
 19        ColumnRight,
 20        /// Scrolls left by one column.
 21        ColumnLeft,
 22        /// Scrolls up by half a page.
 23        ScrollUp,
 24        /// Scrolls down by half a page.
 25        ScrollDown,
 26        /// Scrolls up by one page.
 27        PageUp,
 28        /// Scrolls down by one page.
 29        PageDown,
 30        /// Scrolls right by half a page's width.
 31        HalfPageRight,
 32        /// Scrolls left by half a page's width.
 33        HalfPageLeft,
 34    ]
 35);
 36
 37pub fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
 38    Vim::action(editor, cx, |vim, _: &LineDown, window, cx| {
 39        vim.scroll(false, window, cx, |c| ScrollAmount::Line(c.unwrap_or(1.)))
 40    });
 41    Vim::action(editor, cx, |vim, _: &LineUp, window, cx| {
 42        vim.scroll(false, window, cx, |c| ScrollAmount::Line(-c.unwrap_or(1.)))
 43    });
 44    Vim::action(editor, cx, |vim, _: &ColumnRight, window, cx| {
 45        vim.scroll(false, window, cx, |c| ScrollAmount::Column(c.unwrap_or(1.)))
 46    });
 47    Vim::action(editor, cx, |vim, _: &ColumnLeft, window, cx| {
 48        vim.scroll(false, window, cx, |c| {
 49            ScrollAmount::Column(-c.unwrap_or(1.))
 50        })
 51    });
 52    Vim::action(editor, cx, |vim, _: &PageDown, window, cx| {
 53        vim.scroll(false, window, cx, |c| ScrollAmount::Page(c.unwrap_or(1.)))
 54    });
 55    Vim::action(editor, cx, |vim, _: &PageUp, window, cx| {
 56        vim.scroll(false, window, cx, |c| ScrollAmount::Page(-c.unwrap_or(1.)))
 57    });
 58    Vim::action(editor, cx, |vim, _: &HalfPageRight, window, cx| {
 59        vim.scroll(false, window, cx, |c| {
 60            ScrollAmount::PageWidth(c.unwrap_or(0.5))
 61        })
 62    });
 63    Vim::action(editor, cx, |vim, _: &HalfPageLeft, window, cx| {
 64        vim.scroll(false, window, cx, |c| {
 65            ScrollAmount::PageWidth(-c.unwrap_or(0.5))
 66        })
 67    });
 68    Vim::action(editor, cx, |vim, _: &ScrollDown, window, cx| {
 69        vim.scroll(true, window, cx, |c| {
 70            if let Some(c) = c {
 71                ScrollAmount::Line(c)
 72            } else {
 73                ScrollAmount::Page(0.5)
 74            }
 75        })
 76    });
 77    Vim::action(editor, cx, |vim, _: &ScrollUp, window, cx| {
 78        vim.scroll(true, window, cx, |c| {
 79            if let Some(c) = c {
 80                ScrollAmount::Line(-c)
 81            } else {
 82                ScrollAmount::Page(-0.5)
 83            }
 84        })
 85    });
 86}
 87
 88impl Vim {
 89    fn scroll(
 90        &mut self,
 91        preserve_cursor_position: bool,
 92        window: &mut Window,
 93        cx: &mut Context<Self>,
 94        by: fn(c: Option<f32>) -> ScrollAmount,
 95    ) {
 96        let amount = by(Vim::take_count(cx).map(|c| c as f32));
 97        Vim::take_forced_motion(cx);
 98        self.exit_temporary_normal(window, cx);
 99        self.scroll_editor(preserve_cursor_position, amount, window, cx);
100    }
101
102    fn scroll_editor(
103        &mut self,
104        preserve_cursor_position: bool,
105        amount: ScrollAmount,
106        window: &mut Window,
107        cx: &mut Context<Vim>,
108    ) {
109        self.update_editor(cx, |vim, editor, cx| {
110            let should_move_cursor = editor.newest_selection_on_screen(cx).is_eq();
111            let display_snapshot = editor.display_map.update(cx, |map, cx| map.snapshot(cx));
112            let old_top = editor
113                .scroll_manager
114                .scroll_top_display_point(&display_snapshot, cx);
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) as f32)
125                    } else {
126                        ScrollAmount::Line((amount.lines(visible_line_count) - 1.0) as f32)
127                    }
128                }
129                _ => amount,
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 display_snapshot = editor.display_map.update(cx, |map, cx| map.snapshot(cx));
146            let top = editor
147                .scroll_manager
148                .scroll_top_display_point(&display_snapshot, cx);
149            let vertical_scroll_margin = EditorSettings::get_global(cx).vertical_scroll_margin;
150
151            let mut move_cursor = |map: &editor::display_map::DisplaySnapshot,
152                                   mut head: DisplayPoint,
153                                   goal: SelectionGoal| {
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 max_point = map.max_point();
160                let starting_column = head.column();
161
162                let vertical_scroll_margin =
163                    (vertical_scroll_margin as u32).min(visible_line_count as u32 / 2);
164
165                if preserve_cursor_position {
166                    let new_row =
167                        if old_top.row() == top.row() {
168                            DisplayRow(
169                                head.row()
170                                    .0
171                                    .saturating_add_signed(amount.lines(visible_line_count) as i32),
172                            )
173                        } else {
174                            DisplayRow(top.row().0.saturating_add_signed(
175                                head.row().0 as i32 - old_top.row().0 as i32,
176                            ))
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.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 = if new_row <= map.max_point().row() {
234                    map.line_len(new_row)
235                } else {
236                    0
237                };
238                let max_column = match min_column + visible_column_count as u32 {
239                    max_column if max_column >= max_row_column => max_row_column,
240                    max_column => max_column,
241                };
242
243                // Ensure that the cursor's column stays within the visible
244                // area, otherwise clip it at either the left or right edge of
245                // the visible area.
246                let new_column = match (min_column, max_column) {
247                    (min_column, _) if starting_column < min_column => min_column,
248                    (_, max_column) if starting_column > max_column => max_column,
249                    _ => starting_column,
250                };
251
252                let new_head = map.clip_point(DisplayPoint::new(new_row, new_column), Bias::Left);
253                let goal = match amount {
254                    ScrollAmount::Column(_) | ScrollAmount::PageWidth(_) => SelectionGoal::None,
255                    _ => goal,
256                };
257
258                Some((new_head, goal))
259            };
260
261            if vim.mode == Mode::VisualBlock {
262                vim.visual_block_motion(true, editor, window, cx, &mut move_cursor);
263            } else {
264                editor.change_selections(
265                    SelectionEffects::no_scroll().nav_history(false),
266                    window,
267                    cx,
268                    |s| {
269                        s.move_with(&mut |map, selection| {
270                            if let Some((new_head, goal)) =
271                                move_cursor(map, selection.head(), selection.goal)
272                            {
273                                if selection.is_empty() || !vim.mode.is_visual() {
274                                    selection.collapse_to(new_head, goal)
275                                } else {
276                                    selection.set_head(new_head, goal)
277                                }
278                            }
279                        })
280                    },
281                );
282            }
283        });
284    }
285}
286
287#[cfg(test)]
288mod test {
289    use crate::{
290        state::Mode,
291        test::{NeovimBackedTestContext, VimTestContext},
292    };
293    use editor::ScrollBeyondLastLine;
294    use gpui::{AppContext as _, point, px, size};
295    use indoc::indoc;
296    use language::Point;
297    use settings::SettingsStore;
298
299    pub fn sample_text(rows: usize, cols: usize, start_char: char) -> String {
300        let mut text = String::new();
301        for row in 0..rows {
302            let c: char = (start_char as u32 + row as u32) as u8 as char;
303            let mut line = c.to_string().repeat(cols);
304            if row < rows - 1 {
305                line.push('\n');
306            }
307            text += &line;
308        }
309        text
310    }
311
312    #[gpui::test]
313    async fn test_scroll(cx: &mut gpui::TestAppContext) {
314        let mut cx = VimTestContext::new(cx, true).await;
315
316        let (line_height, visible_line_count) = cx.update_editor(|editor, window, cx| {
317            (
318                editor
319                    .style(cx)
320                    .text
321                    .line_height_in_pixels(window.rem_size()),
322                editor.visible_line_count().unwrap(),
323            )
324        });
325
326        let window = cx.window;
327        let margin = cx
328            .update_window(window, |_, window, _cx| {
329                window.viewport_size().height - line_height * visible_line_count as f32
330            })
331            .unwrap();
332        cx.simulate_window_resize(
333            cx.window,
334            size(px(1000.), margin + 8. * line_height - px(1.0)),
335        );
336
337        cx.set_state(
338            indoc!(
339                "ˇone
340                two
341                three
342                four
343                five
344                six
345                seven
346                eight
347                nine
348                ten
349                eleven
350                twelve
351            "
352            ),
353            Mode::Normal,
354        );
355
356        cx.update_editor(|editor, window, cx| {
357            assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 0.))
358        });
359        cx.simulate_keystrokes("ctrl-e");
360        cx.update_editor(|editor, window, cx| {
361            assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 1.))
362        });
363        cx.simulate_keystrokes("2 ctrl-e");
364        cx.update_editor(|editor, window, cx| {
365            assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 3.))
366        });
367        cx.simulate_keystrokes("ctrl-y");
368        cx.update_editor(|editor, window, cx| {
369            assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 2.))
370        });
371
372        // does not select in normal mode
373        cx.simulate_keystrokes("g g");
374        cx.update_editor(|editor, window, cx| {
375            assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 0.))
376        });
377        cx.simulate_keystrokes("ctrl-d");
378        cx.update_editor(|editor, window, cx| {
379            assert_eq!(
380                editor.snapshot(window, cx).scroll_position(),
381                point(0., 3.0)
382            );
383            assert_eq!(
384                editor
385                    .selections
386                    .newest(&editor.display_snapshot(cx))
387                    .range(),
388                Point::new(6, 0)..Point::new(6, 0)
389            )
390        });
391
392        // does select in visual mode
393        cx.simulate_keystrokes("g g");
394        cx.update_editor(|editor, window, cx| {
395            assert_eq!(editor.snapshot(window, cx).scroll_position(), point(0., 0.))
396        });
397        cx.simulate_keystrokes("v ctrl-d");
398        cx.update_editor(|editor, window, cx| {
399            assert_eq!(
400                editor.snapshot(window, cx).scroll_position(),
401                point(0., 3.0)
402            );
403            assert_eq!(
404                editor
405                    .selections
406                    .newest(&editor.display_snapshot(cx))
407                    .range(),
408                Point::new(0, 0)..Point::new(6, 1)
409            )
410        });
411    }
412
413    #[gpui::test]
414    async fn test_ctrl_d_u(cx: &mut gpui::TestAppContext) {
415        let mut cx = NeovimBackedTestContext::new(cx).await;
416
417        cx.set_scroll_height(10).await;
418
419        let content = "ˇ".to_owned() + &sample_text(26, 2, 'a');
420        cx.set_shared_state(&content).await;
421
422        // skip over the scrolloff at the top
423        // test ctrl-d
424        cx.simulate_shared_keystrokes("4 j ctrl-d").await;
425        cx.shared_state().await.assert_matches();
426        cx.simulate_shared_keystrokes("ctrl-d").await;
427        cx.shared_state().await.assert_matches();
428        cx.simulate_shared_keystrokes("g g ctrl-d").await;
429        cx.shared_state().await.assert_matches();
430
431        // test ctrl-u
432        cx.simulate_shared_keystrokes("ctrl-u").await;
433        cx.shared_state().await.assert_matches();
434        cx.simulate_shared_keystrokes("ctrl-d ctrl-d 4 j ctrl-u ctrl-u")
435            .await;
436        cx.shared_state().await.assert_matches();
437
438        // test returning to top
439        cx.simulate_shared_keystrokes("g g ctrl-d ctrl-u ctrl-u")
440            .await;
441        cx.shared_state().await.assert_matches();
442    }
443
444    #[gpui::test]
445    async fn test_ctrl_f_b(cx: &mut gpui::TestAppContext) {
446        let mut cx = NeovimBackedTestContext::new(cx).await;
447
448        let visible_lines = 10;
449        cx.set_scroll_height(visible_lines).await;
450
451        // First test without vertical scroll margin
452        cx.neovim.set_option(&format!("scrolloff={}", 0)).await;
453        cx.update_global(|store: &mut SettingsStore, cx| {
454            store.update_user_settings(cx, |s| s.editor.vertical_scroll_margin = Some(0.0));
455        });
456
457        let content = "ˇ".to_owned() + &sample_text(26, 2, 'a');
458        cx.set_shared_state(&content).await;
459
460        // scroll down: ctrl-f
461        cx.simulate_shared_keystrokes("ctrl-f").await;
462        cx.shared_state().await.assert_matches();
463
464        cx.simulate_shared_keystrokes("ctrl-f").await;
465        cx.shared_state().await.assert_matches();
466
467        // scroll up: ctrl-b
468        cx.simulate_shared_keystrokes("ctrl-b").await;
469        cx.shared_state().await.assert_matches();
470
471        cx.simulate_shared_keystrokes("ctrl-b").await;
472        cx.shared_state().await.assert_matches();
473
474        // Now go back to start of file, and test with vertical scroll margin
475        cx.simulate_shared_keystrokes("g g").await;
476        cx.shared_state().await.assert_matches();
477
478        cx.neovim.set_option(&format!("scrolloff={}", 3)).await;
479        cx.update_global(|store: &mut SettingsStore, cx| {
480            store.update_user_settings(cx, |s| s.editor.vertical_scroll_margin = Some(3.0));
481        });
482
483        // scroll down: ctrl-f
484        cx.simulate_shared_keystrokes("ctrl-f").await;
485        cx.shared_state().await.assert_matches();
486
487        cx.simulate_shared_keystrokes("ctrl-f").await;
488        cx.shared_state().await.assert_matches();
489
490        // scroll up: ctrl-b
491        cx.simulate_shared_keystrokes("ctrl-b").await;
492        cx.shared_state().await.assert_matches();
493
494        cx.simulate_shared_keystrokes("ctrl-b").await;
495        cx.shared_state().await.assert_matches();
496    }
497
498    #[gpui::test]
499    async fn test_scroll_beyond_last_line(cx: &mut gpui::TestAppContext) {
500        let mut cx = NeovimBackedTestContext::new(cx).await;
501
502        cx.set_scroll_height(10).await;
503
504        let content = "ˇ".to_owned() + &sample_text(26, 2, 'a');
505        cx.set_shared_state(&content).await;
506
507        cx.update_global(|store: &mut SettingsStore, cx| {
508            store.update_user_settings(cx, |s| {
509                s.editor.scroll_beyond_last_line = Some(ScrollBeyondLastLine::Off);
510            });
511        });
512
513        // ctrl-d can reach the end and the cursor stays in the first column
514        cx.simulate_shared_keystrokes("shift-g k").await;
515        cx.shared_state().await.assert_matches();
516        cx.simulate_shared_keystrokes("ctrl-d").await;
517        cx.shared_state().await.assert_matches();
518
519        // ctrl-u from the last line
520        cx.simulate_shared_keystrokes("shift-g").await;
521        cx.shared_state().await.assert_matches();
522        cx.simulate_shared_keystrokes("ctrl-u").await;
523        cx.shared_state().await.assert_matches();
524    }
525
526    #[gpui::test]
527    async fn test_ctrl_y_e(cx: &mut gpui::TestAppContext) {
528        let mut cx = NeovimBackedTestContext::new(cx).await;
529
530        cx.set_scroll_height(10).await;
531
532        let content = "ˇ".to_owned() + &sample_text(26, 2, 'a');
533        cx.set_shared_state(&content).await;
534
535        for _ in 0..8 {
536            cx.simulate_shared_keystrokes("ctrl-e").await;
537            cx.shared_state().await.assert_matches();
538        }
539
540        for _ in 0..8 {
541            cx.simulate_shared_keystrokes("ctrl-y").await;
542            cx.shared_state().await.assert_matches();
543        }
544    }
545
546    #[gpui::test]
547    async fn test_scroll_jumps(cx: &mut gpui::TestAppContext) {
548        let mut cx = NeovimBackedTestContext::new(cx).await;
549
550        cx.set_scroll_height(20).await;
551
552        let content = "ˇ".to_owned() + &sample_text(52, 2, 'a');
553        cx.set_shared_state(&content).await;
554
555        cx.simulate_shared_keystrokes("shift-g g g").await;
556        cx.simulate_shared_keystrokes("ctrl-d ctrl-d ctrl-o").await;
557        cx.shared_state().await.assert_matches();
558        cx.simulate_shared_keystrokes("ctrl-o").await;
559        cx.shared_state().await.assert_matches();
560    }
561
562    #[gpui::test]
563    async fn test_horizontal_scroll(cx: &mut gpui::TestAppContext) {
564        let mut cx = NeovimBackedTestContext::new(cx).await;
565
566        cx.set_scroll_height(20).await;
567        cx.set_shared_wrap(12).await;
568        cx.set_neovim_option("nowrap").await;
569
570        let content = "ˇ01234567890123456789";
571        cx.set_shared_state(content).await;
572
573        cx.simulate_shared_keystrokes("z shift-l").await;
574        cx.shared_state().await.assert_eq("012345ˇ67890123456789");
575
576        // At this point, `z h` should not move the cursor as it should still be
577        // visible within the 12 column width.
578        cx.simulate_shared_keystrokes("z h").await;
579        cx.shared_state().await.assert_eq("012345ˇ67890123456789");
580
581        let content = "ˇ01234567890123456789";
582        cx.set_shared_state(content).await;
583
584        cx.simulate_shared_keystrokes("z l").await;
585        cx.shared_state().await.assert_eq("0ˇ1234567890123456789");
586    }
587}