Skip to content

Commit 192ed76

Browse files
authored
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
2 parents acc2e23 + ae644a2 commit 192ed76

File tree

3 files changed

+55
-42
lines changed

3 files changed

+55
-42
lines changed

src/librustdoc/html/render/mod.rs

+24-37
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,16 @@ fn write_minify(
11941194
}
11951195
}
11961196

1197+
fn write_srclink(cx: &Context, item: &clean::Item, buf: &mut Buffer, cache: &Cache) {
1198+
if let Some(l) = cx.src_href(item, cache) {
1199+
write!(
1200+
buf,
1201+
"<a class=\"srclink\" href=\"{}\" title=\"{}\">[src]</a>",
1202+
l, "goto source code"
1203+
)
1204+
}
1205+
}
1206+
11971207
#[derive(Debug, Eq, PartialEq, Hash)]
11981208
struct ItemEntry {
11991209
url: String,
@@ -1706,13 +1716,7 @@ fn print_item(cx: &Context, item: &clean::Item, buf: &mut Buffer, cache: &Cache)
17061716
// this page, and this link will be auto-clicked. The `id` attribute is
17071717
// used to find the link to auto-click.
17081718
if cx.shared.include_sources && !item.is_primitive() {
1709-
if let Some(l) = cx.src_href(item, cache) {
1710-
write!(
1711-
buf,
1712-
"<a class=\"srclink\" href=\"{}\" title=\"{}\">[src]</a>",
1713-
l, "goto source code"
1714-
);
1715-
}
1719+
write_srclink(cx, item, buf, cache);
17161720
}
17171721

17181722
write!(buf, "</span>"); // out-of-band
@@ -2624,7 +2628,7 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait,
26242628
write!(w, "{}<span class=\"loading-content\">Loading content...</span>", extra_content)
26252629
}
26262630

2627-
fn trait_item(w: &mut Buffer, cx: &Context, m: &clean::Item, t: &clean::Item) {
2631+
fn trait_item(w: &mut Buffer, cx: &Context, m: &clean::Item, t: &clean::Item, cache: &Cache) {
26282632
let name = m.name.as_ref().unwrap();
26292633
info!("Documenting {} on {}", name, t.name.as_deref().unwrap_or_default());
26302634
let item_type = m.type_();
@@ -2633,6 +2637,7 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait,
26332637
render_assoc_item(w, m, AssocItemLink::Anchor(Some(&id)), ItemType::Impl);
26342638
write!(w, "</code>");
26352639
render_stability_since(w, m, t);
2640+
write_srclink(cx, m, w, cache);
26362641
write!(w, "</h3>");
26372642
document(w, cx, m, Some(t));
26382643
}
@@ -2644,8 +2649,8 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait,
26442649
"Associated Types",
26452650
"<div class=\"methods\">",
26462651
);
2647-
for t in &types {
2648-
trait_item(w, cx, *t, it);
2652+
for t in types {
2653+
trait_item(w, cx, t, it, cache);
26492654
}
26502655
write_loading_content(w, "</div>");
26512656
}
@@ -2657,8 +2662,8 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait,
26572662
"Associated Constants",
26582663
"<div class=\"methods\">",
26592664
);
2660-
for t in &consts {
2661-
trait_item(w, cx, *t, it);
2665+
for t in consts {
2666+
trait_item(w, cx, t, it, cache);
26622667
}
26632668
write_loading_content(w, "</div>");
26642669
}
@@ -2671,8 +2676,8 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait,
26712676
"Required methods",
26722677
"<div class=\"methods\">",
26732678
);
2674-
for m in &required {
2675-
trait_item(w, cx, *m, it);
2679+
for m in required {
2680+
trait_item(w, cx, m, it, cache);
26762681
}
26772682
write_loading_content(w, "</div>");
26782683
}
@@ -2683,8 +2688,8 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait,
26832688
"Provided methods",
26842689
"<div class=\"methods\">",
26852690
);
2686-
for m in &provided {
2687-
trait_item(w, cx, *m, it);
2691+
for m in provided {
2692+
trait_item(w, cx, m, it, cache);
26882693
}
26892694
write_loading_content(w, "</div>");
26902695
}
@@ -3693,13 +3698,7 @@ fn render_impl(
36933698
StabilityLevel::Unstable { .. } => None,
36943699
});
36953700
render_stability_since_raw(w, since.as_deref(), outer_version);
3696-
if let Some(l) = cx.src_href(&i.impl_item, cache) {
3697-
write!(
3698-
w,
3699-
"<a class=\"srclink\" href=\"{}\" title=\"{}\">[src]</a>",
3700-
l, "goto source code"
3701-
);
3702-
}
3701+
write_srclink(cx, &i.impl_item, w, cache);
37033702
write!(w, "</h3>");
37043703

