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