@@ -13,17 +13,39 @@ use syntax::{AstNode, SyntaxKind::*, T};
13
13
14
14
use crate :: { doc_links:: token_as_doc_comment, RangeInfo } ;
15
15
16
+ #[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
17
+ pub enum MonikerDescriptorKind {
18
+ Namespace ,
19
+ Type ,
20
+ Term ,
21
+ Method ,
22
+ TypeParameter ,
23
+ Parameter ,
24
+ Macro ,
25
+ Meta ,
26
+ }
27
+
28
+ #[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
29
+ pub struct MonikerDescriptor {
30
+ pub name : Name ,
31
+ pub desc : MonikerDescriptorKind ,
32
+ }
33
+
16
34
#[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
17
35
pub struct MonikerIdentifier {
18
- crate_name : String ,
19
- path : Vec < Name > ,
36
+ pub crate_name : String ,
37
+ pub description : Vec < MonikerDescriptor > ,
20
38
}
21
39
22
40
impl ToString for MonikerIdentifier {
23
41
fn to_string ( & self ) -> String {
24
42
match self {
25
- MonikerIdentifier { path, crate_name } => {
26
- format ! ( "{}::{}" , crate_name, path. iter( ) . map( |x| x. to_string( ) ) . join( "::" ) )
43
+ MonikerIdentifier { description, crate_name } => {
44
+ format ! (
45
+ "{}::{}" ,
46
+ crate_name,
47
+ description. iter( ) . map( |x| x. name. to_string( ) ) . join( "::" )
48
+ )
27
49
}
28
50
}
29
51
}
@@ -42,6 +64,12 @@ pub struct MonikerResult {
42
64
pub package_information : PackageInformation ,
43
65
}
44
66
67
+ impl MonikerResult {
68
+ pub fn from_def ( db : & RootDatabase , def : Definition , from_crate : Crate ) -> Option < Self > {
69
+ def_to_moniker ( db, def, from_crate)
70
+ }
71
+ }
72
+
45
73
#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
46
74
pub struct PackageInformation {
47
75
pub name : String ,
@@ -105,13 +133,23 @@ pub(crate) fn def_to_moniker(
105
133
def : Definition ,
106
134
from_crate : Crate ,
107
135
) -> Option < MonikerResult > {
108
- if matches ! ( def, Definition :: GenericParam ( _) | Definition :: SelfType ( _) | Definition :: Local ( _) ) {
136
+ if matches ! (
137
+ def,
138
+ Definition :: GenericParam ( _)
139
+ | Definition :: Label ( _)
140
+ | Definition :: DeriveHelper ( _)
141
+ | Definition :: BuiltinAttr ( _)
142
+ | Definition :: ToolModule ( _)
143
+ ) {
109
144
return None ;
110
145
}
146
+
111
147
let module = def. module ( db) ?;
112
148
let krate = module. krate ( ) ;
113
- let mut path = vec ! [ ] ;
114
- path. extend ( module. path_to_root ( db) . into_iter ( ) . filter_map ( |x| x. name ( db) ) ) ;
149
+ let mut description = vec ! [ ] ;
150
+ description. extend ( module. path_to_root ( db) . into_iter ( ) . filter_map ( |x| {
151
+ Some ( MonikerDescriptor { name : x. name ( db) ?, desc : MonikerDescriptorKind :: Namespace } )
152
+ } ) ) ;
115
153
116
154
// Handle associated items within a trait
117
155
if let Some ( assoc) = def. as_assoc_item ( db) {
@@ -120,31 +158,98 @@ pub(crate) fn def_to_moniker(
120
158
AssocItemContainer :: Trait ( trait_) => {
121
159
// Because different traits can have functions with the same name,
122
160
// we have to include the trait name as part of the moniker for uniqueness.
123
- path. push ( trait_. name ( db) ) ;
161
+ description. push ( MonikerDescriptor {
162
+ name : trait_. name ( db) ,
163
+ desc : MonikerDescriptorKind :: Type ,
164
+ } ) ;
124
165
}
125
166
AssocItemContainer :: Impl ( impl_) => {
126
167
// Because a struct can implement multiple traits, for implementations
127
168
// we add both the struct name and the trait name to the path
128
169
if let Some ( adt) = impl_. self_ty ( db) . as_adt ( ) {
129
- path. push ( adt. name ( db) ) ;
170
+ description. push ( MonikerDescriptor {
171
+ name : adt. name ( db) ,
172
+ desc : MonikerDescriptorKind :: Type ,
173
+ } ) ;
130
174
}
131
175
132
176
if let Some ( trait_) = impl_. trait_ ( db) {
133
- path. push ( trait_. name ( db) ) ;
177
+ description. push ( MonikerDescriptor {
178
+ name : trait_. name ( db) ,
179
+ desc : MonikerDescriptorKind :: Type ,
180
+ } ) ;
134
181
}
135
182
}
136
183
}
137
184
}
138
185
139
186
if let Definition :: Field ( it) = def {
140
- path. push ( it. parent_def ( db) . name ( db) ) ;
187
+ description. push ( MonikerDescriptor {
188
+ name : it. parent_def ( db) . name ( db) ,
189
+ desc : MonikerDescriptorKind :: Type ,
190
+ } ) ;
141
191
}
142
192
143
- path. push ( def. name ( db) ?) ;
193
+ let name_desc = match def {
194
+ // These are handled by top-level guard (for performance).
195
+ Definition :: GenericParam ( _)
196
+ | Definition :: Label ( _)
197
+ | Definition :: DeriveHelper ( _)
198
+ | Definition :: BuiltinAttr ( _)
199
+ | Definition :: ToolModule ( _) => return None ,
200
+
201
+ Definition :: Local ( local) => {
202
+ if !local. is_param ( db) {
203
+ return None ;
204
+ }
205
+
206
+ MonikerDescriptor { name : local. name ( db) , desc : MonikerDescriptorKind :: Parameter }
207
+ }
208
+ Definition :: Macro ( m) => {
209
+ MonikerDescriptor { name : m. name ( db) , desc : MonikerDescriptorKind :: Macro }
210
+ }
211
+ Definition :: Function ( f) => {
212
+ MonikerDescriptor { name : f. name ( db) , desc : MonikerDescriptorKind :: Method }
213
+ }
214
+ Definition :: Variant ( v) => {
215
+ MonikerDescriptor { name : v. name ( db) , desc : MonikerDescriptorKind :: Type }
216
+ }
217
+ Definition :: Const ( c) => {
218
+ MonikerDescriptor { name : c. name ( db) ?, desc : MonikerDescriptorKind :: Term }
219
+ }
220
+ Definition :: Trait ( trait_) => {
221
+ MonikerDescriptor { name : trait_. name ( db) , desc : MonikerDescriptorKind :: Type }
222
+ }
223
+ Definition :: TypeAlias ( ta) => {
224
+ MonikerDescriptor { name : ta. name ( db) , desc : MonikerDescriptorKind :: TypeParameter }
225
+ }
226
+ Definition :: Module ( m) => {
227
+ MonikerDescriptor { name : m. name ( db) ?, desc : MonikerDescriptorKind :: Namespace }
228
+ }
229
+ Definition :: BuiltinType ( b) => {
230
+ MonikerDescriptor { name : b. name ( ) , desc : MonikerDescriptorKind :: Type }
231
+ }
232
+ Definition :: SelfType ( imp) => MonikerDescriptor {
233
+ name : imp. self_ty ( db) . as_adt ( ) ?. name ( db) ,
234
+ desc : MonikerDescriptorKind :: Type ,
235
+ } ,
236
+ Definition :: Field ( it) => {
237
+ MonikerDescriptor { name : it. name ( db) , desc : MonikerDescriptorKind :: Term }
238
+ }
239
+ Definition :: Adt ( adt) => {
240
+ MonikerDescriptor { name : adt. name ( db) , desc : MonikerDescriptorKind :: Type }
241
+ }
242
+ Definition :: Static ( s) => {
243
+ MonikerDescriptor { name : s. name ( db) , desc : MonikerDescriptorKind :: Meta }
244
+ }
245
+ } ;
246
+
247
+ description. push ( name_desc) ;
248
+
144
249
Some ( MonikerResult {
145
250
identifier : MonikerIdentifier {
146
251
crate_name : krate. display_name ( db) ?. crate_name ( ) . to_string ( ) ,
147
- path ,
252
+ description ,
148
253
} ,
149
254
kind : if krate == from_crate { MonikerKind :: Export } else { MonikerKind :: Import } ,
150
255
package_information : {
0 commit comments