Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/edit_mode/base.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
enums::{ReedlineEvent, ReedlineRawEvent},
enums::{EventStatus, ReedlineEvent, ReedlineRawEvent},
PromptEditMode,
};

Expand All @@ -13,4 +13,9 @@ pub trait EditMode: Send {

/// What to display in the prompt indicator
fn edit_mode(&self) -> PromptEditMode;

/// Handles events that apply only to specific edit modes (e.g changing vi mode)
fn handle_mode_specific_event(&mut self, _event: ReedlineEvent) -> EventStatus {
EventStatus::Inapplicable
}
}
30 changes: 29 additions & 1 deletion src/edit_mode/vi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ mod motion;
mod parser;
mod vi_keybindings;

use std::str::FromStr;

use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
pub use vi_keybindings::{default_vi_insert_keybindings, default_vi_normal_keybindings};

Expand All @@ -11,7 +13,7 @@ use self::motion::ViCharSearch;
use super::EditMode;
use crate::{
edit_mode::{keybindings::Keybindings, vi::parser::parse},
enums::{EditCommand, ReedlineEvent, ReedlineRawEvent},
enums::{EditCommand, EventStatus, ReedlineEvent, ReedlineRawEvent},
PromptEditMode, PromptViMode,
};

Expand All @@ -22,6 +24,19 @@ enum ViMode {
Visual,
}

impl FromStr for ViMode {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"normal" => Ok(ViMode::Normal),
"insert" => Ok(ViMode::Insert),
"visual" => Ok(ViMode::Visual),
_ => Err(()),
}
}
}

/// This parses incoming input `Event`s like a Vi-Style editor
pub struct Vi {
cache: Vec<char>,
Expand Down Expand Up @@ -174,6 +189,19 @@ impl EditMode for Vi {
ViMode::Insert => PromptEditMode::Vi(PromptViMode::Insert),
}
}

fn handle_mode_specific_event(&mut self, event: ReedlineEvent) -> EventStatus {
match event {
ReedlineEvent::ViChangeMode(mode_str) => match ViMode::from_str(&mode_str) {
Ok(mode) => {
self.mode = mode;
EventStatus::Handled
}
Err(_) => EventStatus::Inapplicable,
},
_ => EventStatus::Inapplicable,
}
}
}

#[cfg(test)]
Expand Down
4 changes: 3 additions & 1 deletion src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,8 @@ impl Reedline {
| ReedlineEvent::MenuLeft
| ReedlineEvent::MenuRight
| ReedlineEvent::MenuPageNext
| ReedlineEvent::MenuPagePrevious => Ok(EventStatus::Inapplicable),
| ReedlineEvent::MenuPagePrevious
| ReedlineEvent::ViChangeMode(_) => Ok(EventStatus::Inapplicable),
}
}

Expand Down Expand Up @@ -1273,6 +1274,7 @@ impl Reedline {
// Exhausting the event handlers is still considered handled
Ok(EventStatus::Inapplicable)
}
ReedlineEvent::ViChangeMode(_) => Ok(self.edit_mode.handle_mode_specific_event(event)),
ReedlineEvent::None | ReedlineEvent::Mouse => Ok(EventStatus::Inapplicable),
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,9 @@ pub enum ReedlineEvent {

/// Open text editor
OpenEditor,

/// Change mode (vi mode only)
ViChangeMode(String),
}

impl Display for ReedlineEvent {
Expand Down Expand Up @@ -801,11 +804,12 @@ impl Display for ReedlineEvent {
ReedlineEvent::MenuPagePrevious => write!(f, "MenuPagePrevious"),
ReedlineEvent::ExecuteHostCommand(_) => write!(f, "ExecuteHostCommand"),
ReedlineEvent::OpenEditor => write!(f, "OpenEditor"),
ReedlineEvent::ViChangeMode(_) => write!(f, "ViChangeMode mode: <string>"),
}
}
}

pub(crate) enum EventStatus {
pub enum EventStatus {
Handled,
Inapplicable,
Exits(Signal),
Expand Down