aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_expand/src/db.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_expand/src/db.rs')
-rw-r--r--crates/hir_expand/src/db.rs71
1 files changed, 11 insertions, 60 deletions
diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs
index d61f4b31a..6647e57e7 100644
--- a/crates/hir_expand/src/db.rs
+++ b/crates/hir_expand/src/db.rs
@@ -8,9 +8,7 @@ use parser::FragmentKind;
8use syntax::{ 8use syntax::{
9 algo::diff, 9 algo::diff,
10 ast::{self, NameOwner}, 10 ast::{self, NameOwner},
11 AstNode, GreenNode, Parse, 11 AstNode, GreenNode, Parse, SyntaxNode, SyntaxToken,
12 SyntaxKind::*,
13 SyntaxNode, SyntaxToken,
14}; 12};
15 13
16use crate::{ 14use crate::{
@@ -160,7 +158,7 @@ pub fn expand_hypothetical(
160 158
161 let hypothetical_expansion = macro_def.expand(db, lazy_id, &tt); 159 let hypothetical_expansion = macro_def.expand(db, lazy_id, &tt);
162 160
163 let fragment_kind = to_fragment_kind(db, actual_macro_call); 161 let fragment_kind = macro_fragment_kind(db, actual_macro_call);
164 162
165 let (node, tmap_2) = 163 let (node, tmap_2) =
166 mbe::token_tree_to_syntax_node(&hypothetical_expansion.value, fragment_kind).ok()?; 164 mbe::token_tree_to_syntax_node(&hypothetical_expansion.value, fragment_kind).ok()?;
@@ -226,7 +224,7 @@ fn parse_macro_expansion(
226 None => return ExpandResult { value: None, err: result.err }, 224 None => return ExpandResult { value: None, err: result.err },
227 }; 225 };
228 226
229 let fragment_kind = to_fragment_kind(db, macro_file.macro_call_id); 227 let fragment_kind = macro_fragment_kind(db, macro_file.macro_call_id);
230 228
231 log::debug!("expanded = {}", tt.as_debug_string()); 229 log::debug!("expanded = {}", tt.as_debug_string());
232 log::debug!("kind = {:?}", fragment_kind); 230 log::debug!("kind = {:?}", fragment_kind);
@@ -427,62 +425,15 @@ fn hygiene_frame(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<HygieneFrame>
427 Arc::new(HygieneFrame::new(db, file_id)) 425 Arc::new(HygieneFrame::new(db, file_id))
428} 426}
429 427
430/// Given a `MacroCallId`, return what `FragmentKind` it belongs to. 428fn macro_fragment_kind(db: &dyn AstDatabase, id: MacroCallId) -> FragmentKind {
431/// FIXME: Not completed 429 match id {
432fn to_fragment_kind(db: &dyn AstDatabase, id: MacroCallId) -> FragmentKind { 430 MacroCallId::LazyMacro(id) => {
433 let lazy_id = match id { 431 let loc: MacroCallLoc = db.lookup_intern_macro(id);
434 MacroCallId::LazyMacro(id) => id, 432 loc.kind.fragment_kind()
435 MacroCallId::EagerMacro(id) => {
436 return db.lookup_intern_eager_expansion(id).fragment;
437 }
438 };
439 let syn = db.lookup_intern_macro(lazy_id).kind.node(db).value;
440
441 let parent = match syn.parent() {
442 Some(it) => it,
443 None => return FragmentKind::Statements,
444 };
445
446 match parent.kind() {
447 MACRO_ITEMS | SOURCE_FILE => FragmentKind::Items,
448 MACRO_STMTS => FragmentKind::Statements,
449 MACRO_PAT => FragmentKind::Pattern,
450 MACRO_TYPE => FragmentKind::Type,
451 ITEM_LIST => FragmentKind::Items,
452 LET_STMT => {
453 // FIXME: Handle LHS Pattern
454 FragmentKind::Expr
455 } 433 }
456 EXPR_STMT => FragmentKind::Statements, 434 MacroCallId::EagerMacro(id) => {
457 BLOCK_EXPR => FragmentKind::Statements, 435 let loc: EagerCallLoc = db.lookup_intern_eager_expansion(id);
458 ARG_LIST => FragmentKind::Expr, 436 loc.fragment
459 TRY_EXPR => FragmentKind::Expr,
460 TUPLE_EXPR => FragmentKind::Expr,
461 PAREN_EXPR => FragmentKind::Expr,
462 ARRAY_EXPR => FragmentKind::Expr,
463 FOR_EXPR => FragmentKind::Expr,
464 PATH_EXPR => FragmentKind::Expr,
465 CLOSURE_EXPR => FragmentKind::Expr,
466 CONDITION => FragmentKind::Expr,
467 BREAK_EXPR => FragmentKind::Expr,
468 RETURN_EXPR => FragmentKind::Expr,
469 MATCH_EXPR => FragmentKind::Expr,
470 MATCH_ARM => FragmentKind::Expr,
471 MATCH_GUARD => FragmentKind::Expr,
472 RECORD_EXPR_FIELD => FragmentKind::Expr,
473 CALL_EXPR => FragmentKind::Expr,
474 INDEX_EXPR => FragmentKind::Expr,
475 METHOD_CALL_EXPR => FragmentKind::Expr,
476 FIELD_EXPR => FragmentKind::Expr,
477 AWAIT_EXPR => FragmentKind::Expr,
478 CAST_EXPR => FragmentKind::Expr,
479 REF_EXPR => FragmentKind::Expr,
480 PREFIX_EXPR => FragmentKind::Expr,
481 RANGE_EXPR => FragmentKind::Expr,
482 BIN_EXPR => FragmentKind::Expr,
483 _ => {
484 // Unknown , Just guess it is `Items`
485 FragmentKind::Items
486 } 437 }
487 } 438 }
488} 439}