Skip to content

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

feat: out (of) the window variant matching dialect (#3620)

Andrew Dunbar8d49fe8e0a5c
diff --git a/harper-core/default_config.json b/harper-core/default_config.json
index a9606ecf0..182f1f6fe 100644
--- a/harper-core/default_config.json
+++ b/harper-core/default_config.json
@@ -5077,6 +5077,13 @@
"state": true,
"label": "Touristic"
}
+ },
+ {
+ "Bool": {
+ "name": "OutOfTheWindow",
+ "state": true,
+ "label": "Out (of) the Window"
+ }
}
]
}
diff --git a/harper-core/src/linting/lint_group/mod.rs b/harper-core/src/linting/lint_group/mod.rs
index 27e2e5df5..dc1bb6531 100644
--- a/harper-core/src/linting/lint_group/mod.rs
+++ b/harper-core/src/linting/lint_group/mod.rs
@@ -173,6 +173,7 @@ use super::open_the_light::OpenTheLight;
use super::orthographic_consistency::OrthographicConsistency;
use super::ought_to_be::OughtToBe;
use super::out_of_date::OutOfDate;
+use super::out_of_the_window::OutOfTheWindow;
use super::oxford_comma::OxfordComma;
use super::oxymorons::Oxymorons;
use super::pay_for_price::PayForPrice;
@@ -735,6 +736,7 @@ impl LintGroup {
insert_expr_rule!(OrthographicConsistency, true);
insert_expr_rule!(OughtToBe, true);
insert_expr_rule!(OutOfDate, true);
+ insert_expr_rule_with_dialect!(OutOfTheWindow, true);
insert_struct_rule!(OxfordComma, true);
insert_expr_rule!(Oxymorons, true);
insert_expr_rule!(PayForPrice, true);
@@ -829,9 +831,6 @@ impl LintGroup {
insert_struct_rule!(WordPressDotcom, true);
insert_expr_rule_with_dict!(WorthToDo, true);
insert_expr_rule!(WouldNeverHave, true);
- // Uses Sentence rather than Chunk
- out.add("WrongApostrophe", WrongApostrophe::default());
- out.config.set_rule_enabled("WrongApostrophe", true);
// Uses Sentence rather than Chunk
out.add("AspireTo", AspireTo::default());
@@ -853,10 +852,6 @@ impl LintGroup {
out.add("PluralDecades", PluralDecades::default());
out.config.set_rule_enabled("PluralDecades", true);
- // Uses Sentence rather than Chunk
- out.add("WereWhere", WereWhere::default());
- out.config.set_rule_enabled("WereWhere", true);
-
// Uses Dictionary and Dialect
out.add("SpellCheck", SpellCheck::new(dictionary.clone(), dialect));
out.config.set_rule_enabled("SpellCheck", true);
@@ -872,6 +867,14 @@ impl LintGroup {
out.add("WebScraping", WebScraping::default());
out.config.set_rule_enabled("WebScraping", true);
+ // Uses Sentence rather than Chunk
+ out.add("WereWhere", WereWhere::default());
+ out.config.set_rule_enabled("WereWhere", true);
+
+ // Uses Sentence rather than Chunk
+ out.add("WrongApostrophe", WrongApostrophe::default());
+ out.config.set_rule_enabled("WrongApostrophe", true);
+
out
}
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 2a3f21af0..e3cd64810 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -183,6 +183,7 @@ mod open_the_light;
mod orthographic_consistency;
mod ought_to_be;
mod out_of_date;
+mod out_of_the_window;
mod oxford_comma;
mod oxymorons;
mod pay_for_price;
diff --git a/harper-core/src/linting/out_of_the_window.rs b/harper-core/src/linting/out_of_the_window.rs
new file mode 100644
index 000000000..4ba5498a5
--- /dev/null
+++ b/harper-core/src/linting/out_of_the_window.rs
@@ -0,0 +1,117 @@
+use crate::{
+ Dialect, Lint, Token, TokenStringExt,
+ expr::{Expr, SequenceExpr},
+ linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},
+};
+
+pub struct OutOfTheWindow {
+ expr: SequenceExpr,
+ dialect: Dialect,
+}
+
+impl OutOfTheWindow {
+ pub fn new(dialect: Dialect) -> Self {
+ Self {
+ expr: SequenceExpr::aco("out")
+ .then_optional(SequenceExpr::whitespace().t_aco("of"))
+ .t_ws()
+ .t_aco("the")
+ .t_ws()
+ .t_aco("window"),
+ dialect,
+ }
+ }
+}
+
+impl ExprLinter for OutOfTheWindow {
+ type Unit = Chunk;
+
+ fn match_to_lint(&self, toks: &[Token], _src: &[char]) -> Option<Lint> {
+ let (span, sugg, msg) = match (toks.len(), self.dialect) {
+ (7, Dialect::American | Dialect::Australian | Dialect::Canadian) => {
+ (
+ toks[1..=2].span()?,
+ Suggestion::Remove,
+ format!("If this is the idiom about abandoning a plan, it's more usual to leave out `of` in {} English", self.dialect),
+ )
+ }
+ (5, Dialect::British) => {
+ (
+ toks[0].span,
+ Suggestion::InsertAfter(vec![' ', 'o', 'f']),
+ "If this is the idiom about abandoning a plan, consider using `of` in British English".to_string(),
+ )
+ }
+ _ => return None,
+ };
+
+ Some(Lint {
+ span,
+ lint_kind: LintKind::Regionalism,
+ suggestions: vec![sugg],
+ message: msg,
+ ..Default::default()
+ })
+ }
+
+ fn expr(&self) -> &dyn Expr {
+ &self.expr
+ }
+
+ fn description(&self) -> &str {
+ "A linter for the idiom `out (of) the window`."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::{
+ Dialect,
+ linting::tests::{assert_lint_message, assert_no_lints, assert_suggestion_result},
+ };
+
+ use super::OutOfTheWindow;
+
+ #[test]
+ fn us_english() {
+ assert_suggestion_result(
+ "The whole Gnome session goes out of the window instantly, both upon explicit screen locking and upon automatic locking after a timeout.",
+ OutOfTheWindow::new(Dialect::American),
+ "The whole Gnome session goes out the window instantly, both upon explicit screen locking and upon automatic locking after a timeout.",
+ );
+ }
+
+ #[test]
+ fn uk_english() {
+ assert_suggestion_result(
+ "determinism went out the window, everything is terrible",
+ OutOfTheWindow::new(Dialect::British),
+ "determinism went out of the window, everything is terrible",
+ );
+ }
+
+ #[test]
+ fn in_english_doesnt_flag_with_of() {
+ assert_no_lints(
+ "errors and orchestration in general all go out of the window",
+ OutOfTheWindow::new(Dialect::Indian),
+ );
+ }
+
+ #[test]
+ fn in_english_doesnt_flag_without_of() {
+ assert_no_lints(
+ "I was so excited to develop my new app in .NET MAUI but then all this excitement went out the window due to two things stability and tooling.",
+ OutOfTheWindow::new(Dialect::Indian),
+ );
+ }
+
+ #[test]
+ fn gives_the_right_message_for_canadian_english() {
+ assert_lint_message(
+ "This all works great unless I use middlware and then it all goes out the window.",
+ OutOfTheWindow::new(Dialect::Canadian),
+ "If this is the idiom about abandoning a plan, it's more usual to leave out `of` in Canadian English",
+ );
+ }
+}
diff --git a/harper-core/tests/text/linters/Alice's Adventures in Wonderland.snap.yml b/harper-core/tests/text/linters/Alice's Adventures in Wonderland.snap.yml
index 5b0d4d2f0..3cccb6378 100644
--- a/harper-core/tests/text/linters/Alice's Adventures in Wonderland.snap.yml
+++ b/harper-core/tests/text/linters/Alice's Adventures in Wonderland.snap.yml
@@ -1011,6 +1011,17 @@ Message: |
+Lint: Regionalism (127 priority)
+Message: |
+ 610 | door, and the other arm curled round her head. Still she went on growing, and,
+ 611 | as a last resource, she put one arm out of the window, and one foot up the
+ | ^~~ If this is the idiom about abandoning a plan, it's more usual to leave out `of` in Australian English
+ 612 | chimney, and said to herself “Now I can do no more, whatever happens. What will
+Suggest:
+ - Remove error
+
+
+
Lint: Readability (127 priority)
Message: |
615 | Luckily for Alice, the little magic bottle had now had its full effect, and she
@@ -1068,6 +1079,16 @@ Suggest:
+Lint: Regionalism (127 priority)
+Message: |
+ 681 | thought Alice. “I wonder what they’ll do next! As for pulling me out of the
+ | ^~~ If this is the idiom about abandoning a plan, it's more usual to leave out `of` in Australian English
+ 682 | window, I only wish they could! I’m sure I don’t want to stay in here any
+Suggest:
+ - Remove error
+
+
+
Lint: Capitalization (31 priority)
Message: |
688 | one; Bill’s got the other—Bill! fetch it here, lad!—Here, put ’em up at this