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;
10
11actions!(
12 vim,
13 [LineUp, LineDown, ScrollUp, ScrollDown, PageUp, PageDown]
14);
15
16pub fn register(editor: &mut Editor, cx: &mut ViewContext<Vim>) {
17 Vim::action(editor, cx, |vim, _: &LineDown, cx| {
18 vim.scroll(false, cx, |c| ScrollAmount::Line(c.unwrap_or(1.)))
19 });
20 Vim::action(editor, cx, |vim, _: &LineUp, cx| {
21 vim.scroll(false, cx, |c| ScrollAmount::Line(-c.unwrap_or(1.)))
22 });
23 Vim::action(editor, cx, |vim, _: &PageDown, cx| {
24 vim.scroll(false, cx, |c| ScrollAmount::Page(c.unwrap_or(1.)))
25 });
26 Vim::action(editor, cx, |vim, _: &PageUp, cx| {
27 vim.scroll(false, cx, |c| ScrollAmount::Page(-c.unwrap_or(1.)))
28 });
29 Vim::action(editor, cx, |vim, _: &ScrollDown, cx| {
30 vim.scroll(true, cx, |c| {
31 if let Some(c) = c {
32 ScrollAmount::Line(c)
33 } else {
34 ScrollAmount::Page(0.5)
35 }
36 })
37 });
38 Vim::action(editor, cx, |vim, _: &ScrollUp, cx| {
39 vim.scroll(true, cx, |c| {
40 if let Some(c) = c {
41 ScrollAmount::Line(-c)
42 } else {
43 ScrollAmount::Page(-0.5)
44 }
45 })
46 });
47}
48
49impl Vim {
50 fn scroll(
51 &mut self,
52 move_cursor: bool,
53 cx: &mut ViewContext<Self>,
54 by: fn(c: Option<f32>) -> ScrollAmount,
55 ) {
56 let amount = by(self.take_count(cx).map(|c| c as f32));
57 self.update_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 let starting_column = head.column();
95
96 let vertical_scroll_margin =
97 (vertical_scroll_margin as u32).min(visible_line_count as u32 / 2);
98
99 if preserve_cursor_position {
100 let old_top = old_top_anchor.to_display_point(map);
101 let new_row = if old_top.row() == top.row() {
102 DisplayRow(
103 head.row()
104 .0
105 .saturating_add_signed(amount.lines(visible_line_count) as i32),
106 )
107 } else {
108 DisplayRow(top.row().0 + selection.head().row().0 - old_top.row().0)
109 };
110 head = map.clip_point(DisplayPoint::new(new_row, head.column()), Bias::Left)
111 }
112
113 let min_row = if top.row().0 == 0 {
114 DisplayRow(0)
115 } else {
116 DisplayRow(top.row().0 + vertical_scroll_margin)
117 };
118 let max_row = DisplayRow(map.max_point().row().0.max(top.row().0.saturating_add(
119 (visible_line_count as u32).saturating_sub(1 + vertical_scroll_margin),
120 )));
121
122 let new_row = if head.row() < min_row {
123 min_row
124 } else if head.row() > max_row {
125 max_row
126 } else {
127 head.row()
128 };
129 let new_head = map.clip_point(DisplayPoint::new(new_row, starting_column), Bias::Left);
130
131 if selection.is_empty() {
132 selection.collapse_to(new_head, selection.goal)
133 } else {
134 selection.set_head(new_head, selection.goal)
135 };
136 })
137 });
138}
139
140#[cfg(test)]
141mod test {
142 use crate::{
143 state::Mode,
144 test::{NeovimBackedTestContext, VimTestContext},
145 };
146 use editor::{EditorSettings, ScrollBeyondLastLine};
147 use gpui::{point, px, size, Context};
148 use indoc::indoc;
149 use language::Point;
150 use settings::SettingsStore;
151
152 pub fn sample_text(rows: usize, cols: usize, start_char: char) -> String {
153 let mut text = String::new();
154 for row in 0..rows {
155 let c: char = (start_char as u32 + row as u32) as u8 as char;
156 let mut line = c.to_string().repeat(cols);
157 if row < rows - 1 {
158 line.push('\n');
159 }
160 text += &line;
161 }
162 text
163 }
164
165 #[gpui::test]
166 async fn test_scroll(cx: &mut gpui::TestAppContext) {
167 let mut cx = VimTestContext::new(cx, true).await;
168
169 let (line_height, visible_line_count) = cx.editor(|editor, cx| {
170 (
171 editor
172 .style()
173 .unwrap()
174 .text
175 .line_height_in_pixels(cx.rem_size()),
176 editor.visible_line_count().unwrap(),
177 )
178 });
179
180 let window = cx.window;
181 let margin = cx
182 .update_window(window, |_, cx| {
183 cx.viewport_size().height - line_height * visible_line_count
184 })
185 .unwrap();
186 cx.simulate_window_resize(
187 cx.window,
188 size(px(1000.), margin + 8. * line_height - px(1.0)),
189 );
190
191 cx.set_state(
192 indoc!(
193 "Λone
194 two
195 three
196 four
197 five
198 six
199 seven
200 eight
201 nine
202 ten
203 eleven
204 twelve
205 "
206 ),
207 Mode::Normal,
208 );
209
210 cx.update_editor(|editor, cx| {
211 assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 0.))
212 });
213 cx.simulate_keystrokes("ctrl-e");
214 cx.update_editor(|editor, cx| {
215 assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 1.))
216 });
217 cx.simulate_keystrokes("2 ctrl-e");
218 cx.update_editor(|editor, cx| {
219 assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 3.))
220 });
221 cx.simulate_keystrokes("ctrl-y");
222 cx.update_editor(|editor, cx| {
223 assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 2.))
224 });
225
226 // does not select in normal mode
227 cx.simulate_keystrokes("g g");
228 cx.update_editor(|editor, cx| {
229 assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 0.))
230 });
231 cx.simulate_keystrokes("ctrl-d");
232 cx.update_editor(|editor, cx| {
233 assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 3.0));
234 assert_eq!(
235 editor.selections.newest(cx).range(),
236 Point::new(6, 0)..Point::new(6, 0)
237 )
238 });
239
240 // does select in visual mode
241 cx.simulate_keystrokes("g g");
242 cx.update_editor(|editor, cx| {
243 assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 0.))
244 });
245 cx.simulate_keystrokes("v ctrl-d");
246 cx.update_editor(|editor, cx| {
247 assert_eq!(editor.snapshot(cx).scroll_position(), point(0., 3.0));
248 assert_eq!(
249 editor.selections.newest(cx).range(),
250 Point::new(0, 0)..Point::new(6, 1)
251 )
252 });
253 }
254 #[gpui::test]
255 async fn test_ctrl_d_u(cx: &mut gpui::TestAppContext) {
256 let mut cx = NeovimBackedTestContext::new(cx).await;
257
258 cx.set_scroll_height(10).await;
259
260 let content = "Λ".to_owned() + &sample_text(26, 2, 'a');
261 cx.set_shared_state(&content).await;
262
263 // skip over the scrolloff at the top
264 // test ctrl-d
265 cx.simulate_shared_keystrokes("4 j ctrl-d").await;
266 cx.shared_state().await.assert_matches();
267 cx.simulate_shared_keystrokes("ctrl-d").await;
268 cx.shared_state().await.assert_matches();
269 cx.simulate_shared_keystrokes("g g ctrl-d").await;
270 cx.shared_state().await.assert_matches();
271
272 // test ctrl-u
273 cx.simulate_shared_keystrokes("ctrl-u").await;
274 cx.shared_state().await.assert_matches();
275 cx.simulate_shared_keystrokes("ctrl-d ctrl-d 4 j ctrl-u ctrl-u")
276 .await;
277 cx.shared_state().await.assert_matches();
278
279 // test returning to top
280 cx.simulate_shared_keystrokes("g g ctrl-d ctrl-u ctrl-u")
281 .await;
282 cx.shared_state().await.assert_matches();
283 }
284
285 #[gpui::test]
286 async fn test_scroll_beyond_last_line(cx: &mut gpui::TestAppContext) {
287 let mut cx = NeovimBackedTestContext::new(cx).await;
288
289 cx.set_scroll_height(10).await;
290 cx.neovim.set_option(&format!("scrolloff={}", 0)).await;
291
292 let content = "Λ".to_owned() + &sample_text(26, 2, 'a');
293 cx.set_shared_state(&content).await;
294
295 cx.update_global(|store: &mut SettingsStore, cx| {
296 store.update_user_settings::<EditorSettings>(cx, |s| {
297 s.scroll_beyond_last_line = Some(ScrollBeyondLastLine::Off)
298 });
299 });
300
301 // ctrl-d can reach the end and the cursor stays in the first column
302 cx.simulate_shared_keystrokes("shift-g k").await;
303 cx.shared_state().await.assert_matches();
304 cx.simulate_shared_keystrokes("ctrl-d").await;
305 cx.shared_state().await.assert_matches();
306
307 // ctrl-u from the last line
308 cx.simulate_shared_keystrokes("shift-g").await;
309 cx.shared_state().await.assert_matches();
310 cx.simulate_shared_keystrokes("ctrl-u").await;
311 cx.shared_state().await.assert_matches();
312 }
313}