Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Span visualization command #303

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
Expand Down
Loading