Skip to content

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

feat: reintroduced spellcheck caching for `harper-ls`

Elijah Potterc8071f893dcc
diff --git a/harper-core/src/document.rs b/harper-core/src/document.rs
index bda3393fe..99b3b55d3 100644
--- a/harper-core/src/document.rs
+++ b/harper-core/src/document.rs
@@ -18,6 +18,12 @@ pub struct Document {
parser: Box<dyn Parser>,
}
+impl Default for Document {
+ fn default() -> Self {
+ Self::new("", Box::new(PlainEnglish))
+ }
+}
+
impl Document {
/// Lexes and parses text to produce a document.
/// Choosing to parse with markdown may have a performance penalty
@@ -358,7 +364,7 @@ mod tests {
assert_eq!(document.tokens.len(), final_tok_count);
- let mut markdown_parser = Markdown;
+ let markdown_parser = Markdown;
let document = Document::new(text, Box::new(markdown_parser));
// We add one because the Markdown parser inserts a newline at end-of-input.
diff --git a/harper-core/src/lexing/mod.rs b/harper-core/src/lexing/mod.rs
index 4e743daf1..7d3fa5fcc 100644
--- a/harper-core/src/lexing/mod.rs
+++ b/harper-core/src/lexing/mod.rs
@@ -144,6 +144,8 @@ fn lex_punctuation(source: &[char]) -> Option<FoundToken> {
'–' => EnDash,
'—' => EmDash,
'…' => Ellipsis,
+ '^' => Carrot,
+ '+' => Plus,
_ => return None,
};
diff --git a/harper-core/src/linting/lint_set.rs b/harper-core/src/linting/lint_set.rs
index 86bc404bc..e3c38f3ac 100644
--- a/harper-core/src/linting/lint_set.rs
+++ b/harper-core/src/linting/lint_set.rs
@@ -9,11 +9,11 @@ use super::{
unclosed_quotes::UnclosedQuotes, wrong_quotes::WrongQuotes,
};
-pub struct LintSet<'a> {
- pub(super) linters: Vec<Box<dyn Linter + 'a>>,
+pub struct LintSet {
+ pub(super) linters: Vec<Box<dyn Linter>>,
}
-impl<'a> Linter for LintSet<'a> {
+impl Linter for LintSet {
fn lint(&mut self, document: &Document) -> Vec<Lint> {
let mut lints = Vec::new();
@@ -27,14 +27,14 @@ impl<'a> Linter for LintSet<'a> {
}
}
-impl<'a> LintSet<'a> {
+impl LintSet {
pub fn new() -> Self {
Self {
linters: Vec::new(),
}
}
- pub fn add_standard(&mut self, dictionary: impl Dictionary + 'a) -> &mut Self {
+ pub fn add_standard(&mut self, dictionary: impl Dictionary + 'static) -> &mut Self {
self.add_repeated_words()
.add_long_sentences()
.add_unclosed_quotes()
@@ -45,23 +45,23 @@ impl<'a> LintSet<'a> {
self
}
- pub fn with_standard(mut self, dictionary: impl Dictionary + 'a) -> Self {
+ pub fn with_standard(mut self, dictionary: impl Dictionary + 'static) -> Self {
self.add_standard(dictionary);
self
}
- pub fn add_spell_check(&mut self, dictionary: impl Dictionary + 'a) -> &mut Self {
+ pub fn add_spell_check(&mut self, dictionary: impl Dictionary + 'static) -> &mut Self {
self.linters.push(Box::new(SpellCheck::new(dictionary)));
self
}
- pub fn with_spell_check(mut self, dictionary: impl Dictionary + 'a) -> Self {
+ pub fn with_spell_check(mut self, dictionary: impl Dictionary + 'static) -> Self {
self.add_spell_check(dictionary);
self
}
}
-impl Default for LintSet<'_> {
+impl Default for LintSet {
fn default() -> Self {
Self::new()
}
@@ -70,7 +70,7 @@ impl Default for LintSet<'_> {
/// Create builder methods for the linters that do not take any arguments.
macro_rules! create_simple_builder_methods {
($($linter:ident),*) => {
- impl LintSet<'_> {
+ impl LintSet {
paste! {
$(
#[doc = "Modifies self, adding the `" $linter "` linter to the set."]
diff --git a/harper-core/src/parsers/mod.rs b/harper-core/src/parsers/mod.rs
index 50354a188..9e608c7cd 100644
--- a/harper-core/src/parsers/mod.rs
+++ b/harper-core/src/parsers/mod.rs
@@ -25,7 +25,7 @@ where
#[cfg(test)]
mod tests {
- use super::{Markdown, Parser, PlainEnglish, StrParser};
+ use super::{Markdown, Parser, PlainEnglish};
use crate::{
Punctuation,
TokenKind::{self, *},
diff --git a/harper-core/src/spell/full_dictionary.rs b/harper-core/src/spell/full_dictionary.rs
index 2a8814496..72aa9ce3c 100644
--- a/harper-core/src/spell/full_dictionary.rs
+++ b/harper-core/src/spell/full_dictionary.rs
@@ -10,7 +10,7 @@ use super::{
/// A full, fat dictionary.
/// All of the elements are stored in-memory.
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Eq, PartialEq)]
pub struct FullDictionary {
/// Storing a separate [`Vec`] for iterations speeds up spellchecking by ~16% at the cost of
/// additional memory.
diff --git a/harper-core/src/token.rs b/harper-core/src/token.rs
index e3f75c5dc..d9c3aed64 100644
--- a/harper-core/src/token.rs
+++ b/harper-core/src/token.rs
@@ -60,11 +60,7 @@ impl TokenKind {
/// Checks whether the token is whitespace.
pub fn is_whitespace(&self) -> bool {
- match self {
- TokenKind::Space(_) => true,
- TokenKind::Newline(_) => true,
- _ => false,
- }
+ matches!(self, TokenKind::Space(_) | TokenKind::Newline(_))
}
}
@@ -125,6 +121,10 @@ pub enum Punctuation {
Tilde,
/// `@`
At,
+ /// `^`
+ Carrot,
+ /// `+`
+ Plus,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, PartialOrd)]
diff --git a/harper-ls/src/backend.rs b/harper-ls/src/backend.rs
index abdcce491..3dcd6d408 100644
--- a/harper-ls/src/backend.rs
+++ b/harper-ls/src/backend.rs
@@ -22,15 +22,30 @@ use crate::{
tree_sitter_parser::TreeSitterParser,
};
+#[derive(Default)]
+struct DocumentState {
+ document: Document,
+ ident_dict: Arc<FullDictionary>,
+ linter: LintSet,
+}
+
pub struct Backend {
client: Client,
global_dictionary: Arc<FullDictionary>,
- files: Mutex<HashMap<Url, Document>>,
- /// The identifiers extracted from each file by Tree-sitter.
- ident_dicts: Mutex<HashMap<Url, Arc<FullDictionary>>>,
+ doc_state: Mutex<HashMap<Url, DocumentState>>,
}
impl Backend {
+ pub fn new(client: Client) -> Self {
+ let dictionary = FullDictionary::create_from_curated();
+
+ Self {
+ client,
+ global_dictionary: dictionary.into(),
+ doc_state: Mutex::new(HashMap::new()),
+ }
+ }
+
async fn update_document_from_file(&self, url: &Url) {
let Ok(content) = tokio::fs::read_to_string(url.path()).await else {
// TODO: Proper error handling here.
@@ -40,15 +55,28 @@ impl Backend {
}
async fn update_document(&self, url: &Url, text: &str) {
- let doc = if let Some(extension) = url.to_file_path().unwrap().extension() {
+ let mut lock = self.doc_state.lock().await;
+ let doc_state = lock.entry(url.clone()).or_insert(DocumentState {
+ linter: LintSet::new().with_standard(self.global_dictionary.clone()),
+ ..Default::default()
+ });
+
+ doc_state.document = if let Some(extension) = url.to_file_path().unwrap().extension() {
if let Some(ts_parser) =
TreeSitterParser::new_from_extension(&extension.to_string_lossy())
{
let doc = Document::new(text, Box::new(ts_parser.clone()));
if let Some(new_dict) = ts_parser.create_ident_dict(doc.get_full_content()) {
- let mut ident_dicts = self.ident_dicts.lock().await;
- ident_dicts.insert(url.clone(), new_dict.into());
+ let new_dict = Arc::new(new_dict);
+
+ if doc_state.ident_dict != new_dict {
+ doc_state.ident_dict = new_dict.clone();
+ let mut merged = MergedDictionary::new();
+ merged.add_dictionary(new_dict);
+ merged.add_dictionary(self.global_dictionary.clone());
+ doc_state.linter = LintSet::new().with_standard(merged);
+ }
}
doc
@@ -58,33 +86,18 @@ impl Backend {
} else {
Document::new(text, Box::new(Markdown))
};
-
- let mut files = self.files.lock().await;
- files.insert(url.clone(), doc);
- }
-
- async fn create_linter(&self, url: &Url) -> LintSet {
- let mut dictionary = MergedDictionary::new();
- dictionary.add_dictionary(self.global_dictionary.clone());
-
- if let Some(ident_dict) = self.ident_dicts.lock().await.get(url) {
- dictionary.add_dictionary(ident_dict.clone());
- };
-
- LintSet::new().with_standard(dictionary)
}
async fn generate_code_actions(&self, url: &Url, range: Range) -> Result<Vec<CodeAction>> {
- let files = self.files.lock().await;
- let Some(document) = files.get(url) else {
- return Ok(vec![]);
+ let mut doc_states = self.doc_state.lock().await;
+ let Some(doc_state) = doc_states.get_mut(url) else {
+ return Ok(Vec::new());
};
- let mut linter = self.create_linter(url).await;
- let mut lints = linter.lint(document);
+ let mut lints = doc_state.linter.lint(&doc_state.document);
lints.sort_by_key(|l| l.priority);
- let source_chars = document.get_full_content();
+ let source_chars = doc_state.document.get_full_content();
// Find lints whose span overlaps with range
let span = range_to_span(source_chars, range);
@@ -98,28 +111,15 @@ impl Backend {
Ok(actions)
}
- pub fn new(client: Client) -> Self {
- let dictionary = FullDictionary::create_from_curated();
-
- Self {
- client,
- global_dictionary: dictionary.into(),
- files: Mutex::new(HashMap::new()),
- ident_dicts: Mutex::new(HashMap::new()),
- }
- }
-
async fn generate_diagnostics(&self, url: &Url) -> Vec<Diagnostic> {
- let files = self.files.lock().await;
-
- let Some(document) = files.get(url) else {
- return vec![];
+ let mut doc_states = self.doc_state.lock().await;
+ let Some(doc_state) = doc_states.get_mut(url) else {
+ return Vec::new();
};
- let mut linter = self.create_linter(url).await;
- let lints = linter.lint(document);
+ let lints = doc_state.linter.lint(&doc_state.document);
- lints_to_diagnostics(document.get_full_content(), &lints)
+ lints_to_diagnostics(doc_state.document.get_full_content(), &lints)
}
async fn publish_diagnostics(&self, url: &Url) {
diff --git a/harper-ls/src/tree_sitter_parser.rs b/harper-ls/src/tree_sitter_parser.rs
index f896872e8..0836f1ee9 100644
--- a/harper-ls/src/tree_sitter_parser.rs
+++ b/harper-ls/src/tree_sitter_parser.rs
@@ -89,7 +89,7 @@ impl TreeSitterParser {
});
}
- fn visit_nodes(cursor: &mut TreeCursor, mut visit: &mut impl FnMut(&Node)) {
+ fn visit_nodes(cursor: &mut TreeCursor, visit: &mut impl FnMut(&Node)) {
if !cursor.goto_first_child() {
return;
}