Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat:have a look vs. take a look (#1624)

Andrew Dunbarf4e10c0116b9
diff --git a/harper-core/src/linting/have_take_a_look.rs b/harper-core/src/linting/have_take_a_look.rs
new file mode 100644
index 000000000..ca3694de5
--- /dev/null
+++ b/harper-core/src/linting/have_take_a_look.rs
@@ -0,0 +1,178 @@
+use crate::{
+ Dialect, Token,
+ expr::{Expr, FixedPhrase, SequenceExpr},
+ linting::{ExprLinter, Lint, LintKind, Suggestion},
+ patterns::WordSet,
+};
+
+pub struct HaveTakeALook {
+ expr: Box<dyn Expr>,
+ dialect: Dialect,
+}
+
+impl HaveTakeALook {
+ pub fn new(dialect: Dialect) -> Self {
+ let light_verb = match dialect {
+ // Match the opposite of what is used in the dialect.
+ Dialect::British | Dialect::Australian => &["take", "took", "taken", "takes", "taking"],
+ _ => &["have", "had", "had", "has", "having"],
+ };
+
+ let expr = SequenceExpr::default()
+ .then(WordSet::new(light_verb))
+ .t_ws()
+ .then(FixedPhrase::from_phrase("a look"));
+
+ Self {
+ expr: Box::new(expr),
+ dialect,
+ }
+ }
+}
+
+impl ExprLinter for HaveTakeALook {
+ fn expr(&self) -> &dyn Expr {
+ self.expr.as_ref()
+ }
+
+ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {
+ let light_verb_tok = toks.first().unwrap();
+ let light_verb_str = light_verb_tok.span.get_content_string(src);
+ let light_verb = light_verb_str.to_ascii_lowercase();
+
+ let translated_light_verb: &[&str] = match light_verb.as_str() {
+ "have" => &["take"],
+ "had" => &["took", "taken"],
+ "has" => &["takes"],
+ "having" => &["taking"],
+ "take" => &["have"],
+ "took" => &["had"],
+ "taken" => &["had"],
+ "takes" => &["has"],
+ "taking" => &["having"],
+ _ => return None,
+ };
+
+ let suggestions = translated_light_verb
+ .iter()
+ .map(|s| {
+ Suggestion::replace_with_match_case(
+ s.chars().collect(),
+ light_verb_tok.span.get_content(src),
+ )
+ })
+ .collect();
+
+ let message = format!(
+ "{} English prefers {} over `{} a look`.",
+ self.dialect,
+ translated_light_verb
+ .iter()
+ .map(|lv| format!("`{lv} a look`"))
+ .collect::<Vec<_>>()
+ .join(" or "),
+ light_verb,
+ );
+
+ Some(Lint {
+ span: light_verb_tok.span,
+ lint_kind: LintKind::Regionalism,
+ suggestions,
+ message,
+ priority: 63,
+ })
+ }
+
+ fn description(&self) -> &str {
+ "Corrects either `have a look` or `take a look` to the other, depending on the dialect."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::{
+ Dialect,
+ linting::{HaveTakeALook, tests::assert_suggestion_result},
+ };
+
+ #[test]
+ fn correct_taking_a_look() {
+ assert_suggestion_result(
+ "Consider taking a look at crossorigin attribute.",
+ HaveTakeALook::new(Dialect::British),
+ "Consider having a look at crossorigin attribute.",
+ );
+ }
+
+ #[test]
+ fn correct_take_a_look() {
+ assert_suggestion_result(
+ "Have time to help take a look at the jdk21 upgrade issue.",
+ HaveTakeALook::new(Dialect::Australian),
+ "Have time to help have a look at the jdk21 upgrade issue.",
+ );
+ }
+
+ #[test]
+ fn correct_have_a_look() {
+ assert_suggestion_result(
+ "Have a look at this question crashing histoire using init-state and ref.",
+ HaveTakeALook::new(Dialect::American),
+ "Take a look at this question crashing histoire using init-state and ref.",
+ );
+ }
+
+ #[test]
+ fn correct_taken_a_look() {
+ assert_suggestion_result(
+ "Have you taken a look at HQEMU?",
+ HaveTakeALook::new(Dialect::British),
+ "Have you had a look at HQEMU?",
+ );
+ }
+
+ #[test]
+ fn correct_had_a_look() {
+ assert_suggestion_result(
+ "I had a look at the podman.go and have some theories I could test.",
+ HaveTakeALook::new(Dialect::Canadian),
+ "I took a look at the podman.go and have some theories I could test.",
+ );
+ }
+
+ #[test]
+ fn correct_took_a_look() {
+ assert_suggestion_result(
+ "I though GitHub's “Dashboard” page might help with this, so I took a look.",
+ HaveTakeALook::new(Dialect::Australian),
+ "I though GitHub's “Dashboard” page might help with this, so I had a look.",
+ );
+ }
+
+ #[test]
+ fn correct_takes_a_look() {
+ assert_suggestion_result(
+ "I'm closing this one, but it would be nice if someone takes a look at the notes in the original issue.",
+ HaveTakeALook::new(Dialect::British),
+ "I'm closing this one, but it would be nice if someone has a look at the notes in the original issue.",
+ );
+ }
+
+ #[test]
+ fn correct_having_a_look() {
+ assert_suggestion_result(
+ "It only appeared after I was having a look through the files.",
+ HaveTakeALook::new(Dialect::American),
+ "It only appeared after I was taking a look through the files.",
+ );
+ }
+
+ #[test]
+ fn correct_has_a_look() {
+ assert_suggestion_result(
+ "When Serializing messages the code in SchemaRegistrySerde has a look into the registry using the topic name.",
+ HaveTakeALook::new(Dialect::Canadian),
+ "When Serializing messages the code in SchemaRegistrySerde takes a look into the registry using the topic name.",
+ );
+ }
+}
diff --git a/harper-core/src/linting/in_on_the_cards.rs b/harper-core/src/linting/in_on_the_cards.rs
index bfda21581..5efdf76dd 100644
--- a/harper-core/src/linting/in_on_the_cards.rs
+++ b/harper-core/src/linting/in_on_the_cards.rs
@@ -1,5 +1,5 @@
use crate::{
- Dialect, Token,
+ CharStringExt, Dialect, Token,
expr::{Expr, FirstMatchOf, FixedPhrase, SequenceExpr},
linting::{LintKind, Suggestion},
patterns::{InflectionOfBe, WordSet},
@@ -9,6 +9,7 @@ use super::{ExprLinter, Lint};
pub struct InOnTheCards {
expr: Box<dyn Expr>,
+ dialect: Dialect,
}
impl InOnTheCards {
@@ -34,6 +35,7 @@ impl InOnTheCards {
Self {
expr: Box::new(expr),
+ dialect,
}
}
}
@@ -47,22 +49,26 @@ impl ExprLinter for InOnTheCards {
let prep_span = toks[2].span;
let prep = prep_span.get_content(src);
- let sugg = Suggestion::ReplaceWith(
- [
- match prep[0] {
- 'i' => 'o',
- 'o' => 'i',
- 'I' => 'O',
- 'O' => 'I',
- _ => return None,
- },
- prep[1],
- ]
- .to_vec(),
+ let new_prep = [
+ match prep[0] {
+ 'i' => 'o',
+ 'o' => 'i',
+ 'I' => 'O',
+ 'O' => 'I',
+ _ => return None,
+ },
+ prep[1],
+ ];
+
+ let sugg = Suggestion::ReplaceWith(new_prep.to_vec());
+
+ let message = format!(
+ "Use `{} the cards` instead of `{} the cards` in {} English.",
+ new_prep.to_string(),
+ prep.to_string(),
+ self.dialect,
);
- let message = "Corrects either `in the cards` or `on the cards` to the other, depending on the dialect.".into();
-
Some(Lint {
span: prep_span,
lint_kind: LintKind::Regionalism,
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 4f7080521..0d413a433 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -43,6 +43,7 @@ use super::filler_words::FillerWords;
use super::first_aid_kit::FirstAidKit;
use super::for_noun::ForNoun;
use super::have_pronoun::HavePronoun;
+use super::have_take_a_look::HaveTakeALook;
use super::hedging::Hedging;
use super::hereby::Hereby;
use super::hop_hope::HopHope;
@@ -387,6 +388,7 @@ impl LintGroup {
out.merge_from(&mut initialisms::lint_group());
// Add all the more complex rules to the group.
+ // Please maintain alphabetical order.
// On *nix you can maintain sort order with `sort -t'(' -k2`
insert_expr_rule!(APart, true);
insert_expr_rule!(AdjectiveDoubleDegree, true);
@@ -484,7 +486,6 @@ impl LintGroup {
insert_expr_rule!(WayTooAdjective, true);
insert_expr_rule!(Whereas, true);
insert_expr_rule!(WidelyAccepted, true);
- insert_struct_rule!(WidelyAccepted, true);
insert_expr_rule!(WinPrize, true);
insert_struct_rule!(WordPressDotcom, true);
@@ -512,6 +513,9 @@ impl LintGroup {
out.add("Regionalisms", Regionalisms::new(dialect));
out.config.set_rule_enabled("Regionalisms", true);
+ out.add("HaveTakeALook", HaveTakeALook::new(dialect));
+ out.config.set_rule_enabled("HaveTakeALook", true);
+
out
}
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index adbb0c5e1..b3449ebed 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -37,6 +37,7 @@ mod filler_words;
mod first_aid_kit;
mod for_noun;
mod have_pronoun;
+mod have_take_a_look;
mod hedging;
mod hereby;
mod hop_hope;
@@ -158,6 +159,7 @@ pub use few_units_of_time_ago::FewUnitsOfTimeAgo;
pub use filler_words::FillerWords;
pub use for_noun::ForNoun;
pub use have_pronoun::HavePronoun;
+pub use have_take_a_look::HaveTakeALook;
pub use hedging::Hedging;
pub use hereby::Hereby;
pub use hop_hope::HopHope;
diff --git a/harper-core/src/linting/regionalisms.rs b/harper-core/src/linting/regionalisms.rs
index 2fac098c8..8430f1bf7 100644
--- a/harper-core/src/linting/regionalisms.rs
+++ b/harper-core/src/linting/regionalisms.rs
@@ -25,6 +25,8 @@ use CanFlag::*;
#[derive(PartialEq)]
enum Concept {
AubergineEggplant,
+ // BiscuitCookie - biscuit names different foods in UK/Aus vs US; cookie has other meanings
+ // BiscuitCracker - cracker also has other meanings
BumBagFannyPack,
BurglarizeBurgle,
CampervanRv,
@@ -35,6 +37,7 @@ enum Concept {
ChipsCrisps,
CilantroCoriander,
DiaperNappy,
+ DoonaDuvet,
DummyPacifier,
FaucetTap,
FlashlightTorch,
@@ -163,12 +166,24 @@ const REGIONAL_TERMS: &[Term<'_>] = &[
dialects: &[American, Canadian],
concept: DiaperNappy,
},
+ Term {
+ term: "doona",
+ flag: Flag,
+ dialects: &[Australian],
+ concept: DoonaDuvet,
+ },
Term {
term: "dummy",
flag: HasOtherMeanings,
dialects: &[Australian],
concept: DummyPacifier,
},
+ Term {
+ term: "duvet",
+ flag: Flag,
+ dialects: &[Australian],
+ concept: DoonaDuvet,
+ },
Term {
term: "eggplant",
flag: Flag,
diff --git a/harper-core/tests/text/linters/The Great Gatsby.snap.yml b/harper-core/tests/text/linters/The Great Gatsby.snap.yml
index afe957d49..38328d52f 100644
--- a/harper-core/tests/text/linters/The Great Gatsby.snap.yml
+++ b/harper-core/tests/text/linters/The Great Gatsby.snap.yml
@@ -4985,6 +4985,16 @@ Suggest:
+Lint: Regionalism (63 priority)
+Message: |
+ 3690 | “Come outside,” he suggested to Gatsby, “I’d like you to have a look at the
+ | ^~~~ American English prefers `take a look` over `have a look`.
+ 3691 | place.”
+Suggest:
+ - Replace with: “take”
+
+
+
Lint: Spelling (63 priority)
Message: |
3709 | We had luncheon in the dining-room, darkened too against the heat, and drank