From dab01a05fb0300873728cedff8164f87804f19ef Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 2 May 2021 16:35:48 +0200 Subject: [PATCH] * Put crates list at all levels * Fix bug in module sidebar: the list of items was from the parent module --- src/librustdoc/clean/types.rs | 1 - src/librustdoc/html/layout.rs | 2 +- src/librustdoc/html/render/mod.rs | 67 ++++++++-------- src/librustdoc/html/render/print_item.rs | 2 +- src/librustdoc/html/static/main.js | 97 ++++++++++++------------ 5 files changed, 80 insertions(+), 89 deletions(-) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index de88e249b67..3ae516bd6df 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -526,7 +526,6 @@ impl Item { crate fn is_crate(&self) -> bool { self.is_mod() && self.def_id.as_real().map_or(false, |did| did.index == CRATE_DEF_INDEX) } - crate fn is_mod(&self) -> bool { self.type_() == ItemType::Module } diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index ec04c94dc11..75e4cee9e67 100644 --- a/src/librustdoc/html/layout.rs +++ b/src/librustdoc/html/layout.rs @@ -119,7 +119,7 @@ crate fn render( {after_content}\
+ data-search-js=\"{static_root_path}search{suffix}.js\">\ \ {extra_scripts}\ \ diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 507c5e175f9..a7f3d70e462 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -1703,7 +1703,7 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) { "
\

Version {}

\
", - Escape(version) + Escape(version), ); } } @@ -1713,9 +1713,10 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) { write!( buffer, "

See all {}'s items

", - it.name.as_ref().expect("crates always have a name") + it.name.as_ref().expect("crates always have a name"), ); } + match *it.kind { clean::StructItem(ref s) => sidebar_struct(cx, buffer, it, s), clean::TraitItem(ref t) => sidebar_trait(cx, buffer, it, t), @@ -1725,7 +1726,7 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) { clean::TypedefItem(_, _) => sidebar_typedef(cx, buffer, it), clean::ModuleItem(ref m) => sidebar_module(buffer, &m.items), clean::ForeignTypeItem => sidebar_foreign_type(cx, buffer, it), - _ => (), + _ => {} } // The sidebar is designed to display sibling functions, modules and @@ -1736,22 +1737,24 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) { // as much HTML as possible in order to allow non-JS-enabled browsers // to navigate the documentation (though slightly inefficiently). - buffer.write_str("

"); - for (i, name) in cx.current.iter().take(parentlen).enumerate() { - if i > 0 { - buffer.write_str("::"); + if !it.is_mod() { + buffer.write_str("

"); + for (i, name) in cx.current.iter().take(parentlen).enumerate() { + if i > 0 { + buffer.write_str("::"); + } + write!( + buffer, + "{}", + &cx.root_path()[..(cx.current.len() - i - 1) * 3], + *name + ); } - write!( - buffer, - "{}", - &cx.root_path()[..(cx.current.len() - i - 1) * 3], - *name - ); + buffer.write_str("

"); } - buffer.write_str("

"); // Sidebar refers to the enclosing module, not this module. - let relpath = if it.is_mod() { "../" } else { "" }; + let relpath = if it.is_mod() && parentlen != 0 { "./" } else { "" }; write!( buffer, "
\ @@ -1760,17 +1763,7 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) { ty = it.type_(), path = relpath ); - - if parentlen == 0 { - write!( - buffer, - "", - relpath, cx.shared.resource_suffix - ); - } else { - write!(buffer, "", relpath); - } - + write!(buffer, "", relpath); // Closes sidebar-elems div. buffer.write_str("
"); } @@ -2278,8 +2271,8 @@ fn sidebar_enum(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, e: &clean: } } -fn item_ty_to_strs(ty: &ItemType) -> (&'static str, &'static str) { - match *ty { +fn item_ty_to_strs(ty: ItemType) -> (&'static str, &'static str) { + match ty { ItemType::ExternCrate | ItemType::Import => ("reexports", "Re-exports"), ItemType::Module => ("modules", "Modules"), ItemType::Struct => ("structs", "Structs"), @@ -2311,10 +2304,14 @@ fn item_ty_to_strs(ty: &ItemType) -> (&'static str, &'static str) { fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) { let mut sidebar = String::new(); + // Re-exports are handled a bit differently because they can be extern crates or imports. if items.iter().any(|it| { - it.type_() == ItemType::ExternCrate || (it.type_() == ItemType::Import && !it.is_stripped()) + it.name.is_some() + && (it.type_() == ItemType::ExternCrate + || (it.type_() == ItemType::Import && !it.is_stripped())) }) { - sidebar.push_str("
  • Re-exports
  • "); + let (id, name) = item_ty_to_strs(ItemType::Import); + sidebar.push_str(&format!("
  • {}
  • ", id, name)); } // ordering taken from item_module, reorder, where it prioritized elements in a certain order @@ -2341,13 +2338,9 @@ fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) { ItemType::ForeignType, ItemType::Keyword, ] { - if items.iter().any(|it| !it.is_stripped() && it.type_() == myty) { - let (short, name) = item_ty_to_strs(&myty); - sidebar.push_str(&format!( - "
  • {name}
  • ", - id = short, - name = name - )); + if items.iter().any(|it| !it.is_stripped() && it.type_() == myty && it.name.is_some()) { + let (id, name) = item_ty_to_strs(myty); + sidebar.push_str(&format!("
  • {}
  • ", id, name)); } } diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index e06168c708c..069bec4e1a5 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -262,7 +262,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl w.write_str(""); } curty = myty; - let (short, name) = item_ty_to_strs(&myty.unwrap()); + let (short, name) = item_ty_to_strs(myty.unwrap()); write!( w, "

    \ diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index a96b0c87108..353c7125fc7 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -561,40 +561,36 @@ function hideThemeButtonState() { } }()); - function addSidebarCrates(crates) { - // Draw a convenient sidebar of known crates if we have a listing - if (window.rootPath === "../" || window.rootPath === "./") { - var sidebar = document.getElementsByClassName("sidebar-elems")[0]; - if (sidebar) { - var div = document.createElement("div"); - div.className = "block crate"; - div.innerHTML = "

    Crates

    "; - var ul = document.createElement("ul"); - div.appendChild(ul); - - for (var i = 0; i < crates.length; ++i) { - var klass = "crate"; - if (window.rootPath !== "./" && crates[i] === window.currentCrate) { - klass += " current"; - } - var link = document.createElement("a"); - link.href = window.rootPath + crates[i] + "/index.html"; - link.className = klass; - link.textContent = crates[i]; - - var li = document.createElement("li"); - li.appendChild(link); - ul.appendChild(li); - } - sidebar.appendChild(div); - } - } - } - // delayed sidebar rendering. window.initSidebarItems = function(items) { var sidebar = document.getElementsByClassName("sidebar-elems")[0]; var current = window.sidebarCurrent; + var isModule = hasClass(document.body, "mod"); + + function addSidebarCrates(crates) { + // Draw a convenient sidebar of known crates if we have a listing + var div = document.createElement("div"); + div.className = "block crate"; + div.innerHTML = "

    Crates

    "; + var ul = document.createElement("ul"); + div.appendChild(ul); + + for (var i = 0; i < crates.length; ++i) { + var klass = "crate"; + if (window.rootPath !== "./" && crates[i] === window.currentCrate) { + klass += " current"; + } + var link = document.createElement("a"); + link.href = window.rootPath + crates[i] + "/index.html"; + link.className = klass; + link.textContent = crates[i]; + + var li = document.createElement("li"); + li.appendChild(link); + ul.appendChild(li); + } + sidebar.appendChild(div); + } function block(shortty, longty) { var filtered = items[shortty]; @@ -634,28 +630,31 @@ function hideThemeButtonState() { ul.appendChild(li); } div.appendChild(ul); - if (sidebar) { - sidebar.appendChild(div); - } + sidebar.appendChild(div); } - block("primitive", "Primitive Types"); - block("mod", "Modules"); - block("macro", "Macros"); - block("struct", "Structs"); - block("enum", "Enums"); - block("union", "Unions"); - block("constant", "Constants"); - block("static", "Statics"); - block("trait", "Traits"); - block("fn", "Functions"); - block("type", "Type Definitions"); - block("foreigntype", "Foreign Types"); - block("keyword", "Keywords"); - block("traitalias", "Trait Aliases"); + if (sidebar) { + if (!isModule) { + block("primitive", "Primitive Types"); + block("mod", "Modules"); + block("macro", "Macros"); + block("struct", "Structs"); + block("enum", "Enums"); + block("union", "Unions"); + block("constant", "Constants"); + block("static", "Statics"); + block("trait", "Traits"); + block("fn", "Functions"); + block("type", "Type Definitions"); + block("foreigntype", "Foreign Types"); + block("keyword", "Keywords"); + block("traitalias", "Trait Aliases"); + } - // `crates{version}.js` should always be loaded before this script, so we can use it safely. - addSidebarCrates(window.ALL_CRATES); + // `crates{version}.js` should always be loaded before this script, so we can use + // it safely. + addSidebarCrates(window.ALL_CRATES); + } }; window.register_implementors = function(imp) {