Skip to content

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

Merge remote-tracking branch 'upstream/master' into ls-error-improvements

Grant Lemonsfa90591b53d2
diff --cc harper-ls/src/backend.rs
index d8b6ece8b,91acf5ae2..7a4b83e91
--- a/harper-ls/src/backend.rs
+++ b/harper-ls/src/backend.rs
@@@ -2,7 -2,7 +2,7 @@@ use std::collections::HashMap
use std::path::{Component, PathBuf};
use std::sync::Arc;
- use anyhow::{anyhow, Context};
-use anyhow::{anyhow, Result};
++use anyhow::{anyhow, Context, Result};
use harper_comments::CommentParser;
use harper_core::linting::{LintGroup, Linter};
use harper_core::parsers::{CollapseIdentifiers, IsolateEnglish, Markdown, Parser, PlainEnglish};
@@@ -69,54 -70,46 +74,54 @@@ impl Backend
}
/// Get the location of the file's specific dictionary
- async fn get_file_dict_path(&self, url: &Url) -> Option<PathBuf> {
+ async fn get_file_dict_path(&self, url: &Url) -> anyhow::Result<PathBuf> {
let config = self.config.read().await;
- Some(config.file_dict_path.join(Self::file_dict_name(url)?))
+ Ok(config.file_dict_path.join(Self::file_dict_name(url)?))
}
- /// Load a speCific file's dictionary
- async fn load_file_dictionary(&self, url: &Url) -> Option<FullDictionary> {
- match load_dict(self.get_file_dict_path(url).await?).await {
- Ok(dict) => Some(dict),
- Err(_err) => Some(FullDictionary::new()),
- }
+ /// Load a specific file's dictionary
+ async fn load_file_dictionary(&self, url: &Url) -> anyhow::Result<FullDictionary> {
+ let path = self
+ .get_file_dict_path(url)
+ .await
+ .context("Unable to get file path.")?;
+
+ load_dict(path)
+ .await
+ .map_err(|err| info!("{err}"))
+ .or(Ok(FullDictionary::new()))
}
- async fn save_file_dictionary(&self, url: &Url, dict: impl Dictionary) -> anyhow::Result<()> {
+ async fn save_file_dictionary(&self, url: &Url, dict: impl Dictionary) -> Result<()> {
- Ok(save_dict(
+ save_dict(
self.get_file_dict_path(url)
.await
- .ok_or(anyhow!("Could not compute dictionary path."))?,
+ .context("Unable to get file path.")?,
dict,
)
- .await?)
+ .await
+ .context("Unable to save dictionary to path.")
}
async fn load_user_dictionary(&self) -> FullDictionary {
let config = self.config.read().await;
- match load_dict(&config.user_dict_path).await {
- Ok(dict) => dict,
- Err(_err) => FullDictionary::new(),
- }
+ load_dict(&config.user_dict_path)
+ .await
+ .map_err(|err| info!("{err}"))
+ .unwrap_or(FullDictionary::new())
}
- async fn save_user_dictionary(&self, dict: impl Dictionary) -> anyhow::Result<()> {
+ async fn save_user_dictionary(&self, dict: impl Dictionary) -> Result<()> {
let config = self.config.read().await;
- Ok(save_dict(&config.user_dict_path, dict).await?)
+ save_dict(&config.user_dict_path, dict)
+ .await
+ .map_err(|err| anyhow!("Unable to save dictionary to file: {err}"))
}
- async fn generate_global_dictionary(&self) -> anyhow::Result<MergedDictionary> {
+ async fn generate_global_dictionary(&self) -> Result<MergedDictionary> {
let mut dict = MergedDictionary::new();
dict.add_dictionary(FstDictionary::curated());
let user_dict = self.load_user_dictionary().await;
@@@ -139,17 -133,19 +144,13 @@@
Ok(global_dictionary)
}
- async fn update_document_from_file(
- &self,
- url: &Url,
- language_id: Option<&str>,
- ) -> anyhow::Result<()> {
+ async fn update_document_from_file(&self, url: &Url, language_id: Option<&str>) -> Result<()> {
- let content = match tokio::fs::read_to_string(
+ let content = tokio::fs::read_to_string(
url.to_file_path()
- .map_err(|_| anyhow::format_err!("Could not extract file path."))?,
+ .map_err(|_| anyhow!("Unable to convert URL to file path."))?,
)
.await
- {
- Ok(content) => content,
- Err(err) => {
- error!("Error updating document from file: {}", err);
- return Ok(());
- }
- };
+ .with_context(|| format!("Unable to read from file {:?}", url))?;
self.update_document(url, &content, language_id).await
}