rust/crates/hir-expand/src/span_map.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

62 lines
1.7 KiB
Rust
Raw Normal View History

//! 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;
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-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),
}
}
}