@@ -4,7 +4,7 @@ use crate::{
4
4
doc_links:: token_as_doc_comment, navigation_target:: ToNav , FilePosition , NavigationTarget ,
5
5
RangeInfo , TryToNav ,
6
6
} ;
7
- use hir:: { AsAssocItem , AssocItem , DescendPreference , Semantics } ;
7
+ use hir:: { AsAssocItem , AssocItem , DescendPreference , ModuleDef , Semantics } ;
8
8
use ide_db:: {
9
9
base_db:: { AnchoredPath , FileId , FileLoader } ,
10
10
defs:: { Definition , IdentClass } ,
@@ -73,10 +73,15 @@ pub(crate) fn goto_definition(
73
73
. into_iter ( )
74
74
. filter_map ( |token| {
75
75
let parent = token. parent ( ) ?;
76
+
76
77
if let Some ( tt) = ast:: TokenTree :: cast ( parent. clone ( ) ) {
77
78
if let Some ( x) = try_lookup_include_path ( sema, tt, token. clone ( ) , file_id) {
78
79
return Some ( vec ! [ x] ) ;
79
80
}
81
+
82
+ if let Some ( x) = try_lookup_macro_def_in_macro_use ( sema, token. clone ( ) ) {
83
+ return Some ( vec ! [ x] ) ;
84
+ }
80
85
}
81
86
Some (
82
87
IdentClass :: classify_node ( sema, & parent) ?
@@ -140,6 +145,27 @@ fn try_lookup_include_path(
140
145
} )
141
146
}
142
147
148
+ fn try_lookup_macro_def_in_macro_use (
149
+ sema : & Semantics < ' _ , RootDatabase > ,
150
+ token : SyntaxToken ,
151
+ ) -> Option < NavigationTarget > {
152
+ let extern_crate = token. parent ( ) ?. ancestors ( ) . find_map ( ast:: ExternCrate :: cast) ?;
153
+ let extern_crate = sema. to_def ( & extern_crate) ?;
154
+ let krate = extern_crate. resolved_crate ( sema. db ) ?;
155
+
156
+ for mod_def in krate. root_module ( ) . declarations ( sema. db ) {
157
+ if let ModuleDef :: Macro ( mac) = mod_def {
158
+ if mac. name ( sema. db ) . as_str ( ) == Some ( token. text ( ) ) {
159
+ if let Some ( nav) = mac. try_to_nav ( sema. db ) {
160
+ return Some ( nav. call_site ) ;
161
+ }
162
+ }
163
+ }
164
+ }
165
+
166
+ None
167
+ }
168
+
143
169
/// finds the trait definition of an impl'd item, except function
144
170
/// e.g.
145
171
/// ```rust
@@ -2081,4 +2107,47 @@ fn test() {
2081
2107
"# ,
2082
2108
) ;
2083
2109
}
2110
+
2111
+ #[ test]
2112
+ fn goto_macro_def_from_macro_use ( ) {
2113
+ check (
2114
+ r#"
2115
+ //- /main.rs crate:main deps:mac
2116
+ #[macro_use(foo$0)]
2117
+ extern crate mac;
2118
+
2119
+ //- /mac.rs crate:mac
2120
+ #[macro_export]
2121
+ macro_rules! foo {
2122
+ //^^^
2123
+ () => {};
2124
+ }
2125
+ "# ,
2126
+ ) ;
2127
+
2128
+ check (
2129
+ r#"
2130
+ //- /main.rs crate:main deps:mac
2131
+ #[macro_use(foo, bar$0, baz)]
2132
+ extern crate mac;
2133
+
2134
+ //- /mac.rs crate:mac
2135
+ #[macro_export]
2136
+ macro_rules! foo {
2137
+ () => {};
2138
+ }
2139
+
2140
+ #[macro_export]
2141
+ macro_rules! bar {
2142
+ //^^^
2143
+ () => {};
2144
+ }
2145
+
2146
+ #[macro_export]
2147
+ macro_rules! baz {
2148
+ () => {};
2149
+ }
2150
+ "# ,
2151
+ ) ;
2152
+ }
2084
2153
}
0 commit comments