typst_shim/nightly/
syntax.rs1use std::ops::Range;
3use std::path::Path;
4
5use typst::syntax::DiagSpan;
6use typst::syntax::DiagSpanKind;
7use typst::syntax::LinkedNode;
8use typst::syntax::RootedPath;
9use typst::syntax::Side;
10use typst::syntax::Source;
11use typst::syntax::VirtualPath;
12use typst::syntax::VirtualRoot;
13use typst::syntax::package::PackageSpec;
14
15pub use crate::path::resolve_path_from_id;
16
17pub fn source_range(source: &Source, span: impl Into<DiagSpan>) -> Option<Range<usize>> {
19 match span.into().get() {
20 DiagSpanKind::Detached => None,
21 DiagSpanKind::Number { id, num, sub_range } if id == source.id() => {
22 source.range(num, sub_range)
23 }
24 DiagSpanKind::Range { id, range } if id == source.id() => Some(range),
25 _ => None,
26 }
27}
28
29pub trait LinkedNodeExt: Sized {
32 fn leaf_at_compat(&self, cursor: usize) -> Option<Self>;
34}
35
36impl LinkedNodeExt for LinkedNode<'_> {
37 fn leaf_at_compat(&self, cursor: usize) -> Option<Self> {
38 self.leaf_at(cursor, Side::Before)
39 }
40}
41
42pub trait VirtualPathExt {
45 fn as_rooted_path_compat(&self) -> &Path;
47
48 fn as_rootless_path_compat(&self) -> &Path;
50}
51
52impl VirtualPathExt for VirtualPath {
53 fn as_rooted_path_compat(&self) -> &Path {
54 Path::new(self.get_with_slash())
55 }
56
57 fn as_rootless_path_compat(&self) -> &Path {
58 Path::new(self.get_without_slash())
59 }
60}
61
62pub trait RootedPathExt {
65 fn package_compat(&self) -> Option<&PackageSpec>;
67}
68
69impl RootedPathExt for RootedPath {
70 fn package_compat(&self) -> Option<&PackageSpec> {
71 match self.root() {
72 VirtualRoot::Project => None,
73 VirtualRoot::Package(package) => Some(package),
74 }
75 }
76}