Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: created experimental `harper-cli`
Elijah Potter09a918065b56
diff --git a/Cargo.lock b/Cargo.lockindex e0fbea6db..13d260e97 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -100,9 +100,19 @@ dependencies = [[[package]]name = "anyhow"-version = "1.0.81"+version = "1.0.83"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247"+checksum = "25bdb32cbbdce2b519a9cd7df3a678443100e265d5e25ca763b7572a5104f5f3"++[[package]]+name = "ariadne"+version = "0.4.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "44055e597c674aef7cb903b2b9f6e4cba1277ed0d2d61dae7cd52d7ffa81f8e2"+dependencies = [+ "unicode-width",+ "yansi",+][[package]]name = "async-trait"@@ -245,9 +255,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"[[package]]name = "clap"-version = "4.5.2"+version = "4.5.4"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "b230ab84b0ffdf890d5a10abdbc8b83ae1c4918275daea1ab8801f71536b2651"+checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0"dependencies = ["clap_builder","clap_derive",@@ -268,9 +278,9 @@ dependencies = [[[package]]name = "clap_derive"-version = "4.5.0"+version = "4.5.4"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "307bc0538d5f0f83b8248db3087aa92fe504e4691294d0c96c0eabc33f47ba47"+checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64"dependencies = ["heck","proc-macro2",@@ -531,6 +541,16 @@ version = "0.28.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"+[[package]]+name = "harper-cli"+version = "0.1.0"+dependencies = [+ "anyhow",+ "ariadne",+ "clap",+ "harper-core",+]+[[package]]name = "harper-core"version = "0.8.4"@@ -626,9 +646,9 @@ dependencies = [[[package]]name = "heck"-version = "0.4.1"+version = "0.5.0"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"+checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"[[package]]name = "hermit-abi"@@ -1943,6 +1963,12 @@ version = "0.52.4"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8"+[[package]]+name = "yansi"+version = "1.0.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049"+[[package]]name = "zerocopy"version = "0.7.32"diff --git a/Cargo.toml b/Cargo.tomlindex 1a5ec2722..4af11d1bc 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -1,5 +1,5 @@[workspace]-members = [ "harper-core", "harper-ls", "harper-serve", "harper-wasm"]+members = [ "harper-cli", "harper-core", "harper-ls", "harper-serve", "harper-wasm"]resolver = "2"[profile.release]diff --git a/harper-cli/Cargo.toml b/harper-cli/Cargo.tomlnew file mode 100644index 000000000..34057af17--- /dev/null+++ b/harper-cli/Cargo.toml@@ -0,0 +1,13 @@+[package]+name = "harper-cli"+version = "0.1.0"+edition = "2021"+private = true+publish = false+repository = "https://github.com/elijah-potter/harper"++[dependencies]+anyhow = "1.0.83"+ariadne = "0.4.1"+clap = { version = "4.5.4", features = ["derive"] }+harper-core = { path = "../harper-core", version = "0.8.0" }diff --git a/harper-cli/README.md b/harper-cli/README.mdnew file mode 100644index 000000000..5c840730b--- /dev/null+++ b/harper-cli/README.md@@ -0,0 +1,15 @@+# `harper-cli`++## What?++`harper-cli` is a small, experimental frontend for Harper.+It can be used in any situation where you might need to check a large number of files automatically (like in continuous integration).++Right now it is quite feature barren, mainly because the use-case has not been defined yet.+If you have any thoughts, feel free to reach out.++## Possible Future Features++- On-disk caching+- Custom dictionaries (maybe use the same ones as `harper-ls`?)+- Machine-readable outputdiff --git a/harper-cli/src/main.rs b/harper-cli/src/main.rsnew file mode 100644index 000000000..6b1da237b--- /dev/null+++ b/harper-cli/src/main.rs@@ -0,0 +1,54 @@+use std::path::PathBuf;++use ariadne::{Color, Label, Report, ReportKind, Source};+use clap::Parser;+use harper_core::{remove_overlaps, Document, FullDictionary, LintGroup, LintGroupConfig, Linter};++#[derive(Debug, Parser)]+struct Args {+ /// The Markdown file you wish to grammar check.+ file: PathBuf+}++fn main() -> anyhow::Result<()> {+ let args = Args::parse();++ let source = std::fs::read_to_string(&args.file)?;+ let doc = Document::new_markdown(&source);++ let mut linter = LintGroup::new(+ LintGroupConfig::default(),+ FullDictionary::create_from_curated()+ );+ let mut lints = linter.lint(&doc);++ if lints.is_empty() {+ println!("No lints found");+ return Ok(());+ }++ remove_overlaps(&mut lints);++ let primary_color = Color::Magenta;++ let filename = args+ .file+ .file_name()+ .map(|s| s.to_string_lossy().into())+ .unwrap_or("<file>".to_string());++ let mut report_builder = Report::build(ReportKind::Advice, &filename, 0);++ for lint in lints {+ report_builder = report_builder.with_label(+ Label::new((&filename, lint.span.into()))+ .with_message(lint.message)+ .with_color(primary_color)+ );+ }++ let report = report_builder.finish();+ report.print((&filename, Source::from(source)))?;++ std::process::exit(1);+}diff --git a/harper-core/src/linting/matcher.rs b/harper-core/src/linting/matcher.rsindex 8367c431b..8b1c13775 100644--- a/harper-core/src/linting/matcher.rs+++ b/harper-core/src/linting/matcher.rs@@ -109,6 +109,7 @@ impl Matcher {// This match list needs to be automatically expanded instead of explicitly// defined like it is now.let mut triggers = pt! {+ "CI" => "continuous integration","hashtable" => "hash table","hashmap" => "hash map","CCP" => "Chinese Communist Party",diff --git a/harper-core/src/span.rs b/harper-core/src/span.rsindex 1d0afe653..4286fe98a 100644--- a/harper-core/src/span.rs+++ b/harper-core/src/span.rs@@ -105,6 +105,12 @@ impl From<Range<usize>> for Span {}}+impl From<Span> for Range<usize> {+ fn from(value: Span) -> Self {+ value.start..value.end+ }+}+#[cfg(test)]mod tests {use crate::Span;