Skip to content

Commit

Permalink
Introduce (KeyCode, KeyCode) tuple that functions as ESC in Vi Inse…
Browse files Browse the repository at this point in the history
…rt mode (e.g. press "jk" to exit insert mode)
  • Loading branch information
benvansleen committed Nov 18, 2023
1 parent 93af55c commit dfa469b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
6 changes: 5 additions & 1 deletion examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ fn main() -> std::io::Result<()> {

add_newline_keybinding(&mut insert_keybindings);

Box::new(Vi::new(insert_keybindings, normal_keybindings))
Box::new(Vi::new(
insert_keybindings,
normal_keybindings,
(KeyCode::Null, KeyCode::Null),
))
} else {
let mut keybindings = default_emacs_keybindings();
add_menu_keybindings(&mut keybindings);
Expand Down
66 changes: 54 additions & 12 deletions src/edit_mode/vi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,84 @@ pub struct Vi {
previous: Option<ReedlineEvent>,
// last f, F, t, T motion for ; and ,
last_char_search: Option<ViCharSearch>,
alternate_esc_seq: (KeyCode, KeyCode),
most_recent_keycode: KeyCode,
}

impl Default for Vi {
fn default() -> Self {
Vi {
insert_keybindings: default_vi_insert_keybindings(),
normal_keybindings: default_vi_normal_keybindings(),
alternate_esc_seq: (KeyCode::Null, KeyCode::Null),
cache: Vec::new(),
mode: ViMode::Insert,
previous: None,
last_char_search: None,
most_recent_keycode: KeyCode::Null,
}
}
}

impl Vi {
/// Creates Vi editor using defined keybindings
pub fn new(insert_keybindings: Keybindings, normal_keybindings: Keybindings) -> Self {
pub fn new(
insert_keybindings: Keybindings,
normal_keybindings: Keybindings,
alternate_esc_seq: (KeyCode, KeyCode),
) -> Self {

Check warning on line 58 in src/edit_mode/vi/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/edit_mode/vi/mod.rs#L54-L58

Added lines #L54 - L58 were not covered by tests
Self {
insert_keybindings,
normal_keybindings,
alternate_esc_seq,

Check warning on line 62 in src/edit_mode/vi/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/edit_mode/vi/mod.rs#L62

Added line #L62 was not covered by tests
..Default::default()
}
}
}

fn exit_insert_mode(editor: &mut Vi) -> ReedlineEvent {
editor.most_recent_keycode = KeyCode::Null;
editor.cache.clear();
editor.mode = ViMode::Normal;
ReedlineEvent::Multiple(vec![ReedlineEvent::Esc, ReedlineEvent::Repaint])
}

impl EditMode for Vi {
fn parse_event(&mut self, event: ReedlineRawEvent) -> ReedlineEvent {
match event.into() {
Event::Key(KeyEvent {
code, modifiers, ..
}) => match (self.mode, modifiers, code) {
(ViMode::Normal, modifier, KeyCode::Char(c)) => {
}) => match (
self.mode,
modifiers,
code,
self.alternate_esc_seq,
self.most_recent_keycode,
) {
(ViMode::Insert, KeyModifiers::NONE, code, (e1, e2), mr)

Check warning on line 87 in src/edit_mode/vi/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/edit_mode/vi/mod.rs#L87

Added line #L87 was not covered by tests
if code == e2 && mr == e1 =>
{
exit_insert_mode(self)

Check warning on line 90 in src/edit_mode/vi/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/edit_mode/vi/mod.rs#L89-L90

Added lines #L89 - L90 were not covered by tests
}
(ViMode::Insert, KeyModifiers::NONE, code, (e1, _), _) if code == e1 => {
self.most_recent_keycode = code;
ReedlineEvent::None

Check warning on line 94 in src/edit_mode/vi/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/edit_mode/vi/mod.rs#L93-L94

Added lines #L93 - L94 were not covered by tests
}
(
ViMode::Insert,
KeyModifiers::NONE,
KeyCode::Char(code),
(KeyCode::Char(e1), KeyCode::Char(e2)),
KeyCode::Char(mr),
) if code != e2 && mr == e1 => {
self.most_recent_keycode = KeyCode::Char(code);
ReedlineEvent::Multiple(vec![
ReedlineEvent::Edit(vec![EditCommand::InsertChar(e1)]),
ReedlineEvent::Edit(vec![EditCommand::InsertChar(code)]),
])

Check warning on line 107 in src/edit_mode/vi/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/edit_mode/vi/mod.rs#L99-L107

Added lines #L99 - L107 were not covered by tests
}

(ViMode::Normal, modifier, KeyCode::Char(c), _, _) => {
let c = c.to_ascii_lowercase();

if let Some(event) = self
Expand Down Expand Up @@ -97,7 +142,7 @@ impl EditMode for Vi {
ReedlineEvent::None
}
}
(ViMode::Insert, modifier, KeyCode::Char(c)) => {
(ViMode::Insert, modifier, KeyCode::Char(c), _, _) => {

Check warning on line 145 in src/edit_mode/vi/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/edit_mode/vi/mod.rs#L145

Added line #L145 was not covered by tests
// Note. The modifier can also be a combination of modifiers, for
// example:
// KeyModifiers::CONTROL | KeyModifiers::ALT
Expand All @@ -122,6 +167,7 @@ impl EditMode for Vi {
| KeyModifiers::ALT
| KeyModifiers::SHIFT
{
self.most_recent_keycode = KeyCode::Char(c);

Check warning on line 170 in src/edit_mode/vi/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/edit_mode/vi/mod.rs#L170

Added line #L170 was not covered by tests
ReedlineEvent::Edit(vec![EditCommand::InsertChar(
if modifier == KeyModifiers::SHIFT {
c.to_ascii_uppercase()
Expand All @@ -134,20 +180,16 @@ impl EditMode for Vi {
}
})
}
(_, KeyModifiers::NONE, KeyCode::Esc) => {
self.cache.clear();
self.mode = ViMode::Normal;
ReedlineEvent::Multiple(vec![ReedlineEvent::Esc, ReedlineEvent::Repaint])
}
(_, KeyModifiers::NONE, KeyCode::Enter) => {
(_, KeyModifiers::NONE, KeyCode::Esc, _, _) => exit_insert_mode(self),
(_, KeyModifiers::NONE, KeyCode::Enter, _, _) => {
self.mode = ViMode::Insert;
ReedlineEvent::Enter
}
(ViMode::Normal, _, _) => self
(ViMode::Normal, _, _, _, _) => self

Check warning on line 188 in src/edit_mode/vi/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/edit_mode/vi/mod.rs#L188

Added line #L188 was not covered by tests
.normal_keybindings
.find_binding(modifiers, code)
.unwrap_or(ReedlineEvent::None),
(ViMode::Insert, _, _) => self
(ViMode::Insert, _, _, _, _) => self

Check warning on line 192 in src/edit_mode/vi/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/edit_mode/vi/mod.rs#L192

Added line #L192 was not covered by tests
.insert_keybindings
.find_binding(modifiers, code)
.unwrap_or(ReedlineEvent::None),
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,13 @@
//! // Create a reedline object with custom edit mode
//! // This can define a keybinding setting or enable vi-emulation
//! use reedline::{
//! default_vi_insert_keybindings, default_vi_normal_keybindings, EditMode, Reedline, Vi,
//! default_vi_insert_keybindings, default_vi_normal_keybindings, EditMode, Reedline, Vi, KeyCode
//! };
//!
//! let mut line_editor = Reedline::create().with_edit_mode(Box::new(Vi::new(
//! default_vi_insert_keybindings(),
//! default_vi_normal_keybindings(),
//! (KeyCode::Null, KeyCode::Null),
//! )));
//! ```
//!
Expand Down

0 comments on commit dfa469b

Please sign in to comment.