Skip to content

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

feat(spans-visual): add a `spans` command to harper-cli that shows spans visually

Grant Lemons9493eb7a6b77
diff --git a/harper-cli/src/main.rs b/harper-cli/src/main.rs
index 73b7d45da..40fc638b7 100644
--- a/harper-cli/src/main.rs
+++ b/harper-cli/src/main.rs
@@ -26,6 +26,11 @@ enum Args {
/// The file you wish to parse.
file: PathBuf,
},
+ /// Parse a provided document and show the spans of the detected tokens.
+ Spans {
+ /// The file you wish to display the spans.
+ file: PathBuf,
+ },
/// Emit decompressed, line-separated list of words in Harper's dictionary.
Words,
}
@@ -84,6 +89,29 @@ fn main() -> anyhow::Result<()> {
Ok(())
}
+ Args::Spans { file } => {
+ let (doc, source) = load_file(&file)?;
+
+ let primary_color = Color::Blue;
+ let filename = 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 token in doc.tokens() {
+ report_builder = report_builder.with_label(
+ Label::new((&filename, token.span.into()))
+ .with_message(format!("[{}, {})", token.span.start, token.span.end))
+ .with_color(primary_color),
+ );
+ }
+
+ let report = report_builder.finish();
+ report.print((&filename, Source::from(source)))?;
+
+ std::process::exit(1);
+ }
Args::Words => {
let dict = FstDictionary::curated();
diff --git a/justfile b/justfile
index 2bb499200..1593f3772 100644
--- a/justfile
+++ b/justfile
@@ -173,6 +173,10 @@ parse file:
lint file:
cargo run --bin harper-cli -- lint {{file}}
+# Show the spans of the parsed tokens overlapped on the file.
+spans file:
+ cargo run --bin harper-cli -- spans {{file}}
+
# Add a noun to Harper's curated dictionary.
addnoun noun:
#! /bin/bash