2023-12-18 06:39:44 -06:00
|
|
|
//! Span maps for real files and macro expansions.
|
|
|
|
use span::Span;
|
|
|
|
use syntax::TextRange;
|
2023-11-24 09:38:48 -06:00
|
|
|
use triomphe::Arc;
|
|
|
|
|
2023-12-18 06:39:44 -06:00
|
|
|
pub use span::RealSpanMap;
|
2023-11-24 09:38:48 -06:00
|
|
|
|
2023-12-18 06:30:41 -06:00
|
|
|
pub type ExpansionSpanMap = span::SpanMap<Span>;
|
2023-11-24 09:38:48 -06:00
|
|
|
|
|
|
|
/// Spanmap for a macro file or a real file
|
2023-11-28 03:55:21 -06:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2023-11-24 09:38:48 -06:00
|
|
|
pub enum SpanMap {
|
|
|
|
/// Spanmap for a macro file
|
|
|
|
ExpansionSpanMap(Arc<ExpansionSpanMap>),
|
|
|
|
/// Spanmap for a real file
|
|
|
|
RealSpanMap(Arc<RealSpanMap>),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum SpanMapRef<'a> {
|
|
|
|
/// Spanmap for a macro file
|
|
|
|
ExpansionSpanMap(&'a ExpansionSpanMap),
|
|
|
|
/// Spanmap for a real file
|
|
|
|
RealSpanMap(&'a RealSpanMap),
|
|
|
|
}
|
|
|
|
|
2023-12-18 06:30:41 -06:00
|
|
|
impl mbe::SpanMapper<Span> for SpanMap {
|
|
|
|
fn span_for(&self, range: TextRange) -> Span {
|
2023-11-24 09:38:48 -06:00
|
|
|
self.span_for_range(range)
|
|
|
|
}
|
|
|
|
}
|
2023-12-18 06:30:41 -06:00
|
|
|
impl mbe::SpanMapper<Span> for SpanMapRef<'_> {
|
|
|
|
fn span_for(&self, range: TextRange) -> Span {
|
2023-11-24 09:38:48 -06:00
|
|
|
self.span_for_range(range)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SpanMap {
|
2023-12-18 06:30:41 -06:00
|
|
|
pub fn span_for_range(&self, range: TextRange) -> Span {
|
2023-11-24 09:38:48 -06:00
|
|
|
match self {
|
2023-12-21 02:54:47 -06:00
|
|
|
// FIXME: Is it correct for us to only take the span at the start? This feels somewhat
|
|
|
|
// wrong. The context will be right, but the range could be considered wrong. See
|
|
|
|
// https://github.com/rust-lang/rust/issues/23480, we probably want to fetch the span at
|
|
|
|
// the start and end, then merge them like rustc does in `Span::to
|
2023-11-28 03:55:21 -06:00
|
|
|
Self::ExpansionSpanMap(span_map) => span_map.span_at(range.start()),
|
2023-11-24 09:38:48 -06:00
|
|
|
Self::RealSpanMap(span_map) => span_map.span_for_range(range),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_ref(&self) -> SpanMapRef<'_> {
|
|
|
|
match self {
|
|
|
|
Self::ExpansionSpanMap(span_map) => SpanMapRef::ExpansionSpanMap(span_map),
|
|
|
|
Self::RealSpanMap(span_map) => SpanMapRef::RealSpanMap(span_map),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SpanMapRef<'_> {
|
2023-12-18 06:30:41 -06:00
|
|
|
pub fn span_for_range(self, range: TextRange) -> Span {
|
2023-11-24 09:38:48 -06:00
|
|
|
match self {
|
2023-11-28 03:55:21 -06:00
|
|
|
Self::ExpansionSpanMap(span_map) => span_map.span_at(range.start()),
|
2023-11-24 09:38:48 -06:00
|
|
|
Self::RealSpanMap(span_map) => span_map.span_for_range(range),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|