Skip to content

Commit 9c9625a

Browse files
authored
feat: exclude pub(crate) from docs (#1908)
commit-id:0213c5ab --- **Stack**: - #1909 - #1908⚠️ *Part of a stack created by [spr](https://github.com/ejoffe/spr). Do not merge manually using the UI - doing so may have unexpected results.*
1 parent f1ae7b1 commit 9c9625a

File tree

4 files changed

+55
-859
lines changed

4 files changed

+55
-859
lines changed

extensions/scarb-doc/src/types.rs

+21-58
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use cairo_lang_diagnostics::{DiagnosticAdded, Maybe};
55
use cairo_lang_doc::parser::DocumentationCommentToken;
66
use cairo_lang_semantic::items::functions::GenericFunctionId;
77
use cairo_lang_semantic::items::us::SemanticUseEx;
8-
use cairo_lang_semantic::items::visibility::{self, Visibility};
8+
use cairo_lang_semantic::items::visibility::Visibility;
99
use cairo_lang_semantic::resolve::ResolvedGenericItem;
1010
use cairo_lang_syntax::node::helpers::QueryAttrs;
1111
use cairo_lang_utils::{LookupIntern, Upcast};
@@ -46,24 +46,15 @@ impl Crate {
4646
include_private_items: bool,
4747
) -> Maybe<Self> {
4848
let root_module_id = ModuleId::CrateRoot(crate_id);
49-
let root_module = Module::new(db, root_module_id, root_module_id, include_private_items)?;
49+
let root_module = Module::new(db, root_module_id, include_private_items)?;
5050
Ok(Self { root_module })
5151
}
5252
}
5353

54-
fn is_visible_in_module(
55-
db: &ScarbDocDatabase,
56-
root_module_id: ModuleId,
57-
element_id: &dyn TopLevelLanguageElementId,
58-
) -> Maybe<bool> {
59-
let cotaining_module_id = element_id.parent_module(db);
60-
match db.module_item_info_by_name(cotaining_module_id, element_id.name(db.upcast()))? {
61-
Some(module_item_info) => Ok(visibility::peek_visible_in(
62-
db,
63-
module_item_info.visibility,
64-
cotaining_module_id,
65-
root_module_id,
66-
)),
54+
fn is_public(db: &ScarbDocDatabase, element_id: &dyn TopLevelLanguageElementId) -> Maybe<bool> {
55+
let containing_module_id = element_id.parent_module(db);
56+
match db.module_item_info_by_name(containing_module_id, element_id.name(db.upcast()))? {
57+
Some(module_item_info) => Ok(matches!(module_item_info.visibility, Visibility::Public)),
6758
None => Ok(false),
6859
}
6960
}
@@ -180,7 +171,6 @@ impl Module {
180171
pub fn new(
181172
db: &ScarbDocDatabase,
182173
module_id: ModuleId,
183-
root_module_id: ModuleId,
184174
include_private_items: bool,
185175
) -> Maybe<Self> {
186176
let item_data = match module_id {
@@ -195,10 +185,8 @@ impl Module {
195185
let should_include_item = |id: &dyn TopLevelLanguageElementId| {
196186
let syntax_node = id.stable_location(db.upcast()).syntax_node(db.upcast());
197187

198-
Ok(
199-
(include_private_items || is_visible_in_module(db, root_module_id, id)?)
200-
&& !is_doc_hidden_attr(db, &syntax_node),
201-
)
188+
Ok((include_private_items || is_public(db, id)?)
189+
&& !is_doc_hidden_attr(db, &syntax_node))
202190
};
203191

204192
let module_pubuses = ModulePubUses::new(db, module_id);
@@ -224,7 +212,7 @@ impl Module {
224212
let structs = filter_map_item_id_to_item(
225213
chain!(module_structs.keys(), module_pubuses.use_structs.iter()),
226214
should_include_item,
227-
|id| Struct::new(db, *id, root_module_id, include_private_items),
215+
|id| Struct::new(db, *id, include_private_items),
228216
)?;
229217

230218
let module_enums = db.module_enums(module_id)?;
@@ -340,27 +328,13 @@ impl Module {
340328
module_pubuses.use_submodules.iter()
341329
),
342330
should_include_item,
343-
|id| {
344-
Module::new(
345-
db,
346-
ModuleId::Submodule(*id),
347-
root_module_id,
348-
include_private_items,
349-
)
350-
},
331+
|id| Module::new(db, ModuleId::Submodule(*id), include_private_items),
351332
)?;
352333

353334
let reexported_crates_as_modules: Vec<Module> = module_pubuses
354335
.use_crates
355336
.iter()
356-
.map(|id| {
357-
Module::new(
358-
db,
359-
ModuleId::CrateRoot(*id),
360-
root_module_id,
361-
include_private_items,
362-
)
363-
})
337+
.map(|id| Module::new(db, ModuleId::CrateRoot(*id), include_private_items))
364338
.collect::<Maybe<_>>()?;
365339

366340
submodules.extend(reexported_crates_as_modules);
@@ -617,12 +591,7 @@ pub struct Struct {
617591
}
618592

619593
impl Struct {
620-
pub fn new(
621-
db: &ScarbDocDatabase,
622-
id: StructId,
623-
root_module_id: ModuleId,
624-
include_private_items: bool,
625-
) -> Maybe<Self> {
594+
pub fn new(db: &ScarbDocDatabase, id: StructId, include_private_items: bool) -> Maybe<Self> {
626595
let members = db.struct_members(id)?;
627596

628597
let item_data = ItemData::new(
@@ -633,21 +602,15 @@ impl Struct {
633602
let members = members
634603
.iter()
635604
.filter_map(|(_, semantic_member)| {
636-
match is_visible_in_module(db, root_module_id, &semantic_member.id) {
637-
Ok(visible) => {
638-
let syntax_node = &semantic_member
639-
.id
640-
.stable_location(db.upcast())
641-
.syntax_node(db.upcast());
642-
if (include_private_items || visible)
643-
&& !is_doc_hidden_attr(db, syntax_node)
644-
{
645-
Some(Ok(Member::new(db, semantic_member.id)))
646-
} else {
647-
None
648-
}
649-
}
650-
Err(e) => Some(Err(e)),
605+
let visible = matches!(semantic_member.visibility, Visibility::Public);
606+
let syntax_node = &semantic_member
607+
.id
608+
.stable_location(db.upcast())
609+
.syntax_node(db.upcast());
610+
if (include_private_items || visible) && !is_doc_hidden_attr(db, syntax_node) {
611+
Some(Ok(Member::new(db, semantic_member.id)))
612+
} else {
613+
None
651614
}
652615
})
653616
.collect::<Maybe<Vec<_>>>()?;

extensions/scarb-doc/tests/code/code_5.cairo

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub mod PublicParent {
8888
private_field: felt252
8989
}
9090

91-
/// Parent publi crate struct
91+
/// Parent public crate struct
9292
pub(crate) struct PublicCrateParentStructure {
9393
/// Public struct field
9494
pub public_field: felt252,

0 commit comments

Comments
 (0)