From ac398a33a9fa982739f8476a80c1ee3f442d1bd9 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 4 Nov 2017 20:45:12 +0100 Subject: [PATCH] Add more elements in the sidebar --- src/librustdoc/html/render.rs | 170 ++++++++++++++++++++++++------ src/test/rustdoc/sidebar-items.rs | 59 +++++++++++ 2 files changed, 198 insertions(+), 31 deletions(-) create mode 100644 src/test/rustdoc/sidebar-items.rs diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 88e1f780d0342..eb59c57603aae 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -2498,7 +2498,7 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, if !foreign.is_empty() { write!(w, " -

+

Implementations on Foreign Types

")?; @@ -3591,7 +3591,8 @@ fn sidebar_assoc_items(it: &clean::Item) -> String { let mut links = HashSet::new(); let ret = v.iter() .filter_map(|i| if let Some(ref i) = i.inner_impl().trait_ { - let out = format!("{:#}", i).replace("<", "<").replace(">", ">"); + let i_display = format!("{:#}", i); + let out = Escape(&i_display); let encoded = small_url_encode(&format!("{:#}", i)); let generated = format!("{}", encoded, out); if !links.contains(&generated) && links.insert(generated.clone()) { @@ -3617,11 +3618,12 @@ fn sidebar_assoc_items(it: &clean::Item) -> String { fn sidebar_struct(fmt: &mut fmt::Formatter, it: &clean::Item, s: &clean::Struct) -> fmt::Result { let mut sidebar = String::new(); + let fields = get_struct_fields_name(&s.fields); - if s.fields.iter() - .any(|f| if let clean::StructFieldItem(..) = f.inner { true } else { false }) { + if !fields.is_empty() { if let doctree::Plain = s.struct_type { - sidebar.push_str("
  • Fields
  • "); + sidebar.push_str(&format!("Fields\ +
    {}
    ", fields)); } } @@ -3633,40 +3635,122 @@ fn sidebar_struct(fmt: &mut fmt::Formatter, it: &clean::Item, Ok(()) } +fn extract_for_impl_name(item: &clean::Item) -> Option<(String, String)> { + match item.inner { + clean::ItemEnum::ImplItem(ref i) => { + if let Some(ref trait_) = i.trait_ { + Some((format!("{:#}", i.for_), format!("{:#}", trait_))) + } else { + None + } + }, + _ => None, + } +} + fn sidebar_trait(fmt: &mut fmt::Formatter, it: &clean::Item, t: &clean::Trait) -> fmt::Result { let mut sidebar = String::new(); - let has_types = t.items.iter().any(|m| m.is_associated_type()); - let has_consts = t.items.iter().any(|m| m.is_associated_const()); - let has_required = t.items.iter().any(|m| m.is_ty_method()); - let has_provided = t.items.iter().any(|m| m.is_method()); + let types = t.items + .iter() + .filter_map(|m| { + match m.name { + Some(ref name) if m.is_associated_type() => { + Some(format!("{name}", + name=name)) + } + _ => None, + } + }) + .collect::(); + let consts = t.items + .iter() + .filter_map(|m| { + match m.name { + Some(ref name) if m.is_associated_const() => { + Some(format!("{name}", + name=name)) + } + _ => None, + } + }) + .collect::(); + let required = t.items + .iter() + .filter_map(|m| { + match m.name { + Some(ref name) if m.is_ty_method() => { + Some(format!("{name}", + name=name)) + } + _ => None, + } + }) + .collect::(); + let provided = t.items + .iter() + .filter_map(|m| { + match m.name { + Some(ref name) if m.is_method() => { + Some(format!("{name}", name=name)) + } + _ => None, + } + }) + .collect::(); - if has_types { - sidebar.push_str("
  • Associated Types
  • "); + if !types.is_empty() { + sidebar.push_str(&format!("\ + Associated Types
    {}
    ", + types)); } - if has_consts { - sidebar.push_str("
  • Associated Constants
  • "); + if !consts.is_empty() { + sidebar.push_str(&format!("\ + Associated Constants
    {}
    ", + consts)); } - if has_required { - sidebar.push_str("
  • Required Methods
  • "); + if !required.is_empty() { + sidebar.push_str(&format!("\ + Required Methods
    {}
    ", + required)); } - if has_provided { - sidebar.push_str("
  • Provided Methods
  • "); + if !provided.is_empty() { + sidebar.push_str(&format!("\ + Provided Methods
    {}
    ", + provided)); } let c = cache(); if let Some(implementors) = c.implementors.get(&it.def_id) { - if implementors.iter().any(|i| i.impl_.for_.def_id() - .map_or(false, |d| !c.paths.contains_key(&d))) - { - sidebar.push_str("
  • \ - Implementations on Foreign Types
  • "); - } - } - - sidebar.push_str("
  • Implementors
  • "); + let res = implementors.iter() + .filter(|i| i.impl_.for_.def_id() + .map_or(false, |d| !c.paths.contains_key(&d))) + .filter_map(|i| { + if let Some(item) = implementor2item(&c, i) { + match extract_for_impl_name(&item) { + Some((ref name, ref url)) => { + Some(format!("{}", + small_url_encode(url), + Escape(name))) + } + _ => None, + } + } else { + None + } + }) + .collect::(); + if !res.is_empty() { + sidebar.push_str(&format!("\ + Implementations on Foreign Types
    {}
    ", + res)); + } + } + + sidebar.push_str("Implementors"); sidebar.push_str(&sidebar_assoc_items(it)); @@ -3693,13 +3777,29 @@ fn sidebar_typedef(fmt: &mut fmt::Formatter, it: &clean::Item, Ok(()) } +fn get_struct_fields_name(fields: &[clean::Item]) -> String { + fields.iter() + .filter(|f| if let clean::StructFieldItem(..) = f.inner { + true + } else { + false + }) + .filter_map(|f| match f.name { + Some(ref name) => Some(format!("\ + {name}", name=name)), + _ => None, + }) + .collect() +} + fn sidebar_union(fmt: &mut fmt::Formatter, it: &clean::Item, u: &clean::Union) -> fmt::Result { let mut sidebar = String::new(); + let fields = get_struct_fields_name(&u.fields); - if u.fields.iter() - .any(|f| if let clean::StructFieldItem(..) = f.inner { true } else { false }) { - sidebar.push_str("
  • Fields
  • "); + if !fields.is_empty() { + sidebar.push_str(&format!("Fields\ +
    {}
    ", fields)); } sidebar.push_str(&sidebar_assoc_items(it)); @@ -3714,8 +3814,16 @@ fn sidebar_enum(fmt: &mut fmt::Formatter, it: &clean::Item, e: &clean::Enum) -> fmt::Result { let mut sidebar = String::new(); - if !e.variants.is_empty() { - sidebar.push_str("
  • Variants
  • "); + let variants = e.variants.iter() + .filter_map(|v| match v.name { + Some(ref name) => Some(format!("{name}\ + ", name = name)), + _ => None, + }) + .collect::(); + if !variants.is_empty() { + sidebar.push_str(&format!("Variants\ +
    {}
    ", variants)); } sidebar.push_str(&sidebar_assoc_items(it)); diff --git a/src/test/rustdoc/sidebar-items.rs b/src/test/rustdoc/sidebar-items.rs new file mode 100644 index 0000000000000..9be40441e9d4f --- /dev/null +++ b/src/test/rustdoc/sidebar-items.rs @@ -0,0 +1,59 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![crate_name = "foo"] + +// @has foo/trait.Foo.html +// @has - '//*[@class="sidebar-title"][@href="#required-methods"]' 'Required Methods' +// @has - '//*[@class="sidebar-links"]/a' 'bar' +// @has - '//*[@class="sidebar-title"][@href="#provided-methods"]' 'Provided Methods' +// @has - '//*[@class="sidebar-links"]/a' 'foo' +// @has - '//*[@class="sidebar-title"][@href="#associated-const"]' 'Associated Constants' +// @has - '//*[@class="sidebar-links"]/a' 'BAR' +// @has - '//*[@class="sidebar-title"][@href="#associated-types"]' 'Associated Types' +// @has - '//*[@class="sidebar-links"]/a' 'Output' +pub trait Foo { + const BAR: u32 = 0; + type Output: ?Sized; + + fn foo() {} + fn bar() -> Self::Output; +} + +// @has foo/struct.Bar.html +// @has - '//*[@class="sidebar-title"][@href="#fields"]' 'Fields' +// @has - '//*[@class="sidebar-links"]/a[@href="#structfield.f"]' 'f' +// @has - '//*[@class="sidebar-links"]/a[@href="#structfield.u"]' 'u' +// @!has - '//*[@class="sidebar-links"]/a' 'w' +pub struct Bar { + pub f: u32, + pub u: u32, + w: u32, +} + +// @has foo/enum.En.html +// @has - '//*[@class="sidebar-title"][@href="#variants"]' 'Variants' +// @has - '//*[@class="sidebar-links"]/a' 'foo' +// @has - '//*[@class="sidebar-links"]/a' 'bar' +pub enum En { + foo, + bar, +} + +// @has foo/union.MyUnion.html +// @has - '//*[@class="sidebar-title"][@href="#fields"]' 'Fields' +// @has - '//*[@class="sidebar-links"]/a[@href="#structfield.f1"]' 'f1' +// @has - '//*[@class="sidebar-links"]/a[@href="#structfield.f2"]' 'f2' +// @!has - '//*[@class="sidebar-links"]/a' 'w' +pub union MyUnion { + pub f1: u32, + pub f2: f32, + w: u32, +}