diff --git a/forc-plugins/forc-doc/src/doc/mod.rs b/forc-plugins/forc-doc/src/doc/mod.rs index 801e7bfed96..a60a2617d56 100644 --- a/forc-plugins/forc-doc/src/doc/mod.rs +++ b/forc-plugins/forc-doc/src/doc/mod.rs @@ -79,48 +79,54 @@ impl Documentation { let mut impl_trait_vec: Vec = Vec::new(); let mut inherent_impl_vec: Vec = Vec::new(); - match doc.item_body.ty_decl { + let decl_name = match doc.item_body.ty_decl { TyDecl::StructDecl(ref decl) => { - for (impl_trait, module_info) in impl_traits.iter_mut() { - let struct_decl = engines.de().get_struct(&decl.decl_id); - let struct_name = struct_decl.name().as_str(); - - // Check if this implementation is for this struct. - if struct_name == impl_trait.implementing_for.span.as_str() { - let module_info_override = if let Some(decl_module_info) = - trait_decls.get(&impl_trait.trait_name.suffix) - { - Some(decl_module_info.module_prefixes.clone()) - } else { - impl_trait.trait_name = impl_trait - .trait_name - .to_fullpath(engines, &typed_program.root.namespace); - None - }; - - if struct_name == impl_trait.trait_name.suffix.span().as_str() { - // If the trait name is the same as the struct name, it's an inherent implementation. - inherent_impl_vec.push(DocImplTrait { - impl_for_module: module_info.clone(), - impl_trait: impl_trait.clone(), - module_info_override: None, - }); - } else { - // Otherwise, it's an implementation for a trait. - impl_trait_vec.push(DocImplTrait { - impl_for_module: module_info.clone(), - impl_trait: impl_trait.clone(), - module_info_override, - }); - } + let struct_decl = engines.de().get_struct(&decl.decl_id); + Some(struct_decl.name().to_string()) + } + TyDecl::EnumDecl(ref decl) => { + let enum_decl = engines.de().get_enum(&decl.decl_id); + Some(enum_decl.name().to_string()) + } + _ => None, + }; + + if let Some(decl_name) = decl_name { + for (impl_trait, module_info) in impl_traits.iter_mut() { + // Check if this implementation is for this struct/enum. + if decl_name.as_str() == impl_trait.implementing_for.span.as_str() { + let module_info_override = if let Some(decl_module_info) = + trait_decls.get(&impl_trait.trait_name.suffix) + { + Some(decl_module_info.module_prefixes.clone()) + } else { + impl_trait.trait_name = impl_trait + .trait_name + .to_fullpath(engines, &typed_program.root.namespace); + None + }; + + if decl_name.as_str() == impl_trait.trait_name.suffix.span().as_str() { + // If the trait name is the same as the declaration's name, it's an inherent implementation. + inherent_impl_vec.push(DocImplTrait { + impl_for_module: module_info.clone(), + impl_trait: impl_trait.clone(), + module_info_override: None, + }); + } else { + // Otherwise, it's an implementation for a trait. + impl_trait_vec.push(DocImplTrait { + impl_for_module: module_info.clone(), + impl_trait: impl_trait.clone(), + module_info_override, + }); } } } - _ => continue, } if !impl_trait_vec.is_empty() { - doc.item_body.item_context.impl_traits = Some(impl_trait_vec.clone()); + doc.item_body.item_context.impl_traits = Some(impl_trait_vec); } if !inherent_impl_vec.is_empty() { diff --git a/forc-plugins/forc-doc/src/render/item/context.rs b/forc-plugins/forc-doc/src/render/item/context.rs index f0aff1b2ce1..fc673bf70be 100644 --- a/forc-plugins/forc-doc/src/render/item/context.rs +++ b/forc-plugins/forc-doc/src/render/item/context.rs @@ -350,6 +350,42 @@ impl ItemContext { } } } + + if let Some(inherent_impls) = &self.inherent_impls { + let mut doc_links = Vec::new(); + for inherent_impl in inherent_impls { + for item in &inherent_impl.impl_trait.items { + if let TyTraitItem::Fn(item_fn) = item { + let method_name = item_fn.name().to_string(); + doc_links.push(DocLink { + name: method_name.clone(), + module_info: inherent_impl.impl_for_module.clone(), + html_filename: format!("{}method.{}", IDENTITY, method_name), + preview_opt: None, + }) + } + } + } + links.insert(BlockTitle::ImplMethods, doc_links); + } + + if let Some(impl_traits) = &self.impl_traits { + let doc_links = impl_traits + .iter() + .map(|impl_trait| DocLink { + name: impl_trait.impl_trait.trait_name.suffix.as_str().to_string(), + module_info: impl_trait.impl_for_module.clone(), + html_filename: format!( + "{}impl-{}", + IDENTITY, + impl_trait.impl_trait.trait_name.suffix.as_str() + ), + preview_opt: None, + }) + .collect(); + links.insert(BlockTitle::ImplTraits, doc_links); + } + DocLinks { style: DocStyle::Item { title: None, @@ -407,11 +443,11 @@ impl Renderable for ItemContext { : Raw(context); } @ if !inherent_impls.is_empty() { - h2(id="inherent-implementations", class="small-section-header") { + h2(id="methods", class="small-section-header") { : "Implementations"; - a(href=format!("{IDENTITY}inherent-implementations"), class="anchor"); + a(href=format!("{IDENTITY}methods"), class="anchor"); } - div(id="inherent-implementations-list") { + div(id="methods-list") { @ for inherent_impl in inherent_impls { : inherent_impl; } @@ -472,17 +508,17 @@ impl Renderable for DocImplTrait { a(href=format!("{IDENTITY}impl-{}", trait_name.suffix.as_str()), class="anchor"); h3(class="code-header in-band") { : "impl "; - @ if no_deps && is_external_item { - : trait_name.suffix.as_str(); - } else { - a(class="trait", href=format!("{trait_link}")) { + @ if !is_inherent { + @ if no_deps && is_external_item { : trait_name.suffix.as_str(); + } else { + a(class="trait", href=format!("{trait_link}")) { + : trait_name.suffix.as_str(); + } } - } - @ if !is_inherent { : " for "; - : implementing_for.span.as_str(); } + : implementing_for.span.as_str(); } } } diff --git a/forc-plugins/forc-doc/src/render/sidebar.rs b/forc-plugins/forc-doc/src/render/sidebar.rs index 4894575a964..a0d6cdba793 100644 --- a/forc-plugins/forc-doc/src/render/sidebar.rs +++ b/forc-plugins/forc-doc/src/render/sidebar.rs @@ -132,13 +132,18 @@ impl Renderable for Sidebar { } _ => box_html! { div(class="sidebar-elems") { - section { - div(class="block") { - ul { - @ for (title, _) in self.nav.links { + @ for (title, doc_links) in self.nav.links { + section { + h3 { + a(href=format!("{}{}", IDENTITY, title.html_title_string())) { + : title.as_str(); + } + } + ul(class="block method") { + @ for doc_link in doc_links { li { - a(href=format!("{}{}", IDENTITY, title.html_title_string())) { - : title.as_str(); + a(href=format!("{}", doc_link.html_filename)) { + : doc_link.name; } } } diff --git a/forc-plugins/forc-doc/src/render/title.rs b/forc-plugins/forc-doc/src/render/title.rs index 8afa7deafb7..e08219fbf94 100644 --- a/forc-plugins/forc-doc/src/render/title.rs +++ b/forc-plugins/forc-doc/src/render/title.rs @@ -18,7 +18,10 @@ pub enum BlockTitle { Fields, Variants, RequiredMethods, + ImplMethods, + ImplTraits, } + impl BlockTitle { pub fn as_str(&self) -> &str { match self { @@ -33,6 +36,8 @@ impl BlockTitle { Self::Fields => "Fields", Self::Variants => "Variants", Self::RequiredMethods => "Required Methods", + Self::ImplMethods => "Methods", + Self::ImplTraits => "Trait Implementations", } } pub fn item_title_str(&self) -> &str { @@ -48,6 +53,8 @@ impl BlockTitle { Self::Fields => "Fields", Self::Variants => "Variants", Self::RequiredMethods => "Required Methods", + Self::ImplMethods => "Methods", + Self::ImplTraits => "Trait Implementations", } } pub fn class_title_str(&self) -> &str { diff --git a/forc-plugins/forc-doc/src/static.files/swaydoc.css b/forc-plugins/forc-doc/src/static.files/swaydoc.css index 456264486ae..9ed50926ad1 100644 --- a/forc-plugins/forc-doc/src/static.files/swaydoc.css +++ b/forc-plugins/forc-doc/src/static.files/swaydoc.css @@ -489,8 +489,8 @@ a#all-types:active { .block a, h2.location a { display: block; - padding: 0.25rem; - margin-left: 2em; + padding: 0.5rem; + margin-left: .25rem; text-overflow: ellipsis; overflow: hidden; } @@ -505,14 +505,18 @@ h2.location a { .sidebar h3 { font-size: 1.125rem; font-weight: 500; - padding: 0; + padding: 1rem 1rem 0 0; margin: 0; } -.sidebar-elems .block { - margin-bottom: 2em; +.sidebar-elems, +.sidebar > .version, +.sidebar > h2 { + padding-left: 24px; } -.sidebar-elems .block li a { - white-space: nowrap; +ul.block, .block li { + padding: 0; + margin: 0; + list-style: none; } .mobile-topbar { display: none; diff --git a/forc-plugins/forc-doc/src/tests/expects/impl_trait/mod.rs b/forc-plugins/forc-doc/src/tests/expects/impl_trait/mod.rs index 00f01c8df17..645b5b4e03a 100644 --- a/forc-plugins/forc-doc/src/tests/expects/impl_trait/mod.rs +++ b/forc-plugins/forc-doc/src/tests/expects/impl_trait/mod.rs @@ -38,7 +38,7 @@ fn test_impl_traits_default() { &doc_path, project_name, &expect![[ - r##"Bar in bar - Sway
pub struct Bar {}

Implementations

fn foo_bar()

Trait Implementations

fn foo()

something more about foo();

+ r##"Bar in bar - Sway
pub struct Bar {}

Implementations

fn foo_bar()

Trait Implementations

fn foo()

something more about foo();

fn add(self, other: Self) -> Self

fn subtract(self, other: Self) -> Self

"## ]], ); @@ -121,7 +121,7 @@ fn test_impl_traits_no_deps() { &doc_path, project_name, &expect![[ - r##"Bar in bar - Sway
pub struct Bar {}

Implementations

fn foo_bar()

Trait Implementations

fn foo()

something more about foo();

+ r##"Bar in bar - Sway
pub struct Bar {}

Implementations

fn foo_bar()

Trait Implementations

fn foo()

something more about foo();

fn add(self, other: Self) -> Self

fn subtract(self, other: Self) -> Self

"## ]], );