Skip to content

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

fix: issue #196

Elijah Potter85aa33a9b5c4
diff --git a/harper-core/src/document.rs b/harper-core/src/document.rs
index aa3042631..ed9096c69 100644
--- a/harper-core/src/document.rs
+++ b/harper-core/src/document.rs
@@ -601,6 +601,7 @@ impl TokenStringExt for Document {
create_fns_on_doc!(number);
create_fns_on_doc!(at);
create_fns_on_doc!(ellipsis);
+ create_fns_on_doc!(unlintable);
fn first_sentence_word(&self) -> Option<Token> {
self.tokens.first_sentence_word()
diff --git a/harper-core/src/linting/an_a.rs b/harper-core/src/linting/an_a.rs
index 776c73374..fc82a4ded 100644
--- a/harper-core/src/linting/an_a.rs
+++ b/harper-core/src/linting/an_a.rs
@@ -12,41 +12,51 @@ impl Linter for AnA {
fn lint(&mut self, document: &Document) -> Vec<Lint> {
let mut lints = Vec::new();
- for (first, second) in document.iter_words().tuple_windows() {
- let chars_first = document.get_span_content(first.span);
- let chars_second = document.get_span_content(second.span);
- // Break the second word on hyphens for this lint.
- // Example: "An ML-based" is an acceptable noun phrase.
- let chars_second = chars_second
- .split(|c| !c.is_alphanumeric())
- .next()
- .unwrap_or(chars_second);
-
- let is_a_an = match chars_first {
- ['a'] => Some(true),
- ['a', 'n'] => Some(false),
- _ => None,
- };
-
- let Some(a_an) = is_a_an else {
- continue;
- };
-
- let should_be_a_an = !starts_with_vowel(chars_second);
-
- if a_an != should_be_a_an {
- let replacement = match a_an {
- true => vec!['a', 'n'],
- false => vec!['a'],
+ for chunk in document.chunks() {
+ for (first_idx, second_idx) in chunk.iter_word_indices().tuple_windows() {
+ // [`TokenKind::Unlintable`] might be semantic words.
+ if chunk[first_idx..second_idx].iter_unlintables().count() > 0 {
+ continue;
+ }
+
+ let first = chunk[first_idx];
+ let second = chunk[second_idx];
+
+ let chars_first = document.get_span_content(first.span);
+ let chars_second = document.get_span_content(second.span);
+ // Break the second word on hyphens for this lint.
+ // Example: "An ML-based" is an acceptable noun phrase.
+ let chars_second = chars_second
+ .split(|c| !c.is_alphanumeric())
+ .next()
+ .unwrap_or(chars_second);
+
+ let is_a_an = match chars_first {
+ ['a'] => Some(true),
+ ['a', 'n'] => Some(false),
+ _ => None,
};
- lints.push(Lint {
- span: first.span,
- lint_kind: LintKind::Miscellaneous,
- suggestions: vec![Suggestion::ReplaceWith(replacement)],
- message: "Incorrect indefinite article.".to_string(),
- priority: 31,
- })
+ let Some(a_an) = is_a_an else {
+ continue;
+ };
+
+ let should_be_a_an = !starts_with_vowel(chars_second);
+
+ if a_an != should_be_a_an {
+ let replacement = match a_an {
+ true => vec!['a', 'n'],
+ false => vec!['a'],
+ };
+
+ lints.push(Lint {
+ span: first.span,
+ lint_kind: LintKind::Miscellaneous,
+ suggestions: vec![Suggestion::ReplaceWith(replacement)],
+ message: "Incorrect indefinite article.".to_string(),
+ priority: 31,
+ })
+ }
}
}
@@ -190,4 +200,9 @@ mod tests {
fn once_over() {
assert_lint_count("give this a once-over.", AnA, 0);
}
+
+ #[test]
+ fn issue_196() {
+ assert_lint_count("This is formatted as an `ext4` file system.", AnA, 0);
+ }
}
diff --git a/harper-core/src/token.rs b/harper-core/src/token.rs
index 7e3c6894b..a8573642e 100644
--- a/harper-core/src/token.rs
+++ b/harper-core/src/token.rs
@@ -330,6 +330,7 @@ pub trait TokenStringExt {
create_decl_for!(number);
create_decl_for!(at);
create_decl_for!(ellipsis);
+ create_decl_for!(unlintable);
fn iter_linking_verb_indices(&self) -> impl Iterator<Item = usize> + '_;
fn iter_linking_verbs(&self) -> impl Iterator<Item = Token> + '_;
@@ -344,6 +345,7 @@ impl TokenStringExt for [Token] {
create_fns_for!(number);
create_fns_for!(at);
create_fns_for!(ellipsis);
+ create_fns_for!(unlintable);
fn first_non_whitespace(&self) -> Option<Token> {
self.iter().find(|t| !t.kind.is_whitespace()).copied()