diff --git a/crates/typos-vars/codegen/src/main.rs b/crates/typos-vars/codegen/src/main.rs index c718ee8..981a85c 100644 --- a/crates/typos-vars/codegen/src/main.rs +++ b/crates/typos-vars/codegen/src/main.rs @@ -76,6 +76,9 @@ fn generate_variations(file: &mut W) { writeln!(file, "}}").unwrap(); writeln!(file).unwrap(); + let mut smallest = usize::MAX; + let mut largest = usize::MIN; + writeln!( file, "pub static VARS_DICTIONARY: phf::Map, &'static [(u8, &VariantsMap)]> = " @@ -92,11 +95,17 @@ fn generate_variations(file: &mut W) { referenced_symbols.extend(data.iter().map(|(s, _)| s)); let value = generate_link(&data); builder.entry(unicase::UniCase::new(word), &value); + smallest = std::cmp::min(smallest, word.len()); + largest = std::cmp::max(largest, word.len()); } let codegenned = builder.build(); writeln!(file, "{}", codegenned).unwrap(); writeln!(file, ";").unwrap(); + writeln!(file).unwrap(); + writeln!(file, "pub const WORD_MIN: usize = {};", smallest).unwrap(); + writeln!(file, "pub const WORD_MAX: usize = {};", largest).unwrap(); + for (symbol, entry) in entries.iter() { if !referenced_symbols.contains(symbol.as_str()) { continue; diff --git a/crates/typos-vars/src/vars_codegen.rs b/crates/typos-vars/src/vars_codegen.rs index 03ac102..e511b01 100644 --- a/crates/typos-vars/src/vars_codegen.rs +++ b/crates/typos-vars/src/vars_codegen.rs @@ -113081,6 +113081,9 @@ pub static VARS_DICTIONARY: phf::Map< ), ]), }; + +pub const WORD_MIN: usize = 2; +pub const WORD_MAX: usize = 24; pub(crate) static ENTRY_ABETTORS_7043394254318611656: VariantsMap = [&["abettors"], &["abetters"], &["abettors"], &["abetters"]]; diff --git a/src/dict.rs b/src/dict.rs index 504bec1..d0e0aa9 100644 --- a/src/dict.rs +++ b/src/dict.rs @@ -54,7 +54,14 @@ impl BuiltIn { } fn correct_with_vars(&self, word: &str) -> Option> { - map_lookup(&typos_vars::VARS_DICTIONARY, word).map(|variants| self.select_variant(variants)) + const WORD_RANGE: std::ops::RangeInclusive = + typos_vars::WORD_MIN..=typos_vars::WORD_MAX; + if WORD_RANGE.contains(&word.len()) { + map_lookup(&typos_vars::VARS_DICTIONARY, word) + .map(|variants| self.select_variant(variants)) + } else { + None + } } fn select_variant(