use std::ops::Range;
use std::path::Path;
use typst::syntax::DiagSpan;
use typst::syntax::DiagSpanKind;
use typst::syntax::LinkedNode;
use typst::syntax::RootedPath;
use typst::syntax::Side;
use typst::syntax::Source;
use typst::syntax::VirtualPath;
use typst::syntax::VirtualRoot;
use typst::syntax::package::PackageSpec;
pub use crate::path::resolve_path_from_id;
pub fn source_range(source: &Source, span: impl Into<DiagSpan>) -> Option<Range<usize>> {
match span.into().get() {
DiagSpanKind::Detached => None,
DiagSpanKind::Number { id, num, sub_range } if id == source.id() => {
source.range(num, sub_range)
}
DiagSpanKind::Range { id, range } if id == source.id() => Some(range),
_ => None,
}
}
pub trait LinkedNodeExt: Sized {
fn leaf_at_compat(&self, cursor: usize) -> Option<Self>;
}
impl LinkedNodeExt for LinkedNode<'_> {
fn leaf_at_compat(&self, cursor: usize) -> Option<Self> {
self.leaf_at(cursor, Side::Before)
}
}
pub trait VirtualPathExt {
fn as_rooted_path_compat(&self) -> &Path;
fn as_rootless_path_compat(&self) -> &Path;
}
impl VirtualPathExt for VirtualPath {
fn as_rooted_path_compat(&self) -> &Path {
Path::new(self.get_with_slash())
}
fn as_rootless_path_compat(&self) -> &Path {
Path::new(self.get_without_slash())
}
}
pub trait RootedPathExt {
fn package_compat(&self) -> Option<&PackageSpec>;
}
impl RootedPathExt for RootedPath {
fn package_compat(&self) -> Option<&PackageSpec> {
match self.root() {
VirtualRoot::Project => None,
VirtualRoot::Package(package) => Some(package),
}
}
}