@@ -286,22 +286,6 @@ fn fst_path(db: &dyn DefDatabase, path: &ImportPath) -> String {
286
286
s
287
287
}
288
288
289
- #[ derive( Debug , Eq , PartialEq , Hash ) ]
290
- pub enum ImportKind {
291
- Module ,
292
- Function ,
293
- Adt ,
294
- EnumVariant ,
295
- Const ,
296
- Static ,
297
- Trait ,
298
- TraitAlias ,
299
- TypeAlias ,
300
- BuiltinType ,
301
- AssociatedItem ,
302
- Macro ,
303
- }
304
-
305
289
/// A way to match import map contents against the search query.
306
290
#[ derive( Debug ) ]
307
291
pub enum SearchMode {
@@ -314,15 +298,25 @@ pub enum SearchMode {
314
298
Fuzzy ,
315
299
}
316
300
301
+ /// Three possible ways to search for the name in associated and/or other items.
302
+ #[ derive( Debug , Clone , Copy ) ]
303
+ pub enum AssocSearchMode {
304
+ /// Search for the name in both associated and other items.
305
+ Include ,
306
+ /// Search for the name in other items only.
307
+ Exclude ,
308
+ /// Search for the name in the associated items only.
309
+ AssocItemsOnly ,
310
+ }
311
+
317
312
#[ derive( Debug ) ]
318
313
pub struct Query {
319
314
query : String ,
320
315
lowercased : String ,
321
- assoc_items_only : bool ,
322
316
search_mode : SearchMode ,
317
+ assoc_mode : AssocSearchMode ,
323
318
case_sensitive : bool ,
324
319
limit : usize ,
325
- exclude_import_kinds : FxHashSet < ImportKind > ,
326
320
}
327
321
328
322
impl Query {
@@ -331,24 +325,23 @@ impl Query {
331
325
Self {
332
326
query,
333
327
lowercased,
334
- assoc_items_only : false ,
335
328
search_mode : SearchMode :: Contains ,
329
+ assoc_mode : AssocSearchMode :: Include ,
336
330
case_sensitive : false ,
337
331
limit : usize:: max_value ( ) ,
338
- exclude_import_kinds : FxHashSet :: default ( ) ,
339
332
}
340
333
}
341
334
342
- /// Matches only the entries that are associated items, ignoring the rest.
343
- pub fn assoc_items_only ( self ) -> Self {
344
- Self { assoc_items_only : true , ..self }
345
- }
346
-
347
335
/// Specifies the way to search for the entries using the query.
348
336
pub fn search_mode ( self , search_mode : SearchMode ) -> Self {
349
337
Self { search_mode, ..self }
350
338
}
351
339
340
+ /// Specifies whether we want to include associated items in the result.
341
+ pub fn assoc_search_mode ( self , assoc_mode : AssocSearchMode ) -> Self {
342
+ Self { assoc_mode, ..self }
343
+ }
344
+
352
345
/// Limits the returned number of items to `limit`.
353
346
pub fn limit ( self , limit : usize ) -> Self {
354
347
Self { limit, ..self }
@@ -359,25 +352,17 @@ impl Query {
359
352
Self { case_sensitive : true , ..self }
360
353
}
361
354
362
- /// Do not include imports of the specified kind in the search results.
363
- pub fn exclude_import_kind ( mut self , import_kind : ImportKind ) -> Self {
364
- self . exclude_import_kinds . insert ( import_kind) ;
365
- self
366
- }
367
-
368
355
fn import_matches (
369
356
& self ,
370
357
db : & dyn DefDatabase ,
371
358
import : & ImportInfo ,
372
359
enforce_lowercase : bool ,
373
360
) -> bool {
374
361
let _p = profile:: span ( "import_map::Query::import_matches" ) ;
375
- if import. is_trait_assoc_item {
376
- if self . exclude_import_kinds . contains ( & ImportKind :: AssociatedItem ) {
377
- return false ;
378
- }
379
- } else if self . assoc_items_only {
380
- return false ;
362
+ match ( import. is_trait_assoc_item , self . assoc_mode ) {
363
+ ( true , AssocSearchMode :: Exclude ) => return false ,
364
+ ( false , AssocSearchMode :: AssocItemsOnly ) => return false ,
365
+ _ => { }
381
366
}
382
367
383
368
let mut input = import. path . segments . last ( ) . unwrap ( ) . display ( db. upcast ( ) ) . to_string ( ) ;
@@ -458,10 +443,6 @@ pub fn search_dependencies(
458
443
. take_while ( |item| {
459
444
common_importables_path_fst == fst_path ( db, & import_map. map [ item] . path )
460
445
} )
461
- . filter ( |& item| match item_import_kind ( item) {
462
- Some ( import_kind) => !query. exclude_import_kinds . contains ( & import_kind) ,
463
- None => true ,
464
- } )
465
446
. filter ( |item| {
466
447
!query. case_sensitive // we've already checked the common importables path case-insensitively
467
448
|| query. import_matches ( db, & import_map. map [ item] , false )
@@ -476,22 +457,6 @@ pub fn search_dependencies(
476
457
res
477
458
}
478
459
479
- fn item_import_kind ( item : ItemInNs ) -> Option < ImportKind > {
480
- Some ( match item. as_module_def_id ( ) ? {
481
- ModuleDefId :: ModuleId ( _) => ImportKind :: Module ,
482
- ModuleDefId :: FunctionId ( _) => ImportKind :: Function ,
483
- ModuleDefId :: AdtId ( _) => ImportKind :: Adt ,
484
- ModuleDefId :: EnumVariantId ( _) => ImportKind :: EnumVariant ,
485
- ModuleDefId :: ConstId ( _) => ImportKind :: Const ,
486
- ModuleDefId :: StaticId ( _) => ImportKind :: Static ,
487
- ModuleDefId :: TraitId ( _) => ImportKind :: Trait ,
488
- ModuleDefId :: TraitAliasId ( _) => ImportKind :: TraitAlias ,
489
- ModuleDefId :: TypeAliasId ( _) => ImportKind :: TypeAlias ,
490
- ModuleDefId :: BuiltinType ( _) => ImportKind :: BuiltinType ,
491
- ModuleDefId :: MacroId ( _) => ImportKind :: Macro ,
492
- } )
493
- }
494
-
495
460
#[ cfg( test) ]
496
461
mod tests {
497
462
use base_db:: { fixture:: WithFixture , SourceDatabase , Upcast } ;
@@ -888,7 +853,9 @@ mod tests {
888
853
check_search (
889
854
ra_fixture,
890
855
"main" ,
891
- Query :: new ( "fmt" . to_string ( ) ) . search_mode ( SearchMode :: Fuzzy ) . assoc_items_only ( ) ,
856
+ Query :: new ( "fmt" . to_string ( ) )
857
+ . search_mode ( SearchMode :: Fuzzy )
858
+ . assoc_search_mode ( AssocSearchMode :: AssocItemsOnly ) ,
892
859
expect ! [ [ r#"
893
860
dep::fmt::Display::FMT_CONST (a)
894
861
dep::fmt::Display::format_function (a)
@@ -901,21 +868,11 @@ mod tests {
901
868
"main" ,
902
869
Query :: new ( "fmt" . to_string ( ) )
903
870
. search_mode ( SearchMode :: Fuzzy )
904
- . exclude_import_kind ( ImportKind :: AssociatedItem ) ,
871
+ . assoc_search_mode ( AssocSearchMode :: Exclude ) ,
905
872
expect ! [ [ r#"
906
873
dep::fmt (t)
907
874
"# ] ] ,
908
875
) ;
909
-
910
- check_search (
911
- ra_fixture,
912
- "main" ,
913
- Query :: new ( "fmt" . to_string ( ) )
914
- . search_mode ( SearchMode :: Fuzzy )
915
- . assoc_items_only ( )
916
- . exclude_import_kind ( ImportKind :: AssociatedItem ) ,
917
- expect ! [ [ r#""# ] ] ,
918
- ) ;
919
876
}
920
877
921
878
#[ test]
@@ -1101,34 +1058,4 @@ mod tests {
1101
1058
"# ] ] ,
1102
1059
) ;
1103
1060
}
1104
-
1105
- #[ test]
1106
- fn search_exclusions ( ) {
1107
- let ra_fixture = r#"
1108
- //- /main.rs crate:main deps:dep
1109
- //- /dep.rs crate:dep
1110
-
1111
- pub struct fmt;
1112
- pub struct FMT;
1113
- "# ;
1114
-
1115
- check_search (
1116
- ra_fixture,
1117
- "main" ,
1118
- Query :: new ( "FMT" . to_string ( ) ) ,
1119
- expect ! [ [ r#"
1120
- dep::FMT (t)
1121
- dep::FMT (v)
1122
- dep::fmt (t)
1123
- dep::fmt (v)
1124
- "# ] ] ,
1125
- ) ;
1126
-
1127
- check_search (
1128
- ra_fixture,
1129
- "main" ,
1130
- Query :: new ( "FMT" . to_string ( ) ) . exclude_import_kind ( ImportKind :: Adt ) ,
1131
- expect ! [ [ r#""# ] ] ,
1132
- ) ;
1133
- }
1134
1061
}
0 commit comments