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

This commit is contained in:
Max Brunsfeld 2022-07-21 13:40:48 -07:00
parent beeaec8647
commit 2142fca673
5 changed files with 34 additions and 27 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -11,7 +11,7 @@ mod visual;
use collections::HashMap; use collections::HashMap;
use command_palette::CommandPaletteFilter; 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 gpui::{impl_actions, MutableAppContext, Subscription, ViewContext, WeakViewHandle};
use serde::Deserialize; use serde::Deserialize;
@ -45,16 +45,6 @@ pub fn init(cx: &mut MutableAppContext) {
); );
// Editor Actions // 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| { cx.add_action(|_: &mut Editor, _: &Cancel, cx| {
// If we are in a non normal mode or have an active operator, swap to normal mode // If we are in a non normal mode or have an active operator, swap to normal mode
// Otherwise forward cancel on to the editor // Otherwise forward cancel on to the editor