Skip to content

Commit

Permalink
Merge pull request #340 from grantlemons/span-view-modifications
Browse files Browse the repository at this point in the history
feat: Span View Command Improvements
  • Loading branch information
elijah-potter authored Jan 3, 2025
2 parents 0be2d27 + 153c17a commit 6701c01
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions harper-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use clap::Parser;
use harper_comments::CommentParser;
use harper_core::linting::{LintGroup, LintGroupConfig, Linter};
use harper_core::parsers::Markdown;
use harper_core::{remove_overlaps, Dictionary, Document, FstDictionary};
use harper_core::{remove_overlaps, Dictionary, Document, FstDictionary, TokenKind};

#[derive(Debug, Parser)]
enum Args {
Expand All @@ -30,6 +30,9 @@ enum Args {
Spans {
/// The file you wish to display the spans.
file: PathBuf,
/// Include newlines in the output
#[arg(short, long)]
include_newlines: bool,
},
/// Get the metadata associated with a particular word.
Metadata { word: String },
Expand Down Expand Up @@ -91,11 +94,15 @@ fn main() -> anyhow::Result<()> {

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

let primary_color = Color::Blue;
let secondary_color = Color::Magenta;
let unlintable_color = Color::Red;
let filename = file
.file_name()
.map(|s| s.to_string_lossy().into())
Expand All @@ -104,11 +111,19 @@ fn main() -> anyhow::Result<()> {
let mut report_builder =
Report::build(ReportKind::Custom("Spans", primary_color), &filename, 0);
let mut color = primary_color;
for token in doc.tokens() {

for token in doc.tokens().filter(|t| {
include_newlines
|| !matches!(t.kind, TokenKind::Newline(_) | TokenKind::ParagraphBreak)
}) {
report_builder = report_builder.with_label(
Label::new((&filename, token.span.into()))
.with_message(format!("[{}, {})", token.span.start, token.span.end))
.with_color(color),
.with_color(if matches!(token.kind, TokenKind::Unlintable) {
unlintable_color
} else {
color
}),
);

// Alternate colors so spans are clear
Expand Down

0 comments on commit 6701c01

Please sign in to comment.