aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2019-11-26 07:05:53 +0000
committerEdwin Cheng <[email protected]>2019-11-26 07:05:53 +0000
commit245a9b165acb179c40b8c9d4a085e5ccdd4b75d3 (patch)
tree8074ef35c99dd6345536e11f5fe030daebade95a /crates
parent58a3b3b502580e9f49dcfc9b7223e8aec258adf6 (diff)
Add hygiene information to SourceAnalyzer
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/source_binder.rs13
-rw-r--r--crates/ra_hir_def/src/path.rs2
-rw-r--r--crates/ra_ide_api/src/call_info.rs9
-rw-r--r--crates/ra_ide_api/src/expand_macro.rs23
-rw-r--r--crates/ra_ide_api/src/references/classify.rs2
5 files changed, 36 insertions, 13 deletions
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index cbfeca3ab..287cea880 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -14,7 +14,8 @@ use hir_def::{
14 DefWithBodyId, 14 DefWithBodyId,
15}; 15};
16use hir_expand::{ 16use hir_expand::{
17 name::AsName, AstId, HirFileId, MacroCallId, MacroCallLoc, MacroFileKind, Source, 17 hygiene::Hygiene, name::AsName, AstId, HirFileId, MacroCallId, MacroCallLoc, MacroFileKind,
18 Source,
18}; 19};
19use ra_syntax::{ 20use ra_syntax::{
20 ast::{self, AstNode}, 21 ast::{self, AstNode},
@@ -236,10 +237,10 @@ impl SourceAnalyzer {
236 pub fn resolve_macro_call( 237 pub fn resolve_macro_call(
237 &self, 238 &self,
238 db: &impl HirDatabase, 239 db: &impl HirDatabase,
239 macro_call: &ast::MacroCall, 240 macro_call: Source<&ast::MacroCall>,
240 ) -> Option<MacroDef> { 241 ) -> Option<MacroDef> {
241 // This must be a normal source file rather than macro file. 242 let hygiene = Hygiene::new(db, macro_call.file_id);
242 let path = macro_call.path().and_then(Path::from_ast)?; 243 let path = macro_call.value.path().and_then(|ast| Path::from_src(ast, &hygiene))?;
243 self.resolver.resolve_path_as_macro(db, &path).map(|it| it.into()) 244 self.resolver.resolve_path_as_macro(db, &path).map(|it| it.into())
244 } 245 }
245 246
@@ -441,12 +442,14 @@ impl SourceAnalyzer {
441 db: &impl HirDatabase, 442 db: &impl HirDatabase,
442 macro_call: Source<&ast::MacroCall>, 443 macro_call: Source<&ast::MacroCall>,
443 ) -> Option<Expansion> { 444 ) -> Option<Expansion> {
444 let def = self.resolve_macro_call(db, macro_call.value)?.id; 445 let def = self.resolve_macro_call(db, macro_call)?.id;
445 let ast_id = AstId::new( 446 let ast_id = AstId::new(
446 macro_call.file_id, 447 macro_call.file_id,
447 db.ast_id_map(macro_call.file_id).ast_id(macro_call.value), 448 db.ast_id_map(macro_call.file_id).ast_id(macro_call.value),
448 ); 449 );
449 let macro_call_loc = MacroCallLoc { def, ast_id }; 450 let macro_call_loc = MacroCallLoc { def, ast_id };
451 let kind = to_macro_file_kind(macro_call.value);
452 dbg!(kind);
450 Some(Expansion { 453 Some(Expansion {
451 macro_call_id: db.intern_macro(macro_call_loc), 454 macro_call_id: db.intern_macro(macro_call_loc),
452 macro_file_kind: to_macro_file_kind(macro_call.value), 455 macro_file_kind: to_macro_file_kind(macro_call.value),
diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs
index 0e606fd0e..6810a26db 100644
--- a/crates/ra_hir_def/src/path.rs
+++ b/crates/ra_hir_def/src/path.rs
@@ -97,7 +97,7 @@ impl Path {
97 97
98 /// Converts an `ast::Path` to `Path`. Works with use trees. 98 /// Converts an `ast::Path` to `Path`. Works with use trees.
99 /// It correctly handles `$crate` based path from macro call. 99 /// It correctly handles `$crate` based path from macro call.
100 pub(crate) fn from_src(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path> { 100 pub fn from_src(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path> {
101 let mut kind = PathKind::Plain; 101 let mut kind = PathKind::Plain;
102 let mut segments = Vec::new(); 102 let mut segments = Vec::new();
103 loop { 103 loop {
diff --git a/crates/ra_ide_api/src/call_info.rs b/crates/ra_ide_api/src/call_info.rs
index 9beceb29c..7ebdfc585 100644
--- a/crates/ra_ide_api/src/call_info.rs
+++ b/crates/ra_ide_api/src/call_info.rs
@@ -18,12 +18,9 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<Cal
18 // Find the calling expression and it's NameRef 18 // Find the calling expression and it's NameRef
19 let calling_node = FnCallNode::with_node(&syntax, position.offset)?; 19 let calling_node = FnCallNode::with_node(&syntax, position.offset)?;
20 let name_ref = calling_node.name_ref()?; 20 let name_ref = calling_node.name_ref()?;
21 let name_ref = hir::Source::new(position.file_id.into(), name_ref.syntax());
21 22
22 let analyzer = hir::SourceAnalyzer::new( 23 let analyzer = hir::SourceAnalyzer::new(db, name_ref, None);
23 db,
24 hir::Source::new(position.file_id.into(), name_ref.syntax()),
25 None,
26 );
27 let (mut call_info, has_self) = match &calling_node { 24 let (mut call_info, has_self) = match &calling_node {
28 FnCallNode::CallExpr(expr) => { 25 FnCallNode::CallExpr(expr) => {
29 //FIXME: don't poke into Ty 26 //FIXME: don't poke into Ty
@@ -44,7 +41,7 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<Cal
44 (CallInfo::with_fn(db, function), function.has_self_param(db)) 41 (CallInfo::with_fn(db, function), function.has_self_param(db))
45 } 42 }
46 FnCallNode::MacroCallExpr(expr) => { 43 FnCallNode::MacroCallExpr(expr) => {
47 let macro_def = analyzer.resolve_macro_call(db, &expr)?; 44 let macro_def = analyzer.resolve_macro_call(db, name_ref.with_value(&expr))?;
48 (CallInfo::with_macro(db, macro_def)?, false) 45 (CallInfo::with_macro(db, macro_def)?, false)
49 } 46 }
50 }; 47 };
diff --git a/crates/ra_ide_api/src/expand_macro.rs b/crates/ra_ide_api/src/expand_macro.rs
index 0b540b8cd..abc602244 100644
--- a/crates/ra_ide_api/src/expand_macro.rs
+++ b/crates/ra_ide_api/src/expand_macro.rs
@@ -269,4 +269,27 @@ fn some_thing() -> u32 {
269 assert_eq!(res.name, "foo"); 269 assert_eq!(res.name, "foo");
270 assert_snapshot!(res.expansion, @r###"bar!()"###); 270 assert_snapshot!(res.expansion, @r###"bar!()"###);
271 } 271 }
272
273 #[test]
274 fn macro_expand_with_dollar_crate() {
275 let res = check_expand_macro(
276 r#"
277 //- /lib.rs
278 #[macro_export]
279 macro_rules! bar {
280 () => {0};
281 }
282 macro_rules! foo {
283 () => {$crate::bar!()};
284 }
285
286 fn main() {
287 let res = fo<|>o!();
288 }
289 "#,
290 );
291
292 assert_eq!(res.name, "foo");
293 assert_snapshot!(res.expansion, @r###"0"###);
294 }
272} 295}
diff --git a/crates/ra_ide_api/src/references/classify.rs b/crates/ra_ide_api/src/references/classify.rs
index cab06dea9..227737ad2 100644
--- a/crates/ra_ide_api/src/references/classify.rs
+++ b/crates/ra_ide_api/src/references/classify.rs
@@ -152,7 +152,7 @@ pub(crate) fn classify_name_ref(
152 152
153 if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { 153 if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) {
154 tested_by!(goto_definition_works_for_macros); 154 tested_by!(goto_definition_works_for_macros);
155 if let Some(macro_def) = analyzer.resolve_macro_call(db, &macro_call) { 155 if let Some(macro_def) = analyzer.resolve_macro_call(db, name_ref.with_value(&macro_call)) {
156 let kind = NameKind::Macro(macro_def); 156 let kind = NameKind::Macro(macro_def);
157 return Some(NameDefinition { kind, container, visibility }); 157 return Some(NameDefinition { kind, container, visibility });
158 } 158 }