Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/mbe/src/token_map.rs')
-rw-r--r--crates/mbe/src/token_map.rs18
1 files changed, 16 insertions, 2 deletions
diff --git a/crates/mbe/src/token_map.rs b/crates/mbe/src/token_map.rs
index 1af50c8b3b..c825bd01bc 100644
--- a/crates/mbe/src/token_map.rs
+++ b/crates/mbe/src/token_map.rs
@@ -20,11 +20,12 @@ pub struct TokenMap<S> {
// then a bin search on the ast id
pub span_map: Vec<(TextRange, S)>,
// span_map2: rustc_hash::FxHashMap<TextRange, usize>,
+ pub real_file: bool,
}
impl<S> Default for TokenMap<S> {
fn default() -> Self {
- Self { span_map: Vec::new() }
+ Self { span_map: Vec::new(), real_file: true }
}
}
@@ -49,8 +50,21 @@ impl<S: Span> TokenMap<S> {
)
}
+ // FIXME: Should be infallible
pub fn span_for_range(&self, range: TextRange) -> Option<S> {
- self.span_map.iter().find_map(|(r, s)| if r == &range { Some(s.clone()) } else { None })
+ // TODO FIXME: make this proper
+ self.span_map
+ .iter()
+ .filter_map(|(r, s)| Some((r, s, r.intersect(range)?)))
+ .max_by_key(|(_, _, intersection)| intersection.len())
+ .map(|(_, &s, _)| s)
+ .or_else(|| {
+ if self.real_file {
+ None
+ } else {
+ panic!("no span for range {range:?} in {:#?}", self.span_map)
+ }
+ })
}
// pub fn ranges_by_token(