From 3eec886fe83b30636622d6ba97880bf473de8a0a Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 21 Sep 2021 16:41:47 +0530 Subject: use stderr by default, improved macro code --- Cargo.lock | 8 ++++++++ bin/src/main.rs | 41 +++++++++++++++++++++++++++++------------ macros/src/lib.rs | 4 ++-- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 73188a4..f222d10 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "anyhow" +version = "1.0.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61604a8f862e1d5c3229fdd78f8b02c68dcf73a4c4b05fd636d12240aaa242c1" + [[package]] name = "ariadne" version = "0.1.3" @@ -21,8 +27,10 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" name = "bin" version = "0.1.0" dependencies = [ + "anyhow", "ariadne", "lib", + "rnix", ] [[package]] diff --git a/bin/src/main.rs b/bin/src/main.rs index ae45bfe..60afd5e 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -4,9 +4,12 @@ use std::{ }; use anyhow::{Context, Result}; -use ariadne::{Color, Fmt, Label, Report as CliReport, ReportKind as CliReportKind, Source}; +use ariadne::{ + CharSet, Color, Config as CliConfig, Label, Report as CliReport, + ReportKind as CliReportKind, Source, +}; use lib::{Report, LINTS}; -use rnix::WalkEvent; +use rnix::{TextRange, WalkEvent}; fn analyze(file: &str) -> Result> { let parsed = rnix::parse(file).as_result()?; @@ -28,6 +31,7 @@ fn analyze(file: &str) -> Result> { } fn print_report(report: Report, file_src: &str, file_path: &Path) -> Result<()> { + let range = |at: TextRange| at.start().into()..at.end().into(); let src_id = file_path.to_str().unwrap_or(""); let offset = report .diagnostics @@ -39,20 +43,33 @@ fn print_report(report: Report, file_src: &str, file_path: &Path) -> Result<()> .diagnostics .iter() .fold( - CliReport::build(CliReportKind::Warning, src_id, offset), + CliReport::build(CliReportKind::Warning, src_id, offset) + .with_config( + CliConfig::default() + .with_cross_gap(true) + .with_char_set(CharSet::ExtendedAscii), + ) + .with_message(report.note), |cli_report, diagnostic| { - let range = { - let at = diagnostic.at; - at.start().into()..at.end().into() - }; - cli_report.with_label( - Label::new((src_id, range)) - .with_message(diagnostic.message.as_str().fg(Color::Yellow)), - ) + let cli_report = cli_report.with_label( + Label::new((src_id, range(diagnostic.at))) + .with_message(&diagnostic.message) + .with_color(Color::Magenta), + ); + if let Some(s) = &diagnostic.suggestion { + let replacement_msg = format!("Try: `{}`", s.fix); + cli_report.with_label( + Label::new((src_id, range(s.at))) + .with_message(replacement_msg) + .with_color(Color::Yellow), + ) + } else { + cli_report + } }, ) .finish() - .print((src_id, Source::from(file_src))) + .eprint((src_id, Source::from(file_src))) .context("failed to print report to stdout") } diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 42f7565..36a8bb5 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -73,7 +73,7 @@ fn generate_name_fn(meta: &LintMeta) -> TokenStream2 { if let syn::Expr::Lit(name_lit) = name { if let Lit::Str(name_str) = &name_lit.lit { return quote! { - fn name(&self) -> &str { + fn name() -> &'static str { #name_str } }; @@ -90,7 +90,7 @@ fn generate_note_fn(meta: &LintMeta) -> TokenStream2 { if let syn::Expr::Lit(note_lit) = note { if let Lit::Str(note_str) = ¬e_lit.lit { return quote! { - fn note(&self) -> &str { + fn note() -> &'static str { #note_str } }; -- cgit v1.2.3