Skip to content

rustdoc: Render [src] links for trait implementors #44920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2253,6 +2253,18 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
document(w, cx, it)
}

fn implementor2item<'a>(cache: &'a Cache, imp : &Implementor) -> Option<&'a clean::Item> {
if let Some(t_did) = imp.impl_.for_.def_id() {
if let Some(impl_item) = cache.impls.get(&t_did).and_then(|i| i.iter()
.find(|i| i.impl_item.def_id == imp.def_id))
{
let i = &impl_item.impl_item;
return Some(i);
}
}
None
}

fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
t: &clean::Trait) -> fmt::Result {
let mut bounds = String::new();
Expand Down Expand Up @@ -2463,27 +2475,30 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
")?;

for implementor in foreign {
// need to get from a clean::Impl to a clean::Item so i can use render_impl
if let Some(t_did) = implementor.impl_.for_.def_id() {
if let Some(impl_item) = cache.impls.get(&t_did).and_then(|i| i.iter()
.find(|i| i.impl_item.def_id == implementor.def_id))
{
let i = &impl_item.impl_item;
let impl_ = Impl { impl_item: i.clone() };
let assoc_link = AssocItemLink::GotoSource(
i.def_id, &implementor.impl_.provided_trait_methods
);
render_impl(w, cx, &impl_, assoc_link,
RenderMode::Normal, i.stable_since(), false)?;
}
if let Some(i) = implementor2item(&cache, implementor) {
let impl_ = Impl { impl_item: i.clone() };
let assoc_link = AssocItemLink::GotoSource(
i.def_id, &implementor.impl_.provided_trait_methods
);
render_impl(w, cx, &impl_, assoc_link,
RenderMode::Normal, i.stable_since(), false)?;
}
}
}

write!(w, "{}", impl_header)?;

for implementor in local {
write!(w, "<li><code>")?;
write!(w, "<li>")?;
if let Some(item) = implementor2item(&cache, implementor) {
if let Some(l) = (Item { cx, item }).src_href() {
write!(w, "<div class='out-of-band'>")?;
write!(w, "<a class='srclink' href='{}' title='{}'>[src]</a>",
l, "goto source code")?;
write!(w, "</div>")?;
}
}
write!(w, "<code>")?;
// If there's already another implementor that has the same abbridged name, use the
// full path, for example in `std::iter::ExactSizeIterator`
let use_absolute = match implementor.impl_.for_ {
Expand Down
9 changes: 8 additions & 1 deletion src/librustdoc/html/static/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ h3.impl, h3.method, h4.method, h3.type, h4.type, h4.associatedconstant {
h3.impl, h3.method, h3.type {
margin-top: 15px;
}
h1, h2, h3, h4, .sidebar, a.source, .search-input, .content table :not(code)>a, .collapse-toggle {

h1, h2, h3, h4,
.sidebar, a.source, .search-input, .content table :not(code)>a,
.collapse-toggle, ul.item-list > li > .out-of-band {
font-family: "Fira Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}

Expand Down Expand Up @@ -310,6 +313,10 @@ h4.method > .out-of-band {
font-size: 19px;
}

ul.item-list > li > .out-of-band {
font-size: 19px;
}

h4 > code, h3 > code, .invisible > code {
position: inherit;
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/rustdoc/issue-43893.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2016 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// ignore-cross-compile

#![crate_name = "foo"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove all unneeded empty lines in here (like in the impl blocks).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which empty lines? Before and after the // ignore-corss-compile? They are just retained from issue-34274.rs, which served as a template. I don't think they should be removed (especially the first empty line).

like in the impl blocks

Means "and likewise remove them in impl blocks" or "like you did in impl blocks"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this:

// Copyright 2016 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// ignore-cross-compile

#![crate_name = "foo"]

pub trait SomeTrait {}
pub struct SomeStruct;

// @has foo/trait.SomeTrait.html '//a/@href' '../src/foo/issue-43893.rs.html#23-26'
impl SomeTrait for usize {}

// @has foo/trait.SomeTrait.html '//a/@href' '../src/foo/issue-43893.rs.html#29-32'
impl SomeTrait for SomeStruct {}

Copy link
Contributor Author

@vi vi Oct 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

impl SomeTrait for usize {}

{}

Other [src] link test (issue-34274.rs) tests for single line link. I thought it would be useful to also provide multiple line implementation, so that the link contains line range, not a single line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having multi-line tests is useful to make sure that the link points to the full span.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why I contracted one impl to one line, but left the other impl as three lines in the update.


pub trait SomeTrait {}
pub struct SomeStruct;

// @has foo/trait.SomeTrait.html '//a/@href' '../src/foo/issue-43893.rs.html#19'
impl SomeTrait for usize {}

// @has foo/trait.SomeTrait.html '//a/@href' '../src/foo/issue-43893.rs.html#22-24'
impl SomeTrait for SomeStruct {
// deliberately multi-line impl
}