6
6
7
7
use either:: Either ;
8
8
use hir_expand:: { attrs:: collect_attrs, HirFileId } ;
9
+ use syntax:: ast;
9
10
10
11
use crate :: {
11
12
db:: DefDatabase ,
@@ -17,8 +18,9 @@ use crate::{
17
18
item_tree:: ItemTreeNode ,
18
19
nameres:: DefMap ,
19
20
src:: { HasChildSource , HasSource } ,
20
- AdtId , AssocItemId , DefWithBodyId , EnumId , FieldId , ImplId , ItemTreeLoc , Lookup , MacroId ,
21
- ModuleDefId , ModuleId , TraitId , VariantId ,
21
+ AdtId , AssocItemId , DefWithBodyId , EnumId , FieldId , GenericDefId , ImplId , ItemTreeLoc ,
22
+ LifetimeParamId , Lookup , MacroId , ModuleDefId , ModuleId , TraitId , TypeOrConstParamId ,
23
+ VariantId ,
22
24
} ;
23
25
24
26
pub trait ChildBySource {
@@ -59,14 +61,6 @@ impl ChildBySource for ImplId {
59
61
}
60
62
}
61
63
62
- fn add_assoc_item ( db : & dyn DefDatabase , res : & mut DynMap , file_id : HirFileId , item : AssocItemId ) {
63
- match item {
64
- AssocItemId :: FunctionId ( func) => insert_item_loc ( db, res, file_id, func, keys:: FUNCTION ) ,
65
- AssocItemId :: ConstId ( konst) => insert_item_loc ( db, res, file_id, konst, keys:: CONST ) ,
66
- AssocItemId :: TypeAliasId ( ty) => insert_item_loc ( db, res, file_id, ty, keys:: TYPE_ALIAS ) ,
67
- }
68
- }
69
-
70
64
impl ChildBySource for ModuleId {
71
65
fn child_by_source_to ( & self , db : & dyn DefDatabase , res : & mut DynMap , file_id : HirFileId ) {
72
66
let def_map = self . def_map ( db) ;
@@ -118,14 +112,6 @@ impl ChildBySource for ItemScope {
118
112
file_id : HirFileId ,
119
113
item : ModuleDefId ,
120
114
) {
121
- macro_rules! insert {
122
- ( $map: ident[ $key: path] . $insert: ident( $id: ident) ) => { {
123
- let loc = $id. lookup( db) ;
124
- if loc. id. file_id( ) == file_id {
125
- $map[ $key] . $insert( loc. source( db) . value, $id)
126
- }
127
- } } ;
128
- }
129
115
match item {
130
116
ModuleDefId :: FunctionId ( id) => {
131
117
insert_item_loc ( db, map, file_id, id, keys:: FUNCTION )
@@ -145,9 +131,13 @@ impl ChildBySource for ItemScope {
145
131
AdtId :: EnumId ( id) => insert_item_loc ( db, map, file_id, id, keys:: ENUM ) ,
146
132
} ,
147
133
ModuleDefId :: MacroId ( id) => match id {
148
- MacroId :: Macro2Id ( id) => insert ! ( map[ keys:: MACRO2 ] . insert( id) ) ,
149
- MacroId :: MacroRulesId ( id) => insert ! ( map[ keys:: MACRO_RULES ] . insert( id) ) ,
150
- MacroId :: ProcMacroId ( id) => insert ! ( map[ keys:: PROC_MACRO ] . insert( id) ) ,
134
+ MacroId :: Macro2Id ( id) => insert_item_loc ( db, map, file_id, id, keys:: MACRO2 ) ,
135
+ MacroId :: MacroRulesId ( id) => {
136
+ insert_item_loc ( db, map, file_id, id, keys:: MACRO_RULES )
137
+ }
138
+ MacroId :: ProcMacroId ( id) => {
139
+ insert_item_loc ( db, map, file_id, id, keys:: PROC_MACRO )
140
+ }
151
141
} ,
152
142
ModuleDefId :: ModuleId ( _)
153
143
| ModuleDefId :: EnumVariantId ( _)
@@ -207,6 +197,40 @@ impl ChildBySource for DefWithBodyId {
207
197
}
208
198
}
209
199
200
+ impl ChildBySource for GenericDefId {
201
+ fn child_by_source_to ( & self , db : & dyn DefDatabase , res : & mut DynMap , file_id : HirFileId ) {
202
+ let ( gfile_id, generic_params_list) = self . file_id_and_params_of ( db) ;
203
+ if gfile_id != file_id {
204
+ return ;
205
+ }
206
+
207
+ let generic_params = db. generic_params ( * self ) ;
208
+ let mut toc_idx_iter = generic_params. type_or_consts . iter ( ) . map ( |( idx, _) | idx) ;
209
+ let lts_idx_iter = generic_params. lifetimes . iter ( ) . map ( |( idx, _) | idx) ;
210
+
211
+ // For traits the first type index is `Self`, skip it.
212
+ if let GenericDefId :: TraitId ( _) = * self {
213
+ toc_idx_iter. next ( ) . unwrap ( ) ; // advance_by(1);
214
+ }
215
+
216
+ if let Some ( generic_params_list) = generic_params_list {
217
+ for ( local_id, ast_param) in
218
+ toc_idx_iter. zip ( generic_params_list. type_or_const_params ( ) )
219
+ {
220
+ let id = TypeOrConstParamId { parent : * self , local_id } ;
221
+ match ast_param {
222
+ ast:: TypeOrConstParam :: Type ( a) => res[ keys:: TYPE_PARAM ] . insert ( a, id) ,
223
+ ast:: TypeOrConstParam :: Const ( a) => res[ keys:: CONST_PARAM ] . insert ( a, id) ,
224
+ }
225
+ }
226
+ for ( local_id, ast_param) in lts_idx_iter. zip ( generic_params_list. lifetime_params ( ) ) {
227
+ let id = LifetimeParamId { parent : * self , local_id } ;
228
+ res[ keys:: LIFETIME_PARAM ] . insert ( ast_param, id) ;
229
+ }
230
+ }
231
+ }
232
+ }
233
+
210
234
fn insert_item_loc < ID , N , Data > (
211
235
db : & dyn DefDatabase ,
212
236
res : & mut DynMap ,
@@ -224,3 +248,11 @@ fn insert_item_loc<ID, N, Data>(
224
248
res[ key] . insert ( loc. source ( db) . value , id)
225
249
}
226
250
}
251
+
252
+ fn add_assoc_item ( db : & dyn DefDatabase , res : & mut DynMap , file_id : HirFileId , item : AssocItemId ) {
253
+ match item {
254
+ AssocItemId :: FunctionId ( func) => insert_item_loc ( db, res, file_id, func, keys:: FUNCTION ) ,
255
+ AssocItemId :: ConstId ( konst) => insert_item_loc ( db, res, file_id, konst, keys:: CONST ) ,
256
+ AssocItemId :: TypeAliasId ( ty) => insert_item_loc ( db, res, file_id, ty, keys:: TYPE_ALIAS ) ,
257
+ }
258
+ }
0 commit comments