Skip to content

Commit

Permalink
Improve restore word and stop tracking check performance
Browse files Browse the repository at this point in the history
  • Loading branch information
huytd committed Jul 8, 2023
1 parent 36f5b34 commit 517dedc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
11 changes: 4 additions & 7 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<InputState> = Lazy::new(InputState::new);

Expand Down Expand Up @@ -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
}

Expand Down
11 changes: 7 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ fn event_handler(handle: Handle, keycode: Option<char>, 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();
Expand All @@ -75,7 +77,7 @@ fn event_handler(handle: Handle, keycode: Option<char>, 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
Expand All @@ -88,7 +90,6 @@ fn event_handler(handle: Handle, keycode: Option<char>, 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()
Expand All @@ -97,7 +98,9 @@ fn event_handler(handle: Handle, keycode: Option<char>, 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;
}
}
}
Expand Down

0 comments on commit 517dedc

Please sign in to comment.