Skip to content

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

Disallowed `std::collections::HashMap`s and added initial word repetition lint

Elijah Potter8f9bcbfecdc6
diff --git a/clippy.toml b/clippy.toml
new file mode 100644
index 000000000..ad9bd114b
--- /dev/null
+++ b/clippy.toml
@@ -0,0 +1 @@
+disallowed-types = ["std::collections::HashMap", "std::collections::HashSet"]
diff --git a/harper-core/src/linting/lint.rs b/harper-core/src/linting/lint.rs
index 42e3fd8a6..bb384ec67 100644
--- a/harper-core/src/linting/lint.rs
+++ b/harper-core/src/linting/lint.rs
@@ -19,6 +19,7 @@ pub enum LintKind {
Capitalization,
UnmatchedQuote,
WrongQuotes,
+ Repetition,
}
#[derive(Debug, Clone, Serialize, Deserialize, Is)]
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 8d3ba321d..f2364cf9a 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -1,4 +1,5 @@
mod lint;
+mod repeated_words;
mod sentence_capitalization;
mod spell_check;
mod unclosed_quotes;
@@ -13,11 +14,12 @@ use self::lint::Linter;
pub fn all_linters(document: &Document, dictionary: &Dictionary) -> Vec<Lint> {
let mut lints = Vec::new();
- let linters: [Linter; 4] = [
+ let linters: [Linter; 5] = [
spell_check::spell_check,
sentence_capitalization::sentence_capitalization_lint,
unclosed_quotes::unclosed_quotes,
wrong_quotes::wrong_quotes,
+ repeated_words::repeated_words_lint,
];
for linter in linters {
diff --git a/harper-core/src/linting/repeated_words.rs b/harper-core/src/linting/repeated_words.rs
new file mode 100644
index 000000000..a9e6999e0
--- /dev/null
+++ b/harper-core/src/linting/repeated_words.rs
@@ -0,0 +1,75 @@
+use ahash::HashSet;
+
+use crate::{
+ parsing::TokenStringExt, Dictionary, Document, Lint, LintKind, Span, Suggestion, Token,
+ TokenKind,
+};
+
+/// A linter that checks to make sure the first word of each sentence is capitalized.
+pub fn repeated_words_lint(document: &Document, _dictionary: &Dictionary) -> Vec<Lint> {
+ let mut lints = Vec::new();
+ let set = create_match_set();
+
+ for sentence in document.sentences() {
+ let mut iter = sentence
+ .iter_word_indices()
+ .zip(sentence.iter_words())
+ .peekable();
+
+ while let (Some((idx_a, tok_a)), Some((idx_b, tok_b))) = (iter.next(), iter.peek()) {
+ let word_a = document.get_span_content(tok_a.span);
+ let word_b = document.get_span_content(tok_b.span);
+
+ if set.contains(word_a) && word_a == word_b {
+ let intervening_tokens = &sentence[idx_a + 1..*idx_b];
+
+ // Detect and remove the whitespace between the repetitions.
+ let remove_end = tok_b.span.end;
+
+ let remove_start = if let Some(Token {
+ span,
+ kind: TokenKind::Space(_),
+ }) = intervening_tokens.last()
+ {
+ span.start
+ } else {
+ tok_b.span.start
+ };
+
+ lints.push(Lint {
+ span: Span::new(remove_start, remove_end),
+ lint_kind: LintKind::Repetition,
+ suggestions: vec![Suggestion::ReplaceWith(Vec::new())],
+ message: "Did you mean to repeat this word?".to_string(),
+ })
+ }
+ }
+ }
+
+ lints
+}
+
+fn create_match_set() -> HashSet<Vec<char>> {
+ let mut output = HashSet::default();
+
+ output.insert(vec!['t', 'h', 'e']);
+ output.insert(vec!['T', 'h', 'e']);
+ output.insert(vec!['a']);
+ output.insert(vec!['A']);
+
+ output
+}
+
+#[cfg(test)]
+mod tests {
+ use super::repeated_words_lint;
+ use crate::{Dictionary, Document};
+
+ #[test]
+ fn catches_basic() {
+ let dictionary = Dictionary::new();
+ let test = Document::new("I wanted the the banana.", false);
+ let lints = repeated_words_lint(&test, dictionary);
+ assert!(lints.len() == 1);
+ }
+}
diff --git a/harper-core/src/parsing/token.rs b/harper-core/src/parsing/token.rs
index 542fac73c..85a908ec7 100644
--- a/harper-core/src/parsing/token.rs
+++ b/harper-core/src/parsing/token.rs
@@ -91,10 +91,23 @@ pub struct Quote {
pub trait TokenStringExt {
fn first_word(&self) -> Option<Token>;
+ fn iter_word_indices(&self) -> impl Iterator<Item = usize> + '_;
+ fn iter_words(&self) -> impl Iterator<Item = &Token> + '_;
}
impl TokenStringExt for [Token] {
fn first_word(&self) -> Option<Token> {
self.iter().find(|v| v.kind.is_word()).copied()
}
+
+ fn iter_word_indices(&self) -> impl Iterator<Item = usize> + '_ {
+ self.iter()
+ .enumerate()
+ .filter(|(_, t)| t.kind.is_word())
+ .map(|(i, _)| i)
+ }
+
+ fn iter_words(&self) -> impl Iterator<Item = &Token> + '_ {
+ self.iter().filter(|t| t.kind.is_word())
+ }
}