1
1
// TODO(drknzz): Remove when not needed.
2
2
#![ allow( dead_code) ]
3
3
4
+ use cairo_lang_semantic:: items:: visibility;
5
+ use cairo_lang_utils:: Upcast ;
4
6
use itertools:: Itertools ;
5
7
use serde:: Serialize ;
6
8
@@ -27,13 +29,34 @@ pub struct Crate {
27
29
}
28
30
29
31
impl Crate {
30
- pub fn new ( db : & ScarbDocDatabase , crate_id : CrateId ) -> Self {
32
+ pub fn new ( db : & ScarbDocDatabase , crate_id : CrateId , include_private_items : bool ) -> Self {
33
+ let root_module_id = ModuleId :: CrateRoot ( crate_id) ;
31
34
Self {
32
- root_module : Module :: new ( db, ModuleId :: CrateRoot ( crate_id ) ) ,
35
+ root_module : Module :: new ( db, root_module_id , root_module_id , include_private_items ) ,
33
36
}
34
37
}
35
38
}
36
39
40
+ fn is_visible_in_module (
41
+ db : & ScarbDocDatabase ,
42
+ root_module_id : ModuleId ,
43
+ element_id : & dyn TopLevelLanguageElementId ,
44
+ ) -> bool {
45
+ let cotaining_module_id = element_id. parent_module ( db) ;
46
+ match db
47
+ . module_item_info_by_name ( cotaining_module_id, element_id. name ( db. upcast ( ) ) )
48
+ . unwrap ( )
49
+ {
50
+ Some ( module_item_info) => visibility:: peek_visible_in (
51
+ db,
52
+ module_item_info. visibility ,
53
+ cotaining_module_id,
54
+ root_module_id,
55
+ ) ,
56
+ None => false ,
57
+ }
58
+ }
59
+
37
60
#[ derive( Serialize , Clone ) ]
38
61
pub struct Module {
39
62
#[ serde( skip) ]
@@ -54,7 +77,12 @@ pub struct Module {
54
77
}
55
78
56
79
impl Module {
57
- pub fn new ( db : & ScarbDocDatabase , module_id : ModuleId ) -> Self {
80
+ pub fn new (
81
+ db : & ScarbDocDatabase ,
82
+ root_module_id : ModuleId ,
83
+ module_id : ModuleId ,
84
+ include_private_items : bool ,
85
+ ) -> Self {
58
86
// FIXME(#1438): compiler doesn't support fetching root crate doc
59
87
let item_data = match module_id {
60
88
ModuleId :: CrateRoot ( crate_id) => ItemData {
@@ -70,70 +98,95 @@ impl Module {
70
98
) ,
71
99
} ;
72
100
101
+ let should_include_item = |id : & dyn TopLevelLanguageElementId | {
102
+ if include_private_items {
103
+ return true ;
104
+ }
105
+ is_visible_in_module ( db, root_module_id, id)
106
+ } ;
107
+
73
108
let module_constants = db. module_constants ( module_id) . unwrap ( ) ;
74
109
let constants = module_constants
75
110
. iter ( )
111
+ . filter ( |( id, _) | should_include_item ( * id) )
76
112
. map ( |( id, _) | Constant :: new ( db, * id) )
77
113
. collect ( ) ;
78
114
79
115
let module_free_functions = db. module_free_functions ( module_id) . unwrap ( ) ;
80
116
let free_functions = module_free_functions
81
117
. iter ( )
118
+ . filter ( |( id, _) | should_include_item ( * id) )
82
119
. map ( |( id, _) | FreeFunction :: new ( db, * id) )
83
120
. collect ( ) ;
84
121
85
122
let module_structs = db. module_structs ( module_id) . unwrap ( ) ;
86
123
let structs = module_structs
87
124
. iter ( )
88
- . map ( |( id, _) | Struct :: new ( db, * id) )
125
+ . filter ( |( id, _) | should_include_item ( * id) )
126
+ . map ( |( id, _) | Struct :: new ( db, * id, root_module_id, include_private_items) )
89
127
. collect ( ) ;
90
128
91
129
let module_enums = db. module_enums ( module_id) . unwrap ( ) ;
92
130
let enums = module_enums
93
131
. iter ( )
132
+ . filter ( |( id, _) | should_include_item ( * id) )
94
133
. map ( |( id, _) | Enum :: new ( db, * id) )
95
134
. collect ( ) ;
96
135
97
136
let module_type_aliases = db. module_type_aliases ( module_id) . unwrap ( ) ;
98
137
let type_aliases = module_type_aliases
99
138
. iter ( )
139
+ . filter ( |( id, _) | should_include_item ( * id) )
100
140
. map ( |( id, _) | TypeAlias :: new ( db, * id) )
101
141
. collect ( ) ;
102
142
103
143
let module_impl_aliases = db. module_impl_aliases ( module_id) . unwrap ( ) ;
104
144
let impl_aliases = module_impl_aliases
105
145
. iter ( )
146
+ . filter ( |( id, _) | should_include_item ( * id) )
106
147
. map ( |( id, _) | ImplAlias :: new ( db, * id) )
107
148
. collect ( ) ;
108
149
109
150
let module_traits = db. module_traits ( module_id) . unwrap ( ) ;
110
151
let traits = module_traits
111
152
. iter ( )
153
+ . filter ( |( id, _) | should_include_item ( * id) )
112
154
. map ( |( id, _) | Trait :: new ( db, * id) )
113
155
. collect ( ) ;
114
156
115
157
let module_impls = db. module_impls ( module_id) . unwrap ( ) ;
116
158
let impls = module_impls
117
159
. iter ( )
160
+ . filter ( |( id, _) | should_include_item ( * id) )
118
161
. map ( |( id, _) | Impl :: new ( db, * id) )
119
162
. collect ( ) ;
120
163
121
164
let module_extern_types = db. module_extern_types ( module_id) . unwrap ( ) ;
122
165
let extern_types = module_extern_types
123
166
. iter ( )
167
+ . filter ( |( id, _) | should_include_item ( * id) )
124
168
. map ( |( id, _) | ExternType :: new ( db, * id) )
125
169
. collect ( ) ;
126
170
127
171
let module_extern_functions = db. module_extern_functions ( module_id) . unwrap ( ) ;
128
172
let extern_functions = module_extern_functions
129
173
. iter ( )
174
+ . filter ( |( id, _) | should_include_item ( * id) )
130
175
. map ( |( id, _) | ExternFunction :: new ( db, * id) )
131
176
. collect ( ) ;
132
177
133
178
let module_submodules = db. module_submodules ( module_id) . unwrap ( ) ;
134
179
let submodules = module_submodules
135
180
. iter ( )
136
- . map ( |( id, _) | Self :: new ( db, ModuleId :: Submodule ( * id) ) )
181
+ . filter ( |( id, _) | should_include_item ( * id) )
182
+ . map ( |( id, _) | {
183
+ Self :: new (
184
+ db,
185
+ root_module_id,
186
+ ModuleId :: Submodule ( * id) ,
187
+ include_private_items,
188
+ )
189
+ } )
137
190
. collect ( ) ;
138
191
139
192
Self {
@@ -253,7 +306,12 @@ pub struct Struct {
253
306
}
254
307
255
308
impl Struct {
256
- pub fn new ( db : & ScarbDocDatabase , id : StructId ) -> Self {
309
+ pub fn new (
310
+ db : & ScarbDocDatabase ,
311
+ id : StructId ,
312
+ root_module_id : ModuleId ,
313
+ include_private_items : bool ,
314
+ ) -> Self {
257
315
let members = db. struct_members ( id) . unwrap ( ) ;
258
316
259
317
let item_data = ItemData :: new_without_signature (
@@ -264,6 +322,10 @@ impl Struct {
264
322
265
323
let members = members
266
324
. iter ( )
325
+ . filter ( |( _, semantic_member) | {
326
+ include_private_items
327
+ || is_visible_in_module ( db, root_module_id, & semantic_member. id )
328
+ } )
267
329
. map ( |( _name, semantic_member) | {
268
330
Member :: new ( db, semantic_member. id , item_data. full_path . clone ( ) )
269
331
} )
0 commit comments