Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Fix accidental visual selection on scroll
Conrad Irwin6d7949654bdc
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rsindex 2ea2ec74535..d331b0a268e 100644--- a/crates/editor/src/editor.rs+++ b/crates/editor/src/editor.rs@@ -1654,7 +1654,7 @@ impl Editor {.excerpt_containing(self.selections.newest_anchor().head(), cx)}- fn style(&self, cx: &AppContext) -> EditorStyle {+ pub fn style(&self, cx: &AppContext) -> EditorStyle {build_style(settings::get::<ThemeSettings>(cx),self.get_field_editor_theme.as_deref(),diff --git a/crates/editor/src/scroll/scroll_amount.rs b/crates/editor/src/scroll/scroll_amount.rsindex f9d09adcf50..cadf37b31d9 100644--- a/crates/editor/src/scroll/scroll_amount.rs+++ b/crates/editor/src/scroll/scroll_amount.rs@@ -39,7 +39,7 @@ impl ScrollAmount {.visible_line_count()// subtract one to leave an anchor line// round towards zero (so page-up and page-down are symmetric)- .map(|l| ((l - 1.) * count).trunc())+ .map(|l| (l * count).trunc() - count.signum()).unwrap_or(0.),}}diff --git a/crates/vim/src/normal/scroll.rs b/crates/vim/src/normal/scroll.rsindex a2bbab0478e..1b3dcee6adc 100644--- a/crates/vim/src/normal/scroll.rs+++ b/crates/vim/src/normal/scroll.rs@@ -67,7 +67,8 @@ fn scroll_editor(editor: &mut Editor, amount: &ScrollAmount, cx: &mut ViewContexlet top_anchor = editor.scroll_manager.anchor().anchor;editor.change_selections(None, cx, |s| {- s.move_heads_with(|map, head, goal| {+ s.move_with(|map, selection| {+ let head = selection.head();let top = top_anchor.to_display_point(map);let min_row = top.row() + VERTICAL_SCROLL_MARGIN as u32;let max_row = top.row() + visible_rows - VERTICAL_SCROLL_MARGIN as u32 - 1;@@ -79,7 +80,11 @@ fn scroll_editor(editor: &mut Editor, amount: &ScrollAmount, cx: &mut ViewContex} else {head};- (new_head, goal)+ if selection.is_empty() {+ selection.collapse_to(new_head, selection.goal)+ } else {+ selection.set_head(new_head, selection.goal)+ };})});}@@ -90,12 +95,35 @@ mod test {use crate::{state::Mode, test::VimTestContext};use gpui::geometry::vector::vec2f;use indoc::indoc;+ use language::Point;#[gpui::test]async fn test_scroll(cx: &mut gpui::TestAppContext) {let mut cx = VimTestContext::new(cx, true).await;- cx.set_state(indoc! {"ˇa\nb\nc\nd\ne\n"}, Mode::Normal);+ let window = cx.window;+ let line_height =+ cx.editor(|editor, cx| editor.style(cx).text.line_height(cx.font_cache()));+ window.simulate_resize(vec2f(1000., 8.0 * line_height - 1.0), &mut cx);++ cx.set_state(+ indoc!(+ "ˇone+ two+ three+ four+ five+ six+ seven+ eight+ nine+ ten+ eleven+ twelve+ "+ ),+ Mode::Normal,+ );cx.update_editor(|editor, cx| {assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 0.))@@ -112,5 +140,33 @@ mod test {cx.update_editor(|editor, cx| {assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 2.))});++ // does not select in normal mode+ cx.simulate_keystrokes(["g", "g"]);+ cx.update_editor(|editor, cx| {+ assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 0.))+ });+ cx.simulate_keystrokes(["ctrl-d"]);+ cx.update_editor(|editor, cx| {+ assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 2.0));+ assert_eq!(+ editor.selections.newest(cx).range(),+ Point::new(5, 0)..Point::new(5, 0)+ )+ });++ // does select in visual mode+ cx.simulate_keystrokes(["g", "g"]);+ cx.update_editor(|editor, cx| {+ assert_eq!(editor.snapshot(cx).scroll_position(), vec2f(0., 0.))+ });+ 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.selections.newest(cx).range(),+ Point::new(0, 0)..Point::new(5, 1)+ )+ });}}