Skip to content

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

hotfix(core): properly store spans in `PatternLinter` cache

Elijah Potter4c5805f25b71
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 9eeba6f89..9259a567c 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -383,8 +383,8 @@ impl Linter for LintGroup {
let config_hash = self.hasher_builder.hash_one(&self.config);
let key = (chunk_chars.into(), config_hash);
- if let Some(hit) = self.chunk_pattern_cache.get(&key) {
- results.extend(hit.iter().cloned());
+ let mut chunk_results = if let Some(hit) = self.chunk_pattern_cache.get(&key) {
+ hit.clone()
} else {
let mut pattern_lints = Vec::new();
@@ -394,10 +394,21 @@ impl Linter for LintGroup {
}
}
+ // Make the spans relative to the chunk start
+ for lint in &mut pattern_lints {
+ lint.span.pull_by(chunk_span.start);
+ }
+
self.chunk_pattern_cache.put(key, pattern_lints.clone());
+ pattern_lints
+ };
- results.append(&mut pattern_lints);
+ // Bring the spans back into document-space
+ for lint in &mut chunk_results {
+ lint.span.push_by(chunk_span.start);
}
+
+ results.append(&mut chunk_results);
}
results
diff --git a/harper-core/src/span.rs b/harper-core/src/span.rs
index 227b243f9..736ee4d21 100644
--- a/harper-core/src/span.rs
+++ b/harper-core/src/span.rs
@@ -2,6 +2,8 @@ use std::ops::Range;
use serde::{Deserialize, Serialize};
+use crate::CharStringExt;
+
/// A window in a [`char`] sequence.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct Span {
@@ -57,7 +59,14 @@ impl Span {
/// Get the associated content. Will panic if any aspect is invalid.
pub fn get_content<'a>(&self, source: &'a [char]) -> &'a [char] {
- self.try_get_content(source).unwrap()
+ match self.try_get_content(source) {
+ Some(v) => v,
+ None => panic!(
+ "Could not get position {:?} within \"{}\"",
+ self,
+ source.to_string()
+ ),
+ }
}
pub fn get_content_string(&self, source: &[char]) -> String {