use std::sync::Arc;
pub use svelte_syntax::SourceText;
use crate::error::{CompileError, DiagnosticKind};
use crate::SourceId;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub(crate) struct SourceSpan {
pub start: usize,
pub end: usize,
}
impl SourceSpan {
pub fn new(start: usize, end: usize) -> Self {
Self { start, end }
}
pub fn from_oxc(span: oxc_span::Span) -> Self {
Self {
start: span.start as usize,
end: span.end as usize,
}
}
pub fn offset(self, offset: usize) -> Self {
Self {
start: self.start + offset,
end: self.end + offset,
}
}
pub fn to_compile_error(self, source: &str, kind: DiagnosticKind) -> CompileError {
kind.to_compile_error_in(
SourceText::new(SourceId::new(0), source, None),
self.start,
self.end,
)
}
}
impl From<oxc_span::Span> for SourceSpan {
fn from(span: oxc_span::Span) -> Self {
Self::from_oxc(span)
}
}
impl From<(usize, usize)> for SourceSpan {
fn from((start, end): (usize, usize)) -> Self {
Self { start, end }
}
}
#[derive(Debug, Clone)]
pub(crate) struct NamedSpan {
pub name: Arc<str>,
pub span: SourceSpan,
}
impl NamedSpan {
pub fn new(name: Arc<str>, span: SourceSpan) -> Self {
Self { name, span }
}
}