From 2a76bb5a0b70ae83824aa2e094ae2f592dcee694 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 5 Nov 2019 03:20:54 +0800 Subject: Refactor a bit --- crates/ra_hir_expand/src/db.rs | 3 +-- crates/ra_mbe/src/syntax_bridge.rs | 15 ++++++--------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs index 72c728c93..e2860371f 100644 --- a/crates/ra_hir_expand/src/db.rs +++ b/crates/ra_hir_expand/src/db.rs @@ -151,8 +151,7 @@ pub(crate) fn parse_macro_with_info( let arg_start = arg_tt.map(|t| t.syntax().text_range().start()); let def_start = def_tt.map(|t| t.syntax().text_range().start()); - let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition").ok()?.0; - let shift = macro_rules.shift(); + let shift = db.macro_def(loc.def)?.0.shift(); let arg_map = arg_start .map(|start| exp_map.map_ranges(&expand_info.arg_map, start, shift)) diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 0c499cd03..02ddd25e7 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -133,7 +133,7 @@ impl TokenMap { } impl ExpandedRangeMap { - fn set(&mut self, relative_range: TextRange, token_id: &tt::TokenId) { + fn add(&mut self, relative_range: TextRange, token_id: &tt::TokenId) { self.ranges.push((relative_range, token_id.clone())) } @@ -145,13 +145,11 @@ impl ExpandedRangeMap { ) -> Vec<(TextRange, TextRange)> { self.ranges .iter() - .filter_map(|(r, tid)| if shift <= tid.0 { Some((r, tid.0 - shift)) } else { None }) .filter_map(|(r, tid)| { - if let Some(to_range) = to.relative_range_of(tt::TokenId(tid)) { - Some((*r, TextRange::from_to(to_range.start() + start, to_range.end() + start))) - } else { - None - } + let adjusted_id = tt::TokenId(tid.0.checked_sub(shift)?); + let to_range = to.relative_range_of(adjusted_id)?; + + Some((*r, TextRange::offset_len(to_range.start() + start, to_range.len()))) }) .collect() } @@ -361,8 +359,7 @@ impl<'a> TreeSink for TtTreeSink<'a> { self.text_pos + TextUnit::of_str(&self.buf), TextUnit::of_str(&ident.text), ); - let token_id = ident.id; - self.range_map.set(range, &token_id); + self.range_map.add(range, &ident.id); } } -- cgit v1.2.3