Refactor commands
This commit is contained in:
1 parent
60dc9ebe92
commit
1a8a96e1cd
3 files changed
+18
-16
No files matched your search
@@ -21,7 +21,7 @@ impl Configuration {
|
||||
|
||||
Some(match input {
|
||||
Input::Character(ch) => match mode {
|
||||
Mode::Insert => InsertCharacter(ch),
|
||||
Mode::Insert | Mode::Command => InsertCharacter(ch),
|
||||
Mode::Normal if ch == 'i' => TransitionToMode(Mode::Insert),
|
||||
Mode::Normal if ch == 'q' => Quit,
|
||||
Mode::Normal if ch == 'h' => MoveCursor(CursorMovement::OppositeTextDirection),
|
||||
@@ -29,7 +29,6 @@ impl Configuration {
|
||||
Mode::Normal if ch == 'k' => MoveCursor(CursorMovement::PrevLine),
|
||||
Mode::Normal if ch == 'l' => MoveCursor(CursorMovement::TextDirection),
|
||||
Mode::Normal if ch == ':' => TransitionToMode(Mode::Command),
|
||||
Mode::Command => Command(CommandAction::Insert(ch)),
|
||||
_ => return None,
|
||||
},
|
||||
Input::Newline => match mode {
|
||||
@@ -38,9 +37,8 @@ impl Configuration {
|
||||
_ => return None,
|
||||
},
|
||||
Input::Backspace => match mode {
|
||||
Mode::Insert => DeleteChar,
|
||||
Mode::Insert | Mode::Command => DeleteChar,
|
||||
Mode::Normal => return None,
|
||||
Mode::Command => Command(CommandAction::Delete),
|
||||
},
|
||||
Input::Escape => match mode {
|
||||
Mode::Insert => TransitionToMode(Mode::Normal),
|
||||
|
||||
+16
-10
@@ -155,21 +155,27 @@ impl EditorCore {
|
||||
#[tracing::instrument(skip(self))]
|
||||
fn handle_action(&mut self, action: EditorAction) {
|
||||
use EditorAction::*;
|
||||
let mode = self.mode;
|
||||
|
||||
match action {
|
||||
Command(command) => match command {
|
||||
CommandAction::Insert(ch) => self.commands.add_char(ch),
|
||||
CommandAction::Delete => self.commands.del_char(),
|
||||
CommandAction::Execute => self.commands.execute(),
|
||||
CommandAction::Tab => self.commands.tab(),
|
||||
},
|
||||
InsertCharacter(ch) => {
|
||||
let buf = self.active_buffer_mut();
|
||||
buf.insert_char(ch);
|
||||
}
|
||||
DeleteChar => {
|
||||
let buf = self.active_buffer_mut();
|
||||
buf.delete_char();
|
||||
}
|
||||
InsertCharacter(ch) => match mode {
|
||||
Mode::Command => self.commands.add_char(ch),
|
||||
_ => {
|
||||
let buf = self.active_buffer_mut();
|
||||
buf.insert_char(ch);
|
||||
}
|
||||
},
|
||||
DeleteChar => match mode {
|
||||
Mode::Command => self.commands.del_char(),
|
||||
_ => {
|
||||
let buf = self.active_buffer_mut();
|
||||
buf.delete_char();
|
||||
}
|
||||
},
|
||||
TransitionToMode(mode) => {
|
||||
self.mode = mode;
|
||||
if let Mode::Command = mode {
|
||||
|
||||
@@ -39,8 +39,6 @@ pub(crate) enum CursorMovement {
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum CommandAction {
|
||||
Insert(char),
|
||||
Delete,
|
||||
Execute,
|
||||
Tab,
|
||||
}
|
||||
Reference in new issue
Block a user