Skip to content

Commit

Permalink
Auto merge of #89815 - GuillaumeGomez:associated-consts-sidebar, r=no…
Browse files Browse the repository at this point in the history
…triddle

Associated consts sidebar

Fixes #89354.

A screenshot with `f32`:

![Screenshot from 2021-10-12 15-07-57](https://user-images.githubusercontent.com/3050060/136962078-5faf7b87-7ea5-4d7a-99a4-b2afd77b78e2.png)
  • Loading branch information
bors committed Oct 14, 2021
2 parents 8c852bc + 38f6c07 commit 7807a69
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 16 deletions.
95 changes: 79 additions & 16 deletions src/librustdoc/html/render/mod.rs
Expand Up @@ -1811,23 +1811,53 @@ fn get_next_url(used_links: &mut FxHashSet<String>, url: String) -> String {
format!("{}-{}", url, add)
}

struct SidebarLink {
name: Symbol,
url: String,
}

impl fmt::Display for SidebarLink {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<a href=\"#{}\">{}</a>", self.url, self.name)
}
}

impl PartialEq for SidebarLink {
fn eq(&self, other: &Self) -> bool {
self.url == other.url
}
}

impl Eq for SidebarLink {}

impl PartialOrd for SidebarLink {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl Ord for SidebarLink {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.url.cmp(&other.url)
}
}

fn get_methods(
i: &clean::Impl,
for_deref: bool,
used_links: &mut FxHashSet<String>,
deref_mut: bool,
tcx: TyCtxt<'_>,
) -> Vec<String> {
) -> Vec<SidebarLink> {
i.items
.iter()
.filter_map(|item| match item.name {
Some(ref name) if !name.is_empty() && item.is_method() => {
Some(name) if !name.is_empty() && item.is_method() => {
if !for_deref || should_render_item(item, deref_mut, tcx) {
Some(format!(
"<a href=\"#{}\">{}</a>",
get_next_url(used_links, format!("method.{}", name)),
name
))
Some(SidebarLink {
name,
url: get_next_url(used_links, format!("method.{}", name)),
})
} else {
None
}
Expand All @@ -1837,6 +1867,22 @@ fn get_methods(
.collect::<Vec<_>>()
}

fn get_associated_constants(
i: &clean::Impl,
used_links: &mut FxHashSet<String>,
) -> Vec<SidebarLink> {
i.items
.iter()
.filter_map(|item| match item.name {
Some(name) if !name.is_empty() && item.is_associated_const() => Some(SidebarLink {
name,
url: get_next_url(used_links, format!("associatedconstant.{}", name)),
}),
_ => None,
})
.collect::<Vec<_>>()
}

// The point is to url encode any potential character from a type with genericity.
fn small_url_encode(s: String) -> String {
let mut st = String::new();
Expand Down Expand Up @@ -1881,23 +1927,40 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {

{
let used_links_bor = &mut used_links;
let mut ret = v
let mut assoc_consts = v
.iter()
.flat_map(|i| get_associated_constants(i.inner_impl(), used_links_bor))
.collect::<Vec<_>>();
if !assoc_consts.is_empty() {
// We want links' order to be reproducible so we don't use unstable sort.
assoc_consts.sort();

out.push_str(
"<h3 class=\"sidebar-title\">\
<a href=\"#implementations\">Associated Constants</a>\
</h3>\
<div class=\"sidebar-links\">",
);
for line in assoc_consts {
write!(out, "{}", line);
}
out.push_str("</div>");
}
let mut methods = v
.iter()
.filter(|i| i.inner_impl().trait_.is_none())
.flat_map(move |i| {
get_methods(i.inner_impl(), false, used_links_bor, false, cx.tcx())
})
.flat_map(|i| get_methods(i.inner_impl(), false, used_links_bor, false, cx.tcx()))
.collect::<Vec<_>>();
if !ret.is_empty() {
if !methods.is_empty() {
// We want links' order to be reproducible so we don't use unstable sort.
ret.sort();
methods.sort();

out.push_str(
"<h3 class=\"sidebar-title\"><a href=\"#implementations\">Methods</a></h3>\
<div class=\"sidebar-links\">",
);
for line in ret {
out.push_str(&line);
for line in methods {
write!(out, "{}", line);
}
out.push_str("</div>");
}
Expand Down Expand Up @@ -2032,7 +2095,7 @@ fn sidebar_deref_methods(cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &V
ret.sort();
out.push_str("<div class=\"sidebar-links\">");
for link in ret {
out.push_str(&link);
write!(out, "{}", link);
}
out.push_str("</div>");
}
Expand Down
31 changes: 31 additions & 0 deletions src/test/rustdoc/associated-consts.rs
@@ -0,0 +1,31 @@
#![crate_name = "foo"]

pub trait Trait {
const FOO: u32 = 12;

fn foo();
}

pub struct Bar;

// @has 'foo/struct.Bar.html'
// @has - '//h3[@class="sidebar-title"]' 'Associated Constants'
// @has - '//div[@class="sidebar-elems"]//div[@class="sidebar-links"]/a' 'FOO'
impl Trait for Bar {
const FOO: u32 = 1;

fn foo() {}
}

pub enum Foo {
A,
}

// @has 'foo/enum.Foo.html'
// @has - '//h3[@class="sidebar-title"]' 'Associated Constants'
// @has - '//div[@class="sidebar-elems"]//div[@class="sidebar-links"]/a' 'FOO'
impl Trait for Foo {
const FOO: u32 = 1;

fn foo() {}
}

0 comments on commit 7807a69

Please sign in to comment.