diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-11-21 19:31:19 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-11-21 19:31:19 +0000 |
commit | c9273828b3c44fba62d1b989480c287d923839d2 (patch) | |
tree | c222eabbd7fafcf9d491ba77d34238fe587449ce /crates/ra_hir/src | |
parent | 3f1a0c3c5f4e52505ca83cb217201b116453c896 (diff) | |
parent | 144dc6652c1bdb0a8a522d86d5bb4be612968675 (diff) |
Merge #2342
2342: Use proper MacroFileKind in `SourceAnalyzer` r=matklad a=edwin0cheng
* Add `MacroFileKind::Statements`
* Add `to_macro_file_kind` in `source_binding.rs` to set a proper `MacroFileKind` when expanding a macro.
* Add a test for trying expanding `match_ast` which is not correct before this PR.
* Fix some spacing issues in `insert_whitespaces`
Co-authored-by: Edwin Cheng <[email protected]>
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index c42ceabdf..797f90d50 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -131,6 +131,7 @@ pub struct ReferenceDescriptor { | |||
131 | } | 131 | } |
132 | 132 | ||
133 | pub struct Expansion { | 133 | pub struct Expansion { |
134 | macro_file_kind: MacroFileKind, | ||
134 | macro_call_id: MacroCallId, | 135 | macro_call_id: MacroCallId, |
135 | } | 136 | } |
136 | 137 | ||
@@ -145,7 +146,7 @@ impl Expansion { | |||
145 | } | 146 | } |
146 | 147 | ||
147 | pub fn file_id(&self) -> HirFileId { | 148 | pub fn file_id(&self) -> HirFileId { |
148 | self.macro_call_id.as_file(MacroFileKind::Items) | 149 | self.macro_call_id.as_file(self.macro_file_kind) |
149 | } | 150 | } |
150 | } | 151 | } |
151 | 152 | ||
@@ -439,7 +440,10 @@ impl SourceAnalyzer { | |||
439 | db.ast_id_map(macro_call.file_id).ast_id(macro_call.value), | 440 | db.ast_id_map(macro_call.file_id).ast_id(macro_call.value), |
440 | ); | 441 | ); |
441 | let macro_call_loc = MacroCallLoc { def, ast_id }; | 442 | let macro_call_loc = MacroCallLoc { def, ast_id }; |
442 | Some(Expansion { macro_call_id: db.intern_macro(macro_call_loc) }) | 443 | Some(Expansion { |
444 | macro_call_id: db.intern_macro(macro_call_loc), | ||
445 | macro_file_kind: to_macro_file_kind(macro_call.value), | ||
446 | }) | ||
443 | } | 447 | } |
444 | 448 | ||
445 | #[cfg(test)] | 449 | #[cfg(test)] |
@@ -538,3 +542,35 @@ fn adjust( | |||
538 | }) | 542 | }) |
539 | .map(|(_ptr, scope)| *scope) | 543 | .map(|(_ptr, scope)| *scope) |
540 | } | 544 | } |
545 | |||
546 | /// Given a `ast::MacroCall`, return what `MacroKindFile` it belongs to. | ||
547 | /// FIXME: Not completed | ||
548 | fn to_macro_file_kind(macro_call: &ast::MacroCall) -> MacroFileKind { | ||
549 | let syn = macro_call.syntax(); | ||
550 | let parent = match syn.parent() { | ||
551 | Some(it) => it, | ||
552 | None => { | ||
553 | // FIXME: | ||
554 | // If it is root, which means the parent HirFile | ||
555 | // MacroKindFile must be non-items | ||
556 | // return expr now. | ||
557 | return MacroFileKind::Expr; | ||
558 | } | ||
559 | }; | ||
560 | |||
561 | match parent.kind() { | ||
562 | MACRO_ITEMS | SOURCE_FILE => MacroFileKind::Items, | ||
563 | LET_STMT => { | ||
564 | // FIXME: Handle Pattern | ||
565 | MacroFileKind::Expr | ||
566 | } | ||
567 | EXPR_STMT => MacroFileKind::Statements, | ||
568 | BLOCK => MacroFileKind::Statements, | ||
569 | ARG_LIST => MacroFileKind::Expr, | ||
570 | TRY_EXPR => MacroFileKind::Expr, | ||
571 | _ => { | ||
572 | // Unknown , Just guess it is `Items` | ||
573 | MacroFileKind::Items | ||
574 | } | ||
575 | } | ||
576 | } | ||