aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand/src/db.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_expand/src/db.rs')
-rw-r--r--crates/ra_hir_expand/src/db.rs45
1 files changed, 38 insertions, 7 deletions
diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs
index 013a6c8ba..4bdb41619 100644
--- a/crates/ra_hir_expand/src/db.rs
+++ b/crates/ra_hir_expand/src/db.rs
@@ -6,11 +6,11 @@ use mbe::MacroRules;
6use ra_db::{salsa, SourceDatabase}; 6use ra_db::{salsa, SourceDatabase};
7use ra_parser::FragmentKind; 7use ra_parser::FragmentKind;
8use ra_prof::profile; 8use ra_prof::profile;
9use ra_syntax::{AstNode, Parse, SyntaxNode}; 9use ra_syntax::{AstNode, Parse, SyntaxKind::*, SyntaxNode};
10 10
11use crate::{ 11use crate::{
12 ast_id_map::AstIdMap, BuiltinDeriveExpander, BuiltinFnLikeExpander, HirFileId, HirFileIdRepr, 12 ast_id_map::AstIdMap, BuiltinDeriveExpander, BuiltinFnLikeExpander, HirFileId, HirFileIdRepr,
13 MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile, MacroFileKind, 13 MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile,
14}; 14};
15 15
16#[derive(Debug, Clone, Eq, PartialEq)] 16#[derive(Debug, Clone, Eq, PartialEq)]
@@ -155,11 +155,42 @@ pub(crate) fn parse_macro(
155 }) 155 })
156 .ok()?; 156 .ok()?;
157 157
158 let fragment_kind = match macro_file.macro_file_kind { 158 let fragment_kind = to_fragment_kind(db, macro_call_id);
159 MacroFileKind::Items => FragmentKind::Items, 159
160 MacroFileKind::Expr => FragmentKind::Expr,
161 MacroFileKind::Statements => FragmentKind::Statements,
162 };
163 let (parse, rev_token_map) = mbe::token_tree_to_syntax_node(&tt, fragment_kind).ok()?; 160 let (parse, rev_token_map) = mbe::token_tree_to_syntax_node(&tt, fragment_kind).ok()?;
164 Some((parse, Arc::new(rev_token_map))) 161 Some((parse, Arc::new(rev_token_map)))
165} 162}
163
164/// Given a `MacroCallId`, return what `FragmentKind` it belongs to.
165/// FIXME: Not completed
166fn to_fragment_kind(db: &dyn AstDatabase, macro_call_id: MacroCallId) -> FragmentKind {
167 let syn = db.lookup_intern_macro(macro_call_id).kind.node(db).value;
168
169 let parent = match syn.parent() {
170 Some(it) => it,
171 None => {
172 // FIXME:
173 // If it is root, which means the parent HirFile
174 // MacroKindFile must be non-items
175 // return expr now.
176 return FragmentKind::Expr;
177 }
178 };
179
180 match parent.kind() {
181 MACRO_ITEMS | SOURCE_FILE => FragmentKind::Items,
182 LET_STMT => {
183 // FIXME: Handle Pattern
184 FragmentKind::Expr
185 }
186 EXPR_STMT => FragmentKind::Statements,
187 BLOCK => FragmentKind::Statements,
188 ARG_LIST => FragmentKind::Expr,
189 TRY_EXPR => FragmentKind::Expr,
190 TUPLE_EXPR => FragmentKind::Expr,
191 _ => {
192 // Unknown , Just guess it is `Items`
193 FragmentKind::Items
194 }
195 }
196}