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