diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-08-29 17:35:35 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-08-29 17:35:35 +0200 |
| commit | a71a2057f286677b5bf2e064fea05024aeca0dd2 (patch) | |
| tree | 716d85481aca232abdb6c2e01a0a545c003f4c6b /crates/typst-cli/src | |
| parent | 7bdf1f57b09ea605045254013a8200373451baf0 (diff) | |
More type safety for spans
Diffstat (limited to 'crates/typst-cli/src')
| -rw-r--r-- | crates/typst-cli/src/compile.rs | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/crates/typst-cli/src/compile.rs b/crates/typst-cli/src/compile.rs index 4c4c24be..5ae569ba 100644 --- a/crates/typst-cli/src/compile.rs +++ b/crates/typst-cli/src/compile.rs @@ -8,8 +8,8 @@ use typst::diag::{bail, Severity, SourceDiagnostic, StrResult}; use typst::doc::Document; use typst::eval::{eco_format, Tracer}; use typst::geom::Color; -use typst::syntax::{FileId, Source}; -use typst::World; +use typst::syntax::{FileId, Source, Span}; +use typst::{World, WorldExt}; use crate::args::{CompileCommand, DiagnosticFormat, OutputFormat}; use crate::watch::Status; @@ -231,19 +231,16 @@ pub fn print_diagnostics( .map(|e| (eco_format!("hint: {e}")).into()) .collect(), ) - .with_labels(vec![Label::primary( - diagnostic.span.id(), - world.range(diagnostic.span), - )]); + .with_labels(label(world, diagnostic.span).into_iter().collect()); term::emit(&mut w, &config, world, &diag)?; // Stacktrace-like helper diagnostics. for point in &diagnostic.trace { let message = point.v.to_string(); - let help = Diagnostic::help().with_message(message).with_labels(vec![ - Label::primary(point.span.id(), world.range(point.span)), - ]); + let help = Diagnostic::help() + .with_message(message) + .with_labels(label(world, point.span).into_iter().collect()); term::emit(&mut w, &config, world, &help)?; } @@ -252,6 +249,11 @@ pub fn print_diagnostics( Ok(()) } +/// Create a label for a span. +fn label(world: &SystemWorld, span: Span) -> Option<Label<FileId>> { + Some(Label::primary(span.id()?, world.range(span)?)) +} + impl<'a> codespan_reporting::files::Files<'a> for SystemWorld { type FileId = FileId; type Name = String; |
