1
Fork 0

Rollup merge of #79181 - aDotInTheVoid:provided-method-source-link, r=jyn514,GuillaumeGomez

rustdoc: add [src] links to methods on a trait's page

Closes #45150

![image](https://user-images.githubusercontent.com/28781354/99565541-aba4d500-29c3-11eb-99c7-11c1f91584e9.png)

### Caveats

- The way I've implemented it, links are also provided for required methods, that just link to the signature in the code. I'm not sure if this is the desired behaviour.

![image](https://user-images.githubusercontent.com/28781354/99566222-849ad300-29c4-11eb-9897-08cc5842954f.png)

- I'm not sure if the css changes are correct. I inspected them visualy on firefox on desktop, and they seem to be fine.
- I can't tell how `src/librustdoc/html/render/mod.rs` is structured, so I probably
This commit is contained in:
Dylan DPC 2020-11-19 23:58:39 +01:00 committed by GitHub
commit 192ed76cb9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 42 deletions

View file

@ -1194,6 +1194,16 @@ fn write_minify(
} }
} }
fn write_srclink(cx: &Context, item: &clean::Item, buf: &mut Buffer, cache: &Cache) {
if let Some(l) = cx.src_href(item, cache) {
write!(
buf,
"<a class=\"srclink\" href=\"{}\" title=\"{}\">[src]</a>",
l, "goto source code"
)
}
}
#[derive(Debug, Eq, PartialEq, Hash)] #[derive(Debug, Eq, PartialEq, Hash)]
struct ItemEntry { struct ItemEntry {
url: String, url: String,
@ -1706,13 +1716,7 @@ fn print_item(cx: &Context, item: &clean::Item, buf: &mut Buffer, cache: &Cache)
// this page, and this link will be auto-clicked. The `id` attribute is // this page, and this link will be auto-clicked. The `id` attribute is
// used to find the link to auto-click. // used to find the link to auto-click.
if cx.shared.include_sources && !item.is_primitive() { if cx.shared.include_sources && !item.is_primitive() {
if let Some(l) = cx.src_href(item, cache) { write_srclink(cx, item, buf, cache);
write!(
buf,
"<a class=\"srclink\" href=\"{}\" title=\"{}\">[src]</a>",
l, "goto source code"
);
}
} }
write!(buf, "</span>"); // out-of-band write!(buf, "</span>"); // out-of-band
@ -2624,7 +2628,7 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait,
write!(w, "{}<span class=\"loading-content\">Loading content...</span>", extra_content) write!(w, "{}<span class=\"loading-content\">Loading content...</span>", extra_content)
} }
fn trait_item(w: &mut Buffer, cx: &Context, m: &clean::Item, t: &clean::Item) { fn trait_item(w: &mut Buffer, cx: &Context, m: &clean::Item, t: &clean::Item, cache: &Cache) {
let name = m.name.as_ref().unwrap(); let name = m.name.as_ref().unwrap();
info!("Documenting {} on {}", name, t.name.as_deref().unwrap_or_default()); info!("Documenting {} on {}", name, t.name.as_deref().unwrap_or_default());
let item_type = m.type_(); let item_type = m.type_();
@ -2633,6 +2637,7 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait,
render_assoc_item(w, m, AssocItemLink::Anchor(Some(&id)), ItemType::Impl); render_assoc_item(w, m, AssocItemLink::Anchor(Some(&id)), ItemType::Impl);
write!(w, "</code>"); write!(w, "</code>");
render_stability_since(w, m, t); render_stability_since(w, m, t);
write_srclink(cx, m, w, cache);
write!(w, "</h3>"); write!(w, "</h3>");
document(w, cx, m, Some(t)); document(w, cx, m, Some(t));
} }
@ -2644,8 +2649,8 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait,
"Associated Types", "Associated Types",
"<div class=\"methods\">", "<div class=\"methods\">",
); );
for t in &types { for t in types {
trait_item(w, cx, *t, it); trait_item(w, cx, t, it, cache);
} }
write_loading_content(w, "</div>"); write_loading_content(w, "</div>");
} }
@ -2657,8 +2662,8 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait,
"Associated Constants", "Associated Constants",
"<div class=\"methods\">", "<div class=\"methods\">",
); );
for t in &consts { for t in consts {
trait_item(w, cx, *t, it); trait_item(w, cx, t, it, cache);
} }
write_loading_content(w, "</div>"); write_loading_content(w, "</div>");
} }
@ -2671,8 +2676,8 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait,
"Required methods", "Required methods",
"<div class=\"methods\">", "<div class=\"methods\">",
); );
for m in &required { for m in required {
trait_item(w, cx, *m, it); trait_item(w, cx, m, it, cache);
} }
write_loading_content(w, "</div>"); write_loading_content(w, "</div>");
} }
@ -2683,8 +2688,8 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait,
"Provided methods", "Provided methods",
"<div class=\"methods\">", "<div class=\"methods\">",
); );
for m in &provided { for m in provided {
trait_item(w, cx, *m, it); trait_item(w, cx, m, it, cache);
} }
write_loading_content(w, "</div>"); write_loading_content(w, "</div>");
} }
@ -3693,13 +3698,7 @@ fn render_impl(
StabilityLevel::Unstable { .. } => None, StabilityLevel::Unstable { .. } => None,
}); });
render_stability_since_raw(w, since.as_deref(), outer_version); render_stability_since_raw(w, since.as_deref(), outer_version);
if let Some(l) = cx.src_href(&i.impl_item, cache) { write_srclink(cx, &i.impl_item, w, cache);
write!(
w,
"<a class=\"srclink\" href=\"{}\" title=\"{}\">[src]</a>",
l, "goto source code"
);
}
write!(w, "</h3>"); write!(w, "</h3>");
if trait_.is_some() { if trait_.is_some() {
@ -3765,13 +3764,7 @@ fn render_impl(
render_assoc_item(w, item, link.anchor(&id), ItemType::Impl); render_assoc_item(w, item, link.anchor(&id), ItemType::Impl);
write!(w, "</code>"); write!(w, "</code>");
render_stability_since_raw(w, item.stable_since().as_deref(), outer_version); render_stability_since_raw(w, item.stable_since().as_deref(), outer_version);
if let Some(l) = cx.src_href(item, cache) { write_srclink(cx, item, w, cache);
write!(
w,
"<a class=\"srclink\" href=\"{}\" title=\"{}\">[src]</a>",
l, "goto source code"
);
}
write!(w, "</h4>"); write!(w, "</h4>");
} }
} }
@ -3787,13 +3780,7 @@ fn render_impl(
assoc_const(w, item, ty, default.as_ref(), link.anchor(&id), ""); assoc_const(w, item, ty, default.as_ref(), link.anchor(&id), "");
write!(w, "</code>"); write!(w, "</code>");
render_stability_since_raw(w, item.stable_since().as_deref(), outer_version); render_stability_since_raw(w, item.stable_since().as_deref(), outer_version);
if let Some(l) = cx.src_href(item, cache) { write_srclink(cx, item, w, cache);
write!(
w,
"<a class=\"srclink\" href=\"{}\" title=\"{}\">[src]</a>",
l, "goto source code"
);
}
write!(w, "</h4>"); write!(w, "</h4>");
} }
clean::AssocTypeItem(ref bounds, ref default) => { clean::AssocTypeItem(ref bounds, ref default) => {

View file

@ -659,7 +659,7 @@ a {
text-decoration: underline; text-decoration: underline;
} }
.invisible > .srclink, h4 > code + .srclink { .invisible > .srclink, h4 > code + .srclink, h3 > code + .srclink {
position: absolute; position: absolute;
top: 0; top: 0;
right: 0; right: 0;
@ -857,25 +857,25 @@ body.blur > :not(#help) {
top: 0; top: 0;
} }
.impl-items .since, .impl .since { .impl-items .since, .impl .since, .methods .since {
flex-grow: 0; flex-grow: 0;
padding-left: 12px; padding-left: 12px;
padding-right: 2px; padding-right: 2px;
position: initial; position: initial;
} }
.impl-items .srclink, .impl .srclink { .impl-items .srclink, .impl .srclink, .methods .srclink {
flex-grow: 0; flex-grow: 0;
/* Override header settings otherwise it's too bold */ /* Override header settings otherwise it's too bold */
font-size: 17px; font-size: 17px;
font-weight: normal; font-weight: normal;
} }
.impl-items code, .impl code { .impl-items code, .impl code, .methods code {
flex-grow: 1; flex-grow: 1;
} }
.impl-items h4, h4.impl, h3.impl { .impl-items h4, h4.impl, h3.impl, .methods h3 {
display: flex; display: flex;
flex-basis: 100%; flex-basis: 100%;
font-size: 16px; font-size: 16px;

View file

@ -0,0 +1,26 @@
#![crate_name = "quix"]
pub trait Foo {
// @has quix/trait.Foo.html '//a[@href="../src/quix/trait-src-link.rs.html#4"]' '[src]'
fn required();
// @has quix/trait.Foo.html '//a[@href="../src/quix/trait-src-link.rs.html#7"]' '[src]'
fn provided() {}
}
pub struct Bar;
impl Foo for Bar {
// @has quix/struct.Bar.html '//a[@href="../src/quix/trait-src-link.rs.html#14"]' '[src]'
fn required() {}
// @has quix/struct.Bar.html '//a[@href="../src/quix/trait-src-link.rs.html#7"]' '[src]'
}
pub struct Baz;
impl Foo for Baz {
// @has quix/struct.Baz.html '//a[@href="../src/quix/trait-src-link.rs.html#22"]' '[src]'
fn required() {}
// @has quix/struct.Baz.html '//a[@href="../src/quix/trait-src-link.rs.html#25"]' '[src]'
fn provided() {}
}