37053704
if trait_.is_some() {
@@ -3765,13 +3764,7 @@ fn render_impl(
37653764
render_assoc_item(w, item, link.anchor(&id), ItemType::Impl);
37663765
write!(w, "</code>");
37673766
render_stability_since_raw(w, item.stable_since().as_deref(), outer_version);
3768-
if let Some(l) = cx.src_href(item, cache) {
3769-
write!(
3770-
w,
3771-
"<a class=\"srclink\" href=\"{}\" title=\"{}\">[src]</a>",
3772-
l, "goto source code"
3773-
);
3774-
}
3767+
write_srclink(cx, item, w, cache);
37753768
write!(w, "</h4>");
37763769
}
37773770
}
@@ -3787,13 +3780,7 @@ fn render_impl(
37873780
assoc_const(w, item, ty, default.as_ref(), link.anchor(&id), "");
37883781
write!(w, "</code>");
37893782
render_stability_since_raw(w, item.stable_since().as_deref(), outer_version);
3790-
if let Some(l) = cx.src_href(item, cache) {
3791-
write!(
3792-
w,
3793-
"<a class=\"srclink\" href=\"{}\" title=\"{}\">[src]</a>",
3794-
l, "goto source code"
3795-
);
3796-
}
3783+
write_srclink(cx, item, w, cache);
37973784
write!(w, "</h4>");
37983785
}
37993786
clean::AssocTypeItem(ref bounds, ref default) => {

src/librustdoc/html/static/rustdoc.css

+5-5
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ a {
659659
text-decoration: underline;
660660
}
661661

662-
.invisible > .srclink, h4 > code + .srclink {
662+
.invisible > .srclink, h4 > code + .srclink, h3 > code + .srclink {
663663
position: absolute;
664664
top: 0;
665665
right: 0;
@@ -857,25 +857,25 @@ body.blur > :not(#help) {
857857
top: 0;
858858
}
859859

860-
.impl-items .since, .impl .since {
860+
.impl-items .since, .impl .since, .methods .since {
861861
flex-grow: 0;
862862
padding-left: 12px;
863863
padding-right: 2px;
864864
position: initial;
865865
}
866866

867-
.impl-items .srclink, .impl .srclink {
867+
.impl-items .srclink, .impl .srclink, .methods .srclink {
868868
flex-grow: 0;
869869
/* Override header settings otherwise it's too bold */
870870
font-size: 17px;
871871
font-weight: normal;
872872
}
873873

874-
.impl-items code, .impl code {
874+
.impl-items code, .impl code, .methods code {
875875
flex-grow: 1;
876876
}
877877

878-
.impl-items h4, h4.impl, h3.impl {
878+
.impl-items h4, h4.impl, h3.impl, .methods h3 {
879879
display: flex;
880880
flex-basis: 100%;
881881
font-size: 16px;

src/test/rustdoc/trait-src-link.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#![crate_name = "quix"]
2+
pub trait Foo {
3+
// @has quix/trait.Foo.html '//a[@href="../src/quix/trait-src-link.rs.html#4"]' '[src]'
4+
fn required();
5+
6+
// @has quix/trait.Foo.html '//a[@href="../src/quix/trait-src-link.rs.html#7"]' '[src]'
7+
fn provided() {}
8+
}
9+
10+
pub struct Bar;
11+
12+
impl Foo for Bar {
13+
// @has quix/struct.Bar.html '//a[@href="../src/quix/trait-src-link.rs.html#14"]' '[src]'
14+
fn required() {}
15+
// @has quix/struct.Bar.html '//a[@href="../src/quix/trait-src-link.rs.html#7"]' '[src]'
16+
}
17+
18+
pub struct Baz;
19+
20+
impl Foo for Baz {
21+
// @has quix/struct.Baz.html '//a[@href="../src/quix/trait-src-link.rs.html#22"]' '[src]'
22+
fn required() {}
23+
24+
// @has quix/struct.Baz.html '//a[@href="../src/quix/trait-src-link.rs.html#25"]' '[src]'
25+
fn provided() {}
26+
}

0 commit comments

Comments
 (0)