summaryrefslogtreecommitdiff
path: root/crates/typst-cli
diff options
context:
space:
mode:
authorTobias Schmitz <tobiasschmitz2001@gmail.com>2025-06-10 14:46:27 +0200
committerGitHub <noreply@github.com>2025-06-10 12:46:27 +0000
commita18ca3481da17a4de1cc7f9890f0c61efb480655 (patch)
tree84fb3fb78574856e20626f96754957bde5920dfb /crates/typst-cli
parent82da96ed957a68017e092e2606226b45c34324f1 (diff)
Report errors in external files (#6308)
Co-authored-by: Laurenz <laurmaedje@gmail.com>
Diffstat (limited to 'crates/typst-cli')
-rw-r--r--crates/typst-cli/src/compile.rs4
-rw-r--r--crates/typst-cli/src/timings.rs2
-rw-r--r--crates/typst-cli/src/world.rs23
3 files changed, 22 insertions, 7 deletions
diff --git a/crates/typst-cli/src/compile.rs b/crates/typst-cli/src/compile.rs
index 4edb4c32..207bb7d0 100644
--- a/crates/typst-cli/src/compile.rs
+++ b/crates/typst-cli/src/compile.rs
@@ -16,7 +16,7 @@ use typst::diag::{
use typst::foundations::{Datetime, Smart};
use typst::html::HtmlDocument;
use typst::layout::{Frame, Page, PageRanges, PagedDocument};
-use typst::syntax::{FileId, Source, Span};
+use typst::syntax::{FileId, Lines, Span};
use typst::WorldExt;
use typst_pdf::{PdfOptions, PdfStandards, Timestamp};
@@ -696,7 +696,7 @@ fn label(world: &SystemWorld, span: Span) -> Option<Label<FileId>> {
impl<'a> codespan_reporting::files::Files<'a> for SystemWorld {
type FileId = FileId;
type Name = String;
- type Source = Source;
+ type Source = Lines<String>;
fn name(&'a self, id: FileId) -> CodespanResult<Self::Name> {
let vpath = id.vpath();
diff --git a/crates/typst-cli/src/timings.rs b/crates/typst-cli/src/timings.rs
index 9f017dc1..3d10bbc6 100644
--- a/crates/typst-cli/src/timings.rs
+++ b/crates/typst-cli/src/timings.rs
@@ -85,6 +85,6 @@ fn resolve_span(world: &SystemWorld, span: Span) -> Option<(String, u32)> {
let id = span.id()?;
let source = world.source(id).ok()?;
let range = source.range(span)?;
- let line = source.byte_to_line(range.start)?;
+ let line = source.lines().byte_to_line(range.start)?;
Some((format!("{id:?}"), line as u32 + 1))
}
diff --git a/crates/typst-cli/src/world.rs b/crates/typst-cli/src/world.rs
index 2da03d4d..f63d34b6 100644
--- a/crates/typst-cli/src/world.rs
+++ b/crates/typst-cli/src/world.rs
@@ -9,7 +9,7 @@ use ecow::{eco_format, EcoString};
use parking_lot::Mutex;
use typst::diag::{FileError, FileResult};
use typst::foundations::{Bytes, Datetime, Dict, IntoValue};
-use typst::syntax::{FileId, Source, VirtualPath};
+use typst::syntax::{FileId, Lines, Source, VirtualPath};
use typst::text::{Font, FontBook};
use typst::utils::LazyHash;
use typst::{Library, World};
@@ -181,10 +181,20 @@ impl SystemWorld {
}
}
- /// Lookup a source file by id.
+ /// Lookup line metadata for a file by id.
#[track_caller]
- pub fn lookup(&self, id: FileId) -> Source {
- self.source(id).expect("file id does not point to any source file")
+ pub fn lookup(&self, id: FileId) -> Lines<String> {
+ self.slot(id, |slot| {
+ if let Some(source) = slot.source.get() {
+ let source = source.as_ref().expect("file is not valid");
+ source.lines()
+ } else if let Some(bytes) = slot.file.get() {
+ let bytes = bytes.as_ref().expect("file is not valid");
+ Lines::try_from(bytes).expect("file is not valid utf-8")
+ } else {
+ panic!("file id does not point to any source file");
+ }
+ })
}
}
@@ -339,6 +349,11 @@ impl<T: Clone> SlotCell<T> {
self.accessed = false;
}
+ /// Gets the contents of the cell.
+ fn get(&self) -> Option<&FileResult<T>> {
+ self.data.as_ref()
+ }
+
/// Gets the contents of the cell or initialize them.
fn get_or_init(
&mut self,