scroll.rs

  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)]
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}