Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Fix Helix append escape cursor restore (#57736)
Jamil Ur Rehman Ahmadzai945764f95355
diff --git a/crates/vim/src/helix.rs b/crates/vim/src/helix.rsindex e2857ce30e4..a0b9bf6f3cc 100644--- a/crates/vim/src/helix.rs+++ b/crates/vim/src/helix.rs@@ -24,7 +24,7 @@ use workspace::searchable::{self, Direction, FilteredSearchRange};use crate::motion::{self, MotionKind};use crate::state::{HelixJumpBehaviour, HelixJumpLabel, Mode, Operator, SearchState};use crate::{- PushHelixSurroundAdd, PushHelixSurroundDelete, PushHelixSurroundReplace, Vim,+ HelixAppendState, PushHelixSurroundAdd, PushHelixSurroundDelete, PushHelixSurroundReplace, Vim,motion::{Motion, right},};use std::ops::Range;@@ -718,7 +718,15 @@ impl Vim {fn helix_append(&mut self, _: &HelixAppend, window: &mut Window, cx: &mut Context<Self>) {self.start_recording(cx);self.switch_mode(Mode::Insert, false, window, cx);- self.update_editor(cx, |_, editor, cx| {+ self.update_editor(cx, |vim, editor, cx| {+ let snapshot = editor.display_snapshot(cx);+ let selections_before_append = editor+ .selections+ .all_anchors(&snapshot)+ .iter()+ .map(|selection| selection.tail()..selection.head())+ .collect();+editor.change_selections(Default::default(), window, cx, |s| {s.move_with(&mut |map, selection| {let point = if selection.is_empty() {@@ -729,6 +737,18 @@ impl Vim {selection.collapse_to(point, SelectionGoal::None);});});++ let snapshot = editor.display_snapshot(cx);+ let cursors_after_append = editor+ .selections+ .all_anchors(&snapshot)+ .iter()+ .map(|selection| selection.range())+ .collect();+ vim.helix_append_state = Some(HelixAppendState {+ selections_before_append,+ cursors_after_append,+ });});}@@ -2479,6 +2499,23 @@ mod test {async fn test_append(cx: &mut gpui::TestAppContext) {let mut cx = VimTestContext::new(cx, true).await;cx.enable_helix();++ cx.set_state("Thˇe quick brown", Mode::HelixNormal);++ cx.simulate_keystrokes("a");++ cx.assert_state("Theˇ quick brown", Mode::Insert);++ cx.simulate_keystrokes("escape");++ cx.assert_state("Thˇe quick brown", Mode::HelixNormal);++ cx.set_state("The quick brownˇ", Mode::HelixNormal);++ cx.simulate_keystrokes("a escape");++ cx.assert_state("The quick brownˇ", Mode::HelixNormal);+// test from the end of the selectioncx.set_state(indoc! {"@@ -2498,6 +2535,16 @@ mod test {Mode::Insert,);+ cx.simulate_keystrokes("escape");++ cx.assert_state(+ indoc! {"+ «Theˇ» quick brown+ fox jumps over+ the lazy dog."},+ Mode::HelixNormal,+ );+// test from the beginning of the selectioncx.set_state(indoc! {"@@ -2516,6 +2563,37 @@ mod test {the lazy dog."},Mode::Insert,);++ cx.simulate_keystrokes("escape");++ cx.assert_state(+ indoc! {"+ «ˇThe» quick brown+ fox jumps over+ the lazy dog."},+ Mode::HelixNormal,+ );++ // escape must not restore the selection once text was inserted+ cx.set_state("Thˇe quick brown", Mode::HelixNormal);++ cx.simulate_keystrokes("a x escape");++ cx.assert_state("Thexˇ quick brown", Mode::HelixNormal);++ // or when the cursor moved during insert mode+ cx.set_state("Thˇe quick brown", Mode::HelixNormal);++ cx.simulate_keystrokes("a left left escape");++ cx.assert_state("Tˇhe quick brown", Mode::HelixNormal);++ // all selections restore with multiple cursors+ cx.set_state("ˇaaa bbb\nˇccc ddd", Mode::HelixNormal);++ cx.simulate_keystrokes("a escape");++ cx.assert_state("ˇaaa bbb\nˇccc ddd", Mode::HelixNormal);}#[gpui::test]diff --git a/crates/vim/src/insert.rs b/crates/vim/src/insert.rsindex 75fc456cc18..063c442c141 100644--- a/crates/vim/src/insert.rs+++ b/crates/vim/src/insert.rs@@ -51,8 +51,32 @@ impl Vim {self.create_mark("^".into(), window, cx);if HelixModeSetting::get_global(cx).0 {+ // Restore the pre-append selections only if nothing happened in+ // insert mode: no edits (tracked by current_tx/current_anchor)+ // and the cursors are still where the append left them.+ let no_insert_transaction =+ self.current_tx.is_none() && self.current_anchor.is_none();+ let restore_append_selection = self+ .helix_append_state+ .take()+ .filter(|_| no_insert_transaction);self.update_editor(cx, |_, editor, cx| {editor.dismiss_menus_and_popups(false, window, cx);+ if let Some(append_state) = restore_append_selection {+ let snapshot = editor.display_snapshot(cx);+ let current = editor+ .selections+ .all_anchors(&snapshot)+ .iter()+ .map(|selection| selection.range())+ .collect::<Vec<_>>();++ if current == append_state.cursors_after_append {+ editor.change_selections(Default::default(), window, cx, |s| {+ s.select_anchor_ranges(append_state.selections_before_append);+ });+ }+ }});self.switch_mode(Mode::HelixNormal, false, window, cx);return;diff --git a/crates/vim/src/vim.rs b/crates/vim/src/vim.rsindex d58ad3a2c65..05badab50c9 100644--- a/crates/vim/src/vim.rs+++ b/crates/vim/src/vim.rs@@ -527,6 +527,7 @@ pub(crate) struct Vim {pub(crate) current_tx: Option<TransactionId>,pub(crate) current_anchor: Option<Selection<Anchor>>,+ pub(crate) helix_append_state: Option<HelixAppendState>,pub(crate) undo_modes: HashMap<TransactionId, Mode>,pub(crate) undo_last_line_tx: Option<TransactionId>,extended_pending_selection_id: Option<usize>,@@ -541,6 +542,13 @@ pub(crate) struct Vim {_subscriptions: Vec<Subscription>,}+/// Captured by `helix_append` so that escape can restore the pre-append+/// selections when nothing was inserted, matching Helix.+pub(crate) struct HelixAppendState {+ pub(crate) selections_before_append: Vec<Range<Anchor>>,+ pub(crate) cursors_after_append: Vec<Range<Anchor>>,+}+// Hack: Vim intercepts events dispatched to a window and updates the view in response.// This means it needs a VisualContext. The easiest way to satisfy that constraint is// to make Vim a "View" that is just never actually rendered.@@ -588,6 +596,7 @@ impl Vim {current_tx: None,undo_last_line_tx: None,current_anchor: None,+ helix_append_state: None,extended_pending_selection_id: None,undo_modes: HashMap::default(),@@ -1210,6 +1219,9 @@ impl Vim {if mode == Mode::Normal || mode != last_mode {self.current_tx.take();self.current_anchor.take();+ if mode != Mode::Insert {+ self.helix_append_state.take();+ }self.update_editor(cx, |_, editor, _| {editor.clear_selection_drag_state();});