Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: it´s→it's (#2884)
Andrew Dunbarcadcff5d338d
diff --git a/harper-core/src/expr/sequence_expr.rs b/harper-core/src/expr/sequence_expr.rsindex a510db2ed..bd43d7a9e 100644--- a/harper-core/src/expr/sequence_expr.rs+++ b/harper-core/src/expr/sequence_expr.rs@@ -624,6 +624,7 @@ impl SequenceExpr {gen_then_from_is!(hyphen);gen_then_from_is!(period);gen_then_from_is!(semicolon);+ gen_then_from_is!(acute);gen_then_from_is!(quote);gen_then_from_is!(backslash);gen_then_from_is!(slash);diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex fe362e361..79fcab0cd 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -177,7 +177,6 @@ use super::right_click::RightClick;use super::roller_skated::RollerSkated;use super::safe_to_save::SafeToSave;use super::save_to_safe::SaveToSafe;-use super::semicolon_apostrophe::SemicolonApostrophe;use super::sentence_capitalization::SentenceCapitalization;use super::shoot_oneself_in_the_foot::ShootOneselfInTheFoot;use super::simple_past_to_past_participle::SimplePastToPastParticiple;@@ -235,6 +234,7 @@ use super::wish_could::WishCould;use super::wordpress_dotcom::WordPressDotcom;use super::worth_to_do::WorthToDo;use super::would_never_have::WouldNeverHave;+use super::wrong_apostrophe::WrongApostrophe;use super::{ExprLinter, Lint};use super::{HtmlDescriptionLinter, Linter};@@ -578,7 +578,6 @@ impl LintGroup {insert_expr_rule!(RollerSkated, true);insert_expr_rule!(SafeToSave, true);insert_expr_rule!(SaveToSafe, true);- insert_expr_rule!(SemicolonApostrophe, true);insert_expr_rule!(ShootOneselfInTheFoot, true);insert_expr_rule!(SimplePastToPastParticiple, true);insert_expr_rule!(SinceDuration, true);@@ -603,6 +602,7 @@ impl LintGroup {insert_expr_rule!(ThenThan, true);insert_expr_rule!(Theres, true);insert_expr_rule!(ThesesThese, true);+ insert_struct_rule!(TheyreConfusions, true);insert_expr_rule!(ThingThink, true);insert_expr_rule!(ThisTypeOfThing, true);insert_expr_rule!(ThoughThought, true);@@ -612,7 +612,6 @@ impl LintGroup {insert_struct_rule!(ToTwoToo, true);insert_expr_rule!(Touristic, true);insert_expr_rule!(TryOnesHandAt, true);- insert_struct_rule!(TheyreConfusions, true);insert_struct_rule!(UnclosedQuotes, true);insert_expr_rule!(UpdatePlaceNames, true);insert_expr_rule!(VerbToAdjective, true);@@ -631,6 +630,7 @@ impl LintGroup {insert_expr_rule!(WishCould, true);insert_struct_rule!(WordPressDotcom, true);insert_expr_rule!(WouldNeverHave, true);+ insert_expr_rule!(WrongApostrophe, true);out.add("SpellCheck", SpellCheck::new(dictionary.clone(), dialect));out.config.set_rule_enabled("SpellCheck", true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex d78d264d7..9f86d86a1 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -186,7 +186,6 @@ mod right_click;mod roller_skated;mod safe_to_save;mod save_to_safe;-mod semicolon_apostrophe;mod sentence_capitalization;mod shoot_oneself_in_the_foot;mod simple_past_to_past_participle;@@ -245,6 +244,7 @@ mod wish_could;mod wordpress_dotcom;mod worth_to_do;mod would_never_have;+mod wrong_apostrophe;pub use expr_linter::{Chunk, ExprLinter};pub use initialism_linter::InitialismLinter;diff --git a/harper-core/src/linting/semicolon_apostrophe.rs b/harper-core/src/linting/semicolon_apostrophe.rsdeleted file mode 100644index 6a0d156d7..000000000--- a/harper-core/src/linting/semicolon_apostrophe.rs+++ /dev/null@@ -1,124 +0,0 @@-use crate::linting::expr_linter::Chunk;-use crate::{- Token, TokenStringExt,- expr::{Expr, SequenceExpr},- linting::{ExprLinter, Lint, LintKind, Suggestion},-};--const CONTRACTION_AND_POSSESSIVE_ENDINGS: [&str; 7] = ["d", "ll", "m", "re", "s", "t", "ve"];--pub struct SemicolonApostrophe {- expr: Box<dyn Expr>,-}--impl Default for SemicolonApostrophe {- fn default() -> Self {- Self {- expr: Box::new(- SequenceExpr::any_word()- .then_semicolon()- .then_word_set(&CONTRACTION_AND_POSSESSIVE_ENDINGS),- ),- }- }-}--impl ExprLinter for SemicolonApostrophe {- type Unit = Chunk;-- fn expr(&self) -> &dyn Expr {- self.expr.as_ref()- }-- fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {- let whole_span = toks.span()?;- let base = &toks.first()?;- let ending = &toks.last()?;-- let replacement_str = format!(- "{}'{}",- base.span.get_content_string(src).to_lowercase(),- ending.span.get_content_string(src).to_lowercase()- );-- let mut lettercase_template = base.span.get_content(src).to_vec();- lettercase_template.extend_from_slice(ending.span.get_content(src));-- Some(Lint {- span: whole_span,- lint_kind: LintKind::Typo,- suggestions: vec![Suggestion::replace_with_match_case(- replacement_str.chars().collect(),- &lettercase_template,- )],- message: format!("Did you mean `{replacement_str}`?"),- priority: 57,- })- }-- fn description(&self) -> &str {- "Corrects semicolons accidentally typed instead of apostrophes."- }-}--#[cfg(test)]-mod tests {- use super::SemicolonApostrophe;- use crate::linting::tests::{assert_lint_count, assert_suggestion_result};-- #[test]- fn fix_dont_with_semicolon_to_apostrophe() {- assert_suggestion_result(- "It's better if you don;t type like this.",- SemicolonApostrophe::default(),- "It's better if you don't type like this.",- );- }-- #[test]- fn ignore_correct() {- assert_lint_count("I don't doubt it.", SemicolonApostrophe::default(), 0);- }-- #[test]- fn fix_title_case() {- assert_suggestion_result(- "Don;t type like this.",- SemicolonApostrophe::default(),- "Don't type like this.",- );- }-- #[test]- fn fix_all_caps() {- assert_suggestion_result(- "DON;T TRY THIS AT HOME.",- SemicolonApostrophe::default(),- "DON'T TRY THIS AT HOME.",- );- }-- #[test]- #[ignore = "replace_with_match_case has a bug turning `I'll` into `I'LL`"]- fn fix_ill_and_monkeys() {- assert_suggestion_result(- "Well I;ll be a monkey;s uncle!",- SemicolonApostrophe::default(),- "Well I'll be a monkey's uncle!",- )- }-- #[test]- fn fix_other_contractions_and_possessives() {- assert_suggestion_result(- "Let;s see if we;ve fixed patrakov;s bug. Fun wasn;t it?",- SemicolonApostrophe::default(),- "Let's see if we've fixed patrakov's bug. Fun wasn't it?",- )- }-- #[test]- fn corrects_ive_with_correct_capitalization() {- assert_suggestion_result("I;ve", SemicolonApostrophe::default(), "I've");- }-}diff --git a/harper-core/src/linting/wrong_apostrophe.rs b/harper-core/src/linting/wrong_apostrophe.rsnew file mode 100644index 000000000..4a5ba455c--- /dev/null+++ b/harper-core/src/linting/wrong_apostrophe.rs@@ -0,0 +1,184 @@+use crate::{+ Token, TokenStringExt,+ expr::{Expr, FirstMatchOf, SequenceExpr},+ linting::{ExprLinter, Lint, LintKind, Suggestion, expr_linter::Chunk},+};++const CONTRACTION_AND_POSSESSIVE_ENDINGS: [&str; 7] = ["d", "ll", "m", "re", "s", "t", "ve"];++pub struct WrongApostrophe {+ expr: FirstMatchOf,+}++impl Default for WrongApostrophe {+ fn default() -> Self {+ Self {+ expr: FirstMatchOf::new(vec![+ Box::new(+ SequenceExpr::any_word()+ .then_semicolon()+ .then_word_set(&CONTRACTION_AND_POSSESSIVE_ENDINGS),+ ),+ Box::new(+ SequenceExpr::any_word()+ .then_acute()+ .then_word_set(&CONTRACTION_AND_POSSESSIVE_ENDINGS),+ ),+ ]),+ }+ }+}++impl ExprLinter for WrongApostrophe {+ type Unit = Chunk;++ fn expr(&self) -> &dyn Expr {+ &self.expr+ }++ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {+ let whole_span = toks.span()?;+ let base = &toks.first()?;+ let ending = &toks.last()?;++ let replacement_str = format!(+ "{}'{}",+ base.span.get_content_string(src).to_lowercase(),+ ending.span.get_content_string(src).to_lowercase()+ );++ let lettercase_template =+ [base.span.get_content(src), ending.span.get_content(src)].concat();++ Some(Lint {+ span: whole_span,+ lint_kind: LintKind::Typo,+ suggestions: vec![Suggestion::replace_with_match_case(+ replacement_str.chars().collect(),+ &lettercase_template,+ )],+ message: format!("Did you mean `{replacement_str}`?"),+ priority: 57,+ })+ }++ fn description(&self) -> &str {+ "Corrects semicolons or acute accents typed instead of apostrophes."+ }+}++#[cfg(test)]+mod tests {+ use super::WrongApostrophe;+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};++ #[test]+ fn fix_dont_with_semicolon_to_apostrophe() {+ assert_suggestion_result(+ "It's better if you don;t type like this.",+ WrongApostrophe::default(),+ "It's better if you don't type like this.",+ );+ }++ #[test]+ fn ignore_correct() {+ assert_lint_count("I don't doubt it.", WrongApostrophe::default(), 0);+ }++ #[test]+ fn fix_title_case() {+ assert_suggestion_result(+ "Don;t type like this.",+ WrongApostrophe::default(),+ "Don't type like this.",+ );+ }++ #[test]+ fn fix_all_caps() {+ assert_suggestion_result(+ "DON;T TRY THIS AT HOME.",+ WrongApostrophe::default(),+ "DON'T TRY THIS AT HOME.",+ );+ }++ #[test]+ #[ignore = "replace_with_match_case has a bug turning `I'll` into `I'LL`"]+ fn fix_ill_and_monkeys() {+ assert_suggestion_result(+ "Well I;ll be a monkey;s uncle!",+ WrongApostrophe::default(),+ "Well I'll be a monkey's uncle!",+ )+ }++ #[test]+ fn fix_other_contractions_and_possessives() {+ assert_suggestion_result(+ "Let;s see if we;ve fixed patrakov;s bug. Fun wasn;t it?",+ WrongApostrophe::default(),+ "Let's see if we've fixed patrakov's bug. Fun wasn't it?",+ )+ }++ #[test]+ fn corrects_ive_with_correct_capitalization() {+ assert_suggestion_result("I;ve", WrongApostrophe::default(), "I've");+ }++ #[test]+ fn fix_acute_dont() {+ assert_suggestion_result(+ "To see the list of available bikes for a location, you don´t need any authentication.",+ WrongApostrophe::default(),+ "To see the list of available bikes for a location, you don't need any authentication.",+ );+ }++ #[test]+ fn fix_acute_im() {+ assert_suggestion_result(+ "In my research, I´m applying the latest generation of quantitative methods in epidemiology",+ WrongApostrophe::default(),+ "In my research, I'm applying the latest generation of quantitative methods in epidemiology",+ );+ }++ #[test]+ fn fix_acute_its() {+ assert_suggestion_result(+ "and it´s auto-updated if that project is hosted here on github",+ WrongApostrophe::default(),+ "and it's auto-updated if that project is hosted here on github",+ );+ }++ #[test]+ fn fix_acute_lets() {+ assert_suggestion_result(+ "Let´s now visit the main functionalities provided by GrimoireLab.",+ WrongApostrophe::default(),+ "Let's now visit the main functionalities provided by GrimoireLab.",+ );+ }++ #[test]+ fn fix_acute_microsofts() {+ assert_suggestion_result(+ "Windows 11 Upgrade tool that bypasses new Microsoft´s requirements",+ WrongApostrophe::default(),+ "Windows 11 Upgrade tool that bypasses new Microsoft's requirements",+ );+ }++ #[test]+ fn fix_acute_youre() {+ assert_suggestion_result(+ "You´re looking for clues, but you´re missing all the signs",+ WrongApostrophe::default(),+ "You're looking for clues, but you're missing all the signs",+ );+ }+}diff --git a/harper-core/src/punctuation.rs b/harper-core/src/punctuation.rsindex 63d5e59c4..fa4ea68ec 100644--- a/harper-core/src/punctuation.rs+++ b/harper-core/src/punctuation.rs@@ -78,6 +78,8 @@ pub enum Punctuation {Pipe,/// `_`Underscore,+ /// `´`+ Acute,}impl Punctuation {@@ -119,6 +121,7 @@ impl Punctuation {'+' => Punctuation::Plus,'|' => Punctuation::Pipe,'_' => Punctuation::Underscore,+ '´' => Punctuation::Acute,_ => Punctuation::Currency(Currency::from_char(c)?),};diff --git a/harper-core/src/token_kind.rs b/harper-core/src/token_kind.rsindex 80616be7b..f20ffc69b 100644--- a/harper-core/src/token_kind.rs+++ b/harper-core/src/token_kind.rs@@ -264,6 +264,10 @@ impl TokenKind {matches!(self, TokenKind::Punctuation(Punctuation::Semicolon))}+ pub fn is_acute(&self) -> bool {+ matches!(self, TokenKind::Punctuation(Punctuation::Acute))+ }+pub fn is_ampersand(&self) -> bool {matches!(self, TokenKind::Punctuation(Punctuation::Ampersand))}