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-syntax/src/source.rs | |
| parent | 7bdf1f57b09ea605045254013a8200373451baf0 (diff) | |
More type safety for spans
Diffstat (limited to 'crates/typst-syntax/src/source.rs')
| -rw-r--r-- | crates/typst-syntax/src/source.rs | 24 |
1 files changed, 5 insertions, 19 deletions
diff --git a/crates/typst-syntax/src/source.rs b/crates/typst-syntax/src/source.rs index 036499ab..56b27195 100644 --- a/crates/typst-syntax/src/source.rs +++ b/crates/typst-syntax/src/source.rs @@ -9,6 +9,7 @@ use comemo::Prehashed; use super::reparser::reparse; use super::{is_newline, parse, FileId, LinkedNode, Span, SyntaxNode}; +use crate::VirtualPath; /// A source file. /// @@ -44,19 +45,7 @@ impl Source { /// Create a source file without a real id and path, usually for testing. pub fn detached(text: impl Into<String>) -> Self { - Self::new(FileId::detached(), text.into()) - } - - /// Create a source file with the same synthetic span for all nodes. - pub fn synthesized(text: String, span: Span) -> Self { - let mut root = parse(&text); - root.synthesize(span); - Self(Arc::new(Repr { - id: FileId::detached(), - lines: lines(&text), - text: Prehashed::new(text), - root: Prehashed::new(root), - })) + Self::new(FileId::new(None, VirtualPath::new("main.typ")), text.into()) } /// The root node of the file's untyped syntax tree. @@ -151,12 +140,9 @@ impl Source { /// Get the byte range for the given span in this file. /// - /// Panics if the span does not point into this source file. - #[track_caller] - pub fn range(&self, span: Span) -> Range<usize> { - self.find(span) - .expect("span does not point into this source file") - .range() + /// Returns `None` if the span does not point into this source file. + pub fn range(&self, span: Span) -> Option<Range<usize>> { + Some(self.find(span)?.range()) } /// Return the index of the UTF-16 code unit at the byte index. |
