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 if editor.scroll_hover(amount, cx) {
73 return;
74 }
75
76 editor.scroll_screen(amount, cx);
77 if !should_move_cursor {
78 return;
79 }
80
81 let visible_line_count = if let Some(visible_line_count) = editor.visible_line_count() {
82 visible_line_count
83 } else {
84 return;
85 };
86
87 let top_anchor = editor.scroll_manager.anchor().anchor;
88 let vertical_scroll_margin = EditorSettings::get_global(cx).vertical_scroll_margin;
89
90 editor.change_selections(None, cx, |s| {
91 s.move_with(|map, selection| {
92 let mut head = selection.head();
93 let top = top_anchor.to_display_point(map);
94
95 let vertical_scroll_margin =
96 (vertical_scroll_margin as u32).min(visible_line_count as u32 / 2);
97
98 if preserve_cursor_position {
99 let old_top = old_top_anchor.to_display_point(map);
100 let new_row = if old_top.row() == top.row() {
101 DisplayRow(
102 top.row()
103 .0
104 .saturating_add_signed(amount.lines(visible_line_count) as i32),
105 )
106 } else {
107 DisplayRow(top.row().0 + selection.head().row().0 - old_top.row().0)
108 };
109 head = map.clip_point(DisplayPoint::new(new_row, head.column()), Bias::Left)
110 }
111 let min_row = if top.row().0 == 0 {
112 DisplayRow(0)
113 } else {
114 DisplayRow(top.row().0 + vertical_scroll_margin)
115 };
116 let max_row = DisplayRow(
117 top.row().0
118 + (visible_line_count as u32)
119 .saturating_sub(vertical_scroll_margin)
120 .saturating_sub(1),
121 );
122
123 let new_head = if head.row() < min_row {
124 map.clip_point(DisplayPoint::new(min_row, head.column()), Bias::Left)
125 } else if head.row() > max_row {
126 map.clip_point(DisplayPoint::new(max_row, head.column()), Bias::Left)
127 } else {
128 head
129 };
130 if selection.is_empty() {
131 selection.collapse_to(new_head, selection.goal)
132 } else {
133 selection.set_head(new_head, selection.goal)
134 };
135 })
136 });
137}
138
139#[cfg(test)]
140mod test {
141 use crate::{
142 state::Mode,
143 test::{NeovimBackedTestContext, VimTestContext},
144 };
145 use gpui::{point, px, size, Context};
146 use indoc::indoc;
147 use language::Point;
148
149 #[gpui::test]
150 async fn test_scroll(cx: &mut gpui::TestAppContext) {
151 let mut cx = VimTestContext::new(cx, true).await;
152
153 let (line_height, visible_line_count) = cx.editor(|editor, cx| {
154 (
155 editor
156 .style()
157 .unwrap()
158 .text
159 .line_height_in_pixels(cx.rem_size()),
160 editor.visible_line_count().unwrap(),
161 )
162 });
163
164 let window = cx.window;
165 let margin = cx
166 .update_window(window, |_, cx| {
167 cx.viewport_size().height - line_height * visible_line_count
168 })
169 .unwrap();
170 cx.simulate_window_resize(
171 cx.window,
172 size(px(1000.), margin + 8. * line_height - px(1.0)),
173 );
174
175 cx.set_state(
176 indoc!(
177 "Λone
178 two
179 three
180 four
181 five
182 six
183 seven
184 eight
185 nine
186 ten
187 eleven
188 twelve
189 "
190 ),
191 Mode::Normal,
192 );
193
194 cx.update_editor(|editor, cx| {
195 assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 0.))
196 });
197 cx.simulate_keystrokes("ctrl-e");
198 cx.update_editor(|editor, cx| {
199 assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 1.))
200 });
201 cx.simulate_keystrokes("2 ctrl-e");
202 cx.update_editor(|editor, cx| {
203 assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 3.))
204 });
205 cx.simulate_keystrokes("ctrl-y");
206 cx.update_editor(|editor, cx| {
207 assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 2.))
208 });
209
210 // does not select in normal mode
211 cx.simulate_keystrokes("g g");
212 cx.update_editor(|editor, cx| {
213 assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 0.))
214 });
215 cx.simulate_keystrokes("ctrl-d");
216 cx.update_editor(|editor, cx| {
217 assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 3.0));
218 assert_eq!(
219 editor.selections.newest(cx).range(),
220 Point::new(6, 0)..Point::new(6, 0)
221 )
222 });
223
224 // does select in visual mode
225 cx.simulate_keystrokes("g g");
226 cx.update_editor(|editor, cx| {
227 assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 0.))
228 });
229 cx.simulate_keystrokes("v ctrl-d");
230 cx.update_editor(|editor, cx| {
231 assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 3.0));
232 assert_eq!(
233 editor.selections.newest(cx).range(),
234 Point::new(0, 0)..Point::new(6, 1)
235 )
236 });
237 }
238 #[gpui::test]
239 async fn test_ctrl_d_u(cx: &mut gpui::TestAppContext) {
240 let mut cx = NeovimBackedTestContext::new(cx).await;
241
242 cx.set_scroll_height(10).await;
243
244 pub fn sample_text(rows: usize, cols: usize, start_char: char) -> String {
245 let mut text = String::new();
246 for row in 0..rows {
247 let c: char = (start_char as u32 + row as u32) as u8 as char;
248 let mut line = c.to_string().repeat(cols);
249 if row < rows - 1 {
250 line.push('\n');
251 }
252 text += &line;
253 }
254 text
255 }
256 let content = "Λ".to_owned() + &sample_text(26, 2, 'a');
257 cx.set_shared_state(&content).await;
258
259 // skip over the scrolloff at the top
260 // test ctrl-d
261 cx.simulate_shared_keystrokes("4 j ctrl-d").await;
262 cx.shared_state().await.assert_matches();
263 cx.simulate_shared_keystrokes("ctrl-d").await;
264 cx.shared_state().await.assert_matches();
265 cx.simulate_shared_keystrokes("g g ctrl-d").await;
266 cx.shared_state().await.assert_matches();
267
268 // test ctrl-u
269 cx.simulate_shared_keystrokes("ctrl-u").await;
270 cx.shared_state().await.assert_matches();
271 cx.simulate_shared_keystrokes("ctrl-d ctrl-d 4 j ctrl-u ctrl-u")
272 .await;
273 cx.shared_state().await.assert_matches();
274
275 // test returning to top
276 cx.simulate_shared_keystrokes("g g ctrl-d ctrl-u ctrl-u")
277 .await;
278 cx.shared_state().await.assert_matches();
279 }
280}