diff --git a/crates/typos-vars/codegen/src/main.rs b/crates/typos-vars/codegen/src/main.rs index d861dd92e..9f2225fa2 100644 --- a/crates/typos-vars/codegen/src/main.rs +++ b/crates/typos-vars/codegen/src/main.rs @@ -78,6 +78,7 @@ fn generate_variations(file: &mut W) { let mut smallest = usize::MAX; let mut largest = usize::MIN; + let mut no_invalid = true; writeln!( file, @@ -97,6 +98,8 @@ fn generate_variations(file: &mut W) { builder.entry(unicase::UniCase::new(word), &value); smallest = std::cmp::min(smallest, word.len()); largest = std::cmp::max(largest, word.len()); + + no_invalid &= !is_always_invalid(data); } let codegenned = builder.build(); writeln!(file, "{}", codegenned).unwrap(); @@ -110,6 +113,10 @@ fn generate_variations(file: &mut W) { ) .unwrap(); + writeln!(file).unwrap(); + writeln!(file, "pub const NO_INVALID: bool = {:?};", no_invalid,).unwrap(); + + writeln!(file).unwrap(); for (symbol, entry) in entries.iter() { if !referenced_symbols.contains(symbol.as_str()) { continue; @@ -156,6 +163,15 @@ fn is_always_valid(data: &[(&str, varcon::CategorySet)]) -> bool { false } +fn is_always_invalid(data: &[(&str, varcon::CategorySet)]) -> bool { + for (_symbol, set) in data.iter() { + if set.is_empty() { + return true; + } + } + false +} + fn entries() -> BTreeMap { varcon::VARCON .iter() diff --git a/crates/typos-vars/src/vars_codegen.rs b/crates/typos-vars/src/vars_codegen.rs index 138106457..2664e00e7 100644 --- a/crates/typos-vars/src/vars_codegen.rs +++ b/crates/typos-vars/src/vars_codegen.rs @@ -113083,6 +113083,9 @@ pub static VARS_DICTIONARY: phf::Map< }; pub const WORD_RANGE: std::ops::RangeInclusive = 2..=24; + +pub const NO_INVALID: bool = true; + pub(crate) static ENTRY_ABETTORS_7043394254318611656: VariantsMap = [&["abettors"], &["abetters"], &["abettors"], &["abetters"]]; diff --git a/src/dict.rs b/src/dict.rs index 5541f5dea..4b8f6c997 100644 --- a/src/dict.rs +++ b/src/dict.rs @@ -72,26 +72,30 @@ impl BuiltIn { #[cfg(feature = "vars")] impl BuiltIn { fn chain_with_vars(&self, corrections: &'static [&'static str]) -> Status<'static> { - let mut chained: Vec<_> = corrections - .iter() - .flat_map(|c| match self.correct_with_vars(c) { - Some(Status::Valid) | None => vec![Cow::Borrowed(*c)], - Some(Status::Corrections(vars)) => vars, - Some(Status::Invalid) => { - unreachable!("correct_with_vars should always have valid suggestions") - } - }) - .collect(); - if chained.len() != 1 { - chained.sort_unstable(); - chained.dedup(); + if self.is_vars_enabled() { + let mut chained: Vec<_> = corrections + .iter() + .flat_map(|c| match self.correct_with_vars(c) { + Some(Status::Valid) | None => vec![Cow::Borrowed(*c)], + Some(Status::Corrections(vars)) => vars, + Some(Status::Invalid) => { + unreachable!("correct_with_vars should always have valid suggestions") + } + }) + .collect(); + if chained.len() != 1 { + chained.sort_unstable(); + chained.dedup(); + } + debug_assert!(!chained.is_empty()); + Status::Corrections(chained) + } else { + Status::Corrections(corrections.iter().map(|c| Cow::Borrowed(*c)).collect()) } - debug_assert!(!chained.is_empty()); - Status::Corrections(chained) } fn correct_with_vars(&self, word: &str) -> Option> { - if typos_vars::WORD_RANGE.contains(&word.len()) { + if self.is_vars_enabled() && typos_vars::WORD_RANGE.contains(&word.len()) { map_lookup(&typos_vars::VARS_DICTIONARY, word) .map(|variants| self.select_variant(variants)) } else { @@ -99,6 +103,11 @@ impl BuiltIn { } } + fn is_vars_enabled(&self) -> bool { + debug_assert!(typos_vars::NO_INVALID); + self.locale.is_some() + } + fn select_variant( &self, vars: &'static [(u8, &'static typos_vars::VariantsMap)],