Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
vim: Handle Helix jump labels before IME input (#61315)
Mikhail Pertsev50ac7dc925bb
diff --git a/crates/vim/src/helix.rs b/crates/vim/src/helix.rsindex 8de9fa58894..6408fd7a388 100644--- a/crates/vim/src/helix.rs+++ b/crates/vim/src/helix.rs@@ -1866,7 +1866,10 @@ mod test {use std::{fmt::Write, time::Duration};use editor::{HighlightKey, MultiBufferOffset};- use gpui::{KeyBinding, UpdateGlobal, VisualTestContext};+ use gpui::{+ Bounds, DispatchEventResult, ElementInputHandler, KeyBinding, KeyDownEvent, Keystroke,+ PlatformInput, PlatformInputHandler, UpdateGlobal, VisualTestContext,+ };use indoc::indoc;use language::{CursorShape, Point};use project::FakeFs;@@ -1955,6 +1958,36 @@ mod test {});}+ fn assert_helix_jump_bypasses_text_input(cx: &mut VimTestContext) {+ let editor = cx.update_editor(|_, _, cx| cx.entity());+ let mut input_handler = cx.update(|window, cx| {+ PlatformInputHandler::new(+ window.to_async(cx),+ Box::new(ElementInputHandler::new(Bounds::default(), editor)),+ )+ });+ assert!(!input_handler.query_accepts_text_input());+ assert!(!input_handler.query_prefers_ime_for_printable_keys());+ }++ fn dispatch_native_printable_key(cx: &mut VimTestContext, key: char) -> DispatchEventResult {+ let keystroke = Keystroke::parse(&key.to_string())+ .expect("helix jump labels are valid keystrokes")+ .with_simulated_ime();+ let result = cx.update(|window, cx| {+ window.dispatch_event(+ PlatformInput::KeyDown(KeyDownEvent {+ keystroke,+ is_held: false,+ prefer_character_input: false,+ }),+ cx,+ )+ });+ cx.run_until_parked();+ result+ }+fn active_helix_jump_overlay_counts(cx: &mut VimTestContext) -> (usize, usize) {let covered_text_range_count = cx.update_editor(|editor, window, cx| {let snapshot = editor.snapshot(window, cx);@@ -3810,6 +3843,39 @@ mod test {assert_eq!(cx.active_operator(), None);}+ #[gpui::test]+ async fn test_helix_jump_consumes_label_keystrokes_before_ime(cx: &mut gpui::TestAppContext) {+ let mut cx = VimTestContext::new(cx, true).await;+ bind_vim_jump_to_word(&mut cx, "s");+ cx.set_state("ˇone two three", Mode::Normal);++ cx.simulate_keystrokes("s");+ let label = helix_jump_label_for_word(&mut cx, "two");+ let mut chars = label.chars();+ let first = chars.next().expect("jump labels are two characters long");+ let second = chars.next().expect("jump labels are two characters long");++ cx.update(|window, _| assert!(!window.has_pending_keystrokes()));+ assert_helix_jump_bypasses_text_input(&mut cx);++ let result = dispatch_native_printable_key(&mut cx, first);+ assert!(!result.propagate, "jump label keystroke should be consumed");+ assert!(matches!(+ cx.active_operator(),+ Some(Operator::HelixJump {+ first_char: Some(input),+ ..+ }) if input == first.to_ascii_lowercase()+ ));+ assert_helix_jump_bypasses_text_input(&mut cx);++ let result = dispatch_native_printable_key(&mut cx, second);+ assert!(!result.propagate, "jump label keystroke should be consumed");++ cx.assert_state("one ˇtwo three", Mode::Normal);+ assert_eq!(cx.active_operator(), None);+ }+#[gpui::test]async fn test_vim_jump_keeps_normal_cursor_shape(cx: &mut gpui::TestAppContext) {let mut cx = VimTestContext::new(cx, true).await;diff --git a/crates/vim/src/vim.rs b/crates/vim/src/vim.rsindex 05badab50c9..0c6c59f36e5 100644--- a/crates/vim/src/vim.rs+++ b/crates/vim/src/vim.rs@@ -1115,6 +1115,21 @@ impl Vim {cx,);}+ operator @ Operator::HelixJump { .. } if keystroke_event.action.is_none() => {+ let modifiers = keystroke_event.keystroke.modifiers;+ let mut input = keystroke_event.keystroke.key.chars();+ if !modifiers.control+ && !modifiers.alt+ && !modifiers.platform+ && !modifiers.function+ && let Some(input_char) = input.next()+ && input.next().is_none()+ {+ // Jump overlays use ASCII labels even on non-ASCII keyboard layouts.+ self.handle_helix_jump_input(operator, input_char, window, cx);+ cx.stop_propagation();+ }+ }_ if !operator.is_waiting(self.mode) => {self.clear_operator(window, cx);self.stop_recording_immediately(Box::new(ClearOperators), cx)@@ -1412,7 +1427,8 @@ impl Vim {fn expects_character_input(&self) -> bool {if let Some(operator) = self.operator_stack.last() {if operator.is_waiting(self.mode) {- return true;+ // Helix jump labels are commands that need to reach Vim before an active IME.+ return !matches!(operator, Operator::HelixJump { .. });}}self.editor_input_enabled()