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