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, false, |c| ScrollAmount::Line(c.unwrap_or(1.)))
19 });
20 cx.add_action(|_: &mut Workspace, _: &LineUp, cx| {
21 scroll(cx, false, |c| ScrollAmount::Line(-c.unwrap_or(1.)))
22 });
23 cx.add_action(|_: &mut Workspace, _: &PageDown, cx| {
24 scroll(cx, false, |c| ScrollAmount::Page(c.unwrap_or(1.)))
25 });
26 cx.add_action(|_: &mut Workspace, _: &PageUp, cx| {
27 scroll(cx, false, |c| ScrollAmount::Page(-c.unwrap_or(1.)))
28 });
29 cx.add_action(|_: &mut Workspace, _: &ScrollDown, cx| {
30 scroll(cx, true, |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, true, |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(
50 cx: &mut ViewContext<Workspace>,
51 move_cursor: bool,
52 by: fn(c: Option<f32>) -> ScrollAmount,
53) {
54 Vim::update(cx, |vim, cx| {
55 let amount = by(vim.take_count(cx).map(|c| c as f32));
56 vim.update_active_editor(cx, |editor, cx| {
57 scroll_editor(editor, move_cursor, &amount, cx)
58 });
59 })
60}
61
62fn scroll_editor(
63 editor: &mut Editor,
64 preserve_cursor_position: bool,
65 amount: &ScrollAmount,
66 cx: &mut ViewContext<Editor>,
67) {
68 let should_move_cursor = editor.newest_selection_on_screen(cx).is_eq();
69 let old_top_anchor = editor.scroll_manager.anchor().anchor;
70
71 editor.scroll_screen(amount, cx);
72 if should_move_cursor {
73 let visible_rows = if let Some(visible_rows) = editor.visible_line_count() {
74 visible_rows as u32
75 } else {
76 return;
77 };
78
79 let top_anchor = editor.scroll_manager.anchor().anchor;
80
81 editor.change_selections(None, cx, |s| {
82 s.move_with(|map, selection| {
83 let mut head = selection.head();
84 let top = top_anchor.to_display_point(map);
85
86 if preserve_cursor_position {
87 let old_top = old_top_anchor.to_display_point(map);
88 let new_row = top.row() + selection.head().row() - old_top.row();
89 head = map.clip_point(DisplayPoint::new(new_row, head.column()), Bias::Left)
90 }
91 let min_row = top.row() + VERTICAL_SCROLL_MARGIN as u32;
92 let max_row = top.row() + visible_rows - VERTICAL_SCROLL_MARGIN as u32 - 1;
93
94 let new_head = if head.row() < min_row {
95 map.clip_point(DisplayPoint::new(min_row, head.column()), Bias::Left)
96 } else if head.row() > max_row {
97 map.clip_point(DisplayPoint::new(max_row, head.column()), Bias::Left)
98 } else {
99 head
100 };
101 if selection.is_empty() {
102 selection.collapse_to(new_head, selection.goal)
103 } else {
104 selection.set_head(new_head, selection.goal)
105 };
106 })
107 });
108 }
109}
110
111#[cfg(test)]
112mod test {
113 use crate::{
114 state::Mode,
115 test::{NeovimBackedTestContext, VimTestContext},
116 };
117 use gpui::geometry::vector::vec2f;
118 use indoc::indoc;
119 use language::Point;
120
121 #[gpui::test]
122 async fn test_scroll(cx: &mut gpui::TestAppContext) {
123 let mut cx = VimTestContext::new(cx, true).await;
124
125 let window = cx.window;
126 let line_height =
127 cx.editor(|editor, cx| editor.style(cx).text.line_height(cx.font_cache()));
128 window.simulate_resize(vec2f(1000., 8.0 * line_height - 1.0), &mut cx);
129
130 cx.set_state(
131 indoc!(
132 "Λone
133 two
134 three
135 four
136 five
137 six
138 seven
139 eight
140 nine
141 ten
142 eleven
143 twelve
144 "
145 ),
146 Mode::Normal,
147 );
148
149 cx.update_editor(|editor, cx| {
150 assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 0.))
151 });
152 cx.simulate_keystrokes(["ctrl-e"]);
153 cx.update_editor(|editor, cx| {
154 assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 1.))
155 });
156 cx.simulate_keystrokes(["2", "ctrl-e"]);
157 cx.update_editor(|editor, cx| {
158 assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 3.))
159 });
160 cx.simulate_keystrokes(["ctrl-y"]);
161 cx.update_editor(|editor, cx| {
162 assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 2.))
163 });
164
165 // does not select in normal mode
166 cx.simulate_keystrokes(["g", "g"]);
167 cx.update_editor(|editor, cx| {
168 assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 0.))
169 });
170 cx.simulate_keystrokes(["ctrl-d"]);
171 cx.update_editor(|editor, cx| {
172 assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 3.0));
173 assert_eq!(
174 editor.selections.newest(cx).range(),
175 Point::new(6, 0)..Point::new(6, 0)
176 )
177 });
178
179 // does select in visual mode
180 cx.simulate_keystrokes(["g", "g"]);
181 cx.update_editor(|editor, cx| {
182 assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 0.))
183 });
184 cx.simulate_keystrokes(["v", "ctrl-d"]);
185 cx.update_editor(|editor, cx| {
186 assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 3.0));
187 assert_eq!(
188 editor.selections.newest(cx).range(),
189 Point::new(0, 0)..Point::new(6, 1)
190 )
191 });
192 }
193 #[gpui::test]
194 async fn test_ctrl_d_u(cx: &mut gpui::TestAppContext) {
195 let mut cx = NeovimBackedTestContext::new(cx).await;
196
197 cx.set_scroll_height(10).await;
198
199 pub fn sample_text(rows: usize, cols: usize, start_char: char) -> String {
200 let mut text = String::new();
201 for row in 0..rows {
202 let c: char = (start_char as u32 + row as u32) as u8 as char;
203 let mut line = c.to_string().repeat(cols);
204 if row < rows - 1 {
205 line.push('\n');
206 }
207 text += &line;
208 }
209 text
210 }
211 let content = "Λ".to_owned() + &sample_text(26, 2, 'a');
212 cx.set_shared_state(&content).await;
213
214 // skip over the scrolloff at the top
215 // test ctrl-d
216 cx.simulate_shared_keystrokes(["4", "j", "ctrl-d"]).await;
217 cx.assert_state_matches().await;
218 cx.simulate_shared_keystrokes(["ctrl-d"]).await;
219 cx.assert_state_matches().await;
220 cx.simulate_shared_keystrokes(["g", "g", "ctrl-d"]).await;
221 cx.assert_state_matches().await;
222
223 // test ctrl-u
224 cx.simulate_shared_keystrokes(["ctrl-u"]).await;
225 cx.assert_state_matches().await;
226 cx.simulate_shared_keystrokes(["ctrl-d", "ctrl-d", "4", "j", "ctrl-u", "ctrl-u"])
227 .await;
228 cx.assert_state_matches().await;
229 }
230}