Skip to content

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

fix(core): using the simple case solves most false-positives

Elijah Potter450170321be9
diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dict
index a525e4864..6cb5b7194 100644
--- a/harper-core/dictionary.dict
+++ b/harper-core/dictionary.dict
@@ -50036,3 +50036,5 @@ AlphaFold/2M
DeepMind/2M
GLFW/2M
ChatGPT/2M
+amidst/+
+reimagined/4
diff --git a/harper-core/src/linting/oxford_comma.rs b/harper-core/src/linting/oxford_comma.rs
new file mode 100644
index 000000000..6a1b4a7af
--- /dev/null
+++ b/harper-core/src/linting/oxford_comma.rs
@@ -0,0 +1,197 @@
+use crate::{
+ Lrc, Token, TokenStringExt,
+ linting::Linter,
+ patterns::{OwnedPatternExt, Pattern, SequencePattern, WordSet},
+};
+
+use super::{super::Lint, LintKind, Suggestion};
+
+pub struct OxfordComma {
+ pattern: Box<dyn Pattern>,
+}
+
+impl Default for OxfordComma {
+ fn default() -> Self {
+ let item = Lrc::new(
+ SequencePattern::default()
+ .then_article()
+ .then_whitespace()
+ .then_noun()
+ .or(Box::new(SequencePattern::default().then_noun())),
+ );
+ let item_chunk = SequencePattern::default()
+ .then(item.clone())
+ .then_comma()
+ .then_whitespace();
+
+ let pattern = SequencePattern::default()
+ .then_one_or_more(item_chunk)
+ .then(item.clone())
+ .then_whitespace()
+ .then(WordSet::new(&["and", "or", "nor"]))
+ .then_whitespace()
+ .then(item.clone());
+
+ Self {
+ pattern: Box::new(pattern),
+ }
+ }
+}
+
+impl OxfordComma {
+ fn match_to_lint(&self, matched_toks: &[Token], _source: &[char]) -> Option<Lint> {
+ let conj_index = matched_toks.last_conjunction_index()?;
+ let offender = matched_toks[conj_index - 2];
+
+ Some(Lint {
+ span: offender.span,
+ lint_kind: LintKind::Style,
+ suggestions: vec![Suggestion::InsertAfter(vec![','])],
+ message: "An Oxford comma is necessary here.".to_owned(),
+ priority: 31,
+ })
+ }
+}
+
+impl Linter for OxfordComma {
+ fn lint(&mut self, document: &crate::Document) -> Vec<crate::linting::Lint> {
+ let mut lints = Vec::new();
+ for sentence in document.iter_sentences() {
+ let mut tok_cursor = 0;
+
+ loop {
+ if tok_cursor >= sentence.len() {
+ break;
+ }
+
+ let match_len = self
+ .pattern
+ .matches(&sentence[tok_cursor..], document.get_source());
+
+ if match_len != 0 {
+ let lint = self.match_to_lint(
+ &sentence[tok_cursor..tok_cursor + match_len],
+ document.get_source(),
+ );
+
+ lints.extend(lint);
+ tok_cursor += match_len;
+ } else {
+ tok_cursor += 1;
+ }
+ }
+ }
+
+ lints
+ }
+
+ fn description(&self) -> &str {
+ "The Oxford comma is one of the more controversial rules in common use today. Enabling this lint checks that there is a comma before `and`, `or`, or `nor` when listing out more than two ideas."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
+
+ use super::OxfordComma;
+
+ #[test]
+ fn fruits() {
+ assert_lint_count(
+ "An apple, a banana and a pear walk into a bar.",
+ OxfordComma::default(),
+ 1,
+ );
+ }
+
+ #[test]
+ fn people() {
+ assert_suggestion_result(
+ "Nancy, Steve and Carl are going to the coffee shop.",
+ OxfordComma::default(),
+ "Nancy, Steve, and Carl are going to the coffee shop.",
+ );
+ }
+
+ #[test]
+ fn places() {
+ assert_suggestion_result(
+ "I've always wanted to visit Paris, Tokyo and Rome.",
+ OxfordComma::default(),
+ "I've always wanted to visit Paris, Tokyo, and Rome.",
+ );
+ }
+
+ #[test]
+ fn foods() {
+ assert_suggestion_result(
+ "My favorite foods are pizza, sushi, tacos and burgers.",
+ OxfordComma::default(),
+ "My favorite foods are pizza, sushi, tacos, and burgers.",
+ );
+ }
+
+ #[test]
+ fn allows_clean_music() {
+ assert_lint_count(
+ "I enjoy listening to pop music, rock, hip-hop, electronic dance, and classical music.",
+ OxfordComma::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn allows_clean_nations() {
+ assert_lint_count(
+ "The team consists of players from different countries: France, Germany, Italy, and Spain.",
+ OxfordComma::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn or_writing() {
+ assert_suggestion_result(
+ "Harper can be a lifesaver when writing technical documents, emails or other formal forms of communication.",
+ OxfordComma::default(),
+ "Harper can be a lifesaver when writing technical documents, emails, or other formal forms of communication.",
+ );
+ }
+
+ #[test]
+ fn sports() {
+ assert_suggestion_result(
+ "They enjoy playing soccer, basketball or tennis.",
+ OxfordComma::default(),
+ "They enjoy playing soccer, basketball, or tennis.",
+ );
+ }
+
+ #[test]
+ fn nor_vegetables() {
+ assert_suggestion_result(
+ "I like carrots, kale nor broccoli.",
+ OxfordComma::default(),
+ "I like carrots, kale, nor broccoli.",
+ );
+ }
+
+ #[test]
+ fn allow_non_list_transportation() {
+ assert_lint_count(
+ "In transportation, autonomous vehicles and smart traffic management systems promise to reduce accidents and optimize travel routes.",
+ OxfordComma::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn allow_pill() {
+ assert_lint_count(
+ "Develop a pill that causes partial amnesia, affecting relationships and identity.",
+ OxfordComma::default(),
+ 0,
+ );
+ }
+}
diff --git a/harper-core/src/linting/oxford_comma/general.rs b/harper-core/src/linting/oxford_comma/general.rs
deleted file mode 100644
index 06af2494e..000000000
--- a/harper-core/src/linting/oxford_comma/general.rs
+++ /dev/null
@@ -1,99 +0,0 @@
-use crate::{
- Document, Token, TokenStringExt,
- patterns::{NounPhrase, Pattern, SequencePattern, WordSet},
-};
-
-use super::super::{Lint, LintKind, Linter, Suggestion};
-
-pub struct General {
- pattern: SequencePattern,
-}
-
-impl General {
- pub fn new() -> Self {
- let pattern = SequencePattern::default()
- .then_one_or_more(
- SequencePattern::default()
- .then(NounPhrase)
- .then_comma()
- .then_whitespace(),
- )
- .then(NounPhrase)
- .then_whitespace()
- .then(WordSet::new(&["and", "or", "nor"]))
- .then_whitespace()
- .then(NounPhrase);
-
- Self { pattern }
- }
-
- fn match_to_lint(&self, matched_toks: &[Token], _source: &[char]) -> Option<Lint> {
- let conj_index = matched_toks.last_conjunction_index()?;
- let offender = matched_toks[conj_index - 2];
-
- Some(Lint {
- span: offender.span,
- lint_kind: LintKind::Style,
- suggestions: vec![Suggestion::InsertAfter(vec![','])],
- message: "An Oxford comma is necessary here.".to_owned(),
- priority: 31,
- })
- }
-}
-
-impl Default for General {
- fn default() -> Self {
- Self::new()
- }
-}
-
-impl Linter for General {
- fn lint(&mut self, document: &Document) -> Vec<Lint> {
- let mut lints = Vec::new();
-
- for sentence in document.iter_sentences() {
- let mut tok_cursor = 0;
-
- if let Some(first) = sentence
- .first()
- .and_then(|t| t.kind.as_word().cloned())
- .flatten()
- {
- if first.preposition {
- tok_cursor = sentence
- .iter()
- .position(|t| t.kind.is_comma())
- .unwrap_or(sentence.iter().len())
- }
- }
-
- loop {
- if tok_cursor >= sentence.len() {
- break;
- }
-
- let match_len = self
- .pattern
- .matches(&sentence[tok_cursor..], document.get_source());
-
- if match_len != 0 {
- let lint = self.match_to_lint(
- &sentence[tok_cursor..tok_cursor + match_len],
- document.get_source(),
- );
-
- lints.extend(lint);
- tok_cursor += match_len;
- } else {
- tok_cursor += 1;
- }
- }
- }
-
- lints
- }
-
- fn description(&self) -> &str {
- "Covers the general cases of missing Oxford commas."
- }
-}
diff --git a/harper-core/src/linting/oxford_comma/mod.rs b/harper-core/src/linting/oxford_comma/mod.rs
deleted file mode 100644
index f8e0c2696..000000000
--- a/harper-core/src/linting/oxford_comma/mod.rs
+++ /dev/null
@@ -1,105 +0,0 @@
-use super::merge_linters::merge_linters;
-
-mod general;
-mod simple;
-use general::General;
-use simple::Simple;
-
-merge_linters!(OxfordComma => General, Simple => "The Oxford comma is one of the more controversial rules in common use today. Enabling this lint checks that there is a comma before `and`, `or`, or `nor` when listing out more than two ideas.");
-
-#[cfg(test)]
-mod tests {
- use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
-
- use super::OxfordComma;
-
- #[test]
- fn fruits() {
- assert_lint_count(
- "An apple, a banana and a pear walk into a bar.",
- OxfordComma::default(),
- 1,
- );
- }
-
- #[test]
- fn people() {
- assert_suggestion_result(
- "Nancy, Steve and Carl are going to the coffee shop.",
- OxfordComma::default(),
- "Nancy, Steve, and Carl are going to the coffee shop.",
- );
- }
-
- #[test]
- fn places() {
- assert_suggestion_result(
- "I've always wanted to visit Paris, Tokyo and Rome.",
- OxfordComma::default(),
- "I've always wanted to visit Paris, Tokyo, and Rome.",
- );
- }
-
- #[test]
- fn foods() {
- assert_suggestion_result(
- "My favorite foods are pizza, sushi, tacos and burgers.",
- OxfordComma::default(),
- "My favorite foods are pizza, sushi, tacos, and burgers.",
- );
- }
-
- #[test]
- fn allows_clean_music() {
- assert_lint_count(
- "I enjoy listening to pop music, rock, hip-hop, electronic dance, and classical music.",
- OxfordComma::default(),
- 0,
- );
- }
-
- #[test]
- fn allows_clean_nations() {
- assert_lint_count(
- "The team consists of players from different countries: France, Germany, Italy, and Spain.",
- OxfordComma::default(),
- 0,
- );
- }
-
- #[test]
- fn or_writing() {
- assert_suggestion_result(
- "Harper can be a lifesaver when writing technical documents, emails or other formal forms of communication.",
- OxfordComma::default(),
- "Harper can be a lifesaver when writing technical documents, emails, or other formal forms of communication.",
- );
- }
-
- #[test]
- fn sports() {
- assert_suggestion_result(
- "They enjoy playing soccer, basketball or tennis.",
- OxfordComma::default(),
- "They enjoy playing soccer, basketball, or tennis.",
- );
- }
-
- #[test]
- fn nor_vegetables() {
- assert_suggestion_result(
- "I like carrots, kale nor broccoli.",
- OxfordComma::default(),
- "I like carrots, kale, nor broccoli.",
- );
- }
-
- #[test]
- fn allow_non_list_transportation() {
- assert_lint_count(
- "In transportation, autonomous vehicles and smart traffic management systems promise to reduce accidents and optimize travel routes.",
- OxfordComma::default(),
- 0,
- );
- }
-}
diff --git a/harper-core/src/linting/oxford_comma/simple.rs b/harper-core/src/linting/oxford_comma/simple.rs
deleted file mode 100644
index c3980ed24..000000000
--- a/harper-core/src/linting/oxford_comma/simple.rs
+++ /dev/null
@@ -1,91 +0,0 @@
-use crate::{
- Lrc, Token, TokenStringExt,
- linting::Linter,
- patterns::{OwnedPatternExt, Pattern, SequencePattern, WordSet},
-};
-
-use super::super::{Lint, LintKind, Suggestion};
-
-pub struct Simple {
- pattern: Box<dyn Pattern>,
-}
-
-impl Default for Simple {
- fn default() -> Self {
- let item = Lrc::new(
- SequencePattern::default()
- .then_article()
- .then_whitespace()
- .then_noun()
- .or(Box::new(SequencePattern::default().then_noun())),
- );
- let item_chunk = SequencePattern::default()
- .then(item.clone())
- .then_comma()
- .then_whitespace();
-
- let pattern = SequencePattern::default()
- .then_one_or_more(item_chunk)
- .then(item.clone())
- .then_whitespace()
- .then(WordSet::new(&["and", "or", "nor"]))
- .then_whitespace()
- .then(item.clone());
-
- Self {
- pattern: Box::new(pattern),
- }
- }
-}
-
-impl Simple {
- fn match_to_lint(&self, matched_toks: &[Token], _source: &[char]) -> Option<Lint> {
- let conj_index = matched_toks.last_conjunction_index()?;
- let offender = matched_toks[conj_index - 2];
-
- Some(Lint {
- span: offender.span,
- lint_kind: LintKind::Style,
- suggestions: vec![Suggestion::InsertAfter(vec![','])],
- message: "An Oxford comma is necessary here.".to_owned(),
- priority: 31,
- })
- }
-}
-
-impl Linter for Simple {
- fn lint(&mut self, document: &crate::Document) -> Vec<crate::linting::Lint> {
- let mut lints = Vec::new();
- for sentence in document.iter_sentences() {
- let mut tok_cursor = 0;
-
- loop {
- if tok_cursor >= sentence.len() {
- break;
- }
-
- let match_len = self
- .pattern
- .matches(&sentence[tok_cursor..], document.get_source());
-
- if match_len != 0 {
- let lint = self.match_to_lint(
- &sentence[tok_cursor..tok_cursor + match_len],
- document.get_source(),
- );
-
- lints.extend(lint);
- tok_cursor += match_len;
- } else {
- tok_cursor += 1;
- }
- }
- }
-
- lints
- }
-
- fn description(&self) -> &str {
- "Covers the simple cases."
- }
-}
diff --git a/justfile b/justfile
index 7aeffab3f..0e655b5df 100644
--- a/justfile
+++ b/justfile
@@ -192,8 +192,8 @@ precommit: check test build-harperjs build-obsidian build-web
# Install `harper-cli` and `harper-ls` to your machine via `cargo`
install:
- cargo install --path harper-ls --locked
- cargo install --path harper-cli --locked
+ cargo install --path harper-ls --locked --debug
+ cargo install --path harper-cli --locked --debug
# Run `harper-cli` on the Harper repository
dogfood: