@@ -207,7 +207,7 @@ impl ImportAssets {
207
207
prefix_kind : PrefixKind ,
208
208
prefer_no_std : bool ,
209
209
prefer_prelude : bool ,
210
- ) -> Vec < LocatedImport > {
210
+ ) -> impl Iterator < Item = LocatedImport > {
211
211
let _p = profile:: span ( "import_assets::search_for_imports" ) ;
212
212
self . search_for ( sema, Some ( prefix_kind) , prefer_no_std, prefer_prelude)
213
213
}
@@ -218,7 +218,7 @@ impl ImportAssets {
218
218
sema : & Semantics < ' _ , RootDatabase > ,
219
219
prefer_no_std : bool ,
220
220
prefer_prelude : bool ,
221
- ) -> Vec < LocatedImport > {
221
+ ) -> impl Iterator < Item = LocatedImport > {
222
222
let _p = profile:: span ( "import_assets::search_for_relative_paths" ) ;
223
223
self . search_for ( sema, None , prefer_no_std, prefer_prelude)
224
224
}
@@ -259,9 +259,15 @@ impl ImportAssets {
259
259
prefixed : Option < PrefixKind > ,
260
260
prefer_no_std : bool ,
261
261
prefer_prelude : bool ,
262
- ) -> Vec < LocatedImport > {
262
+ ) -> impl Iterator < Item = LocatedImport > {
263
263
let _p = profile:: span ( "import_assets::search_for" ) ;
264
264
265
+ let scope = match sema. scope ( & self . candidate_node ) {
266
+ Some ( it) => it,
267
+ None => return <FxHashSet < _ > >:: default ( ) . into_iter ( ) ,
268
+ } ;
269
+
270
+ let krate = self . module_with_candidate . krate ( ) ;
265
271
let scope_definitions = self . scope_definitions ( sema) ;
266
272
let mod_path = |item| {
267
273
get_mod_path (
@@ -272,30 +278,30 @@ impl ImportAssets {
272
278
prefer_no_std,
273
279
prefer_prelude,
274
280
)
275
- } ;
276
-
277
- let krate = self . module_with_candidate . krate ( ) ;
278
- let scope = match sema. scope ( & self . candidate_node ) {
279
- Some ( it) => it,
280
- None => return Vec :: new ( ) ,
281
+ . filter ( |path| path. len ( ) > 1 )
281
282
} ;
282
283
283
284
match & self . import_candidate {
284
285
ImportCandidate :: Path ( path_candidate) => {
285
- path_applicable_imports ( sema, krate, path_candidate, mod_path)
286
- }
287
- ImportCandidate :: TraitAssocItem ( trait_candidate) => {
288
- trait_applicable_items ( sema, krate, & scope, trait_candidate, true , mod_path)
289
- }
290
- ImportCandidate :: TraitMethod ( trait_candidate) => {
291
- trait_applicable_items ( sema, krate, & scope, trait_candidate, false , mod_path)
286
+ path_applicable_imports ( sema, krate, path_candidate, mod_path, |item_to_import| {
287
+ !scope_definitions. contains ( & ScopeDef :: from ( item_to_import) )
288
+ } )
292
289
}
290
+ ImportCandidate :: TraitAssocItem ( trait_candidate)
291
+ | ImportCandidate :: TraitMethod ( trait_candidate) => trait_applicable_items (
292
+ sema,
293
+ krate,
294
+ & scope,
295
+ trait_candidate,
296
+ matches ! ( self . import_candidate, ImportCandidate :: TraitAssocItem ( _) ) ,
297
+ mod_path,
298
+ |trait_to_import| {
299
+ !scope_definitions
300
+ . contains ( & ScopeDef :: ModuleDef ( ModuleDef :: Trait ( trait_to_import) ) )
301
+ } ,
302
+ ) ,
293
303
}
294
304
. into_iter ( )
295
- . filter ( |import| import. import_path . len ( ) > 1 )
296
- . filter ( |import| !scope_definitions. contains ( & ScopeDef :: from ( import. item_to_import ) ) )
297
- . sorted_by ( |a, b| a. import_path . cmp ( & b. import_path ) )
298
- . collect ( )
299
305
}
300
306
301
307
fn scope_definitions ( & self , sema : & Semantics < ' _ , RootDatabase > ) -> FxHashSet < ScopeDef > {
@@ -315,6 +321,7 @@ fn path_applicable_imports(
315
321
current_crate : Crate ,
316
322
path_candidate : & PathImportCandidate ,
317
323
mod_path : impl Fn ( ItemInNs ) -> Option < ModPath > + Copy ,
324
+ scope_filter : impl Fn ( ItemInNs ) -> bool + Copy ,
318
325
) -> FxHashSet < LocatedImport > {
319
326
let _p = profile:: span ( "import_assets::path_applicable_imports" ) ;
320
327
@@ -335,6 +342,9 @@ fn path_applicable_imports(
335
342
AssocSearchMode :: Exclude ,
336
343
)
337
344
. filter_map ( |item| {
345
+ if !scope_filter ( item) {
346
+ return None ;
347
+ }
338
348
let mod_path = mod_path ( item) ?;
339
349
Some ( LocatedImport :: new ( mod_path, item, item) )
340
350
} )
@@ -347,7 +357,7 @@ fn path_applicable_imports(
347
357
path_candidate. name . clone ( ) ,
348
358
AssocSearchMode :: Include ,
349
359
)
350
- . filter_map ( |item| import_for_item ( sema. db , mod_path, & qualifier, item) )
360
+ . filter_map ( |item| import_for_item ( sema. db , mod_path, & qualifier, item, scope_filter ) )
351
361
. take ( DEFAULT_QUERY_SEARCH_LIMIT . inner ( ) )
352
362
. collect ( ) ,
353
363
}
@@ -358,6 +368,7 @@ fn import_for_item(
358
368
mod_path : impl Fn ( ItemInNs ) -> Option < ModPath > ,
359
369
unresolved_qualifier : & [ SmolStr ] ,
360
370
original_item : ItemInNs ,
371
+ scope_filter : impl Fn ( ItemInNs ) -> bool ,
361
372
) -> Option < LocatedImport > {
362
373
let _p = profile:: span ( "import_assets::import_for_item" ) ;
363
374
let [ first_segment, ..] = unresolved_qualifier else { return None } ;
@@ -413,15 +424,16 @@ fn import_for_item(
413
424
// especially in case of lazy completion edit resolutions.
414
425
return None ;
415
426
}
416
- ( false , Some ( trait_to_import) ) => {
427
+ ( false , Some ( trait_to_import) ) if scope_filter ( trait_to_import ) => {
417
428
LocatedImport :: new ( mod_path ( trait_to_import) ?, trait_to_import, original_item)
418
429
}
419
- ( true , None ) => {
430
+ ( true , None ) if scope_filter ( original_item_candidate ) => {
420
431
LocatedImport :: new ( import_path_candidate, original_item_candidate, original_item)
421
432
}
422
- ( false , None ) => {
433
+ ( false , None ) if scope_filter ( segment_import ) => {
423
434
LocatedImport :: new ( mod_path ( segment_import) ?, segment_import, original_item)
424
435
}
436
+ _ => return None ,
425
437
} )
426
438
}
427
439
@@ -490,6 +502,7 @@ fn trait_applicable_items(
490
502
trait_candidate : & TraitImportCandidate ,
491
503
trait_assoc_item : bool ,
492
504
mod_path : impl Fn ( ItemInNs ) -> Option < ModPath > ,
505
+ scope_filter : impl Fn ( hir:: Trait ) -> bool ,
493
506
) -> FxHashSet < LocatedImport > {
494
507
let _p = profile:: span ( "import_assets::trait_applicable_items" ) ;
495
508
@@ -533,7 +546,8 @@ fn trait_applicable_items(
533
546
None ,
534
547
|assoc| {
535
548
if required_assoc_items. contains ( & assoc) {
536
- let located_trait = assoc. containing_trait ( db) ?;
549
+ let located_trait =
550
+ assoc. containing_trait ( db) . filter ( |& it| scope_filter ( it) ) ?;
537
551
let trait_item = ItemInNs :: from ( ModuleDef :: from ( located_trait) ) ;
538
552
let import_path = trait_import_paths
539
553
. entry ( trait_item)
@@ -558,7 +572,8 @@ fn trait_applicable_items(
558
572
|function| {
559
573
let assoc = function. as_assoc_item ( db) ?;
560
574
if required_assoc_items. contains ( & assoc) {
561
- let located_trait = assoc. containing_trait ( db) ?;
575
+ let located_trait =
576
+ assoc. containing_trait ( db) . filter ( |& it| scope_filter ( it) ) ?;
562
577
let trait_item = ItemInNs :: from ( ModuleDef :: from ( located_trait) ) ;
563
578
let import_path = trait_import_paths
564
579
. entry ( trait_item)
0 commit comments