Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: `Word` ctor for standard case combos; fave → favourite (#3263)
Andrew Dunbar11f0671049e5
diff --git a/harper-core/default_config.json b/harper-core/default_config.jsonindex 3283a0670..f545abaeb 100644--- a/harper-core/default_config.json+++ b/harper-core/default_config.json@@ -3032,6 +3032,13 @@"label": "Expand Control"}},+ {+ "Bool": {+ "name": "ExpandFavourite",+ "state": true,+ "label": "Expand Favourite"+ }+ },{"Bool": {"name": "ExpandForward",diff --git a/harper-core/src/linting/expand_favourite.rs b/harper-core/src/linting/expand_favourite.rsnew file mode 100644index 000000000..63a70a2aa--- /dev/null+++ b/harper-core/src/linting/expand_favourite.rs@@ -0,0 +1,157 @@+use crate::{+ Dialect, Lint, Token, TokenStringExt,+ expr::{Expr, FirstMatchOf},+ linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},+ patterns::Word,+};++pub struct ExpandFavourite {+ expr: FirstMatchOf,+ dialect: Dialect,+}++impl ExpandFavourite {+ pub fn new(dialect: Dialect) -> Self {+ Self {+ expr: FirstMatchOf::new(vec![+ Box::new(Word::new_standard_case("fav")),+ Box::new(Word::new_standard_case("favs")),+ Box::new(Word::new_standard_case("fave")),+ Box::new(Word::new_standard_case("faves")),+ ]),+ dialect,+ }+ }+}++impl ExprLinter for ExpandFavourite {+ type Unit = Chunk;++ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {+ let span = toks.span()?;++ let abbr = span.get_content_string(src).to_ascii_lowercase();++ let expanded = match (self.dialect, abbr.ends_with('s')) {+ (Dialect::American, false) => "favorite",+ (Dialect::American, true) => "favorites",+ (_, false) => "favourite",+ (_, true) => "favourites",+ };++ let suggestions = vec![Suggestion::replace_with_match_case_str(+ expanded,+ span.get_content(src),+ )];+ let message = format!("Use `{expanded}` instead of `{abbr}`.");++ Some(Lint {+ span,+ lint_kind: LintKind::Style,+ suggestions,+ message,+ ..Default::default()+ })+ }++ fn expr(&self) -> &dyn Expr {+ &self.expr+ }++ fn description(&self) -> &str {+ "Expands the abbreviations `fav` and `fave` to the full word `favorite` or `favourite` for clarity."+ }+}++#[cfg(test)]+mod tests {+ use crate::{+ Dialect,+ linting::tests::{assert_no_lints, assert_suggestion_result},+ };++ use super::ExpandFavourite;++ // Basic case tests++ #[test]+ fn fix_lowercase() {+ assert_suggestion_result(+ "It's my fav",+ ExpandFavourite::new(Dialect::American),+ "It's my favorite",+ );+ }++ #[test]+ fn fix_all_caps() {+ assert_suggestion_result(+ "IT'S MY FAV",+ ExpandFavourite::new(Dialect::American),+ "IT'S MY FAVORITE",+ );+ }++ #[test]+ fn fix_title_case() {+ assert_suggestion_result(+ "It's My Fav",+ ExpandFavourite::new(Dialect::American),+ "It's My Favorite",+ );+ }++ #[test]+ fn dont_flag_pascal_case() {+ assert_no_lints(+ "It's a project called FaVe",+ ExpandFavourite::new(Dialect::American),+ );+ }++ // Real-world tests++ #[test]+ fn fix_favs_lower_us() {+ assert_suggestion_result(+ "Each single favorite group - no matter if it contains 1 or 1000 favs - in here is displayed as 11MB",+ ExpandFavourite::new(Dialect::American),+ "Each single favorite group - no matter if it contains 1 or 1000 favorites - in here is displayed as 11MB",+ );+ }++ #[test]+ fn fix_favs_title_au() {+ assert_suggestion_result(+ "Lost all my Favs again on reinstalling mac - got angry - then used claude.ai to help make a tool",+ ExpandFavourite::new(Dialect::Australian),+ "Lost all my Favourites again on reinstalling mac - got angry - then used claude.ai to help make a tool",+ );+ }++ #[test]+ fn dont_flag_fave_mixed_case_uk() {+ assert_no_lints(+ "FaVe employs end-to-end encryption and leverages the inherent security features of FairOS and Swarm to ensure data integrity and confidentiality.",+ ExpandFavourite::new(Dialect::British),+ );+ }++ #[test]+ fn fix_fave_and_faves_ca() {+ assert_suggestion_result(+ "Once faves are created, a user's fave posts can be queried",+ ExpandFavourite::new(Dialect::Canadian),+ "Once favourites are created, a user's favourite posts can be queried",+ );+ }++ #[test]+ fn fix_fav_title_in() {+ assert_suggestion_result(+ "Save your Data ,by not Streaming your Fav Songs Online again & again(Just Download Them!)",+ ExpandFavourite::new(Dialect::Indian),+ "Save your Data ,by not Streaming your Favourite Songs Online again & again(Just Download Them!)",+ );+ }+}diff --git a/harper-core/src/linting/lint_group/mod.rs b/harper-core/src/linting/lint_group/mod.rsindex 256691eed..92672af38 100644--- a/harper-core/src/linting/lint_group/mod.rs+++ b/harper-core/src/linting/lint_group/mod.rs@@ -91,6 +91,7 @@ use super::ever_every::EverEvery;use super::ever_pronoun_rel_pronoun::EverPronounRelPronoun;use super::everyday::Everyday;use super::except_of::ExceptOf;+use super::expand_favourite::ExpandFavourite;use super::expand_memory_shorthands::ExpandMemoryShorthands;use super::expand_people::ExpandPeople;use super::expand_time_shorthands::ExpandTimeShorthands;@@ -701,6 +702,7 @@ impl LintGroup {insert_expr_rule!(EverPronounRelPronoun);insert_expr_rule!(Everyday);insert_expr_rule!(ExceptOf);+ insert_expr_rule_with_dialect!(ExpandFavourite);insert_expr_rule!(ExpandMemoryShorthands);insert_expr_rule!(ExpandPeople);insert_expr_rule!(ExpandTimeShorthands);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 334d7e889..9b0037904 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -91,6 +91,7 @@ mod ever_every;mod ever_pronoun_rel_pronoun;mod everyday;mod except_of;+mod expand_favourite;mod expand_memory_shorthands;mod expand_people;mod expand_time_shorthands;diff --git a/harper-core/src/patterns/word.rs b/harper-core/src/patterns/word.rsindex b20caf7e3..49ed5a32e 100644--- a/harper-core/src/patterns/word.rs+++ b/harper-core/src/patterns/word.rs@@ -2,13 +2,24 @@ use super::SingleTokenPattern;use crate::{CharString, Token};+/// Determines how case matching is performed for a word pattern.+#[derive(Clone, Copy, PartialEq, Eq)]+enum CaseMatchingMode {+ /// Match regardless of case (case insensitive)+ CaseInsensitive,+ /// Match exact case only (case sensitive)+ CaseSensitive,+ /// Match only standard case patterns: all lowercase, all uppercase, or title case+ StandardCase,+}+/// Matches a predefined word.#[derive(Clone)]pub struct Word {/// The word to match.word: CharString,- /// Determines whether the match is case-sensitive.- case_sensitive: bool,+ /// Determines how case matching is performed.+ case_mode: CaseMatchingMode,}impl Word {@@ -16,7 +27,7 @@ impl Word {pub fn new(word: &'static str) -> Self {Self {word: word.chars().collect(),- case_sensitive: false,+ case_mode: CaseMatchingMode::CaseInsensitive,}}@@ -24,7 +35,7 @@ impl Word {pub fn from_chars(word: &[char]) -> Self {Self {word: word.iter().copied().collect(),- case_sensitive: false,+ case_mode: CaseMatchingMode::CaseInsensitive,}}@@ -32,7 +43,7 @@ impl Word {pub fn from_char_string(word: CharString) -> Self {Self {word,- case_sensitive: false,+ case_mode: CaseMatchingMode::CaseInsensitive,}}@@ -40,7 +51,41 @@ impl Word {pub fn new_exact(word: &'static str) -> Self {Self {word: word.chars().collect(),- case_sensitive: true,+ case_mode: CaseMatchingMode::CaseSensitive,+ }+ }++ /// Matches the provided word, but only if it follows standard case patterns:+ /// all lowercase, all uppercase, or title case (first letter uppercase, rest lowercase).+ /// This avoids matching trademarks, app names, and other mixed-case patterns.+ pub fn new_standard_case(word: &'static str) -> Self {+ Self {+ word: word.chars().collect(),+ case_mode: CaseMatchingMode::StandardCase,+ }+ }++ /// Checks if the given characters follow a standard case pattern:+ /// - All lowercase+ /// - All uppercase+ /// - Title case (first letter uppercase, rest lowercase)+ fn is_standard_case(chars: &[char]) -> bool {+ match chars.len() {+ 0 => false,+ 1 => true,+ _ => {+ let (first, rest) = chars.split_at(1);+ let c0 = first[0];+ let c0_is_lowercase = c0.is_lowercase();++ let (basis, rest) = if !c0_is_lowercase {+ (rest[0].is_lowercase(), &rest[1..])+ } else {+ (c0_is_lowercase, rest)+ };++ rest.iter().all(|c| c.is_lowercase() == basis)+ }}}}@@ -55,26 +100,35 @@ impl SingleTokenPattern for Word {}let chars = token.get_ch(source);- if self.case_sensitive {- chars == self.word.as_slice()- } else {- chars- .iter()- .zip(&self.word)- .all(|(a, b)| a.eq_ignore_ascii_case(b))++ match self.case_mode {+ CaseMatchingMode::CaseSensitive => chars == self.word.as_slice(),+ _ => {+ // Both CaseInsensitive and StandardCase need case-insensitive matching+ // but StandardCase has an additional filter+ if matches!(self.case_mode, CaseMatchingMode::StandardCase)+ && !Self::is_standard_case(chars)+ {+ return false;+ }+ chars+ .iter()+ .zip(&self.word)+ .all(|(a, b)| a.eq_ignore_ascii_case(b))+ }}}}#[cfg(test)]mod tests {- use crate::{Document, Span, patterns::DocPattern};+ use crate::{Document, Span, linting::tests::SpanVecExt, patterns::DocPattern};use super::Word;#[test]fn fruit() {- let doc = Document::new_markdown_default_curated("I ate a banana and an apple today.");+ let doc = Document::new_plain_english_curated("I ate a banana and an apple today.");assert_eq!(Word::new("banana").find_all_matches_in_doc(&doc),@@ -88,7 +142,7 @@ mod tests {#[test]fn fruit_whack_capitalization() {- let doc = Document::new_markdown_default_curated("I Ate A bAnaNa And aN apPlE today.");+ let doc = Document::new_plain_english_curated("I Ate A bAnaNa And aN apPlE today.");assert_eq!(Word::new("banana").find_all_matches_in_doc(&doc),@@ -99,4 +153,135 @@ mod tests {vec![]);}++ #[test]+ fn standard_case_basic_matches() {+ let doc =+ Document::new_plain_english_curated("I ate a banana and BANANA and Banana today.");++ // Should match all three standard case variations+ let matches = Word::new_standard_case("banana").find_all_matches_in_doc(&doc);+ assert_eq!(matches.to_strings(&doc), vec!["banana", "BANANA", "Banana"]);+ }++ #[test]+ fn standard_case_rejects_mixed_case() {+ let doc = Document::new_plain_english_curated("I saw iPhone and iPad and YouTube today.");++ // Should not match any mixed-case words+ let iphone_matches: Vec<String> = Word::new_standard_case("iphone")+ .find_all_matches_in_doc(&doc)+ .to_strings(&doc);+ let ipad_matches: Vec<String> = Word::new_standard_case("ipad")+ .find_all_matches_in_doc(&doc)+ .to_strings(&doc);+ let youtube_matches: Vec<String> = Word::new_standard_case("youtube")+ .find_all_matches_in_doc(&doc)+ .to_strings(&doc);++ assert_eq!(iphone_matches, Vec::<String>::new());+ assert_eq!(ipad_matches, Vec::<String>::new());+ assert_eq!(youtube_matches, Vec::<String>::new());+ }++ #[test]+ fn standard_case_rejects_pascal_and_camel_case() {+ let doc = Document::new_plain_english_curated(+ "I saw BananaTree and bananaTree and BaNaNa today.",+ );++ // Should not match PascalCase or camelCase+ let bananatree_matches: Vec<String> = Word::new_standard_case("bananatree")+ .find_all_matches_in_doc(&doc)+ .to_strings(&doc);+ let banana_matches: Vec<String> = Word::new_standard_case("banana")+ .find_all_matches_in_doc(&doc)+ .to_strings(&doc);++ assert_eq!(bananatree_matches, Vec::<String>::new());+ assert_eq!(banana_matches, Vec::<String>::new());+ }++ #[test]+ fn standard_case_single_letters() {+ let doc = Document::new_plain_english_curated("A B C a b c I i");++ // Should match single letters in both cases+ let a_matches: Vec<String> = Word::new_standard_case("a")+ .find_all_matches_in_doc(&doc)+ .to_strings(&doc);+ let b_matches: Vec<String> = Word::new_standard_case("b")+ .find_all_matches_in_doc(&doc)+ .to_strings(&doc);+ let i_matches: Vec<String> = Word::new_standard_case("i")+ .find_all_matches_in_doc(&doc)+ .to_strings(&doc);++ assert_eq!(a_matches, vec!["A", "a"]);+ assert_eq!(b_matches, vec!["B", "b"]);+ assert_eq!(i_matches, vec!["I", "i"]);+ }++ #[test]+ fn standard_case_vs_exact_case() {+ let doc = Document::new_plain_english_curated("I ate banana and BANANA and Banana.");++ // new_exact should only match exact case+ let exact_matches: Vec<String> = Word::new_exact("banana")+ .find_all_matches_in_doc(&doc)+ .to_strings(&doc);+ assert_eq!(exact_matches, vec!["banana"]);++ // new_standard_case should match all standard case variations+ let standard_matches: Vec<String> = Word::new_standard_case("banana")+ .find_all_matches_in_doc(&doc)+ .to_strings(&doc);+ assert_eq!(standard_matches, vec!["banana", "BANANA", "Banana"]);+ }++ #[test]+ fn standard_case_edge_cases() {+ let doc = Document::new_plain_english_curated("A a B b I i.");++ // Test single characters and common pronouns+ let a_matches: Vec<String> = Word::new_standard_case("a")+ .find_all_matches_in_doc(&doc)+ .to_strings(&doc);+ let i_matches: Vec<String> = Word::new_standard_case("i")+ .find_all_matches_in_doc(&doc)+ .to_strings(&doc);++ assert_eq!(a_matches, vec!["A", "a"]);+ assert_eq!(i_matches, vec!["I", "i"]);+ }++ #[test]+ fn standard_case_complex_examples() {+ let doc = Document::new_plain_english_curated(+ "The iPhone is made by Apple but the apple is fruit.",+ );++ // Should match both "Apple" and "apple" (both are standard case)+ let apple_matches: Vec<String> = Word::new_standard_case("apple")+ .find_all_matches_in_doc(&doc)+ .to_strings(&doc);+ assert_eq!(apple_matches, vec!["Apple", "apple"]);++ // Should not match iPhone (not standard case)+ let iphone_matches: Vec<String> = Word::new_standard_case("iphone")+ .find_all_matches_in_doc(&doc)+ .to_strings(&doc);+ assert_eq!(iphone_matches, Vec::<String>::new());+ }++ #[test]+ fn match_all_standard_3_letter_sets() {+ let doc = Document::new_plain_english_curated("abc abC aBc aBC Abc AbC ABc ABC");++ let matches: Vec<String> = Word::new_standard_case("abc")+ .find_all_matches_in_doc(&doc)+ .to_strings(&doc);++ assert_eq!(matches, vec!["abc", "Abc", "ABC"]);+ }}