2023-12-18 06:30:41 -06:00
|
|
|
//! A map that maps a span to every position in a file. Usually maps a span to some range of positions.
|
|
|
|
//! Allows bidirectional lookup.
|
2021-05-24 14:47:01 -05:00
|
|
|
|
2024-03-14 06:02:23 -05:00
|
|
|
use std::{fmt, hash::Hash};
|
2021-08-21 11:06:03 -05:00
|
|
|
|
2023-12-08 04:26:22 -06:00
|
|
|
use stdx::{always, itertools::Itertools};
|
2023-11-28 03:55:21 -06:00
|
|
|
use syntax::{TextRange, TextSize};
|
2023-12-18 06:39:44 -06:00
|
|
|
use vfs::FileId;
|
|
|
|
|
2024-03-12 07:24:52 -05:00
|
|
|
use crate::{
|
|
|
|
ErasedFileAstId, Span, SpanAnchor, SpanData, SyntaxContextId, ROOT_ERASED_FILE_AST_ID,
|
|
|
|
};
|
2023-09-29 05:37:57 -05:00
|
|
|
|
|
|
|
/// Maps absolute text ranges for the corresponding file to the relevant span data.
|
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
|
2023-12-18 06:30:41 -06:00
|
|
|
pub struct SpanMap<S> {
|
2024-03-12 07:24:52 -05:00
|
|
|
spans: Vec<(TextSize, SpanData<S>)>,
|
2021-05-24 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
2024-03-12 07:24:52 -05:00
|
|
|
impl<S> SpanMap<S>
|
|
|
|
where
|
|
|
|
SpanData<S>: Copy,
|
|
|
|
{
|
2023-12-03 11:50:29 -06:00
|
|
|
/// Creates a new empty [`SpanMap`].
|
2023-12-01 09:29:58 -06:00
|
|
|
pub fn empty() -> Self {
|
2023-11-28 03:55:21 -06:00
|
|
|
Self { spans: Vec::new() }
|
2021-05-24 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
2023-12-03 11:50:29 -06:00
|
|
|
/// Finalizes the [`SpanMap`], shrinking its backing storage and validating that the offsets are
|
|
|
|
/// in order.
|
2023-12-01 09:29:58 -06:00
|
|
|
pub fn finish(&mut self) {
|
2023-12-08 04:26:22 -06:00
|
|
|
always!(
|
|
|
|
self.spans.iter().tuple_windows().all(|(a, b)| a.0 < b.0),
|
|
|
|
"spans are not in order"
|
|
|
|
);
|
2023-11-28 03:55:21 -06:00
|
|
|
self.spans.shrink_to_fit();
|
2022-02-08 11:13:18 -06:00
|
|
|
}
|
|
|
|
|
2023-12-03 11:50:29 -06:00
|
|
|
/// Pushes a new span onto the [`SpanMap`].
|
2024-03-12 07:24:52 -05:00
|
|
|
pub fn push(&mut self, offset: TextSize, span: SpanData<S>) {
|
2023-12-08 04:26:22 -06:00
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
if let Some(&(last_offset, _)) = self.spans.last() {
|
|
|
|
assert!(
|
|
|
|
last_offset < offset,
|
|
|
|
"last_offset({last_offset:?}) must be smaller than offset({offset:?})"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-11-28 03:55:21 -06:00
|
|
|
self.spans.push((offset, span));
|
2021-05-24 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
2023-12-03 11:50:29 -06:00
|
|
|
/// Returns all [`TextRange`]s that correspond to the given span.
|
|
|
|
///
|
|
|
|
/// Note this does a linear search through the entire backing vector.
|
2024-03-14 06:02:23 -05:00
|
|
|
pub fn ranges_with_span_exact(&self, span: SpanData<S>) -> impl Iterator<Item = TextRange> + '_
|
2023-12-18 06:30:41 -06:00
|
|
|
where
|
2024-03-12 07:24:52 -05:00
|
|
|
S: Copy,
|
2023-12-18 06:30:41 -06:00
|
|
|
{
|
2023-11-28 03:55:21 -06:00
|
|
|
self.spans.iter().enumerate().filter_map(move |(idx, &(end, s))| {
|
2024-03-12 07:24:52 -05:00
|
|
|
if !s.eq_ignoring_ctx(span) {
|
2023-11-28 03:55:21 -06:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let start = idx.checked_sub(1).map_or(TextSize::new(0), |prev| self.spans[prev].0);
|
|
|
|
Some(TextRange::new(start, end))
|
|
|
|
})
|
2021-05-24 11:43:42 -05:00
|
|
|
}
|
2023-07-13 02:17:07 -05:00
|
|
|
|
2024-03-14 06:02:23 -05:00
|
|
|
/// Returns all [`TextRange`]s whose spans contain the given span.
|
|
|
|
///
|
|
|
|
/// Note this does a linear search through the entire backing vector.
|
|
|
|
pub fn ranges_with_span(&self, span: SpanData<S>) -> impl Iterator<Item = TextRange> + '_
|
|
|
|
where
|
|
|
|
S: Copy,
|
|
|
|
{
|
|
|
|
self.spans.iter().enumerate().filter_map(move |(idx, &(end, s))| {
|
|
|
|
if s.anchor != span.anchor {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
if !s.range.contains_range(span.range) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let start = idx.checked_sub(1).map_or(TextSize::new(0), |prev| self.spans[prev].0);
|
|
|
|
Some(TextRange::new(start, end))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-03 11:50:29 -06:00
|
|
|
/// Returns the span at the given position.
|
2024-03-12 07:24:52 -05:00
|
|
|
pub fn span_at(&self, offset: TextSize) -> SpanData<S> {
|
2023-11-28 03:55:21 -06:00
|
|
|
let entry = self.spans.partition_point(|&(it, _)| it <= offset);
|
|
|
|
self.spans[entry].1
|
2023-11-24 09:38:48 -06:00
|
|
|
}
|
|
|
|
|
2023-12-03 11:50:29 -06:00
|
|
|
/// Returns the spans associated with the given range.
|
|
|
|
/// In other words, this will return all spans that correspond to all offsets within the given range.
|
2024-03-12 07:24:52 -05:00
|
|
|
pub fn spans_for_range(&self, range: TextRange) -> impl Iterator<Item = SpanData<S>> + '_ {
|
2023-11-28 03:55:21 -06:00
|
|
|
let (start, end) = (range.start(), range.end());
|
|
|
|
let start_entry = self.spans.partition_point(|&(it, _)| it <= start);
|
|
|
|
let end_entry = self.spans[start_entry..].partition_point(|&(it, _)| it <= end); // FIXME: this might be wrong?
|
2024-01-18 06:59:49 -06:00
|
|
|
self.spans[start_entry..][..end_entry].iter().map(|&(_, s)| s)
|
2023-07-13 02:17:07 -05:00
|
|
|
}
|
2023-12-01 09:29:58 -06:00
|
|
|
|
2024-03-12 07:24:52 -05:00
|
|
|
pub fn iter(&self) -> impl Iterator<Item = (TextSize, SpanData<S>)> + '_ {
|
2023-12-01 09:29:58 -06:00
|
|
|
self.spans.iter().copied()
|
|
|
|
}
|
2021-05-24 11:43:42 -05:00
|
|
|
}
|
2023-12-18 06:39:44 -06:00
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Hash, Debug)]
|
|
|
|
pub struct RealSpanMap {
|
|
|
|
file_id: FileId,
|
|
|
|
/// Invariant: Sorted vec over TextSize
|
|
|
|
// FIXME: SortedVec<(TextSize, ErasedFileAstId)>?
|
|
|
|
pairs: Box<[(TextSize, ErasedFileAstId)]>,
|
|
|
|
end: TextSize,
|
|
|
|
}
|
|
|
|
|
2024-03-14 06:02:23 -05:00
|
|
|
impl fmt::Display for RealSpanMap {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
writeln!(f, "RealSpanMap({:?}):", self.file_id)?;
|
|
|
|
for span in self.pairs.iter() {
|
|
|
|
writeln!(f, "{}: {}", u32::from(span.0), span.1.into_raw().into_u32())?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-18 06:39:44 -06:00
|
|
|
impl RealSpanMap {
|
|
|
|
/// Creates a real file span map that returns absolute ranges (relative ranges to the root ast id).
|
|
|
|
pub fn absolute(file_id: FileId) -> Self {
|
|
|
|
RealSpanMap {
|
|
|
|
file_id,
|
|
|
|
pairs: Box::from([(TextSize::new(0), ROOT_ERASED_FILE_AST_ID)]),
|
|
|
|
end: TextSize::new(!0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_file(
|
|
|
|
file_id: FileId,
|
|
|
|
pairs: Box<[(TextSize, ErasedFileAstId)]>,
|
|
|
|
end: TextSize,
|
|
|
|
) -> Self {
|
|
|
|
Self { file_id, pairs, end }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn span_for_range(&self, range: TextRange) -> Span {
|
|
|
|
assert!(
|
|
|
|
range.end() <= self.end,
|
|
|
|
"range {range:?} goes beyond the end of the file {:?}",
|
|
|
|
self.end
|
|
|
|
);
|
|
|
|
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];
|
|
|
|
Span {
|
|
|
|
range: range - offset,
|
|
|
|
anchor: SpanAnchor { file_id: self.file_id, ast_id },
|
|
|
|
ctx: SyntaxContextId::ROOT,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|