diff options
| author | lolstork <137357423+lolstork@users.noreply.github.com> | 2023-07-19 12:52:47 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-19 12:52:47 +0200 |
| commit | b37c1e27314ed9b9341dd82c1bbc8238121c7578 (patch) | |
| tree | cb8d9d4d179fdbcdbc94b3104d068cd21bf24074 /crates/typst-cli | |
| parent | 8a57395ee48ecee02c2eb833d232979730f0e445 (diff) | |
Add infrastructure for compiler warnings (#1731)
Diffstat (limited to 'crates/typst-cli')
| -rw-r--r-- | crates/typst-cli/src/compile.rs | 55 | ||||
| -rw-r--r-- | crates/typst-cli/src/watch.rs | 5 |
2 files changed, 41 insertions, 19 deletions
diff --git a/crates/typst-cli/src/compile.rs b/crates/typst-cli/src/compile.rs index 9dfa4d41..f6819319 100644 --- a/crates/typst-cli/src/compile.rs +++ b/crates/typst-cli/src/compile.rs @@ -4,9 +4,9 @@ use std::path::Path; use codespan_reporting::diagnostic::{Diagnostic, Label}; use codespan_reporting::term::{self, termcolor}; use termcolor::{ColorChoice, StandardStream}; -use typst::diag::{bail, SourceError, StrResult}; +use typst::diag::{bail, Severity, SourceDiagnostic, StrResult}; use typst::doc::Document; -use typst::eval::eco_format; +use typst::eval::{eco_format, Tracer}; use typst::geom::Color; use typst::syntax::{FileId, Source}; use typst::World; @@ -46,9 +46,13 @@ pub fn compile_once( world.reset(); world.source(world.main()).map_err(|err| err.to_string())?; - let result = typst::compile(world); + let mut tracer = Tracer::default(); + + let result = typst::compile(world, &mut tracer); let duration = start.elapsed(); + let warnings = tracer.warnings(); + match result { // Export the PDF / PNG. Ok(document) => { @@ -56,9 +60,16 @@ pub fn compile_once( tracing::info!("Compilation succeeded in {duration:?}"); if watching { - Status::Success(duration).print(command).unwrap(); + if warnings.is_empty() { + Status::Success(duration).print(command).unwrap(); + } else { + Status::PartialSuccess(duration).print(command).unwrap(); + } } + print_diagnostics(world, &[], &warnings, command.diagnostic_format) + .map_err(|_| "failed to print diagnostics")?; + if let Some(open) = command.open.take() { open_file(open.as_deref(), &command.output())?; } @@ -73,7 +84,7 @@ pub fn compile_once( Status::Error.print(command).unwrap(); } - print_diagnostics(world, *errors, command.diagnostic_format) + print_diagnostics(world, &errors, &warnings, command.diagnostic_format) .map_err(|_| "failed to print diagnostics")?; } } @@ -143,7 +154,8 @@ fn open_file(open: Option<&str>, path: &Path) -> StrResult<()> { /// Print diagnostic messages to the terminal. fn print_diagnostics( world: &SystemWorld, - errors: Vec<SourceError>, + errors: &[SourceDiagnostic], + warnings: &[SourceDiagnostic], diagnostic_format: DiagnosticFormat, ) -> Result<(), codespan_reporting::files::Error> { let mut w = match diagnostic_format { @@ -156,23 +168,28 @@ fn print_diagnostics( config.display_style = term::DisplayStyle::Short; } - for error in errors { - // The main diagnostic. - let diag = Diagnostic::error() - .with_message(error.message) - .with_notes( - error - .hints - .iter() - .map(|e| (eco_format!("hint: {e}")).into()) - .collect(), - ) - .with_labels(vec![Label::primary(error.span.id(), world.range(error.span))]); + for diagnostic in warnings.iter().chain(errors.iter()) { + let diag = match diagnostic.severity { + Severity::Error => Diagnostic::error(), + Severity::Warning => Diagnostic::warning(), + } + .with_message(diagnostic.message.clone()) + .with_notes( + diagnostic + .hints + .iter() + .map(|e| (eco_format!("hint: {e}")).into()) + .collect(), + ) + .with_labels(vec![Label::primary( + diagnostic.span.id(), + world.range(diagnostic.span), + )]); term::emit(&mut w, &config, world, &diag)?; // Stacktrace-like helper diagnostics. - for point in error.trace { + for point in &diagnostic.trace { let message = point.v.to_string(); let help = Diagnostic::help().with_message(message).with_labels(vec