Skip to content

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

fix: linter cache getting unnecessarily busted

Elijah Potter36e478e365b8
diff --git a/harper-core/src/document.rs b/harper-core/src/document.rs
index 66bb9a96a..8fa6fac8f 100644
--- a/harper-core/src/document.rs
+++ b/harper-core/src/document.rs
@@ -14,6 +14,7 @@ use crate::{Dictionary, FatToken, FullDictionary, Lrc, Token, TokenKind, TokenSt
/// A document containing some amount of lexed and parsed English text.
/// This is
+#[derive(Debug, Clone)]
pub struct Document {
source: Lrc<Vec<char>>,
tokens: Vec<Token>
diff --git a/harper-core/src/spell/merged_dictionary.rs b/harper-core/src/spell/merged_dictionary.rs
index b27e2ac33..505c23f92 100644
--- a/harper-core/src/spell/merged_dictionary.rs
+++ b/harper-core/src/spell/merged_dictionary.rs
@@ -7,7 +7,7 @@ use crate::{CharString, WordMetadata};
/// A simple wrapper over [`Dictionary`] that allows
/// one to merge multiple dictionaries without copying.
-#[derive(Clone)]
+#[derive(Clone, PartialEq)]
pub struct MergedDictionary<T>
where
T: Dictionary + Clone
diff --git a/harper-ls/src/backend.rs b/harper-ls/src/backend.rs
index 95fd9bf83..25496f4e8 100644
--- a/harper-ls/src/backend.rs
+++ b/harper-ls/src/backend.rs
@@ -10,7 +10,6 @@ use harper_core::{
Dictionary,
Document,
FullDictionary,
- Lrc,
MergedDictionary,
Token,
TokenKind,
@@ -55,19 +54,10 @@ use tracing::{error, info};
use crate::config::Config;
use crate::diagnostics::{lint_to_code_actions, lints_to_diagnostics};
use crate::dictionary_io::{load_dict, save_dict};
+use crate::document_state::DocumentState;
use crate::git_commit_parser::GitCommitParser;
use crate::pos_conv::range_to_span;
-#[derive(Default)]
-struct DocumentState {
- document: Document,
- ident_dict: Lrc<FullDictionary>,
- dict: Lrc<MergedDictionary<FullDictionary>>,
- linter: LintGroup<Lrc<MergedDictionary<FullDictionary>>>,
- language_id: Option<String>
-}
-
-/// Deallocate
pub struct Backend {
client: Client,
config: RwLock<Config>,
@@ -198,20 +188,19 @@ impl Backend {
let mut doc_lock = self.doc_state.lock().await;
let config_lock = self.config.read().await;
- let prev_state = doc_lock.get(url);
-
- // TODO: Only reset linter when underlying dictionaries change
-
let dict = Arc::new(self.generate_file_dictionary(url).await?);
- let mut doc_state = DocumentState {
+ let doc_state = doc_lock.entry(url.clone()).or_insert(DocumentState {
linter: LintGroup::new(config_lock.lint_config, dict.clone()),
- language_id: language_id
- .map(|v| v.to_string())
- .or(prev_state.and_then(|s| s.language_id.clone())),
+ language_id: language_id.map(|v| v.to_string()),
dict: dict.clone(),
..Default::default()
- };
+ });
+
+ if doc_state.dict != dict {
+ doc_state.dict = dict.clone();
+ doc_state.linter = LintGroup::new(config_lock.lint_config, dict.clone());
+ }
let Some(language_id) = &doc_state.language_id else {
doc_lock.remove(url);
@@ -251,8 +240,6 @@ impl Backend {
return Ok(());
};
- doc_lock.insert(url.clone(), doc_state);
-
Ok(())
}
diff --git a/harper-ls/src/document_state.rs b/harper-ls/src/document_state.rs
new file mode 100644
index 000000000..a3b1b611b
--- /dev/null
+++ b/harper-ls/src/document_state.rs
@@ -0,0 +1,11 @@
+use harper_core::linting::LintGroup;
+use harper_core::{Document, FullDictionary, Lrc, MergedDictionary};
+
+#[derive(Default)]
+pub struct DocumentState {
+ pub document: Document,
+ pub ident_dict: Lrc<FullDictionary>,
+ pub dict: Lrc<MergedDictionary<FullDictionary>>,
+ pub linter: LintGroup<Lrc<MergedDictionary<FullDictionary>>>,
+ pub language_id: Option<String>
+}
diff --git a/harper-ls/src/main.rs b/harper-ls/src/main.rs
index d54da6413..daeaa8083 100644
--- a/harper-ls/src/main.rs
+++ b/harper-ls/src/main.rs
@@ -8,6 +8,7 @@ mod backend;
mod config;
mod diagnostics;
mod dictionary_io;
+mod document_state;
mod git_commit_parser;
mod pos_conv;