@@ -66,22 +66,28 @@ pub struct SymbolCollector<'a> {
6666 symbols : FxIndexSet < FileSymbol > ,
6767 work : Vec < SymbolCollectorWork > ,
6868 current_container_name : Option < Symbol > ,
69+ collect_pub_only : bool ,
6970}
7071
7172/// Given a [`ModuleId`] and a [`HirDatabase`], use the DefMap for the module's crate to collect
7273/// all symbols that should be indexed for the given module.
7374impl < ' a > SymbolCollector < ' a > {
74- pub fn new ( db : & ' a dyn HirDatabase ) -> Self {
75+ pub fn new ( db : & ' a dyn HirDatabase , collect_pub_only : bool ) -> Self {
7576 SymbolCollector {
7677 db,
7778 symbols : Default :: default ( ) ,
7879 work : Default :: default ( ) ,
7980 current_container_name : None ,
81+ collect_pub_only,
8082 }
8183 }
8284
83- pub fn new_module ( db : & dyn HirDatabase , module : Module ) -> Box < [ FileSymbol ] > {
84- let mut symbol_collector = SymbolCollector :: new ( db) ;
85+ pub fn new_module (
86+ db : & dyn HirDatabase ,
87+ module : Module ,
88+ collect_pub_only : bool ,
89+ ) -> Box < [ FileSymbol ] > {
90+ let mut symbol_collector = SymbolCollector :: new ( db, collect_pub_only) ;
8591 symbol_collector. collect ( module) ;
8692 symbol_collector. finish ( )
8793 }
@@ -113,7 +119,11 @@ impl<'a> SymbolCollector<'a> {
113119 }
114120
115121 fn collect_from_module ( & mut self , module_id : ModuleId ) {
116- let push_decl = |this : & mut Self , def, name| {
122+ let collect_pub_only = self . collect_pub_only ;
123+ let push_decl = |this : & mut Self , def : ModuleDefId , name, vis| {
124+ if collect_pub_only && vis != Visibility :: Public {
125+ return ;
126+ }
117127 match def {
118128 ModuleDefId :: ModuleId ( id) => this. push_module ( id, name) ,
119129 ModuleDefId :: FunctionId ( id) => {
@@ -175,6 +185,9 @@ impl<'a> SymbolCollector<'a> {
175185 } ;
176186
177187 let mut push_import = |this : & mut Self , i : ImportId , name : & Name , def : ModuleDefId , vis| {
188+ if collect_pub_only && vis != Visibility :: Public {
189+ return ;
190+ }
178191 let source = import_child_source_cache
179192 . entry ( i. use_ )
180193 . or_insert_with ( || i. use_ . child_source ( this. db ) ) ;
@@ -209,6 +222,9 @@ impl<'a> SymbolCollector<'a> {
209222
210223 let push_extern_crate =
211224 |this : & mut Self , i : ExternCrateId , name : & Name , def : ModuleDefId , vis| {
225+ if collect_pub_only && vis != Visibility :: Public {
226+ return ;
227+ }
212228 let loc = i. lookup ( this. db ) ;
213229 let source = loc. source ( this. db ) ;
214230 let rename = source. value . rename ( ) . and_then ( |rename| rename. name ( ) ) ;
@@ -258,7 +274,7 @@ impl<'a> SymbolCollector<'a> {
258274 continue ;
259275 }
260276 // self is a declaration
261- push_decl ( self , def, name)
277+ push_decl ( self , def, name, vis )
262278 }
263279
264280 for ( name, Item { def, vis, import } ) in scope. macros ( ) {
@@ -271,7 +287,7 @@ impl<'a> SymbolCollector<'a> {
271287 continue ;
272288 }
273289 // self is a declaration
274- push_decl ( self , def . into ( ) , name)
290+ push_decl ( self , ModuleDefId :: MacroId ( def ) , name, vis )
275291 }
276292
277293 for ( name, Item { def, vis, import } ) in scope. values ( ) {
@@ -283,7 +299,7 @@ impl<'a> SymbolCollector<'a> {
283299 continue ;
284300 }
285301 // self is a declaration
286- push_decl ( self , def, name)
302+ push_decl ( self , def, name, vis )
287303 }
288304
289305 for const_id in scope. unnamed_consts ( ) {
@@ -304,6 +320,9 @@ impl<'a> SymbolCollector<'a> {
304320 }
305321
306322 fn collect_from_body ( & mut self , body_id : impl Into < DefWithBodyId > , name : Option < Name > ) {
323+ if self . collect_pub_only {
324+ return ;
325+ }
307326 let body_id = body_id. into ( ) ;
308327 let body = self . db . body ( body_id) ;
309328
@@ -330,6 +349,11 @@ impl<'a> SymbolCollector<'a> {
330349 ) ;
331350 self . with_container_name ( impl_name. as_deref ( ) . map ( Symbol :: intern) , |s| {
332351 for & ( ref name, assoc_item_id) in & impl_id. impl_items ( self . db ) . items {
352+ if s. collect_pub_only && s. db . assoc_visibility ( assoc_item_id) != Visibility :: Public
353+ {
354+ continue ;
355+ }
356+
333357 s. push_assoc_item ( assoc_item_id, name, None )
334358 }
335359 } )
0 commit comments