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