Skip to content

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

feat(harper-cli): add user_dict_path and file_dict_path to lint

Donny Kim4fbfab4ef1d1
diff --git a/Cargo.lock b/Cargo.lock
index dd75a08ed..e34806c89 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -689,6 +689,7 @@ dependencies = [
"anyhow",
"ariadne",
"clap",
+ "dirs 6.0.0",
"harper-comments",
"harper-core",
"harper-literate-haskell",
diff --git a/harper-cli/Cargo.toml b/harper-cli/Cargo.toml
index 0fccb5875..fd7ded3e9 100644
--- a/harper-cli/Cargo.toml
+++ b/harper-cli/Cargo.toml
@@ -9,7 +9,8 @@ repository = "https://github.com/automattic/harper"
[dependencies]
anyhow = "1.0.97"
ariadne = "0.4.1"
-clap = { version = "4.5.32", features = ["derive"] }
+clap = { version = "4.5.32", features = ["derive", "string"] }
+dirs = "6.0.0"
harper-literate-haskell = { path = "../harper-literate-haskell", version = "0.26.0" }
harper-core = { path = "../harper-core", version = "0.26.0" }
harper-comments = { path = "../harper-comments", version = "0.26.0" }
diff --git a/harper-cli/src/main.rs b/harper-cli/src/main.rs
index 0993e4422..200ecb863 100644
--- a/harper-cli/src/main.rs
+++ b/harper-cli/src/main.rs
@@ -1,18 +1,20 @@
#![doc = include_str!("../README.md")]
use std::collections::BTreeMap;
-use std::path::{Path, PathBuf};
-use std::process;
+use std::path::{Component, Path, PathBuf};
+use std::sync::Arc;
+use std::{fs, process};
use anyhow::format_err;
use ariadne::{Color, Label, Report, ReportKind, Source};
use clap::Parser;
+use dirs::{config_dir, data_local_dir};
use harper_comments::CommentParser;
use harper_core::linting::{LintGroup, Linter};
use harper_core::parsers::{Markdown, MarkdownOptions};
use harper_core::{
- remove_overlaps, CharStringExt, Dialect, Dictionary, Document, FstDictionary,
- MutableDictionary, TokenKind, TokenStringExt, WordId,
+ remove_overlaps, CharStringExt, Dialect, Dictionary, Document, FstDictionary, MergedDictionary,
+ MutableDictionary, TokenKind, TokenStringExt, WordId, WordMetadata,
};
use harper_literate_haskell::LiterateHaskellParser;
use hashbrown::HashMap;
@@ -36,6 +38,12 @@ enum Args {
only_lint_with: Option<Vec<String>>,
#[arg(short, long)]
dialect: Dialect,
+ /// Path to the user dictionary.
+ #[arg(short, long, default_value = config_dir().unwrap().join("harper-ls/dictionary.txt").into_os_string())]
+ user_dict_path: PathBuf,
+ /// Path to the directory for file-local dictionaries.
+ #[arg(short, long, default_value = data_local_dir().unwrap().join("harper-ls/file_dictionaries/").into_os_string())]
+ file_dict_path: PathBuf,
},
/// Parse a provided document and print the detected symbols.
Parse {
@@ -76,10 +84,26 @@ fn main() -> anyhow::Result<()> {
count,
only_lint_with,
dialect,
+ user_dict_path,
+ file_dict_path,
} => {
- let (doc, source) = load_file(&file, markdown_options)?;
+ let mut merged_dict = MergedDictionary::new();
+ merged_dict.add_dictionary(dictionary);
- let mut linter = LintGroup::new_curated(dictionary, dialect);
+ match load_dict(&user_dict_path) {
+ Ok(user_dict) => merged_dict.add_dictionary(Arc::new(user_dict)),
+ Err(err) => println!("{}: {}", user_dict_path.display(), err),
+ }
+
+ let file_dict_path = file_dict_path.join(file_dict_name(&file));
+ match load_dict(&file_dict_path) {
+ Ok(file_dict) => merged_dict.add_dictionary(Arc::new(file_dict)),
+ Err(err) => println!("{}: {}", file_dict_path.display(), err),
+ }
+
+ let (doc, source) = load_file(&file, markdown_options, &merged_dict)?;
+
+ let mut linter = LintGroup::new_curated(Arc::new(merged_dict), dialect);
if let Some(rules) = only_lint_with {
linter.set_all_rules_to(Some(false));
@@ -374,3 +398,30 @@ fn print_word_derivations(word: &str, annot: &str, dictionary: &impl Dictionary)
println!(" - {}", child_str);
}
}
+
+/// Sync version of harper-ls/src/dictionary_io@load_dict
+fn load_dict(path: &Path) -> anyhow::Result<MutableDictionary> {
+ let str = fs::read_to_string(path)?;
+
+ let mut dict = MutableDictionary::new();
+ dict.extend_words(
+ str.lines()
+ .map(|l| (l.chars().collect::<Vec<_>>(), WordMetadata::default())),
+ );
+
+ Ok(dict)
+}
+
+/// Path version of harper-ls/src/dictionary_io@file_dict_name
+fn file_dict_name(path: &Path) -> PathBuf {
+ let mut rewritten = String::new();
+
+ for seg in path.components() {
+ if !matches!(seg, Component::RootDir) {
+ rewritten.push_str(&seg.as_os_str().to_string_lossy());
+ rewritten.push('%');
+ }
+ }
+
+ rewritten.into()
+}