aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_expand/src')
-rw-r--r--crates/ra_hir_expand/src/builtin_derive.rs4
-rw-r--r--crates/ra_hir_expand/src/builtin_macro.rs9
-rw-r--r--crates/ra_hir_expand/src/db.rs45
-rw-r--r--crates/ra_hir_expand/src/lib.rs13
4 files changed, 46 insertions, 25 deletions
diff --git a/crates/ra_hir_expand/src/builtin_derive.rs b/crates/ra_hir_expand/src/builtin_derive.rs
index 78fa9b09a..574637602 100644
--- a/crates/ra_hir_expand/src/builtin_derive.rs
+++ b/crates/ra_hir_expand/src/builtin_derive.rs
@@ -208,7 +208,7 @@ fn partial_ord_expand(
208#[cfg(test)] 208#[cfg(test)]
209mod tests { 209mod tests {
210 use super::*; 210 use super::*;
211 use crate::{test_db::TestDB, AstId, MacroCallKind, MacroCallLoc, MacroFileKind}; 211 use crate::{test_db::TestDB, AstId, MacroCallKind, MacroCallLoc};
212 use ra_db::{fixture::WithFixture, SourceDatabase}; 212 use ra_db::{fixture::WithFixture, SourceDatabase};
213 213
214 fn expand_builtin_derive(s: &str, expander: BuiltinDeriveExpander) -> String { 214 fn expand_builtin_derive(s: &str, expander: BuiltinDeriveExpander) -> String {
@@ -229,7 +229,7 @@ mod tests {
229 }; 229 };
230 230
231 let id = db.intern_macro(loc); 231 let id = db.intern_macro(loc);
232 let parsed = db.parse_or_expand(id.as_file(MacroFileKind::Items)).unwrap(); 232 let parsed = db.parse_or_expand(id.as_file()).unwrap();
233 233
234 // FIXME text() for syntax nodes parsed from token tree looks weird 234 // FIXME text() for syntax nodes parsed from token tree looks weird
235 // because there's no whitespace, see below 235 // because there's no whitespace, see below
diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs
index 99303188b..c7071fe85 100644
--- a/crates/ra_hir_expand/src/builtin_macro.rs
+++ b/crates/ra_hir_expand/src/builtin_macro.rs
@@ -2,8 +2,7 @@
2use crate::db::AstDatabase; 2use crate::db::AstDatabase;
3use crate::{ 3use crate::{
4 ast::{self, AstNode}, 4 ast::{self, AstNode},
5 name, AstId, CrateId, HirFileId, MacroCallId, MacroDefId, MacroDefKind, MacroFileKind, 5 name, AstId, CrateId, HirFileId, MacroCallId, MacroDefId, MacroDefKind, TextUnit,
6 TextUnit,
7}; 6};
8 7
9use crate::quote; 8use crate::quote;
@@ -90,7 +89,7 @@ fn line_expand(
90 let arg = loc.kind.arg(db).ok_or_else(|| mbe::ExpandError::UnexpectedToken)?; 89 let arg = loc.kind.arg(db).ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
91 let arg_start = arg.text_range().start(); 90 let arg_start = arg.text_range().start();
92 91
93 let file = id.as_file(MacroFileKind::Expr); 92 let file = id.as_file();
94 let line_num = to_line_number(db, file, arg_start); 93 let line_num = to_line_number(db, file, arg_start);
95 94
96 let expanded = quote! { 95 let expanded = quote! {
@@ -158,7 +157,7 @@ fn column_expand(
158 let _arg = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?; 157 let _arg = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
159 let col_start = macro_call.syntax().text_range().start(); 158 let col_start = macro_call.syntax().text_range().start();
160 159
161 let file = id.as_file(MacroFileKind::Expr); 160 let file = id.as_file();
162 let col_num = to_col_number(db, file, col_start); 161 let col_num = to_col_number(db, file, col_start);
163 162
164 let expanded = quote! { 163 let expanded = quote! {
@@ -269,7 +268,7 @@ mod tests {
269 }; 268 };
270 269
271 let id = db.intern_macro(loc); 270 let id = db.intern_macro(loc);
272 let parsed = db.parse_or_expand(id.as_file(MacroFileKind::Expr)).unwrap(); 271 let parsed = db.parse_or_expand(id.as_file()).unwrap();
273 272
274 parsed.text().to_string() 273 parsed.text().to_string()
275 } 274 }
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}
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs
index 0a5da7e54..94e1e466a 100644
--- a/crates/ra_hir_expand/src/lib.rs
+++ b/crates/ra_hir_expand/src/lib.rs
@@ -117,14 +117,6 @@ impl HirFileId {
117#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 117#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
118pub struct MacroFile { 118pub struct MacroFile {
119 macro_call_id: MacroCallId, 119 macro_call_id: MacroCallId,
120 macro_file_kind: MacroFileKind,
121}
122
123#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
124pub enum MacroFileKind {
125 Items,
126 Expr,
127 Statements,
128} 120}
129 121
130/// `MacroCallId` identifies a particular macro invocation, like 122/// `MacroCallId` identifies a particular macro invocation, like
@@ -205,9 +197,8 @@ impl MacroCallKind {
205} 197}
206 198
207impl MacroCallId { 199impl MacroCallId {
208 pub fn as_file(self, kind: MacroFileKind) -> HirFileId { 200 pub fn as_file(self) -> HirFileId {
209 let macro_file = MacroFile { macro_call_id: self, macro_file_kind: kind }; 201 MacroFile { macro_call_id: self }.into()
210 macro_file.into()
211 } 202 }
212} 203}
213 204