From 70b354e887cf735db070f766e4812357dddb8a40 Mon Sep 17 00:00:00 2001 From: spore Date: Mon, 5 Feb 2024 21:05:26 +0800 Subject: Support reading input from stdin (#3339) --- crates/typst-syntax/src/file.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'crates/typst-syntax') diff --git a/crates/typst-syntax/src/file.rs b/crates/typst-syntax/src/file.rs index 40659c6a..6699f05d 100644 --- a/crates/typst-syntax/src/file.rs +++ b/crates/typst-syntax/src/file.rs @@ -57,6 +57,24 @@ impl FileId { id } + /// Create a new unique ("fake") file specification, which is not + /// accessible by path. + /// + /// Caution: the ID returned by this method is the *only* identifier of the + /// file, constructing a file ID with a path will *not* reuse the ID even + /// if the path is the same. This method should only be used for generating + /// "virtual" file ids such as content read from stdin. + #[track_caller] + pub fn new_fake(path: VirtualPath) -> Self { + let mut interner = INTERNER.write().unwrap(); + let num = interner.from_id.len().try_into().expect("out of file ids"); + + let id = FileId(num); + let leaked = Box::leak(Box::new((None, path))); + interner.from_id.push(leaked); + id + } + /// The package the file resides in, if any. pub fn package(&self) -> Option<&'static PackageSpec> { self.pair().0.as_ref() -- cgit v1.2.3