Skip to content

Commit

Permalink
Merge pull request #303 from grantlemons/span-visualization-command
Browse files Browse the repository at this point in the history
feat: Span visualization command
  • Loading branch information
elijah-potter authored Dec 26, 2024
2 parents 5a190db + bfffc05 commit 608433b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
40 changes: 39 additions & 1 deletion harper-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -72,7 +77,7 @@ fn main() -> anyhow::Result<()> {
let report = report_builder.finish();
report.print((&filename, Source::from(source)))?;

std::process::exit(1);
Ok(())
}
Args::Parse { file } => {
let (doc, _) = load_file(&file)?;
Expand All @@ -84,6 +89,39 @@ fn main() -> anyhow::Result<()> {

Ok(())
}
Args::Spans { file } => {
let (doc, source) = load_file(&file)?;

let primary_color = Color::Blue;
let secondary_color = Color::Magenta;
let filename = file
.file_name()
.map(|s| s.to_string_lossy().into())
.unwrap_or("<file>".to_string());

let mut report_builder =
Report::build(ReportKind::Custom("Spans", primary_color), &filename, 0);
let mut color = primary_color;
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(color),
);

// Alternate colors so spans are clear
color = if color == primary_color {
secondary_color
} else {
primary_color
};
}

let report = report_builder.finish();
report.print((&filename, Source::from(source)))?;

Ok(())
}
Args::Words => {
let dict = FstDictionary::curated();

Expand Down
4 changes: 4 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,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
Expand Down

0 comments on commit 608433b

Please sign in to comment.