@@ -15,19 +15,19 @@ actions!(
pub fn init(cx: &mut AppContext) {
cx.add_action(|_: &mut Workspace, _: &LineDown, cx| {
- scroll(cx, |c| ScrollAmount::Line(c.unwrap_or(1.)))
+ scroll(cx, false, |c| ScrollAmount::Line(c.unwrap_or(1.)))
});
cx.add_action(|_: &mut Workspace, _: &LineUp, cx| {
- scroll(cx, |c| ScrollAmount::Line(-c.unwrap_or(1.)))
+ scroll(cx, false, |c| ScrollAmount::Line(-c.unwrap_or(1.)))
});
cx.add_action(|_: &mut Workspace, _: &PageDown, cx| {
- scroll(cx, |c| ScrollAmount::Page(c.unwrap_or(1.)))
+ scroll(cx, false, |c| ScrollAmount::Page(c.unwrap_or(1.)))
});
cx.add_action(|_: &mut Workspace, _: &PageUp, cx| {
- scroll(cx, |c| ScrollAmount::Page(-c.unwrap_or(1.)))
+ scroll(cx, false, |c| ScrollAmount::Page(-c.unwrap_or(1.)))
});
cx.add_action(|_: &mut Workspace, _: &ScrollDown, cx| {
- scroll(cx, |c| {
+ scroll(cx, true, |c| {
if let Some(c) = c {
ScrollAmount::Line(c)
} else {
@@ -36,7 +36,7 @@ pub fn init(cx: &mut AppContext) {
})
});
cx.add_action(|_: &mut Workspace, _: &ScrollUp, cx| {
- scroll(cx, |c| {
+ scroll(cx, true, |c| {
if let Some(c) = c {
ScrollAmount::Line(-c)
} else {
@@ -46,15 +46,27 @@ pub fn init(cx: &mut AppContext) {
});
}
-fn scroll(cx: &mut ViewContext<Workspace>, by: fn(c: Option<f32>) -> ScrollAmount) {
+fn scroll(
+ cx: &mut ViewContext<Workspace>,
+ move_cursor: bool,
+ by: fn(c: Option<f32>) -> ScrollAmount,
+) {
Vim::update(cx, |vim, cx| {
let amount = by(vim.take_count(cx).map(|c| c as f32));
- vim.update_active_editor(cx, |editor, cx| scroll_editor(editor, &amount, cx));
+ vim.update_active_editor(cx, |editor, cx| {
+ scroll_editor(editor, move_cursor, &amount, cx)
+ });
})
}
-fn scroll_editor(editor: &mut Editor, amount: &ScrollAmount, cx: &mut ViewContext<Editor>) {
+fn scroll_editor(
+ editor: &mut Editor,
+ preserve_cursor_position: bool,
+ amount: &ScrollAmount,
+ cx: &mut ViewContext<Editor>,
+) {
let should_move_cursor = editor.newest_selection_on_screen(cx).is_eq();
+ let old_top_anchor = editor.scroll_manager.anchor().anchor;
editor.scroll_screen(amount, cx);
if should_move_cursor {
@@ -68,8 +80,14 @@ fn scroll_editor(editor: &mut Editor, amount: &ScrollAmount, cx: &mut ViewContex
editor.change_selections(None, cx, |s| {
s.move_with(|map, selection| {
- let head = selection.head();
+ let mut head = selection.head();
let top = top_anchor.to_display_point(map);
+
+ if preserve_cursor_position {
+ let old_top = old_top_anchor.to_display_point(map);
+ let new_row = top.row() + selection.head().row() - old_top.row();
+ head = map.clip_point(DisplayPoint::new(new_row, head.column()), Bias::Left)
+ }
let min_row = top.row() + VERTICAL_SCROLL_MARGIN as u32;
let max_row = top.row() + visible_rows - VERTICAL_SCROLL_MARGIN as u32 - 1;
@@ -92,7 +110,10 @@ fn scroll_editor(editor: &mut Editor, amount: &ScrollAmount, cx: &mut ViewContex
#[cfg(test)]
mod test {
- use crate::{state::Mode, test::VimTestContext};
+ use crate::{
+ state::Mode,
+ test::{NeovimBackedTestContext, VimTestContext},
+ };
use gpui::geometry::vector::vec2f;
use indoc::indoc;
use language::Point;
@@ -148,10 +169,10 @@ mod test {
});
cx.simulate_keystrokes(["ctrl-d"]);
cx.update_editor(|editor, cx| {
- assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 2.0));
+ assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 3.0));
assert_eq!(
editor.selections.newest(cx).range(),
- Point::new(5, 0)..Point::new(5, 0)
+ Point::new(6, 0)..Point::new(6, 0)
)
});
@@ -162,11 +183,48 @@ mod test {
});
cx.simulate_keystrokes(["v", "ctrl-d"]);
cx.update_editor(|editor, cx| {
- assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 2.0));
+ assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 3.0));
assert_eq!(
editor.selections.newest(cx).range(),
- Point::new(0, 0)..Point::new(5, 1)
+ Point::new(0, 0)..Point::new(6, 1)
)
});
}
+ #[gpui::test]
+ async fn test_ctrl_d_u(cx: &mut gpui::TestAppContext) {
+ let mut cx = NeovimBackedTestContext::new(cx).await;
+
+ cx.set_scroll_height(10).await;
+
+ pub fn sample_text(rows: usize, cols: usize, start_char: char) -> String {
+ let mut text = String::new();
+ for row in 0..rows {
+ let c: char = (start_char as u32 + row as u32) as u8 as char;
+ let mut line = c.to_string().repeat(cols);
+ if row < rows - 1 {
+ line.push('\n');
+ }
+ text += &line;
+ }
+ text
+ }
+ let content = "ˇ".to_owned() + &sample_text(26, 2, 'a');
+ cx.set_shared_state(&content).await;
+
+ // skip over the scrolloff at the top
+ // test ctrl-d
+ cx.simulate_shared_keystrokes(["4", "j", "ctrl-d"]).await;
+ cx.assert_state_matches().await;
+ cx.simulate_shared_keystrokes(["ctrl-d"]).await;
+ cx.assert_state_matches().await;
+ cx.simulate_shared_keystrokes(["g", "g", "ctrl-d"]).await;
+ cx.assert_state_matches().await;
+
+ // test ctrl-u
+ cx.simulate_shared_keystrokes(["ctrl-u"]).await;
+ cx.assert_state_matches().await;
+ cx.simulate_shared_keystrokes(["ctrl-d", "ctrl-d", "4", "j", "ctrl-u", "ctrl-u"])
+ .await;
+ cx.assert_state_matches().await;
+ }
}
@@ -1,9 +1,10 @@
+use editor::scroll::VERTICAL_SCROLL_MARGIN;
use indoc::indoc;
use settings::SettingsStore;
use std::ops::{Deref, DerefMut, Range};
use collections::{HashMap, HashSet};
-use gpui::ContextHandle;
+use gpui::{geometry::vector::vec2f, ContextHandle};
use language::{
language_settings::{AllLanguageSettings, SoftWrap},
OffsetRangeExt,
@@ -132,7 +133,9 @@ impl<'a> NeovimBackedTestContext<'a> {
panic!("nvim doesn't support columns < 12")
}
self.neovim.set_option("wrap").await;
- self.neovim.set_option("columns=12").await;
+ self.neovim
+ .set_option(&format!("columns={}", columns))
+ .await;
self.update(|cx| {
cx.update_global(|settings: &mut SettingsStore, cx| {
@@ -144,6 +147,20 @@ impl<'a> NeovimBackedTestContext<'a> {
})
}
+ pub async fn set_scroll_height(&mut self, rows: u32) {
+ // match Zed's scrolling behavior
+ self.neovim
+ .set_option(&format!("scrolloff={}", VERTICAL_SCROLL_MARGIN))
+ .await;
+ // +2 to account for the vim command UI at the bottom.
+ self.neovim.set_option(&format!("lines={}", rows + 2)).await;
+ let window = self.window;
+ let line_height =
+ self.editor(|editor, cx| editor.style(cx).text.line_height(cx.font_cache()));
+
+ window.simulate_resize(vec2f(1000., (rows as f32) * line_height), &mut self.cx);
+ }
+
pub async fn set_neovim_option(&mut self, option: &str) {
self.neovim.set_option(option).await;
}