diff options
author | Akshay <[email protected]> | 2021-09-21 12:11:47 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-09-21 12:12:30 +0100 |
commit | 3eec886fe83b30636622d6ba97880bf473de8a0a (patch) | |
tree | d456eac8a9a78bc211cdd1223dadcbdf647552b5 /bin/src/main.rs | |
parent | 40867728c023a9f95f346671b51be68002cc7552 (diff) |
use stderr by default, improved macro code
Diffstat (limited to 'bin/src/main.rs')
-rw-r--r-- | bin/src/main.rs | 41 |
1 files changed, 29 insertions, 12 deletions
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::{ | |||
4 | }; | 4 | }; |
5 | 5 | ||
6 | use anyhow::{Context, Result}; | 6 | use anyhow::{Context, Result}; |
7 | use ariadne::{Color, Fmt, Label, Report as CliReport, ReportKind as CliReportKind, Source}; | 7 | use ariadne::{ |
8 | CharSet, Color, Config as CliConfig, Label, Report as CliReport, | ||
9 | ReportKind as CliReportKind, Source, | ||
10 | }; | ||
8 | use lib::{Report, LINTS}; | 11 | use lib::{Report, LINTS}; |
9 | use rnix::WalkEvent; | 12 | use rnix::{TextRange, WalkEvent}; |
10 | 13 | ||
11 | fn analyze(file: &str) -> Result<Vec<Report>> { | 14 | fn analyze(file: &str) -> Result<Vec<Report>> { |
12 | let parsed = rnix::parse(file).as_result()?; | 15 | let parsed = rnix::parse(file).as_result()?; |
@@ -28,6 +31,7 @@ fn analyze(file: &str) -> Result<Vec<Report>> { | |||
28 | } | 31 | } |
29 | 32 | ||
30 | fn print_report(report: Report, file_src: &str, file_path: &Path) -> Result<()> { | 33 | fn print_report(report: Report, file_src: &str, file_path: &Path) -> Result<()> { |
34 | let range = |at: TextRange| at.start().into()..at.end().into(); | ||
31 | let src_id = file_path.to_str().unwrap_or("<unknown>"); | 35 | let src_id = file_path.to_str().unwrap_or("<unknown>"); |
32 | let offset = report | 36 | let offset = report |
33 | .diagnostics | 37 | .diagnostics |
@@ -39,20 +43,33 @@ fn print_report(report: Report, file_src: &str, file_path: &Path) -> Result<()> | |||
39 | .diagnostics | 43 | .diagnostics |
40 | .iter() | 44 | .iter() |
41 | .fold( | 45 | .fold( |
42 | CliReport::build(CliReportKind::Warning, src_id, offset), | 46 | CliReport::build(CliReportKind::Warning, src_id, offset) |
47 | .with_config( | ||
48 | CliConfig::default() | ||
49 | .with_cross_gap(true) | ||
50 | .with_char_set(CharSet::ExtendedAscii), | ||
51 | ) | ||
52 | .with_message(report.note), | ||
43 | |cli_report, diagnostic| { | 53 | |cli_report, diagnostic| { |
44 | let range = { | 54 | let cli_report = cli_report.with_label( |
45 | let at = diagnostic.at; | 55 | Label::new((src_id, range(diagnostic.at))) |
46 | at.start().into()..at.end().into() | 56 | .with_message(&diagnostic.message) |
47 | }; | 57 | .with_color(Color::Magenta), |
48 | cli_report.with_label( | 58 | ); |
49 | Label::new((src_id, range)) | 59 | if let Some(s) = &diagnostic.suggestion { |
50 | .with_message(diagnostic.message.as_str().fg(Color::Yellow)), | 60 | let replacement_msg = format!("Try: `{}`", s.fix); |
51 | ) | 61 | cli_report.with_label( |
62 | Label::new((src_id, range(s.at))) | ||
63 | .with_message(replacement_msg) | ||
64 | .with_color(Color::Yellow), | ||
65 | ) | ||
66 | } else { | ||
67 | cli_report | ||
68 | } | ||
52 | }, | 69 | }, |
53 | ) | 70 | ) |
54 | .finish() | 71 | .finish() |
55 | .print((src_id, Source::from(file_src))) | 72 | .eprint((src_id, Source::from(file_src))) |
56 | .context("failed to print report to stdout") | 73 | .context("failed to print report to stdout") |
57 | } | 74 | } |
58 | 75 | ||