1
1
//! This module generates [moniker](https://microsoft.github.io/language-server-protocol/specifications/lsif/0.6.0/specification/#exportsImports)
2
2
//! for LSIF and LSP.
3
3
4
- use hir:: { AsAssocItem , AssocItemContainer , Crate , DescendPreference , Semantics } ;
4
+ use hir:: { Adt , AsAssocItem , AssocItemContainer , Crate , DescendPreference , MacroKind , Semantics } ;
5
5
use ide_db:: {
6
6
base_db:: { CrateOrigin , FilePosition , LangCrateOrigin } ,
7
7
defs:: { Definition , IdentClass } ,
@@ -25,6 +25,62 @@ pub enum MonikerDescriptorKind {
25
25
Meta ,
26
26
}
27
27
28
+ // Subset of scip_types::SymbolInformation::Kind
29
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
30
+ pub enum SymbolInformationKind {
31
+ AssociatedType ,
32
+ Attribute ,
33
+ Constant ,
34
+ Enum ,
35
+ EnumMember ,
36
+ Field ,
37
+ Function ,
38
+ Macro ,
39
+ Method ,
40
+ Module ,
41
+ Parameter ,
42
+ SelfParameter ,
43
+ StaticMethod ,
44
+ StaticVariable ,
45
+ Struct ,
46
+ Trait ,
47
+ TraitMethod ,
48
+ Type ,
49
+ TypeAlias ,
50
+ TypeParameter ,
51
+ Union ,
52
+ Variable ,
53
+ }
54
+
55
+ impl From < SymbolInformationKind > for MonikerDescriptorKind {
56
+ fn from ( value : SymbolInformationKind ) -> Self {
57
+ match value {
58
+ SymbolInformationKind :: AssociatedType => Self :: TypeParameter ,
59
+ SymbolInformationKind :: Attribute => Self :: Macro ,
60
+ SymbolInformationKind :: Constant => Self :: Term ,
61
+ SymbolInformationKind :: Enum => Self :: Type ,
62
+ SymbolInformationKind :: EnumMember => Self :: Type ,
63
+ SymbolInformationKind :: Field => Self :: Term ,
64
+ SymbolInformationKind :: Function => Self :: Method ,
65
+ SymbolInformationKind :: Macro => Self :: Macro ,
66
+ SymbolInformationKind :: Method => Self :: Method ,
67
+ SymbolInformationKind :: Module => Self :: Namespace ,
68
+ SymbolInformationKind :: Parameter => Self :: Parameter ,
69
+ SymbolInformationKind :: SelfParameter => Self :: Parameter ,
70
+ SymbolInformationKind :: StaticMethod => Self :: Method ,
71
+ SymbolInformationKind :: StaticVariable => Self :: Meta ,
72
+ SymbolInformationKind :: Struct => Self :: Type ,
73
+ SymbolInformationKind :: Trait => Self :: Type ,
74
+ SymbolInformationKind :: TraitMethod => Self :: Method ,
75
+ SymbolInformationKind :: Type => Self :: Type ,
76
+ SymbolInformationKind :: TypeAlias => Self :: Type ,
77
+ SymbolInformationKind :: TypeParameter => Self :: TypeParameter ,
78
+ SymbolInformationKind :: Union => Self :: Type ,
79
+ SymbolInformationKind :: Variable => Self :: Term ,
80
+ }
81
+ }
82
+ }
83
+
28
84
#[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
29
85
pub struct MonikerDescriptor {
30
86
pub name : String ,
@@ -112,6 +168,69 @@ pub(crate) fn moniker(
112
168
Some ( RangeInfo :: new ( original_token. text_range ( ) , navs) )
113
169
}
114
170
171
+ pub ( crate ) fn def_to_kind ( db : & RootDatabase , def : Definition ) -> SymbolInformationKind {
172
+ use SymbolInformationKind :: * ;
173
+
174
+ match def {
175
+ Definition :: Macro ( it) => match it. kind ( db) {
176
+ MacroKind :: Declarative => Macro ,
177
+ MacroKind :: Derive => Attribute ,
178
+ MacroKind :: BuiltIn => Macro ,
179
+ MacroKind :: Attr => Attribute ,
180
+ MacroKind :: ProcMacro => Macro ,
181
+ } ,
182
+ Definition :: Field ( ..) => Field ,
183
+ Definition :: Module ( ..) => Module ,
184
+ Definition :: Function ( it) => {
185
+ if it. as_assoc_item ( db) . is_some ( ) {
186
+ if it. has_self_param ( db) {
187
+ if it. has_body ( db) {
188
+ Method
189
+ } else {
190
+ TraitMethod
191
+ }
192
+ } else {
193
+ StaticMethod
194
+ }
195
+ } else {
196
+ Function
197
+ }
198
+ }
199
+ Definition :: Adt ( Adt :: Struct ( ..) ) => Struct ,
200
+ Definition :: Adt ( Adt :: Union ( ..) ) => Union ,
201
+ Definition :: Adt ( Adt :: Enum ( ..) ) => Enum ,
202
+ Definition :: Variant ( ..) => EnumMember ,
203
+ Definition :: Const ( ..) => Constant ,
204
+ Definition :: Static ( ..) => StaticVariable ,
205
+ Definition :: Trait ( ..) => Trait ,
206
+ Definition :: TraitAlias ( ..) => Trait ,
207
+ Definition :: TypeAlias ( it) => {
208
+ if it. as_assoc_item ( db) . is_some ( ) {
209
+ AssociatedType
210
+ } else {
211
+ TypeAlias
212
+ }
213
+ }
214
+ Definition :: BuiltinType ( ..) => Type ,
215
+ Definition :: SelfType ( ..) => TypeAlias ,
216
+ Definition :: GenericParam ( ..) => TypeParameter ,
217
+ Definition :: Local ( it) => {
218
+ if it. is_self ( db) {
219
+ SelfParameter
220
+ } else if it. is_param ( db) {
221
+ Parameter
222
+ } else {
223
+ Variable
224
+ }
225
+ }
226
+ Definition :: Label ( ..) => Variable , // For lack of a better variant
227
+ Definition :: DeriveHelper ( ..) => Attribute ,
228
+ Definition :: BuiltinAttr ( ..) => Attribute ,
229
+ Definition :: ToolModule ( ..) => Module ,
230
+ Definition :: ExternCrateDecl ( ..) => Module ,
231
+ }
232
+ }
233
+
115
234
pub ( crate ) fn def_to_moniker (
116
235
db : & RootDatabase ,
117
236
def : Definition ,
@@ -134,7 +253,7 @@ pub(crate) fn def_to_moniker(
134
253
description. extend ( module. path_to_root ( db) . into_iter ( ) . filter_map ( |x| {
135
254
Some ( MonikerDescriptor {
136
255
name : x. name ( db) ?. display ( db) . to_string ( ) ,
137
- desc : MonikerDescriptorKind :: Namespace ,
256
+ desc : def_to_kind ( db , x . into ( ) ) . into ( ) ,
138
257
} )
139
258
} ) ) ;
140
259
@@ -147,7 +266,7 @@ pub(crate) fn def_to_moniker(
147
266
// we have to include the trait name as part of the moniker for uniqueness.
148
267
description. push ( MonikerDescriptor {
149
268
name : trait_. name ( db) . display ( db) . to_string ( ) ,
150
- desc : MonikerDescriptorKind :: Type ,
269
+ desc : def_to_kind ( db , trait_ . into ( ) ) . into ( ) ,
151
270
} ) ;
152
271
}
153
272
AssocItemContainer :: Impl ( impl_) => {
@@ -156,14 +275,14 @@ pub(crate) fn def_to_moniker(
156
275
if let Some ( adt) = impl_. self_ty ( db) . as_adt ( ) {
157
276
description. push ( MonikerDescriptor {
158
277
name : adt. name ( db) . display ( db) . to_string ( ) ,
159
- desc : MonikerDescriptorKind :: Type ,
278
+ desc : def_to_kind ( db , adt . into ( ) ) . into ( ) ,
160
279
} ) ;
161
280
}
162
281
163
282
if let Some ( trait_) = impl_. trait_ ( db) {
164
283
description. push ( MonikerDescriptor {
165
284
name : trait_. name ( db) . display ( db) . to_string ( ) ,
166
- desc : MonikerDescriptorKind :: Type ,
285
+ desc : def_to_kind ( db , trait_ . into ( ) ) . into ( ) ,
167
286
} ) ;
168
287
}
169
288
}
@@ -173,21 +292,26 @@ pub(crate) fn def_to_moniker(
173
292
if let Definition :: Field ( it) = def {
174
293
description. push ( MonikerDescriptor {
175
294
name : it. parent_def ( db) . name ( db) . display ( db) . to_string ( ) ,
176
- desc : MonikerDescriptorKind :: Type ,
295
+ desc : def_to_kind ( db , it . parent_def ( db ) . into ( ) ) . into ( ) ,
177
296
} ) ;
178
297
}
179
298
180
299
// Qualify locals/parameters by their parent definition name.
181
300
if let Definition :: Local ( it) = def {
182
- let parent_name = it. parent ( db) . name ( db) ;
183
- if let Some ( name) = parent_name {
184
- description. push ( MonikerDescriptor {
185
- name : name. display ( db) . to_string ( ) ,
186
- desc : MonikerDescriptorKind :: Method ,
187
- } ) ;
301
+ let parent = Definition :: try_from ( it. parent ( db) ) . ok ( ) ;
302
+ if let Some ( parent) = parent {
303
+ let parent_name = parent. name ( db) ;
304
+ if let Some ( name) = parent_name {
305
+ description. push ( MonikerDescriptor {
306
+ name : name. display ( db) . to_string ( ) ,
307
+ desc : def_to_kind ( db, parent) . into ( ) ,
308
+ } ) ;
309
+ }
188
310
}
189
311
}
190
312
313
+ let desc = def_to_kind ( db, def) . into ( ) ;
314
+
191
315
let name_desc = match def {
192
316
// These are handled by top-level guard (for performance).
193
317
Definition :: GenericParam ( _)
@@ -201,67 +325,51 @@ pub(crate) fn def_to_moniker(
201
325
return None ;
202
326
}
203
327
204
- MonikerDescriptor {
205
- name : local. name ( db) . display ( db) . to_string ( ) ,
206
- desc : MonikerDescriptorKind :: Parameter ,
207
- }
328
+ MonikerDescriptor { name : local. name ( db) . display ( db) . to_string ( ) , desc }
329
+ }
330
+ Definition :: Macro ( m) => {
331
+ MonikerDescriptor { name : m. name ( db) . display ( db) . to_string ( ) , desc }
332
+ }
333
+ Definition :: Function ( f) => {
334
+ MonikerDescriptor { name : f. name ( db) . display ( db) . to_string ( ) , desc }
335
+ }
336
+ Definition :: Variant ( v) => {
337
+ MonikerDescriptor { name : v. name ( db) . display ( db) . to_string ( ) , desc }
338
+ }
339
+ Definition :: Const ( c) => {
340
+ MonikerDescriptor { name : c. name ( db) ?. display ( db) . to_string ( ) , desc }
341
+ }
342
+ Definition :: Trait ( trait_) => {
343
+ MonikerDescriptor { name : trait_. name ( db) . display ( db) . to_string ( ) , desc }
344
+ }
345
+ Definition :: TraitAlias ( ta) => {
346
+ MonikerDescriptor { name : ta. name ( db) . display ( db) . to_string ( ) , desc }
347
+ }
348
+ Definition :: TypeAlias ( ta) => {
349
+ MonikerDescriptor { name : ta. name ( db) . display ( db) . to_string ( ) , desc }
350
+ }
351
+ Definition :: Module ( m) => {
352
+ MonikerDescriptor { name : m. name ( db) ?. display ( db) . to_string ( ) , desc }
353
+ }
354
+ Definition :: BuiltinType ( b) => {
355
+ MonikerDescriptor { name : b. name ( ) . display ( db) . to_string ( ) , desc }
208
356
}
209
- Definition :: Macro ( m) => MonikerDescriptor {
210
- name : m. name ( db) . display ( db) . to_string ( ) ,
211
- desc : MonikerDescriptorKind :: Macro ,
212
- } ,
213
- Definition :: Function ( f) => MonikerDescriptor {
214
- name : f. name ( db) . display ( db) . to_string ( ) ,
215
- desc : MonikerDescriptorKind :: Method ,
216
- } ,
217
- Definition :: Variant ( v) => MonikerDescriptor {
218
- name : v. name ( db) . display ( db) . to_string ( ) ,
219
- desc : MonikerDescriptorKind :: Type ,
220
- } ,
221
- Definition :: Const ( c) => MonikerDescriptor {
222
- name : c. name ( db) ?. display ( db) . to_string ( ) ,
223
- desc : MonikerDescriptorKind :: Term ,
224
- } ,
225
- Definition :: Trait ( trait_) => MonikerDescriptor {
226
- name : trait_. name ( db) . display ( db) . to_string ( ) ,
227
- desc : MonikerDescriptorKind :: Type ,
228
- } ,
229
- Definition :: TraitAlias ( ta) => MonikerDescriptor {
230
- name : ta. name ( db) . display ( db) . to_string ( ) ,
231
- desc : MonikerDescriptorKind :: Type ,
232
- } ,
233
- Definition :: TypeAlias ( ta) => MonikerDescriptor {
234
- name : ta. name ( db) . display ( db) . to_string ( ) ,
235
- desc : MonikerDescriptorKind :: TypeParameter ,
236
- } ,
237
- Definition :: Module ( m) => MonikerDescriptor {
238
- name : m. name ( db) ?. display ( db) . to_string ( ) ,
239
- desc : MonikerDescriptorKind :: Namespace ,
240
- } ,
241
- Definition :: BuiltinType ( b) => MonikerDescriptor {
242
- name : b. name ( ) . display ( db) . to_string ( ) ,
243
- desc : MonikerDescriptorKind :: Type ,
244
- } ,
245
357
Definition :: SelfType ( imp) => MonikerDescriptor {
246
358
name : imp. self_ty ( db) . as_adt ( ) ?. name ( db) . display ( db) . to_string ( ) ,
247
- desc : MonikerDescriptorKind :: Type ,
248
- } ,
249
- Definition :: Field ( it) => MonikerDescriptor {
250
- name : it. name ( db) . display ( db) . to_string ( ) ,
251
- desc : MonikerDescriptorKind :: Term ,
252
- } ,
253
- Definition :: Adt ( adt) => MonikerDescriptor {
254
- name : adt. name ( db) . display ( db) . to_string ( ) ,
255
- desc : MonikerDescriptorKind :: Type ,
256
- } ,
257
- Definition :: Static ( s) => MonikerDescriptor {
258
- name : s. name ( db) . display ( db) . to_string ( ) ,
259
- desc : MonikerDescriptorKind :: Meta ,
260
- } ,
261
- Definition :: ExternCrateDecl ( m) => MonikerDescriptor {
262
- name : m. name ( db) . display ( db) . to_string ( ) ,
263
- desc : MonikerDescriptorKind :: Namespace ,
359
+ desc,
264
360
} ,
361
+ Definition :: Field ( it) => {
362
+ MonikerDescriptor { name : it. name ( db) . display ( db) . to_string ( ) , desc }
363
+ }
364
+ Definition :: Adt ( adt) => {
365
+ MonikerDescriptor { name : adt. name ( db) . display ( db) . to_string ( ) , desc }
366
+ }
367
+ Definition :: Static ( s) => {
368
+ MonikerDescriptor { name : s. name ( db) . display ( db) . to_string ( ) , desc }
369
+ }
370
+ Definition :: ExternCrateDecl ( m) => {
371
+ MonikerDescriptor { name : m. name ( db) . display ( db) . to_string ( ) , desc }
372
+ }
265
373
} ;
266
374
267
375
description. push ( name_desc) ;
0 commit comments