-
Notifications
You must be signed in to change notification settings - Fork 215
helix-mode: add basic mode switching #1039
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
e3b7604
helix: add basic mode switching
schlich 886bc14
helix-mode: Implement ModalKit infrastructure for basic mode switching
schlich 79f17af
new clippy fixes from upgrade
schlich a3ad4ac
avoid modalkit::key::TerminalKey
fdncred 4b943e5
Merge branch 'main' into helix/mode-switching
schlich 9ba71a2
add 'a' keymap/action for append_mode
schlich 9d0aafb
refactor mode initialization
schlich 9e54024
refactor parse_event, HelixAction
schlich feeb126
refactor: remove key normalization logic for now
schlich bda3153
refactor HelixBindings logic
schlich 64538bd
refactor HelixKey conversion to use From trait
schlich 0128846
remove more key normalization logic
schlich 30cd391
factor out into files
schlich 5052ba8
refactor
schlich e644fa3
refactor out event handling logic
schlich e842ad0
use proptest for insert mode char fallback behavior
schlich fdb6be9
pure format
schlich 09f3ba7
Revert "use proptest for insert mode char fallback behavior"
schlich 44fc122
install and import keybindings crate directly; revert MSRV
schlich 6a7fc6b
revert clippy changes in demo example
schlich 020ca2f
add more context to prompt in runnable example
schlich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,64 +1,292 @@ | ||
| use crate::{ | ||
| enums::{EventStatus, ReedlineEvent, ReedlineRawEvent}, | ||
| edit_mode::EditMode, | ||
| enums::{EditCommand, ReedlineEvent, ReedlineRawEvent}, | ||
| PromptEditMode, PromptViMode, | ||
| }; | ||
| use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; | ||
| use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; | ||
| use modalkit::{ | ||
| key::TerminalKey, | ||
| keybindings::{ | ||
| BindingMachine, EdgeEvent, EdgePath, EdgeRepeat, EmptyKeyClass, EmptyKeyState, | ||
| InputBindings, InputKey, ModalMachine, Mode, ModeKeys, | ||
| }, | ||
| }; | ||
|
|
||
| use super::EditMode; | ||
| #[derive(Clone, Copy, Debug, Default, Hash, Eq, PartialEq)] | ||
| enum HelixMode { | ||
| #[default] | ||
| Insert, | ||
| Normal, | ||
| } | ||
|
|
||
| impl Mode<HelixAction, EmptyKeyState> for HelixMode {} | ||
|
|
||
| impl ModeKeys<TerminalKey, HelixAction, EmptyKeyState> for HelixMode { | ||
| fn unmapped( | ||
| &self, | ||
| key: &TerminalKey, | ||
| _: &mut EmptyKeyState, | ||
| ) -> (Vec<HelixAction>, Option<HelixMode>) { | ||
| match self { | ||
| HelixMode::Normal => (vec![], None), | ||
| HelixMode::Insert => { | ||
| if let Some(c) = key.get_char() { | ||
| return (vec![HelixAction::Type(c)], None); | ||
| } | ||
|
|
||
| (vec![], None) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, Default, Eq, Hash, PartialEq)] | ||
| enum HelixAction { | ||
| Type(char), | ||
| MoveCharRight, | ||
| MoveCharLeft, | ||
| #[default] | ||
| NoOp, | ||
| } | ||
|
|
||
| type HelixStep = (Option<HelixAction>, Option<HelixMode>); | ||
|
|
||
| type HelixEdgePath = EdgePath<TerminalKey, EmptyKeyClass>; | ||
|
|
||
| type HelixMachine = ModalMachine<TerminalKey, HelixStep>; | ||
|
|
||
| /// A minimal custom edit mode example for Helix-style integrations. | ||
| #[derive(Default)] | ||
| pub struct Helix; | ||
| struct HelixBindings; | ||
|
|
||
| impl HelixBindings { | ||
| fn add_single_keypress_mapping( | ||
| machine: &mut HelixMachine, | ||
| mode: HelixMode, | ||
| code: KeyCode, | ||
| step: HelixStep, | ||
| ) { | ||
| let path: &HelixEdgePath = &[(EdgeRepeat::Once, EdgeEvent::Key(TerminalKey::from(code)))]; | ||
| machine.add_mapping(mode, path, &step); | ||
| } | ||
| } | ||
|
|
||
| impl InputBindings<TerminalKey, HelixStep> for HelixBindings { | ||
| fn setup(&self, machine: &mut HelixMachine) { | ||
| Self::add_single_keypress_mapping( | ||
| machine, | ||
| HelixMode::Insert, | ||
| KeyCode::Esc, | ||
| (None, Some(HelixMode::Normal)), | ||
| ); | ||
| Self::add_single_keypress_mapping( | ||
| machine, | ||
| HelixMode::Normal, | ||
| KeyCode::Char('i'), | ||
| (None, Some(HelixMode::Insert)), | ||
| ); | ||
| for code in [KeyCode::Char('h'), KeyCode::Left] { | ||
| Self::add_single_keypress_mapping( | ||
| machine, | ||
| HelixMode::Normal, | ||
| code, | ||
| (Some(HelixAction::MoveCharLeft), None), | ||
| ); | ||
| } | ||
| for code in [KeyCode::Char('l'), KeyCode::Right] { | ||
| Self::add_single_keypress_mapping( | ||
| machine, | ||
| HelixMode::Normal, | ||
| code, | ||
| (Some(HelixAction::MoveCharRight), None), | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// A minimal custom edit mode example for Helix-style integrations. | ||
| pub struct Helix { | ||
| machine: HelixMachine, | ||
| } | ||
|
|
||
| impl std::fmt::Debug for Helix { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| f.debug_struct("Helix") | ||
| .field("mode", &self.machine.mode()) | ||
| .finish_non_exhaustive() | ||
| } | ||
| } | ||
|
|
||
| impl Default for Helix { | ||
| fn default() -> Self { | ||
| Self::new(PromptViMode::Insert) | ||
| } | ||
| } | ||
|
|
||
| impl Helix { | ||
| /// Creates a Helix editor with the requested initial mode. | ||
| pub fn new(initial_mode: PromptViMode) -> Self { | ||
| let mut machine = HelixMachine::from_bindings::<HelixBindings>(); | ||
|
|
||
| if matches!(initial_mode, PromptViMode::Normal) { | ||
| machine.input_key(TerminalKey::from(KeyCode::Esc)); | ||
| let _ = machine.pop(); | ||
| } | ||
| Self { machine } | ||
| } | ||
| } | ||
|
|
||
| impl EditMode for Helix { | ||
| fn parse_event(&mut self, event: ReedlineRawEvent) -> ReedlineEvent { | ||
| match Event::from(event) { | ||
| Event::Key(KeyEvent { | ||
| let Ok(key_event) = KeyEvent::try_from(event) else { | ||
| return ReedlineEvent::None; | ||
| }; | ||
|
|
||
| if matches!( | ||
| &key_event, | ||
| KeyEvent { | ||
| code: KeyCode::Char('c'), | ||
| modifiers: KeyModifiers::CONTROL, | ||
| .. | ||
| }) => ReedlineEvent::CtrlC, | ||
| _ => ReedlineEvent::None, | ||
| } | ||
| ) { | ||
| return ReedlineEvent::CtrlC; | ||
| } | ||
| } | ||
|
|
||
| fn edit_mode(&self) -> PromptEditMode { | ||
| PromptEditMode::Vi(PromptViMode::Normal) | ||
| let previous_mode = self.machine.mode(); | ||
| self.machine.input_key(key_event.into()); | ||
| let mode_changed = self.machine.mode() != previous_mode; | ||
|
|
||
| let Some((action, _ctx)) = self.machine.pop() else { | ||
| return if mode_changed { | ||
| ReedlineEvent::Repaint | ||
| } else { | ||
| ReedlineEvent::None | ||
| }; | ||
| }; | ||
|
|
||
| match action { | ||
| HelixAction::Type(c) => ReedlineEvent::Edit(vec![EditCommand::InsertChar(c)]), | ||
| HelixAction::MoveCharLeft => { | ||
| ReedlineEvent::Edit(vec![EditCommand::MoveLeft { select: false }]) | ||
| } | ||
| HelixAction::MoveCharRight => { | ||
| ReedlineEvent::Edit(vec![EditCommand::MoveRight { select: false }]) | ||
| } | ||
| HelixAction::NoOp => { | ||
| if mode_changed { | ||
| ReedlineEvent::Repaint | ||
| } else { | ||
| ReedlineEvent::None | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn handle_mode_specific_event(&mut self, _event: ReedlineEvent) -> EventStatus { | ||
| EventStatus::Inapplicable | ||
| fn edit_mode(&self) -> PromptEditMode { | ||
| match self.machine.mode() { | ||
| HelixMode::Insert => PromptEditMode::Vi(PromptViMode::Insert), | ||
| HelixMode::Normal => PromptEditMode::Vi(PromptViMode::Normal), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::PromptViMode; | ||
| use crossterm::event::{Event, KeyEventKind, KeyEventState}; | ||
| use rstest::rstest; | ||
|
|
||
| fn key_press(code: KeyCode, modifiers: KeyModifiers) -> ReedlineRawEvent { | ||
| Event::Key(KeyEvent { | ||
| code, | ||
| modifiers, | ||
| kind: KeyEventKind::Press, | ||
| state: KeyEventState::NONE, | ||
| }) | ||
| .try_into() | ||
| .expect("valid crossterm key event") | ||
| } | ||
|
|
||
| #[test] | ||
| fn helix_edit_mode_defaults_to_normal_mode() { | ||
| let helix_mode = Helix; | ||
| fn helix_editor_defaults_to_insert_mode() { | ||
| let helix_editor = Helix::default(); | ||
|
|
||
| let edit_mode = helix_mode.edit_mode(); | ||
| assert!(matches!( | ||
| helix_editor.edit_mode(), | ||
| PromptEditMode::Vi(PromptViMode::Insert) | ||
| )); | ||
| } | ||
|
|
||
| #[test] | ||
| fn ctrl_c_maps_to_interrupt_event() { | ||
| let mut helix_mode = Helix::default(); | ||
|
|
||
| assert_eq!( | ||
| helix_mode.parse_event(key_press(KeyCode::Char('c'), KeyModifiers::CONTROL)), | ||
| ReedlineEvent::CtrlC | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn pressing_esc_in_insert_mode_switches_to_normal() { | ||
| let mut helix_mode = Helix::new(PromptViMode::Insert); | ||
|
|
||
| assert_eq!( | ||
| helix_mode.parse_event(key_press(KeyCode::Esc, KeyModifiers::NONE)), | ||
| ReedlineEvent::Repaint | ||
| ); | ||
|
|
||
| assert!(matches!( | ||
| edit_mode, | ||
| helix_mode.edit_mode(), | ||
| PromptEditMode::Vi(PromptViMode::Normal) | ||
| )); | ||
| } | ||
|
|
||
| #[test] | ||
| fn helix_edit_mode_parses_ctrl_c_event() { | ||
| let mut helix_mode = Helix; | ||
| let ctrl_c_raw_event = ReedlineRawEvent::try_from(Event::Key(KeyEvent::new( | ||
| KeyCode::Char('c'), | ||
| KeyModifiers::CONTROL, | ||
| ))); | ||
| fn pressing_i_in_normal_mode_switches_to_insert() { | ||
| let mut helix_mode = Helix::new(PromptViMode::Normal); | ||
|
|
||
| assert_eq!( | ||
| helix_mode.parse_event(ctrl_c_raw_event.unwrap()), | ||
| ReedlineEvent::CtrlC | ||
| helix_mode.parse_event(key_press(KeyCode::Char('i'), KeyModifiers::NONE)), | ||
| ReedlineEvent::Repaint | ||
| ); | ||
| assert!(matches!( | ||
| helix_mode.edit_mode(), | ||
| PromptEditMode::Vi(PromptViMode::Insert) | ||
| )); | ||
| } | ||
|
|
||
| #[test] | ||
| fn typing_in_insert_mode_produces_insert_char_event() { | ||
| let mut helix_mode = Helix::new(PromptViMode::Insert); | ||
|
|
||
| assert_eq!( | ||
| helix_mode.parse_event(key_press(KeyCode::Char('a'), KeyModifiers::NONE)), | ||
| ReedlineEvent::Edit(vec![EditCommand::InsertChar('a')]) | ||
| ); | ||
| } | ||
|
|
||
| #[rstest] | ||
| #[case(KeyCode::Char('h'))] | ||
| #[case(KeyCode::Left)] | ||
| fn pressing_left_key_or_h_in_normal_mode_moves_cursor_left(#[case] key_code: KeyCode) { | ||
| let mut helix_mode = Helix::new(PromptViMode::Normal); | ||
|
|
||
| assert_eq!( | ||
| helix_mode.parse_event(key_press(key_code, KeyModifiers::NONE)), | ||
| ReedlineEvent::Edit(vec![EditCommand::MoveLeft { select: false }]) | ||
| ); | ||
| } | ||
|
|
||
| #[rstest] | ||
| #[case(KeyCode::Char('l'))] | ||
| #[case(KeyCode::Right)] | ||
| fn pressing_right_key_or_l_in_normal_mode_moves_cursor_right(#[case] key_code: KeyCode) { | ||
| let mut helix_mode = Helix::new(PromptViMode::Normal); | ||
|
|
||
| assert_eq!( | ||
| helix_mode.parse_event(key_press(key_code, KeyModifiers::NONE)), | ||
| ReedlineEvent::Edit(vec![EditCommand::MoveRight { select: false }]) | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.