Skip to content

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

fix: wrong dictionary on `harper-ls`

Elijah Pottere20fead7b46c
diff --git a/harper-core/src/document.rs b/harper-core/src/document.rs
index f1e88c7a5..66bb9a96a 100644
--- a/harper-core/src/document.rs
+++ b/harper-core/src/document.rs
@@ -218,7 +218,7 @@ impl Document {
/// Here is an example, it is short.
/// ```
pub fn chunks(&self) -> impl Iterator<Item = &'_ [Token]> + '_ {
- let first_sentence = self
+ let first_chunk = self
.chunk_terminators()
.next()
.map(|first_term| &self.tokens[0..=first_term]);
@@ -238,7 +238,7 @@ impl Document {
Some(self.tokens.as_slice())
};
- first_sentence.into_iter().chain(rest).chain(last)
+ first_chunk.into_iter().chain(rest).chain(last)
}
/// Iterate over the locations of the sentence terminators in the document.
@@ -594,7 +594,9 @@ fn is_chunk_terminator(token: &TokenKind) -> bool {
}
match token {
- TokenKind::Punctuation(punct) => [Punctuation::Comma].contains(punct),
+ TokenKind::Punctuation(punct) => {
+ matches!(punct, Punctuation::Comma | Punctuation::Quote { .. })
+ }
_ => false
}
}
diff --git a/harper-core/src/language_detection.rs b/harper-core/src/language_detection.rs
index f0571a8d0..dd15e133f 100644
--- a/harper-core/src/language_detection.rs
+++ b/harper-core/src/language_detection.rs
@@ -22,10 +22,6 @@ pub fn is_likely_english(doc: &Document, dict: &impl Dictionary) -> bool {
}
}
- dbg!(total_words);
- dbg!(valid_words);
- dbg!(punctuation);
-
if (punctuation as f32 * 1.25) > valid_words as f32 {
return false;
}
diff --git a/harper-core/src/linting/linking_verbs.rs b/harper-core/src/linting/linking_verbs.rs
index 4b8f29656..c355b1318 100644
--- a/harper-core/src/linting/linking_verbs.rs
+++ b/harper-core/src/linting/linking_verbs.rs
@@ -47,7 +47,12 @@ mod tests {
}
#[test]
- fn working() {
+ fn working_wrong() {
assert_lint_count("working is not a noun.", LinkingVerbs, 1);
}
+
+ #[test]
+ fn working_right() {
+ assert_lint_count("\"working\" is a noun.", LinkingVerbs, 0);
+ }
}
diff --git a/harper-core/src/spell/hunspell/mod.rs b/harper-core/src/spell/hunspell/mod.rs
index 98a5be770..bf81f2b1c 100644
--- a/harper-core/src/spell/hunspell/mod.rs
+++ b/harper-core/src/spell/hunspell/mod.rs
@@ -216,6 +216,15 @@ mod tests {
assert!(!has.unwrap().is_noun(),)
}
+ #[test]
+ fn is_is_linking_verb() {
+ let expanded = build_expanded();
+
+ let is = expanded.get(&split("is"));
+ assert!(is.is_some());
+ assert!(is.unwrap().is_linking_verb());
+ }
+
fn split(text: &str) -> CharString {
text.chars().collect()
}
diff --git a/harper-ls/src/backend.rs b/harper-ls/src/backend.rs
index 9256574cf..95fd9bf83 100644
--- a/harper-ls/src/backend.rs
+++ b/harper-ls/src/backend.rs
@@ -70,18 +70,14 @@ struct DocumentState {
/// Deallocate
pub struct Backend {
client: Client,
- static_dictionary: Arc<FullDictionary>,
config: RwLock<Config>,
doc_state: Mutex<HashMap<Url, DocumentState>>
}
impl Backend {
pub fn new(client: Client, config: Config) -> Self {
- let dictionary = FullDictionary::curated();
-
Self {
client,
- static_dictionary: dictionary,
doc_state: Mutex::new(HashMap::new()),
config: RwLock::new(config)
}
@@ -131,15 +127,6 @@ impl Backend {
async fn load_user_dictionary(&self) -> FullDictionary {
let config = self.config.read().await;
- info!(
- "Loading user dictionary from `{}`",
- config
- .user_dict_path
- .clone()
- .into_os_string()
- .to_string_lossy()
- );
-
match load_dict(&config.user_dict_path).await {
Ok(dict) => dict,
Err(_err) => FullDictionary::new()
@@ -149,21 +136,12 @@ impl Backend {
async fn save_user_dictionary(&self, dict: impl Dictionary) -> anyhow::Result<()> {
let config = self.config.read().await;
- info!(
- "Saving user dictionary to `{}`",
- config
- .user_dict_path
- .clone()
- .into_os_string()
- .to_string_lossy()
- );
-
Ok(save_dict(&config.user_dict_path, dict).await?)
}
async fn generate_global_dictionary(&self) -> anyhow::Result<MergedDictionary<FullDictionary>> {
let mut dict = MergedDictionary::new();
- dict.add_dictionary(self.static_dictionary.clone());
+ dict.add_dictionary(FullDictionary::curated());
let user_dict = self.load_user_dictionary().await;
dict.add_dictionary(Arc::new(user_dict));
Ok(dict)
@@ -224,14 +202,14 @@ impl Backend {
// TODO: Only reset linter when underlying dictionaries change
+ let dict = Arc::new(self.generate_file_dictionary(url).await?);
+
let mut doc_state = DocumentState {
- linter: LintGroup::new(
- config_lock.lint_config,
- self.generate_file_dictionary(url).await?.into()
- ),
+ 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())),
+ dict: dict.clone(),
..Default::default()
};