scroll.rs

  1use crate::Vim;
  2use editor::{
  3    display_map::ToDisplayPoint,
  4    scroll::{scroll_amount::ScrollAmount, VERTICAL_SCROLL_MARGIN},
  5    DisplayPoint, Editor,
  6};
  7use gpui::{actions, AppContext, ViewContext};
  8use language::Bias;
  9use workspace::Workspace;
 10
 11actions!(
 12    vim,
 13    [LineUp, LineDown, ScrollUp, ScrollDown, PageUp, PageDown,]
 14);
 15
 16pub fn init(cx: &mut AppContext) {
 17    cx.add_action(|_: &mut Workspace, _: &LineDown, cx| {
 18        scroll(cx, |c| ScrollAmount::Line(c.unwrap_or(1.)))
 19    });
 20    cx.add_action(|_: &mut Workspace, _: &LineUp, cx| {
 21        scroll(cx, |c| ScrollAmount::Line(-c.unwrap_or(1.)))
 22    });
 23    cx.add_action(|_: &mut Workspace, _: &PageDown, cx| {
 24        scroll(cx, |c| ScrollAmount::Page(c.unwrap_or(1.)))
 25    });
 26    cx.add_action(|_: &mut Workspace, _: &PageUp, cx| {
 27        scroll(cx, |c| ScrollAmount::Page(-c.unwrap_or(1.)))
 28    });
 29    cx.add_action(|_: &mut Workspace, _: &ScrollDown, cx| {
 30        scroll(cx, |c| {
 31            if let Some(c) = c {
 32                ScrollAmount::Line(c)
 33            } else {
 34                ScrollAmount::Page(0.5)
 35            }
 36        })
 37    });
 38    cx.add_action(|_: &mut Workspace, _: &ScrollUp, cx| {
 39        scroll(cx, |c| {
 40            if let Some(c) = c {
 41                ScrollAmount::Line(-c)
 42            } else {
 43                ScrollAmount::Page(-0.5)
 44            }
 45        })
 46    });
 47}
 48
 49fn scroll(cx: &mut ViewContext<Workspace>, by: fn(c: Option<f32>) -> ScrollAmount) {
 50    Vim::update(cx, |vim, cx| {
 51        let amount = by(vim.pop_number_operator(cx).map(|c| c as f32));
 52        vim.update_active_editor(cx, |editor, cx| scroll_editor(editor, &amount, cx));
 53    })
 54}
 55
 56fn scroll_editor(editor: &mut Editor, amount: &ScrollAmount, cx: &mut ViewContext<Editor>) {
 57    let should_move_cursor = editor.newest_selection_on_screen(cx).is_eq();
 58
 59    editor.scroll_screen(amount, cx);
 60    if should_move_cursor {
 61        let visible_rows = if let Some(visible_rows) = editor.visible_line_count() {
 62            visible_rows as u32
 63        } else {
 64            return;
 65        };
 66
 67        let top_anchor = editor.scroll_manager.anchor().anchor;
 68
 69        editor.change_selections(None, cx, |s| {
 70            s.move_with(|map, selection| {
 71                let head = selection.head();
 72                let top = top_anchor.to_display_point(map);
 73                let min_row = top.row() + VERTICAL_SCROLL_MARGIN as u32;
 74                let max_row = top.row() + visible_rows - VERTICAL_SCROLL_MARGIN as u32 - 1;
 75
 76                let new_head = if head.row() < min_row {
 77                    map.clip_point(DisplayPoint::new(min_row, head.column()), Bias::Left)
 78                } else if head.row() > max_row {
 79                    map.clip_point(DisplayPoint::new(max_row, head.column()), Bias::Left)
 80                } else {
 81                    head
 82                };
 83                if selection.is_empty() {
 84                    selection.collapse_to(new_head, selection.goal)
 85                } else {
 86                    selection.set_head(new_head, selection.goal)
 87                };
 88            })
 89        });
 90    }
 91}
 92
 93#[cfg(test)]
 94mod test {
 95    use crate::{state::Mode, test::VimTestContext};
 96    use gpui::geometry::vector::vec2f;
 97    use indoc::indoc;
 98    use language::Point;
 99
100    #[gpui::test]
101    async fn test_scroll(cx: &mut gpui::TestAppContext) {
102        let mut cx = VimTestContext::new(cx, true).await;
103
104        let window = cx.window;
105        let line_height =
106            cx.editor(|editor, cx| editor.style(cx).text.line_height(cx.font_cache()));
107        window.simulate_resize(vec2f(1000., 8.0 * line_height - 1.0), &mut cx);
108
109        cx.set_state(
110            indoc!(
111                "Λ‡one
112                two
113                three
114                four
115                five
116                six
117                seven
118                eight
119                nine
120                ten
121                eleven
122                twelve
123            "
124            ),
125            Mode::Normal,
126        );
127
128        cx.update_editor(|editor, cx| {
129            assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 0.))
130        });
131        cx.simulate_keystrokes(["ctrl-e"]);
132        cx.update_editor(|editor, cx| {
133            assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 1.))
134        });
135        cx.simulate_keystrokes(["2", "ctrl-e"]);
136        cx.update_editor(|editor, cx| {
137            assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 3.))
138        });
139        cx.simulate_keystrokes(["ctrl-y"]);
140        cx.update_editor(|editor, cx| {
141            assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 2.))
142        });
143
144        // does not select in normal mode
145        cx.simulate_keystrokes(["g", "g"]);
146        cx.update_editor(|editor, cx| {
147            assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 0.))
148        });
149        cx.simulate_keystrokes(["ctrl-d"]);
150        cx.update_editor(|editor, cx| {
151            assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 2.0));
152            assert_eq!(
153                editor.selections.newest(cx).range(),
154                Point::new(5, 0)..Point::new(5, 0)
155            )
156        });
157
158        // does select in visual mode
159        cx.simulate_keystrokes(["g", "g"]);
160        cx.update_editor(|editor, cx| {
161            assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 0.))
162        });
163        cx.simulate_keystrokes(["v", "ctrl-d"]);
164        cx.update_editor(|editor, cx| {
165            assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 2.0));
166            assert_eq!(
167                editor.selections.newest(cx).range(),
168                Point::new(0, 0)..Point::new(5, 1)
169            )
170        });
171    }
172}