Handle suffixes of cancelled keymaps in insert mode

pull/8486/head
Ken Micklas 1 year ago
parent 079f544260
commit e0d825fb8a

@ -907,28 +907,37 @@ impl EditorView {
} }
fn insert_mode(&mut self, cx: &mut commands::Context, event: KeyEvent) { fn insert_mode(&mut self, cx: &mut commands::Context, event: KeyEvent) {
if let Some(keyresult) = self.handle_keymap_event(Mode::Insert, cx, event) { let mut stack = vec![event];
match keyresult {
KeymapResult::NotFound => { while let Some(event) = stack.pop() {
if let Some(ch) = event.char() { if let Some(keyresult) = self.handle_keymap_event(Mode::Insert, cx, event) {
commands::insert::insert_char(cx, ch) match keyresult {
KeymapResult::NotFound => {
if let Some(ch) = event.char() {
commands::insert::insert_char(cx, ch)
}
} }
} KeymapResult::Cancelled(pending) => {
KeymapResult::Cancelled(pending) => { let mut pending = pending.into_iter();
for ev in pending { if let Some(first) = pending.next() {
match ev.char() { // Note that since this is the first pending key, we know
Some(ch) => commands::insert::insert_char(cx, ch), // it can't map to a command by itself.
None => { match first.char() {
if let KeymapResult::Matched(command) = // The first key is both the start of a menu and a regular
self.keymaps.get(Mode::Insert, ev) // insert key. The user may have intended to type it as an
{ // insert, and then execute the remaining suffix of keys.
command.execute(cx); Some(ch) => commands::insert::insert_char(cx, ch),
} // If the first key is not a character to insert, then we
// assume the user intended to enter a command menu, so we
// should just discard pending keys if they don't match.
None => continue,
} }
} }
stack.extend(pending.rev());
} }
_ => unreachable!(),
} }
_ => unreachable!(),
} }
} }
} }

@ -18,6 +18,7 @@ mod test {
mod auto_indent; mod auto_indent;
mod auto_pairs; mod auto_pairs;
mod commands; mod commands;
mod insert_keymap_suffix;
mod languages; mod languages;
mod movement; mod movement;
mod prompt; mod prompt;

@ -349,8 +349,6 @@ impl AppBuilder {
self self
} }
// Remove this attribute once `with_config` is used in a test:
#[allow(dead_code)]
pub fn with_config(mut self, mut config: Config) -> Self { pub fn with_config(mut self, mut config: Config) -> Self {
let keys = replace(&mut config.keys, helix_term::keymap::default()); let keys = replace(&mut config.keys, helix_term::keymap::default());
merge_keys(&mut config.keys, keys); merge_keys(&mut config.keys, keys);

@ -0,0 +1,36 @@
use super::*;
#[tokio::test(flavor = "multi_thread")]
async fn insert_keymap_suffix() -> anyhow::Result<()> {
test_with_config(
AppBuilder::new().with_config(config()),
("#[|]#", "iselffd", "self#[|]#"),
)
.await?;
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn insert_keymap_suffix_non_char() -> anyhow::Result<()> {
test_with_config(
AppBuilder::new().with_config(config()),
("#[|]#", "i<F1>ua", "a#[|]#"),
)
.await?;
Ok(())
}
fn config() -> Config {
let config = r#"
[keys.insert]
f.d = "normal_mode"
F1.j = "insert_newline"
"#;
Config::load(
Ok(config.to_owned()),
Err(helix_term::config::ConfigLoadError::default()),
)
.unwrap()
}
Loading…
Cancel
Save