11use std:: cmp:: Ordering ;
22
33use hir:: {
4- base_db:: { source_db:: SourceRootDb , source_root:: SourceRootRole } ,
4+ base_db:: {
5+ source_db:: { SourceDb , SourceRootDb } ,
6+ source_root:: SourceRootRole ,
7+ } ,
58 container:: InModule ,
69 db:: HirDb ,
710 hir_def:: {
@@ -11,7 +14,7 @@ use hir::{
1114 lower_ident_opt,
1215 module:: { ModuleId , instantiation:: Instantiation } ,
1316 } ,
14- scope:: { ModuleEntry , ScopeResolution } ,
17+ scope:: ModuleEntry ,
1518 semantics:: pathres:: PathResolution ,
1619} ;
1720use syntax:: {
@@ -21,7 +24,7 @@ use syntax::{
2124use utils:: get:: GetRef ;
2225use vfs:: { FileId , VfsPath } ;
2326
24- use crate :: db:: root_db:: RootDb ;
27+ use crate :: db:: { root_db:: RootDb , workspace_symbol_index_db :: source_root_module_index_for_root } ;
2528
2629#[ derive( Debug , Clone , PartialEq , Eq ) ]
2730pub ( crate ) enum ModuleResolution {
@@ -154,13 +157,34 @@ fn resolve_module_name_with_policy(
154157 name : & Ident ,
155158 policy : ModuleResolutionPolicy ,
156159) -> ModuleResolution {
157- match db. unit_scope ( ) . resolve_module ( name) {
158- ScopeResolution :: Unique ( module_id) => ModuleResolution :: Unique ( module_id) ,
159- ScopeResolution :: Unresolved => ModuleResolution :: Unresolved ,
160- ScopeResolution :: Ambiguous ( candidates) => {
161- policy. resolve_ambiguous ( db, candidates. into_vec ( ) )
162- }
160+ let candidates = module_candidates ( db, name) ;
161+ match candidates. as_slice ( ) {
162+ [ module_id] => ModuleResolution :: Unique ( * module_id) ,
163+ [ ] => ModuleResolution :: Unresolved ,
164+ _ => policy. resolve_ambiguous ( db, candidates) ,
165+ }
166+ }
167+
168+ fn module_candidates ( db : & RootDb , name : & Ident ) -> Vec < ModuleId > {
169+ let mut source_root_ids =
170+ db. files ( ) . iter ( ) . map ( |& file_id| db. source_root_id ( file_id) ) . collect :: < Vec < _ > > ( ) ;
171+ source_root_ids. sort_unstable ( ) ;
172+ source_root_ids. dedup ( ) ;
173+
174+ let mut candidates = Vec :: new ( ) ;
175+ for source_root_id in source_root_ids {
176+ let module_index = source_root_module_index_for_root ( db, source_root_id) ;
177+ candidates. extend (
178+ module_index
179+ . module_definitions ( name)
180+ . iter ( )
181+ . map ( |module| ( module. file_id , module. name_range . start ( ) , module. module_id ) ) ,
182+ ) ;
163183 }
184+
185+ candidates. sort_by_key ( |( file_id, name_start, _) | ( file_id. 0 , * name_start) ) ;
186+ candidates. dedup_by_key ( |( _, _, module_id) | * module_id) ;
187+ candidates. into_iter ( ) . map ( |( _, _, module_id) | module_id) . collect ( )
164188}
165189
166190#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
0 commit comments