Skip to content

feat: mask out comments beginning with spellchecker:ignore #861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions harper-comments/src/comment_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use std::path::Path;
use comment_parsers::{Go, JavaDoc, JsDoc, Unit};
use harper_core::parsers::{self, MarkdownOptions, Parser};
use harper_core::{MutableDictionary, Token};
use harper_tree_sitter::TreeSitterMasker;
use tree_sitter::Node;

use crate::comment_parsers;
use crate::masker::CommentMasker;

pub struct CommentParser {
inner: parsers::Mask<TreeSitterMasker, Box<dyn Parser>>,
inner: parsers::Mask<CommentMasker, Box<dyn Parser>>,
}

impl CommentParser {
Expand Down Expand Up @@ -57,7 +57,7 @@ impl CommentParser {

Some(Self {
inner: parsers::Mask::new(
TreeSitterMasker::new(language, Self::node_condition),
CommentMasker::new(language, Self::node_condition),
comment_parser,
),
})
Expand Down
1 change: 1 addition & 0 deletions harper-comments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

mod comment_parser;
mod comment_parsers;
mod masker;
pub use comment_parser::CommentParser;
56 changes: 56 additions & 0 deletions harper-comments/src/masker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use harper_core::{Masker, MutableDictionary};
use harper_tree_sitter::TreeSitterMasker;

pub struct CommentMasker {
inner: TreeSitterMasker,
ignore_condition: Box<dyn Fn(&String) -> bool + Send + Sync>,
}

impl CommentMasker {
pub fn create_ident_dict(&self, source: &[char]) -> Option<MutableDictionary> {
self.inner.create_ident_dict(source)
}

pub fn new(
language: tree_sitter::Language,
ts_node_condition: fn(&tree_sitter::Node) -> bool,
) -> Self {
Self::new_with_ignore_condition(
language,
ts_node_condition,
Box::new(|text| {
text.contains("spellchecker:ignore")
|| text.contains("spellchecker: ignore")
|| text.contains("spellcheck:ignore")
|| text.contains("spellcheck: ignore")
|| text.contains("cspell:ignore")
|| text.contains("cspell: ignore")
|| text.contains("harper:ignore")
|| text.contains("harper: ignore")
}),
)
}

pub fn new_with_ignore_condition(
language: tree_sitter::Language,
ts_node_condition: fn(&tree_sitter::Node) -> bool,
ignore_condition: Box<dyn Fn(&String) -> bool + Send + Sync>,
) -> Self {
Self {
inner: TreeSitterMasker::new(language, ts_node_condition),
ignore_condition,
}
}
}

impl Masker for CommentMasker {
fn create_mask(&self, source: &[char]) -> harper_core::Mask {
self.inner
.create_mask(source)
.iter_allowed(source)
.map(|(span, chars)| (span, chars.iter().collect::<String>()))
.filter(|(_, text)| !(self.ignore_condition)(text))
.map(|(span, _)| span)
.collect()
}
}
4 changes: 4 additions & 0 deletions harper-comments/tests/language_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ create_test!(javadoc_complex.java, 4);
create_test!(issue_132.rs, 1);
create_test!(laravel_app.php, 2);

// Checks that some comments are masked out
create_test!(ignore_comments.rs, 1);
create_test!(ignore_comments.c, 1);

// These are to make sure nothing crashes.
create_test!(empty.js, 0);
create_test!(issue_229.js, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This comment is spellcheckd
int main() {
// spellchecker:ignore Thear be code in here
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// spellcheck:ignore splling error
/// This applies to the entire comment block
#[derive(Debug)]
/// Ths comment block is checked
pub struct Testing {
// spellchecker: ignore ths struct isnt done yt
}
22 changes: 21 additions & 1 deletion harper-core/src/mask/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use itertools::Itertools;

use crate::Span;

/// A Masker is a tool that can be composed to eliminate chunks of text from
Expand All @@ -20,6 +22,21 @@ pub struct Mask {
pub(self) allowed: Vec<Span>,
}

impl FromIterator<Span> for Mask {
fn from_iter<T: IntoIterator<Item = Span>>(iter: T) -> Self {
let allowed = iter
.into_iter()
.sorted_by_key(|span| span.start)
.collect_vec();
assert!(
allowed.is_sorted_by(|a, b| a.end <= b.start),
"Masker elements cannot overlap and must be sorted!"
);

Self { allowed }
}
}

impl Mask {
/// Create a new Mask for a given piece of text, marking all text as
/// disallowed.
Expand All @@ -39,7 +56,10 @@ impl Mask {
/// Mark a span of the text as allowed.
pub fn push_allowed(&mut self, allowed: Span) {
if let Some(last) = self.allowed.last_mut() {
assert!(allowed.start >= last.end);
assert!(
allowed.start >= last.end,
"Masker elements cannot overlap and must be sorted!"
);

if allowed.start == last.end {
last.end = allowed.end;
Expand Down