2023-11-28 09:50:05 -06:00
|
|
|
//! Spanmaps allow turning absolute ranges into relative ranges for incrementality purposes as well
|
|
|
|
//! as associating spans with text ranges in a particular file.
|
2023-12-18 06:30:41 -06:00
|
|
|
|
|
|
|
// FIXME: Consider moving this into the span crate
|
|
|
|
|
|
|
|
use base_db::FileId;
|
|
|
|
use span::{ErasedFileAstId, Span, SpanAnchor, SyntaxContextId, ROOT_ERASED_FILE_AST_ID};
|
2023-11-24 09:38:48 -06:00
|
|
|
use syntax::{ast::HasModuleItem, AstNode, TextRange, TextSize};
|
|
|
|
use triomphe::Arc;
|
|
|
|
|
|
|
|
use crate::db::ExpandDatabase;
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2023-12-18 06:30:41 -06:00
|
|
|
impl mbe::SpanMapper<Span> for RealSpanMap {
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Hash, Debug)]
|
|
|
|
pub struct RealSpanMap {
|
|
|
|
file_id: FileId,
|
|
|
|
/// Invariant: Sorted vec over TextSize
|
|
|
|
// FIXME: SortedVec<(TextSize, ErasedFileAstId)>?
|
|
|
|
pairs: Box<[(TextSize, ErasedFileAstId)]>,
|
2023-12-12 14:54:27 -06:00
|
|
|
end: TextSize,
|
2023-11-24 09:38:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RealSpanMap {
|
2023-11-25 10:10:18 -06:00
|
|
|
/// Creates a real file span map that returns absolute ranges (relative ranges to the root ast id).
|
|
|
|
pub fn absolute(file_id: FileId) -> Self {
|
2023-12-12 14:54:27 -06:00
|
|
|
RealSpanMap {
|
|
|
|
file_id,
|
|
|
|
pairs: Box::from([(TextSize::new(0), ROOT_ERASED_FILE_AST_ID)]),
|
|
|
|
end: TextSize::new(!0),
|
|
|
|
}
|
2023-11-24 09:38:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_file(db: &dyn ExpandDatabase, file_id: FileId) -> Self {
|
|
|
|
let mut pairs = vec![(TextSize::new(0), ROOT_ERASED_FILE_AST_ID)];
|
|
|
|
let ast_id_map = db.ast_id_map(file_id.into());
|
2023-12-12 14:54:27 -06:00
|
|
|
let tree = db.parse(file_id).tree();
|
|
|
|
pairs
|
|
|
|
.extend(tree.items().map(|item| {
|
|
|
|
(item.syntax().text_range().start(), ast_id_map.ast_id(&item).erase())
|
|
|
|
}));
|
|
|
|
RealSpanMap {
|
|
|
|
file_id,
|
|
|
|
pairs: pairs.into_boxed_slice(),
|
|
|
|
end: tree.syntax().text_range().end(),
|
|
|
|
}
|
2023-11-24 09:38:48 -06:00
|
|
|
}
|
|
|
|
|
2023-12-18 06:30:41 -06:00
|
|
|
pub fn span_for_range(&self, range: TextRange) -> Span {
|
2023-12-12 14:54:27 -06:00
|
|
|
assert!(
|
|
|
|
range.end() <= self.end,
|
|
|
|
"range {range:?} goes beyond the end of the file {:?}",
|
|
|
|
self.end
|
|
|
|
);
|
2023-11-24 09:38:48 -06:00
|
|
|
let start = range.start();
|
|
|
|
let idx = self
|
|
|
|
.pairs
|
|
|
|
.binary_search_by(|&(it, _)| it.cmp(&start).then(std::cmp::Ordering::Less))
|
|
|
|
.unwrap_err();
|
|
|
|
let (offset, ast_id) = self.pairs[idx - 1];
|
2023-12-18 06:30:41 -06:00
|
|
|
Span {
|
2023-11-24 09:38:48 -06:00
|
|
|
range: range - offset,
|
|
|
|
anchor: SpanAnchor { file_id: self.file_id, ast_id },
|
|
|
|
ctx: SyntaxContextId::ROOT,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|