Skip to content

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

refactor: upgrade `VeryLess` to Rust linter to handle "too less" etc. (#4229)

Andrew Dunbar80d42c6f387d
diff --git a/harper-core/default_config.json b/harper-core/default_config.json
index 397f65335..f7734cf4d 100644
--- a/harper-core/default_config.json
+++ b/harper-core/default_config.json
@@ -1380,6 +1380,13 @@
"label": "Verse As Verb"
}
},
+ {
+ "Bool": {
+ "name": "VeryLess",
+ "state": true,
+ "label": "Very Less"
+ }
+ },
{
"Bool": {
"name": "WholeEntire",
@@ -3966,13 +3973,6 @@
"label": "Verb To Adjective"
}
},
- {
- "Bool": {
- "name": "VeryLess",
- "state": true,
- "label": "Very Less"
- }
- },
{
"Bool": {
"name": "VeryMuchSo",
diff --git a/harper-core/src/linting/lint_group/mod.rs b/harper-core/src/linting/lint_group/mod.rs
index d7975711f..0e0da0759 100644
--- a/harper-core/src/linting/lint_group/mod.rs
+++ b/harper-core/src/linting/lint_group/mod.rs
@@ -297,6 +297,7 @@ use super::update_place_names::UpdatePlaceNames;
use super::use_ellipsis_character::UseEllipsisCharacter;
use super::use_title_case::UseTitleCase;
use super::verb_to_adjective::VerbToAdjective;
+use super::very_less::VeryLess;
use super::very_unique::VeryUnique;
use super::vice_versa::ViceVersa;
use super::vicious_loop::ViciousCircle;
@@ -894,6 +895,7 @@ impl LintGroup {
insert_struct_rule!(UseEllipsisCharacter);
insert_struct_rule_with_dict!(UseTitleCase);
insert_expr_rule!(VerbToAdjective);
+ insert_expr_rule!(VeryLess);
insert_expr_rule!(VeryUnique);
insert_expr_rule!(ViceVersa);
insert_expr_rule!(ViciousCircle);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index acdc97161..a478d445c 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -316,6 +316,7 @@ mod update_place_names;
mod use_ellipsis_character;
mod use_title_case;
mod verb_to_adjective;
+mod very_less;
mod very_unique;
mod vice_versa;
mod vicious_loop;
diff --git a/harper-core/src/linting/very_less.rs b/harper-core/src/linting/very_less.rs
new file mode 100644
index 000000000..0a628c9c3
--- /dev/null
+++ b/harper-core/src/linting/very_less.rs
@@ -0,0 +1,246 @@
+use crate::{
+ CharStringExt, Lint, Token, TokenStringExt,
+ expr::{All, Expr, OwnedExprExt, SequenceExpr},
+ linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},
+ patterns::WordSet,
+};
+
+/// Like `Itertools::Either`, but with three variants
+enum Fork<K, S, T> {
+ Kind(K),
+ Sort(S),
+ Type(T),
+}
+
+impl<K, S, T, Item> Iterator for Fork<K, S, T>
+where
+ K: Iterator<Item = Item>,
+ S: Iterator<Item = Item>,
+ T: Iterator<Item = Item>,
+{
+ type Item = Item;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ match self {
+ Fork::Kind(k) => k.next(),
+ Fork::Sort(s) => s.next(),
+ Fork::Type(t) => t.next(),
+ }
+ }
+}
+
+const DEGREE_ADVERB_BLACKLIST: &[&str] = &["comparably", "indeed", "notably", "really", "so"];
+
+pub struct VeryLess {
+ expr: All,
+}
+
+impl Default for VeryLess {
+ fn default() -> Self {
+ Self {
+ expr: SequenceExpr::default()
+ .then_degree_adverb()
+ .t_ws()
+ .t_aco("less")
+ .but_not(WordSet::new(DEGREE_ADVERB_BLACKLIST)),
+ }
+ }
+}
+
+impl ExprLinter for VeryLess {
+ type Unit = Chunk;
+
+ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {
+ let span = toks.span()?;
+
+ let [deg_tok, ws_tok, less_tok] = toks else {
+ return None;
+ };
+
+ let replace_less = ["few", "little"];
+ let replace_very = ["a lot", "far", "much"];
+ let replace_quite = ["quite a bit", "quite a lot"];
+
+ let corrections = if deg_tok.get_ch(src).eq_str("very") {
+ Fork::Kind(replace_very.iter().map(|v| {
+ v.chars()
+ .chain(ws_tok.get_ch(src).iter().copied())
+ .chain(less_tok.get_ch(src).iter().copied())
+ .collect::<Vec<char>>()
+ }))
+ } else if deg_tok.get_ch(src).eq_str("quite") {
+ Fork::Sort(replace_quite.iter().map(|q| {
+ q.chars()
+ .chain(ws_tok.get_ch(src).iter().copied())
+ .chain(less_tok.get_ch(src).iter().copied())
+ .collect::<Vec<char>>()
+ }))
+ } else {
+ Fork::Type(replace_less.iter().map(|l| {
+ deg_tok
+ .get_ch(src)
+ .iter()
+ .copied()
+ .chain(ws_tok.get_ch(src).iter().copied())
+ .chain(l.chars())
+ .collect::<Vec<char>>()
+ }))
+ };
+
+ let suggestions = corrections
+ .map(|c| Suggestion::replace_with_match_case(c, span.get_content(src)))
+ .collect();
+
+ Some(Lint {
+ span,
+ lint_kind: LintKind::Usage,
+ suggestions,
+ message: "English does not use words like ‘too’ or ‘very’ to modify ‘less’. \
+ • For comparisons, use ‘far less’ or ‘much less’, etc. \
+ • If describing a small amoung or quantity, use ‘very little’ (furniture, information, traffic, etc.) or ‘very few’ (items, people, things, etc.)."
+ .to_owned(),
+ ..Default::default()
+ })
+ }
+
+ fn expr(&self) -> &dyn Expr {
+ &self.expr
+ }
+
+ fn description(&self) -> &str {
+ "Corrects adverbs of degree (`too`, `very`, etc.) used with `less` mostly in the writing of native German speakers.`"
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::tests::{assert_no_lints, assert_suggestion_result};
+
+ use super::VeryLess;
+
+ // Old tests from the `VeryLess` Weir rule that only replaced `very`
+
+ #[test]
+ fn very_to_much() {
+ assert_suggestion_result(
+ "here is a simple way to do it with very less coding ... ;)",
+ VeryLess::default(),
+ "here is a simple way to do it with much less coding ... ;)",
+ );
+ }
+
+ #[test]
+ fn very_to_far() {
+ assert_suggestion_result(
+ "algorithm for processing large datasets with very less pre-configuration",
+ VeryLess::default(),
+ "algorithm for processing large datasets with far less pre-configuration",
+ );
+ }
+
+ #[test]
+ fn very_to_a_lot() {
+ assert_suggestion_result(
+ "Also the gpu memory usage is very less.",
+ VeryLess::default(),
+ "Also the gpu memory usage is a lot less.",
+ );
+ }
+
+ // Additional tests that may replace `less` or the adverb of degree
+
+ #[test]
+ fn too_less() {
+ assert_suggestion_result(
+ "Too much code for too less, maybe too many requests for only 10$.. who knows?!",
+ VeryLess::default(),
+ "Too much code for too little, maybe too many requests for only 10$.. who knows?!",
+ );
+ }
+
+ #[test]
+ fn too_less_info() {
+ assert_suggestion_result(
+ "codex showing too less information when he is think chain.",
+ VeryLess::default(),
+ "codex showing too little information when he is think chain.",
+ );
+ }
+
+ #[test]
+ fn very_less_time() {
+ assert_suggestion_result(
+ "Connection also takes very less time.",
+ VeryLess::default(),
+ "Connection also takes a lot less time.",
+ );
+ }
+
+ #[test]
+ fn too_less_space() {
+ assert_suggestion_result(
+ "Auto-abbreviate text if there is too less space",
+ VeryLess::default(),
+ "Auto-abbreviate text if there is too little space",
+ );
+ }
+
+ #[test]
+ fn cant_fix_is_quite_less() {
+ assert_suggestion_result(
+ "Based on the result, the number of genes in the braker.gtf from braker3 is quite less (25943) than braker2 (48396).",
+ VeryLess::default(),
+ "Based on the result, the number of genes in the braker.gtf from braker3 is quite a lot less (25943) than braker2 (48396).",
+ );
+ }
+
+ #[test]
+ fn cant_fix_become_quite_less() {
+ assert_suggestion_result(
+ "But those things became quite less important compared to having a consistent formatting style across a team",
+ VeryLess::default(),
+ "But those things became quite a lot less important compared to having a consistent formatting style across a team",
+ );
+ }
+
+ // Avoid false positives with certain adverbs of degree
+
+ #[test]
+ fn dont_flag_comparably_less() {
+ assert_no_lints(
+ "This is comparably less retraining, but the incorrect keypresses are the bigger issue.",
+ VeryLess::default(),
+ );
+ }
+
+ // Edge cases and false positives we don't or can't yet address
+
+ #[test]
+ #[ignore = "May be beyond the typical Germlish usage? Requires 'very low/small' etc."]
+ fn cant_fix_very_less_ccr() {
+ assert_suggestion_result(
+ "[Bug]: Disabled primary button having very less color contrast ratio",
+ VeryLess::default(),
+ "[Bug]: Disabled primary button having very low color contrast ratio",
+ );
+ }
+
+ #[test]
+ #[ignore = "Too hard to parse. Does it mean the log is too small?"]
+ fn cant_fix_log_too_less() {
+ assert_no_lints(
+ "build error log too less to find the key position",
+ VeryLess::default(),
+ );
+ }
+
+ #[test]
+ #[ignore = "Proper fix would be something like 'pretty few items' or 'a pretty small number of items', etc."]
+ fn cant_fix_pretty_less() {
+ assert_suggestion_result(
+ "I have pretty less number of items in x-axis",
+ VeryLess::default(),
+ "I have pretty few items in x-axis",
+ );
+ }
+}
diff --git a/harper-core/src/linting/weir_rules/VeryLess.weir b/harper-core/src/linting/weir_rules/VeryLess.weir
deleted file mode 100644
index b19b2468f..000000000
--- a/harper-core/src/linting/weir_rules/VeryLess.weir
+++ /dev/null
@@ -1,10 +0,0 @@
-expr main (very less)
-
-let message "English doesn't use `very` with `less`."
-let description "Corrects `very less`."
-let kind "Grammar"
-let becomes ["much less", "far less", "a lot less"]
-
-test "here is a simple way to do it with very less coding ... ;)" "here is a simple way to do it with much less coding ... ;)"
-test "algorithm for processing large datasets with very less pre-configuration" "algorithm for processing large datasets with far less pre-configuration"
-test "Also the gpu memory usage is very less." "Also the gpu memory usage is a lot less."