Skip to content
Draft
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
62 changes: 43 additions & 19 deletions src/edit_mode/vi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ impl Vi {
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) {
Event::Key(
key_event @ KeyEvent {
code, modifiers, ..
},
) => match (self.mode, modifiers, code) {
(ViMode::Normal, KeyModifiers::NONE, KeyCode::Char('v')) => {
self.cache.clear();
self.mode = ViMode::Visual;
Expand All @@ -91,6 +93,8 @@ impl EditMode for Vi {
.find_binding(modifiers, KeyCode::Char(c))
{
event
} else if let Some(event) = self.handle_alt_as_esc(key_event) {
event
} else if modifier == KeyModifiers::NONE || modifier == KeyModifiers::SHIFT {
self.cache.push(if modifier == KeyModifiers::SHIFT {
c.to_ascii_uppercase()
Expand Down Expand Up @@ -134,23 +138,28 @@ impl EditMode for Vi {
self.insert_keybindings
.find_binding(modifier, KeyCode::Char(c))
.unwrap_or_else(|| {
if modifier == KeyModifiers::NONE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this part, with or without your changes. Do you have any clue?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, it seems to be handling inputs with key modifiers. His change checks for alt at the beginning and handles it like vim does there, then does the other checks reedline does already.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to just be checking if it can enter that key as a regular input (not handling cases where just the control modifier is held) and also making the key uppercase if shift is held.

My function does "consume" alt inputs (by flipping the alt bit) so checking if alt is pressed in that list is a bit redundant hence the comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean what's so special about ctrl + alt and ctrl + alt + shift? Why not just check whether shift is pressed? The comments before make me even more confused...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

honestly not entirely sure but I've decided to keep the original logic in-case it's just something I don't understand

|| modifier == KeyModifiers::SHIFT
|| modifier == KeyModifiers::CONTROL | KeyModifiers::ALT
|| modifier
== KeyModifiers::CONTROL
| KeyModifiers::ALT
| KeyModifiers::SHIFT
{
ReedlineEvent::Edit(vec![EditCommand::InsertChar(
if modifier == KeyModifiers::SHIFT {
c.to_ascii_uppercase()
} else {
c
},
)])
if let Some(event) = self.handle_alt_as_esc(key_event) {
event
} else {
ReedlineEvent::None
// including alt here is a bit redundant now
if modifier == KeyModifiers::NONE
|| modifier == KeyModifiers::SHIFT
|| modifier == KeyModifiers::CONTROL | KeyModifiers::ALT
|| modifier
== KeyModifiers::CONTROL
| KeyModifiers::ALT
| KeyModifiers::SHIFT
{
ReedlineEvent::Edit(vec![EditCommand::InsertChar(
if modifier == KeyModifiers::SHIFT {
c.to_ascii_uppercase()
} else {
c
},
)])
} else {
ReedlineEvent::None
}
}
})
}
Expand Down Expand Up @@ -204,6 +213,21 @@ impl EditMode for Vi {
}
}

impl Vi {
fn handle_alt_as_esc(&mut self, mut event: KeyEvent) -> Option<ReedlineEvent> {
if event.modifiers.contains(KeyModifiers::ALT) {
self.mode = ViMode::Normal;
event.modifiers &= !KeyModifiers::ALT;

ReedlineRawEvent::try_from(Event::Key(event))
.ok()
.map(|event| self.parse_event(event))
} else {
None
}
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down