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