Skip to content
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

incomplete key sequences should be completed first #693

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
56 changes: 51 additions & 5 deletions src/edit_mode/vi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct Vi {
previous: Option<ReedlineEvent>,
// last f, F, t, T motion for ; and ,
last_char_search: Option<ViCharSearch>,
seq_completed: bool,
}

impl Default for Vi {
Expand All @@ -41,6 +42,7 @@ impl Default for Vi {
mode: ViMode::Insert,
previous: None,
last_char_search: None,
seq_completed: true,
}
}
}
Expand All @@ -65,20 +67,21 @@ impl EditMode for Vi {
(ViMode::Normal, modifier, KeyCode::Char(c)) => {
let c = c.to_ascii_lowercase();

if let Some(event) = self
let binding = self
.normal_keybindings
.find_binding(modifiers, KeyCode::Char(c))
.find_binding(modifiers, KeyCode::Char(c));
if !self.seq_completed
|| binding.is_none()
&& (modifier == KeyModifiers::NONE || modifier == KeyModifiers::SHIFT)
{
event
} else if modifier == KeyModifiers::NONE || modifier == KeyModifiers::SHIFT {
self.cache.push(if modifier == KeyModifiers::SHIFT {
c.to_ascii_uppercase()
} else {
c
});

let res = parse(&mut self.cache.iter().peekable());

self.seq_completed = res.is_complete();
if !res.is_valid() {
self.cache.clear();
ReedlineEvent::None
Expand All @@ -93,6 +96,8 @@ impl EditMode for Vi {
} else {
ReedlineEvent::None
}
} else if let Some(event) = binding {
event
} else {
ReedlineEvent::None
}
Expand Down Expand Up @@ -277,4 +282,45 @@ mod test {

assert_eq!(result, ReedlineEvent::None);
}

#[test]
fn find_custom_keybinding_test() {
let mut keybindings = default_vi_normal_keybindings();
keybindings.add_binding(
KeyModifiers::SHIFT,
KeyCode::Char('B'),
ReedlineEvent::Edit(vec![EditCommand::MoveBigWordLeft { select: false }]),
);
let mut vi = Vi {
insert_keybindings: default_vi_insert_keybindings(),
normal_keybindings: keybindings,
mode: ViMode::Normal,
..Default::default()
};

let _ = vi.parse_event(
ReedlineRawEvent::convert_from(Event::Key(KeyEvent::new(
KeyCode::Char('f'),
KeyModifiers::NONE,
)))
.unwrap(),
);
let res = vi.parse_event(
ReedlineRawEvent::convert_from(Event::Key(KeyEvent::new(
KeyCode::Char('B'),
KeyModifiers::SHIFT,
)))
.unwrap(),
);

assert_eq!(
res,
ReedlineEvent::Multiple(vec![ReedlineEvent::Edit(vec![
EditCommand::MoveRightUntil {
c: 'B',
select: false
}
])])
);
}
}
Loading