summaryrefslogtreecommitdiff
path: root/crates/typst-syntax
diff options
context:
space:
mode:
authorspore <spore2050@gmail.com>2024-02-05 21:05:26 +0800
committerGitHub <noreply@github.com>2024-02-05 13:05:26 +0000
commit70b354e887cf735db070f766e4812357dddb8a40 (patch)
treec7a62d63db4518a6e7bd51ddc58df0a24c3c589f /crates/typst-syntax
parent302b87032121125bcfdb0490ad865a585ee0e3aa (diff)
Support reading input from stdin (#3339)
Diffstat (limited to 'crates/typst-syntax')
-rw-r--r--crates/typst-syntax/src/file.rs18
1 files changed, 18 insertions, 0 deletions
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()