Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: a jar & a kimbo → ajar & akimbo (#4249)
Andrew Dunbar87fafd912c60
diff --git a/harper-core/default_config.json b/harper-core/default_config.jsonindex 862d90fc7..64111d637 100644--- a/harper-core/default_config.json+++ b/harper-core/default_config.json@@ -4039,6 +4039,20 @@"label": "Ahead And"}},+ {+ "Bool": {+ "name": "Ajar",+ "state": true,+ "label": "Ajar"+ }+ },+ {+ "Bool": {+ "name": "Akimbo",+ "state": true,+ "label": "Akimbo"+ }+ },{"Bool": {"name": "ALittleOfPractice",diff --git a/harper-core/src/linting/ajar.rs b/harper-core/src/linting/ajar.rsnew file mode 100644index 000000000..3a1d2bfa8--- /dev/null+++ b/harper-core/src/linting/ajar.rs@@ -0,0 +1,136 @@+use crate::{+ Lint, Token, TokenStringExt,+ expr::{All, Expr, OwnedExprExt, SequenceExpr},+ linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},+};++pub struct Ajar {+ expr: All,+}++impl Default for Ajar {+ fn default() -> Self {+ Self {+ expr: SequenceExpr::word_set(&["door", "doors", "gate", "gates", "mouth", "slightly"])+ .t_ws_h()+ .t_aco("a")+ .t_ws_h()+ .t_aco("jar")+ .but_not(+ SequenceExpr::anything()+ .t_any()+ .t_any()+ .t_any()+ .t_any()+ .t_ws()+ .t_set(&["containing", "contains", "full", "of"]),+ ),+ }+ }+}++impl ExprLinter for Ajar {+ type Unit = Chunk;++ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {+ let span = toks.get(2..=4)?.span()?;++ let a = toks.get(2)?;+ let jar = toks.get(4)?;++ let fix: Vec<char> = a+ .get_ch(src)+ .iter()+ .copied()+ .chain(jar.get_ch(src).iter().copied())+ .collect();++ let suggestions = vec![Suggestion::ReplaceWith(fix)];++ Some(Lint {+ span,+ lint_kind: LintKind::Usage,+ suggestions,+ message: "Did you mean `ajar` (slightly open)?".to_owned(),+ ..Default::default()+ })+ }++ fn expr(&self) -> &dyn Expr {+ &self.expr+ }++ fn description(&self) -> &str {+ "Corrects `a jar` to `ajar`."+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::tests::assert_suggestion_result;++ use super::Ajar;++ #[test]+ fn fix_door_a_jar_hyphen() {+ assert_suggestion_result(+ "not simply breaking into the house of a man they suspected of a crime but also them leaving his door a-jar after they left",+ Ajar::default(),+ "not simply breaking into the house of a man they suspected of a crime but also them leaving his door ajar after they left",+ );+ }++ #[test]+ fn fix_slightly_a_jar_hyphen() {+ assert_suggestion_result(+ "A door to democratizing investing that's slightly a-jar.",+ Ajar::default(),+ "A door to democratizing investing that's slightly ajar.",+ );+ }++ #[test]+ fn fix_door_a_jar_space() {+ assert_suggestion_result(+ "so it's easier just to read my book indoors with the door a jar so ...",+ Ajar::default(),+ "so it's easier just to read my book indoors with the door ajar so ...",+ );+ }++ #[test]+ fn fix_car_door() {+ assert_suggestion_result(+ "Why is my car calling my door a jar? Is it stupid?",+ Ajar::default(),+ "Why is my car calling my door ajar? Is it stupid?",+ );+ }++ #[test]+ fn fix_mouth() {+ assert_suggestion_result(+ "I dropped my book on the ground mouth a jar when it happened.",+ Ajar::default(),+ "I dropped my book on the ground mouth ajar when it happened.",+ );+ }++ #[test]+ fn fix_rational_doors() {+ assert_suggestion_result(+ "It keeps my rational doors a jar.",+ Ajar::default(),+ "It keeps my rational doors ajar.",+ );+ }++ #[test]+ fn fix_gate() {+ assert_suggestion_result(+ "Leaving the garden gate a jar ...",+ Ajar::default(),+ "Leaving the garden gate ajar ...",+ );+ }+}diff --git a/harper-core/src/linting/akimbo.rs b/harper-core/src/linting/akimbo.rsnew file mode 100644index 000000000..037d3443a--- /dev/null+++ b/harper-core/src/linting/akimbo.rs@@ -0,0 +1,112 @@+use crate::{+ Lint, Token, TokenStringExt,+ expr::{All, Expr, OwnedExprExt, SequenceExpr},+ linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},+};++pub struct Akimbo {+ expr: All,+}++impl Default for Akimbo {+ fn default() -> Self {+ Self {+ expr: SequenceExpr::word_set(&[+ "arm", "arms", "elbows", "all", "fingers", "hand", "hands", "legs", "limbs",+ "stand", "standing", "stands", "stood",+ ])+ .t_ws_h()+ .t_aco("a")+ .t_ws_h()+ .t_aco("kimbo")+ .but_not(+ SequenceExpr::anything()+ .t_any()+ .t_any()+ .t_any()+ .t_any()+ .t_ws()+ .t_set(&["slice"]),+ ),+ }+ }+}++impl ExprLinter for Akimbo {+ type Unit = Chunk;++ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {+ let span = toks.get(2..=4)?.span()?;++ let a = toks.get(2)?;+ let kimbo = toks.get(4)?;++ let fix: Vec<char> = a+ .get_ch(src)+ .iter()+ .copied()+ .chain(kimbo.get_ch(src).iter().copied())+ .collect();++ let suggestions = vec![Suggestion::ReplaceWith(fix)];++ Some(Lint {+ span,+ lint_kind: LintKind::Usage,+ suggestions,+ message: "Did you mean `akimbo` (standing with both hands on hips)?".to_owned(),+ ..Default::default()+ })+ }++ fn expr(&self) -> &dyn Expr {+ &self.expr+ }++ fn description(&self) -> &str {+ "Corrects `a kimbo` to `akimbo`."+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::tests::assert_suggestion_result;++ use super::Akimbo;++ #[test]+ fn fix_legs_all_spaces() {+ assert_suggestion_result(+ "I'm also a fan of the “ on the back and legs all a kimbo”.",+ Akimbo::default(),+ "I'm also a fan of the “ on the back and legs all akimbo”.",+ );+ }++ #[test]+ fn fix_legs_hyphens() {+ assert_suggestion_result(+ "I thought I'd have legs-a-kimbo, but not so.",+ Akimbo::default(),+ "I thought I'd have legs-akimbo, but not so.",+ );+ }++ #[test]+ fn fix_limbs() {+ assert_suggestion_result(+ "Just lying there, limbs a kimbo.",+ Akimbo::default(),+ "Just lying there, limbs akimbo.",+ );+ }++ #[test]+ fn fix_letters() {+ assert_suggestion_result(+ "Them syllables are all a-kimbo....",+ Akimbo::default(),+ "Them syllables are all akimbo....",+ );+ }+}diff --git a/harper-core/src/linting/lint_group/mod.rs b/harper-core/src/linting/lint_group/mod.rsindex aa4097dc9..1510c490f 100644--- a/harper-core/src/linting/lint_group/mod.rs+++ b/harper-core/src/linting/lint_group/mod.rs@@ -15,6 +15,8 @@ use super::addicting::Addicting;use super::adjective_double_degree::AdjectiveDoubleDegree;use super::adjective_of_a::AdjectiveOfA;use super::after_later::AfterLater;+use super::ajar::Ajar;+use super::akimbo::Akimbo;use super::all_hell_break_loose::AllHellBreakLoose;use super::all_intents_and_purposes::AllIntentsAndPurposes;use super::allow_to::AllowTo;@@ -622,6 +624,8 @@ impl LintGroup {insert_expr_rule!(AdjectiveDoubleDegree);insert_struct_rule!(AdjectiveOfA);insert_expr_rule!(AfterLater);+ insert_expr_rule!(Ajar);+ insert_expr_rule!(Akimbo);insert_expr_rule!(AllHellBreakLoose);insert_expr_rule!(AllIntentsAndPurposes);insert_expr_rule!(AllowTo);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex f6a75b31f..552fbd271 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -11,6 +11,8 @@ mod addicting;mod adjective_double_degree;mod adjective_of_a;mod after_later;+mod ajar;+mod akimbo;mod all_hell_break_loose;mod all_intents_and_purposes;mod allow_to;