aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand/src
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-11-09 10:16:48 +0000
committerGitHub <[email protected]>2019-11-09 10:16:48 +0000
commitdefc7ad772123a449f7cc384dd46d88c3a45fb53 (patch)
treeb0b01d1397542585802a3622fdf9491e20fa8c0b /crates/ra_hir_expand/src
parent561bb979cecd786f5d311ea7bddb1e15d77a3848 (diff)
parent70f2a21b55c1b09e575798a75807e13991f2cfec (diff)
Merge #2197
2197: Remove typed macro parsing API r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir_expand/src')
-rw-r--r--crates/ra_hir_expand/src/db.rs15
-rw-r--r--crates/ra_hir_expand/src/lib.rs14
2 files changed, 15 insertions, 14 deletions
diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs
index b789c6e7b..b4dafe1d8 100644
--- a/crates/ra_hir_expand/src/db.rs
+++ b/crates/ra_hir_expand/src/db.rs
@@ -4,6 +4,7 @@ use std::sync::Arc;
4 4
5use mbe::MacroRules; 5use mbe::MacroRules;
6use ra_db::{salsa, SourceDatabase}; 6use ra_db::{salsa, SourceDatabase};
7use ra_parser::FragmentKind;
7use ra_prof::profile; 8use ra_prof::profile;
8use ra_syntax::{AstNode, Parse, SyntaxNode}; 9use ra_syntax::{AstNode, Parse, SyntaxNode};
9 10
@@ -108,12 +109,10 @@ pub(crate) fn parse_macro(
108 }) 109 })
109 .ok()?; 110 .ok()?;
110 111
111 match macro_file.macro_file_kind { 112 let fragment_kind = match macro_file.macro_file_kind {
112 MacroFileKind::Items => { 113 MacroFileKind::Items => FragmentKind::Items,
113 mbe::token_tree_to_items(&tt).ok().map(|(p, map)| (p.to_syntax(), Arc::new(map))) 114 MacroFileKind::Expr => FragmentKind::Expr,
114 } 115 };
115 MacroFileKind::Expr => { 116 let (parse, rev_token_map) = mbe::token_tree_to_syntax_node(&tt, fragment_kind).ok()?;
116 mbe::token_tree_to_expr(&tt).ok().map(|(p, map)| (p.to_syntax(), Arc::new(map))) 117 Some((parse, Arc::new(rev_token_map)))
117 }
118 }
119} 118}
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs
index b219b8fbf..151d1d785 100644
--- a/crates/ra_hir_expand/src/lib.rs
+++ b/crates/ra_hir_expand/src/lib.rs
@@ -151,19 +151,21 @@ pub struct ExpansionInfo {
151 151
152impl ExpansionInfo { 152impl ExpansionInfo {
153 pub fn find_range(&self, from: TextRange) -> Option<(HirFileId, TextRange)> { 153 pub fn find_range(&self, from: TextRange) -> Option<(HirFileId, TextRange)> {
154 fn look_in_rev_map(exp_map: &mbe::RevTokenMap, from: TextRange) -> Option<tt::TokenId> {
155 exp_map.ranges.iter().find(|&it| it.0.is_subrange(&from)).map(|it| it.1)
156 }
157
158 let token_id = look_in_rev_map(&self.exp_map, from)?; 154 let token_id = look_in_rev_map(&self.exp_map, from)?;
159 let (token_map, file_offset, token_id) = if token_id.0 >= self.shift { 155
156 let (token_map, (file_id, start_offset), token_id) = if token_id.0 >= self.shift {
160 (&self.macro_arg.1, self.arg_start, tt::TokenId(token_id.0 - self.shift).into()) 157 (&self.macro_arg.1, self.arg_start, tt::TokenId(token_id.0 - self.shift).into())
161 } else { 158 } else {
162 (&self.macro_def.1, self.def_start, token_id) 159 (&self.macro_def.1, self.def_start, token_id)
163 }; 160 };
164 161
165 let range = token_map.relative_range_of(token_id)?; 162 let range = token_map.relative_range_of(token_id)?;
166 Some((file_offset.0, TextRange::offset_len(range.start() + file_offset.1, range.len()))) 163
164 return Some((file_id, range + start_offset));
165
166 fn look_in_rev_map(exp_map: &mbe::RevTokenMap, from: TextRange) -> Option<tt::TokenId> {
167 exp_map.ranges.iter().find(|&it| it.0.is_subrange(&from)).map(|it| it.1)
168 }
167 } 169 }
168} 170}
169 171