Skip to content

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

feat(core): created store for ignored lints

Elijah Potter657a84c8b688
diff --git a/harper-core/src/document.rs b/harper-core/src/document.rs
index c17722b24..4a5e5202d 100644
--- a/harper-core/src/document.rs
+++ b/harper-core/src/document.rs
@@ -25,6 +25,15 @@ impl Default for Document {
}
impl Document {
+ /// Locate all the tokens that intersect a provided span.
+ ///
+ /// Desperately needs optimization.
+ pub fn tokens_intersecting(&self, span: Span) -> Vec<Token> {
+ self.tokens()
+ .filter(|tok| tok.span.overlaps_with(span))
+ .collect()
+ }
+
/// Lexes and parses text to produce a document using a provided language
/// parser and dictionary.
pub fn new(text: &str, parser: &impl Parser, dictionary: &impl Dictionary) -> Self {
diff --git a/harper-core/src/ignored_lints.rs b/harper-core/src/ignored_lints.rs
new file mode 100644
index 000000000..a56141b89
--- /dev/null
+++ b/harper-core/src/ignored_lints.rs
@@ -0,0 +1,119 @@
+use std::hash::{DefaultHasher, Hash, Hasher};
+
+use hashbrown::HashSet;
+
+use crate::{linting::Lint, Document};
+
+/// A structure that keeps track of lints that have been ignored by users.
+#[derive(Debug)]
+pub struct IgnoredLints<'a> {
+ context_hashes: HashSet<u64>,
+ document: &'a Document,
+}
+
+impl<'a> IgnoredLints<'a> {
+ pub fn new(document: &'a Document) -> Self {
+ Self {
+ context_hashes: Default::default(),
+ document,
+ }
+ }
+
+ fn hash_lint_context(&self, lint: &Lint) -> u64 {
+ let problem_tokens = self.document.tokens_intersecting(lint.span);
+
+ let mut hasher = DefaultHasher::default();
+
+ problem_tokens
+ .into_iter()
+ .for_each(|tok| tok.kind.hash(&mut hasher));
+
+ let lint_hash = lint.spanless_hash();
+ lint_hash.hash(&mut hasher);
+
+ hasher.finish()
+ }
+
+ /// Add a lint to the list.
+ pub fn ignore_lint(&mut self, lint: &Lint) {
+ let context_hash = self.hash_lint_context(lint);
+
+ self.context_hashes.insert(context_hash);
+ }
+
+ pub fn is_ignored(&self, lint: &Lint) -> bool {
+ let hash = self.hash_lint_context(lint);
+
+ self.context_hashes.contains(&hash)
+ }
+
+ /// Remove ignored Lints from a [`Vec`].
+ pub fn remove_ignored(&self, lints: &mut Vec<Lint>) {
+ lints.retain(|lint| !self.is_ignored(lint));
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use quickcheck::TestResult;
+ use quickcheck_macros::quickcheck;
+
+ use super::IgnoredLints;
+ use crate::{
+ linting::{LintGroup, LintGroupConfig, Linter},
+ Document, FstDictionary,
+ };
+
+ #[quickcheck]
+ fn can_ignore_all(text: String) -> bool {
+ let document = Document::new_markdown_default_curated(&text);
+
+ let mut lints =
+ LintGroup::new(LintGroupConfig::default(), FstDictionary::curated()).lint(&document);
+
+ let mut ignored = IgnoredLints::new(&document);
+
+ for lint in &lints {
+ ignored.ignore_lint(lint);
+ }
+
+ ignored.remove_ignored(&mut lints);
+ lints.is_empty()
+ }
+
+ //#[test]
+ //fn throwaway() {
+ // let document = Document::new_markdown_default_curated("\0\t\0\t\0");
+ //
+ // let mut lints =
+ // LintGroup::new(LintGroupConfig::default(), FstDictionary::curated()).lint(&document);
+ //
+ // let first = lints.first().cloned().unwrap();
+ //
+ // let mut ignored = IgnoredLints::new(&document);
+ // ignored.ignore_lint(&first);
+ //
+ // dbg!(&lints);
+ // ignored.remove_ignored(&mut lints);
+ // dbg!(&lints);
+ //}
+
+ #[quickcheck]
+ fn can_ignore_first(text: String) -> TestResult {
+ let document = Document::new_markdown_default_curated(&text);
+
+ let mut lints =
+ LintGroup::new(LintGroupConfig::default(), FstDictionary::curated()).lint(&document);
+
+ let Some(first) = lints.first().cloned() else {
+ return TestResult::discard();
+ };
+
+ let mut ignored = IgnoredLints::new(&document);
+ ignored.ignore_lint(&first);
+
+ ignored.remove_ignored(&mut lints);
+
+ TestResult::from_bool(!lints.contains(&first))
+ }
+}
diff --git a/harper-core/src/lib.rs b/harper-core/src/lib.rs
index 37801a975..b31bcf805 100644
--- a/harper-core/src/lib.rs
+++ b/harper-core/src/lib.rs
@@ -6,6 +6,7 @@ mod char_string;
mod currency;
mod document;
mod fat_token;
+mod ignored_lints;
pub mod language_detection;
mod lexing;
pub mod linting;
diff --git a/harper-core/src/linting/correct_number_suffix.rs b/harper-core/src/linting/correct_number_suffix.rs
index f5f7d5f0f..7237b49c6 100644
--- a/harper-core/src/linting/correct_number_suffix.rs
+++ b/harper-core/src/linting/correct_number_suffix.rs
@@ -11,7 +11,9 @@ impl Linter for CorrectNumberSuffix {
let mut output = Vec::new();
for number_tok in document.iter_numbers() {
- let suffix_span = Span::new_with_len(number_tok.span.end, 2).pulled_by(2);
+ let Some(suffix_span) = Span::new_with_len(number_tok.span.end, 2).pulled_by(2) else {
+ continue;
+ };
if let TokenKind::Number(number, Some(suffix)) = number_tok.kind {
if let Some(correct_suffix) = NumberSuffix::correct_suffix_for(number) {
diff --git a/harper-core/src/linting/lint.rs b/harper-core/src/linting/lint.rs
index 960c5439c..2ff8b0d11 100644
--- a/harper-core/src/linting/lint.rs
+++ b/harper-core/src/linting/lint.rs
@@ -1,3 +1,5 @@
+use std::hash::{DefaultHasher, Hash, Hasher};
+
use serde::{Deserialize, Serialize};
use crate::Span;
@@ -5,7 +7,7 @@ use crate::Span;
use super::{LintKind, Suggestion};
/// An error found in text.
-#[derive(Debug, Clone, Serialize, Deserialize)]
+#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Lint {
/// The location in the source text the error lies.
/// Important for automatic lint resolution through [`Self::suggestions`].
@@ -25,6 +27,23 @@ pub struct Lint {
pub priority: u8,
}
+impl Lint {
+ /// Creates a SHA-3 hash of all elements of the lint, sans [`Self::span`].
+ /// This is useful for comparing lints while ignoring their position within the document.
+ ///
+ /// Do not assume that these hash values are stable across Harper versions.
+ pub fn spanless_hash(&self) -> u64 {
+ let mut hasher = DefaultHasher::new();
+
+ self.lint_kind.hash(&mut hasher);
+ self.suggestions.hash(&mut hasher);
+ self.message.hash(&mut hasher);
+ self.priority.hash(&mut hasher);
+
+ hasher.finish()
+ }
+}
+
impl Default for Lint {
fn default() -> Self {
Self {
diff --git a/harper-core/src/linting/lint_kind.rs b/harper-core/src/linting/lint_kind.rs
index 3357bf45b..0f1a70f01 100644
--- a/harper-core/src/linting/lint_kind.rs
+++ b/harper-core/src/linting/lint_kind.rs
@@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
/// The general category a [`Lint`] falls into.
/// There's no reason not to add a new item here if you are adding a new rule that doesn't fit
/// the existing categories.
-#[derive(Debug, Clone, Copy, Serialize, Deserialize, Is, Default)]
+#[derive(Debug, Clone, Copy, Serialize, Deserialize, Is, Default, Hash, PartialEq)]
pub enum LintKind {
Spelling,
Capitalization,
diff --git a/harper-core/src/linting/number_suffix_capitalization.rs b/harper-core/src/linting/number_suffix_capitalization.rs
index 350b198be..7c46950f4 100644
--- a/harper-core/src/linting/number_suffix_capitalization.rs
+++ b/harper-core/src/linting/number_suffix_capitalization.rs
@@ -15,7 +15,9 @@ impl Linter for NumberSuffixCapitalization {
continue;
}
- let suffix_span = Span::new_with_len(number_tok.span.end, 2).pulled_by(2);
+ let suffix_span = Span::new_with_len(number_tok.span.end, 2)
+ .pulled_by(2)
+ .unwrap();
let chars = document.get_span_content(suffix_span);
if chars.iter().any(|c| !c.is_lowercase()) {
diff --git a/harper-core/src/linting/suggestion.rs b/harper-core/src/linting/suggestion.rs
index 40fb5fb51..8c0bc1099 100644
--- a/harper-core/src/linting/suggestion.rs
+++ b/harper-core/src/linting/suggestion.rs
@@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
use crate::Span;
/// A suggested edit that could resolve a [`Lint`].
-#[derive(Debug, Clone, Serialize, Deserialize, Is, PartialEq, Eq)]
+#[derive(Debug, Clone, Serialize, Deserialize, Is, PartialEq, Eq, Hash)]
pub enum Suggestion {
/// Replace the offending text with a specific character sequence.
ReplaceWith(Vec<char>),
diff --git a/harper-core/src/span.rs b/harper-core/src/span.rs
index c12b8a9b1..227b243f9 100644
--- a/harper-core/src/span.rs
+++ b/harper-core/src/span.rs
@@ -95,11 +95,15 @@ impl Span {
}
// Subtract an amount to a copy of both [`Self::start`] and [`Self::end`]
- pub fn pulled_by(&self, by: usize) -> Self {
+ pub fn pulled_by(&self, by: usize) -> Option<Self> {
+ if by > self.start {
+ return None;
+ }
+
let mut clone = *self;
clone.start -= by;
clone.end -= by;
- clone
+ Some(clone)
}
// Add an amount a copy of both [`Self::start`] and [`Self::end`]