Skip to content

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

feat: allowed use of stuff like `@param ...`

Elijah Potter222f10511038
diff --git a/harper-comments/src/comment_parsers/javadoc.rs b/harper-comments/src/comment_parsers/javadoc.rs
index d3e6d42e6..dac0438a3 100644
--- a/harper-comments/src/comment_parsers/javadoc.rs
+++ b/harper-comments/src/comment_parsers/javadoc.rs
@@ -57,6 +57,23 @@ impl Parser for JavaDoc {
token.span.push_by(actual.start);
}
+ super::jsdoc::mark_inline_tags(&mut tokens, source);
+
+ // Mark @tags as unlintable
+ for i in 3..tokens.len() {
+ let a = tokens[i - 3];
+ let b = tokens[i - 2];
+ let c = tokens[i - 1];
+ let d = tokens[i];
+
+ if a.kind.is_at() && b.kind.is_word() && c.kind.is_space() && d.kind.is_word() {
+ tokens[i - 3].kind = TokenKind::Unlintable;
+ tokens[i - 2].kind = TokenKind::Unlintable;
+ tokens[i - 1].kind = TokenKind::Unlintable;
+ tokens[i].kind = TokenKind::Unlintable;
+ }
+ }
+
tokens
}
}
diff --git a/harper-comments/src/comment_parsers/jsdoc.rs b/harper-comments/src/comment_parsers/jsdoc.rs
index 0948008aa..3578b2906 100644
--- a/harper-comments/src/comment_parsers/jsdoc.rs
+++ b/harper-comments/src/comment_parsers/jsdoc.rs
@@ -17,7 +17,7 @@ impl Parser for JsDoc {
new_tokens.push(Token::new(
Span::new_with_len(line.len(), 1),
- harper_core::TokenKind::Newline(1)
+ harper_core::TokenKind::Newline(1),
));
new_tokens
@@ -43,38 +43,8 @@ fn parse_line(source: &[char]) -> Vec<Token> {
let mut new_tokens = Markdown.parse(source_line);
- let mut cursor = 0;
-
// Handle inline tags
- loop {
- if cursor >= new_tokens.len() {
- break;
- }
-
- if let Some(new_cursor) = &new_tokens[cursor..]
- .iter()
- .position(|t| t.kind == TokenKind::Punctuation(Punctuation::OpenCurly))
- .map(|i| i + cursor)
- {
- cursor = *new_cursor;
- } else {
- break;
- }
-
- let parsers = [parse_link, parse_tutorial];
-
- for parser in parsers {
- if let Some(p) = parser(&new_tokens[cursor..], source_line) {
- for tok in &mut new_tokens[cursor..cursor + p] {
- tok.kind = TokenKind::Unlintable;
- }
-
- cursor += p;
- continue;
- }
- cursor += 1;
- }
- }
+ mark_inline_tags(&mut new_tokens, source_line);
// Handle the block tag, if it exists on the current line.
if let Some(tag_start) = new_tokens.iter().tuple_windows().position(|(a, b)| {
@@ -104,17 +74,40 @@ fn parse_line(source: &[char]) -> Vec<Token> {
new_tokens
}
-fn parse_link(tokens: &[Token], source: &[char]) -> Option<usize> {
- parse_inline_tag(&['l', 'i', 'n', 'k'], tokens, source)
-}
+/// Locate all inline tags (i.e. `{@tag ..}`) and mark them as unlintable
+pub(super) fn mark_inline_tags(tokens: &mut [Token], source: &[char]) {
+ let mut cursor = 0;
+
+ loop {
+ if cursor >= tokens.len() {
+ break;
+ }
+
+ if let Some(new_cursor) = &tokens[cursor..]
+ .iter()
+ .position(|t| t.kind == TokenKind::Punctuation(Punctuation::OpenCurly))
+ .map(|i| i + cursor)
+ {
+ cursor = *new_cursor;
+ } else {
+ break;
+ }
-fn parse_tutorial(tokens: &[Token], source: &[char]) -> Option<usize> {
- parse_inline_tag(&['t', 'u', 't', 'o', 'r', 'i', 'a', 'l'], tokens, source)
+ if let Some(p) = parse_inline_tag(&tokens[cursor..]) {
+ for tok in &mut tokens[cursor..cursor + p] {
+ tok.kind = TokenKind::Unlintable;
+ }
+
+ cursor += p;
+ continue;
+ }
+ cursor += 1;
+ }
}
/// Checks if the provided token slice begins with an inline tag, returning it's
/// end if so.
-fn parse_inline_tag(tag_name: &[char], tokens: &[Token], source: &[char]) -> Option<usize> {
+fn parse_inline_tag(tokens: &[Token]) -> Option<usize> {
if !matches!(
tokens,
[
@@ -136,10 +129,6 @@ fn parse_inline_tag(tag_name: &[char], tokens: &[Token], source: &[char]) -> Opt
return None;
}
- if tokens[2].span.get_content(source) != tag_name {
- return None;
- }
-
let mut cursor = 3;
while !matches!(
diff --git a/harper-comments/src/comment_parsers/mod.rs b/harper-comments/src/comment_parsers/mod.rs
index c668e968a..f39c5bd24 100644
--- a/harper-comments/src/comment_parsers/mod.rs
+++ b/harper-comments/src/comment_parsers/mod.rs
@@ -30,7 +30,7 @@ fn without_initiators(source: &[char]) -> Span {
}
fn is_comment_character(c: char) -> bool {
- matches!(c, '#' | '-' | '/' | '*')
+ matches!(c, '#' | '-' | '/' | '*' | '!')
}
#[cfg(test)]
diff --git a/harper-comments/tests/language_support.rs b/harper-comments/tests/language_support.rs
index 9d73e2d27..6793aa35b 100644
--- a/harper-comments/tests/language_support.rs
+++ b/harper-comments/tests/language_support.rs
@@ -41,3 +41,5 @@ create_test!(clean.rs, 0);
create_test!(jsdoc.ts, 4);
create_test!(issue_96.lua, 0);
create_test!(merged_lines.ts, 1);
+create_test!(javadoc_clean_simple.java, 0);
+create_test!(javadoc_complex.java, 4);
diff --git a/harper-comments/tests/language_support_sources/javadoc_clean_simple.java b/harper-comments/tests/language_support_sources/javadoc_clean_simple.java
new file mode 100644
index 000000000..54278f565
--- /dev/null
+++ b/harper-comments/tests/language_support_sources/javadoc_clean_simple.java
@@ -0,0 +1,9 @@
+class TestClass {
+
+ /**
+ * This is a JavaDoc without any of the fancy frills that come with it.
+ */
+ public static void main(String[] args) {
+ System.out.println("Hello world.");
+ }
+}
diff --git a/harper-comments/tests/language_support_sources/javadoc_complex.java b/harper-comments/tests/language_support_sources/javadoc_complex.java
new file mode 100644
index 000000000..2c2bb23d8
--- /dev/null
+++ b/harper-comments/tests/language_support_sources/javadoc_complex.java
@@ -0,0 +1,27 @@
+class TestClass {
+ /**
+ * This is a JavaDoc with <i>many</i> of the fancy frills that come with it.
+ *
+ * <p>
+ * Notably, the allowed use of HTML inline to <i>format</i> the text.
+ * </p>
+ *
+ * Also, the allowed use of the various metadata tags we can attach to methods
+ * and classes.
+ *
+ * @param args these are the arguents passed to the program from the command
+ * lin.
+ */
+ public static void main(String[] args) {
+ greet("world");
+ }
+
+ /**
+ * This doc has a link in it: {@link this sould b ignor} but not tis
+ *
+ * @param this is an other test.
+ */
+ public static void greet(String name) {
+ System.out.println("Hello " + name + ".");
+ }
+}
diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dict
index 7f66ee172..08ea4e3d2 100644
--- a/harper-core/dictionary.dict
+++ b/harper-core/dictionary.dict
@@ -49623,3 +49623,4 @@ suprachiasmatic
cannabinoids
eudaimonia
whitespace
+JavaDoc
diff --git a/harper-core/src/linting/matcher.rs b/harper-core/src/linting/matcher.rs
index d645af88f..185d374b1 100644
--- a/harper-core/src/linting/matcher.rs
+++ b/harper-core/src/linting/matcher.rs
@@ -1,20 +1,11 @@
use crate::{
- CharString,
- Document,
- Lint,
- LintKind,
- Linter,
- Punctuation,
- Span,
- Suggestion,
- Token,
- TokenKind
+ CharString, Document, Lint, LintKind, Linter, Punctuation, Span, Suggestion, Token, TokenKind,
};
#[derive(Debug, PartialEq, PartialOrd, Clone)]
struct PatternToken {
kind: TokenKind,
- content: Option<CharString>
+ content: Option<CharString>,
}
impl PatternToken {
@@ -22,12 +13,12 @@ impl PatternToken {
if token.kind.is_word() {
Self {
kind: token.kind,
- content: Some(document.get_span_content(token.span).into())
+ content: Some(document.get_span_content(token.span).into()),
}
} else {
Self {
kind: token.kind,
- content: None
+ content: None,
}
}
}
@@ -95,13 +86,13 @@ macro_rules! pt {
struct Rule {
pattern: Vec<PatternToken>,
- replace_with: Vec<char>
+ replace_with: Vec<char>,
}
/// A linter that uses a variety of curated pattern matches to find and fix
/// common grammatical issues.
pub struct Matcher {
- triggers: Vec<Rule>
+ triggers: Vec<Rule>,
}
impl Matcher {
@@ -281,31 +272,32 @@ impl Matcher {
"TreeSitter" => "Tree-sitter",
"Treesitter" => "Tree-sitter",
"Tree", "sitter" => "Tree-sitter",
- "all", "of", "the" => "all the"
+ "all", "of", "the" => "all the",
+ "an", "other" => "another"
};
// TODO: Improve the description for this lint specifically.
// We need to be more explicit that we are replacing with an Em dash
triggers.push(Rule {
pattern: vec![pt!(Hyphen), pt!(Hyphen), pt!(Hyphen)],
- replace_with: vecword!("—")
+ replace_with: vecword!("—"),
});
// Same goes for this En dash
triggers.push(Rule {
pattern: vec![pt!(Hyphen), pt!(Hyphen)],
- replace_with: vecword!("–")
+ replace_with: vecword!("–"),
});
// And this ellipsis
triggers.push(Rule {
pattern: vec![pt!(Period), pt!(Period), pt!(Period)],
- replace_with: vecword!("…")
+ replace_with: vecword!("…"),
});
triggers.push(Rule {
pattern: vec![pt!("L"), pt!(Period), pt!("L"), pt!(Period), pt!("M")],
- replace_with: vecword!("large language model")
+ replace_with: vecword!("large language model"),
});
triggers.push(Rule {
@@ -317,7 +309,7 @@ impl Matcher {
pt!("M"),
pt!(Period),
],
- replace_with: vecword!("large language model")
+ replace_with: vecword!("large language model"),
});
Self { triggers }
@@ -357,7 +349,7 @@ impl Linter for Matcher {
if match_tokens.len() == trigger.pattern.len() && !match_tokens.is_empty() {
let span = Span::new(
match_tokens.first().unwrap().span.start,
- match_tokens.last().unwrap().span.end
+ match_tokens.last().unwrap().span.end,
);
lints.push(Lint {
@@ -368,7 +360,7 @@ impl Linter for Matcher {
"Did you mean “{}”?",
trigger.replace_with.iter().collect::<String>()
),
- priority: 15
+ priority: 15,
})
}
}