From 517dedc02259d09a4738c2e956ef61104d5264e7 Mon Sep 17 00:00:00 2001 From: Huy Tran Date: Sat, 8 Jul 2023 01:02:29 -0700 Subject: [PATCH] Improve restore word and stop tracking check performance --- src/input.rs | 11 ++++------- src/main.rs | 11 +++++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/input.rs b/src/input.rs index 827411d..6917ca4 100644 --- a/src/input.rs +++ b/src/input.rs @@ -18,7 +18,7 @@ use crate::{ // be around 10 to 12. const MAX_POSSIBLE_WORD_LENGTH: usize = 10; const MAX_DUPLICATE_LENGTH: usize = 4; -const TONE_DUPLICATE_PATTERNS: [&str; 6] = ["ss", "ff", "jj", "rr", "xx", "ww"]; +const TONE_DUPLICATE_PATTERNS: [&str; 16] = ["ss", "ff", "jj", "rr", "xx", "ww", "kk", "tt", "nn", "mm", "yy", "hh", "ii", "aaa", "eee", "ooo"]; pub static mut INPUT_STATE: Lazy = Lazy::new(InputState::new); @@ -305,18 +305,15 @@ impl InputState { // later on. pub fn should_stop_tracking(&mut self) -> bool { let len = self.buffer.len(); + if len > MAX_POSSIBLE_WORD_LENGTH { + return true; + } // detect attempts to restore a word // by doubling tone marks like ss, rr, ff, jj, xx let buf = &self.buffer; if TONE_DUPLICATE_PATTERNS.iter().find(|p| buf.contains(*p)).is_some() { return true; } - // detect things like vim key movements - if len >= MAX_DUPLICATE_LENGTH { - let buf = &self.buffer[len - MAX_DUPLICATE_LENGTH..]; - let first = buf.chars().next().unwrap(); - return buf.chars().all(|c| c == first); - } false } diff --git a/src/main.rs b/src/main.rs index e4df4ee..834a0e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -66,7 +66,9 @@ fn event_handler(handle: Handle, keycode: Option, modifiers: KeyModifier) if INPUT_STATE.is_enabled() { match keycode { KEY_ENTER | KEY_TAB | KEY_SPACE | KEY_ESCAPE => { - if !vi::validation::is_valid_word(INPUT_STATE.get_displaying_word()) { + let is_valid_word = vi::validation::is_valid_word(INPUT_STATE.get_displaying_word()); + let is_transformed_word = !INPUT_STATE.get_typing_buffer().eq(INPUT_STATE.get_displaying_word()); + if is_transformed_word && !is_valid_word { do_restore_word(handle); } INPUT_STATE.new_word(); @@ -75,7 +77,7 @@ fn event_handler(handle: Handle, keycode: Option, modifiers: KeyModifier) INPUT_STATE.clear(); } c => { - if "()[]{}<>/\\!@#$%^&*-_=+|~`'\"".contains(c) + if "()[]{}<>/\\!@#$%^&*-_=+|~`,.?'\"".contains(c) || (c.is_numeric() && modifiers.is_shift()) { // If special characters detected, dismiss the current tracking word @@ -88,7 +90,6 @@ fn event_handler(handle: Handle, keycode: Option, modifiers: KeyModifier) { INPUT_STATE.new_word(); } else if INPUT_STATE.is_tracking() { - INPUT_STATE.stop_tracking_if_needed(); INPUT_STATE.push( if modifiers.is_shift() || modifiers.is_capslock() { c.to_ascii_uppercase() @@ -97,7 +98,9 @@ fn event_handler(handle: Handle, keycode: Option, modifiers: KeyModifier) }, ); if INPUT_STATE.should_transform_keys(&c) { - return do_transform_keys(handle, false); + let ret = do_transform_keys(handle, false); + INPUT_STATE.stop_tracking_if_needed(); + return ret; } } }