Remove Input action, detect ignored input in vim via an event

Max Brunsfeld created

Change summary

assets/keymaps/default.json           |  5 +----
crates/editor/src/editor.rs           | 15 +++++++++++----
crates/file_finder/src/file_finder.rs | 12 +++++++-----
crates/vim/src/editor_events.rs       | 17 ++++++++++++++---
crates/vim/src/vim.rs                 | 12 +-----------
5 files changed, 34 insertions(+), 27 deletions(-)

Detailed changes

assets/keymaps/default.json 🔗

@@ -138,10 +138,7 @@
     {
         "context": "Editor && mode == auto_height",
         "bindings": {
-            "alt-enter": [
-                "editor::Input",
-                "\n"
-            ]
+            "alt-enter": "editor::Newline"
         }
     },
     {

crates/editor/src/editor.rs 🔗

@@ -98,9 +98,6 @@ pub struct Jump {
     anchor: language::Anchor,
 }
 
-#[derive(Clone, Deserialize, PartialEq)]
-pub struct Input(pub String);
-
 #[derive(Clone, Deserialize, PartialEq)]
 pub struct SelectToBeginningOfLine {
     #[serde(default)]
@@ -214,7 +211,6 @@ actions!(
 impl_actions!(
     editor,
     [
-        Input,
         SelectNext,
         SelectToBeginningOfLine,
         SelectToEndOfLine,
@@ -5772,6 +5768,7 @@ pub enum Event {
     SelectionsChanged { local: bool },
     ScrollPositionChanged { local: bool },
     Closed,
+    IgnoredInput,
 }
 
 pub struct EditorFocused(pub ViewHandle<Editor>);
@@ -5916,6 +5913,11 @@ impl View for Editor {
         text: &str,
         cx: &mut ViewContext<Self>,
     ) {
+        if !self.input_enabled {
+            cx.emit(Event::IgnoredInput);
+            return;
+        }
+
         self.transact(cx, |this, cx| {
             if let Some(range) = range_utf16.or_else(|| this.marked_text_range(cx)) {
                 this.change_selections(None, cx, |selections| {
@@ -5934,6 +5936,11 @@ impl View for Editor {
         new_selected_range_utf16: Option<Range<usize>>,
         cx: &mut ViewContext<Self>,
     ) {
+        if !self.input_enabled {
+            cx.emit(Event::IgnoredInput);
+            return;
+        }
+
         self.transact(cx, |this, cx| {
             let range_to_replace = if let Some(mut marked_range) = this.marked_text_range(cx) {
                 if let Some(relative_range_utf16) = range_utf16.as_ref() {

crates/file_finder/src/file_finder.rs 🔗

@@ -279,7 +279,7 @@ impl PickerDelegate for FileFinder {
 #[cfg(test)]
 mod tests {
     use super::*;
-    use editor::{Editor, Input};
+    use editor::Editor;
     use menu::{Confirm, SelectNext};
     use serde_json::json;
     use workspace::{AppState, Workspace};
@@ -326,12 +326,14 @@ mod tests {
                 .downcast::<FileFinder>()
                 .unwrap()
         });
-        cx.dispatch_action(window_id, Input("b".into()));
-        cx.dispatch_action(window_id, Input("n".into()));
-        cx.dispatch_action(window_id, Input("a".into()));
         finder
-            .condition(&cx, |finder, _| finder.matches.len() == 2)
+            .update(cx, |finder, cx| {
+                finder.update_matches("bna".to_string(), cx)
+            })
             .await;
+        finder.read_with(cx, |finder, _| {
+            assert_eq!(finder.matches.len(), 2);
+        });
 
         let active_pane = cx.read(|cx| workspace.read(cx).active_pane().clone());
         cx.dispatch_action(window_id, SelectNext);

crates/vim/src/editor_events.rs 🔗

@@ -22,9 +22,20 @@ fn editor_focused(EditorFocused(editor): &EditorFocused, cx: &mut MutableAppCont
         vim.active_editor = Some(editor.downgrade());
         vim.selection_subscription = Some(cx.subscribe(editor, |editor, event, cx| {
             if editor.read(cx).leader_replica_id().is_none() {
-                if let editor::Event::SelectionsChanged { local: true } = event {
-                    let newest_empty = editor.read(cx).selections.newest::<usize>(cx).is_empty();
-                    editor_local_selections_changed(newest_empty, cx);
+                match event {
+                    editor::Event::SelectionsChanged { local: true } => {
+                        let newest_empty =
+                            editor.read(cx).selections.newest::<usize>(cx).is_empty();
+                        editor_local_selections_changed(newest_empty, cx);
+                    }
+                    editor::Event::IgnoredInput => {
+                        Vim::update(cx, |vim, cx| {
+                            if vim.active_operator().is_some() {
+                                vim.clear_operator(cx);
+                            }
+                        });
+                    }
+                    _ => (),
                 }
             }
         }));

crates/vim/src/vim.rs 🔗

@@ -11,7 +11,7 @@ mod visual;
 
 use collections::HashMap;
 use command_palette::CommandPaletteFilter;
-use editor::{Bias, Cancel, CursorShape, Editor, Input};
+use editor::{Bias, Cancel, CursorShape, Editor};
 use gpui::{impl_actions, MutableAppContext, Subscription, ViewContext, WeakViewHandle};
 use serde::Deserialize;
 
@@ -45,16 +45,6 @@ pub fn init(cx: &mut MutableAppContext) {
     );
 
     // Editor Actions
-    cx.add_action(|_: &mut Editor, _: &Input, cx| {
-        // If we have an unbound input with an active operator, cancel that operator. Otherwise forward
-        // the input to the editor
-        if Vim::read(cx).active_operator().is_some() {
-            // Defer without updating editor
-            MutableAppContext::defer(cx, |cx| Vim::update(cx, |vim, cx| vim.clear_operator(cx)))
-        } else {
-            cx.propagate_action()
-        }
-    });
     cx.add_action(|_: &mut Editor, _: &Cancel, cx| {
         // If we are in a non normal mode or have an active operator, swap to normal mode
         // Otherwise forward cancel on to the editor