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_heads_with(|map, head, goal| {
71 let top = top_anchor.to_display_point(map);
72 let min_row = top.row() + VERTICAL_SCROLL_MARGIN as u32;
73 let max_row = top.row() + visible_rows - VERTICAL_SCROLL_MARGIN as u32 - 1;
74
75 let new_head = if head.row() < min_row {
76 map.clip_point(DisplayPoint::new(min_row, head.column()), Bias::Left)
77 } else if head.row() > max_row {
78 map.clip_point(DisplayPoint::new(max_row, head.column()), Bias::Left)
79 } else {
80 head
81 };
82 (new_head, goal)
83 })
84 });
85 }
86}
87
88#[cfg(test)]
89mod test {
90 use crate::{state::Mode, test::VimTestContext};
91 use gpui::geometry::vector::vec2f;
92 use indoc::indoc;
93
94 #[gpui::test]
95 async fn test_scroll(cx: &mut gpui::TestAppContext) {
96 let mut cx = VimTestContext::new(cx, true).await;
97
98 cx.set_state(indoc! {"ˇa\nb\nc\nd\ne\n"}, Mode::Normal);
99
100 cx.update_editor(|editor, cx| {
101 assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 0.))
102 });
103 cx.simulate_keystrokes(["ctrl-e"]);
104 cx.update_editor(|editor, cx| {
105 assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 1.))
106 });
107 cx.simulate_keystrokes(["2", "ctrl-e"]);
108 cx.update_editor(|editor, cx| {
109 assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 3.))
110 });
111 cx.simulate_keystrokes(["ctrl-y"]);
112 cx.update_editor(|editor, cx| {
113 assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 2.))
114 });
115 }
116}