From b491587c91c5cce0ed49751f552b0729e9b69d2f Mon Sep 17 00:00:00 2001 From: Nixon Enraght-Moony Date: Wed, 18 Nov 2020 12:48:54 +0000 Subject: [PATCH 1/2] Extract write_srclink to its own method --- src/librustdoc/html/render/mod.rs | 54 ++++++++++++++----------------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 28f7a4d316248..70969984b0739 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -86,7 +86,11 @@ crate type NameDoc = (String, Option); crate fn ensure_trailing_slash(v: &str) -> impl fmt::Display + '_ { crate::html::format::display_fn(move |f| { - if !v.ends_with('/') && !v.is_empty() { write!(f, "{}/", v) } else { write!(f, "{}", v) } + if !v.ends_with('/') && !v.is_empty() { + write!(f, "{}/", v) + } else { + write!(f, "{}", v) + } }) } @@ -1194,6 +1198,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, + "[src]", + l, "goto source code" + ) + } +} + #[derive(Debug, Eq, PartialEq, Hash)] struct ItemEntry { url: String, @@ -1706,13 +1720,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 // used to find the link to auto-click. if cx.shared.include_sources && !item.is_primitive() { - if let Some(l) = cx.src_href(item, cache) { - write!( - buf, - "[src]", - l, "goto source code" - ); - } + write_srclink(cx, item, buf, cache); } write!(buf, ""); // out-of-band @@ -1942,7 +1950,11 @@ fn document_stability( } fn document_non_exhaustive_header(item: &clean::Item) -> &str { - if item.is_non_exhaustive() { " (Non-exhaustive)" } else { "" } + if item.is_non_exhaustive() { + " (Non-exhaustive)" + } else { + "" + } } fn document_non_exhaustive(w: &mut Buffer, item: &clean::Item) { @@ -3693,13 +3705,7 @@ fn render_impl( StabilityLevel::Unstable { .. } => None, }); render_stability_since_raw(w, since.as_deref(), outer_version); - if let Some(l) = cx.src_href(&i.impl_item, cache) { - write!( - w, - "[src]", - l, "goto source code" - ); - } + write_srclink(cx, &i.impl_item, w, cache); write!(w, ""); if trait_.is_some() { @@ -3765,13 +3771,7 @@ fn render_impl( render_assoc_item(w, item, link.anchor(&id), ItemType::Impl); write!(w, ""); render_stability_since_raw(w, item.stable_since().as_deref(), outer_version); - if let Some(l) = cx.src_href(item, cache) { - write!( - w, - "[src]", - l, "goto source code" - ); - } + write_srclink(cx, item, w, cache); write!(w, ""); } } @@ -3787,13 +3787,7 @@ fn render_impl( assoc_const(w, item, ty, default.as_ref(), link.anchor(&id), ""); write!(w, ""); render_stability_since_raw(w, item.stable_since().as_deref(), outer_version); - if let Some(l) = cx.src_href(item, cache) { - write!( - w, - "[src]", - l, "goto source code" - ); - } + write_srclink(cx, item, w, cache); write!(w, ""); } clean::AssocTypeItem(ref bounds, ref default) => { From ae644a2d4df673f5063d5b127488b7f03fef15a6 Mon Sep 17 00:00:00 2001 From: Nixon Enraght-Moony Date: Wed, 18 Nov 2020 13:38:17 +0000 Subject: [PATCH 2/2] add [src] links to methods on a trait's page --- src/librustdoc/html/render/mod.rs | 31 ++++++++++---------------- src/librustdoc/html/static/rustdoc.css | 10 ++++----- src/test/rustdoc/trait-src-link.rs | 26 +++++++++++++++++++++ 3 files changed, 43 insertions(+), 24 deletions(-) create mode 100644 src/test/rustdoc/trait-src-link.rs diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 70969984b0739..147a8d33765af 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -86,11 +86,7 @@ crate type NameDoc = (String, Option); crate fn ensure_trailing_slash(v: &str) -> impl fmt::Display + '_ { crate::html::format::display_fn(move |f| { - if !v.ends_with('/') && !v.is_empty() { - write!(f, "{}/", v) - } else { - write!(f, "{}", v) - } + if !v.ends_with('/') && !v.is_empty() { write!(f, "{}/", v) } else { write!(f, "{}", v) } }) } @@ -1950,11 +1946,7 @@ fn document_stability( } fn document_non_exhaustive_header(item: &clean::Item) -> &str { - if item.is_non_exhaustive() { - " (Non-exhaustive)" - } else { - "" - } + if item.is_non_exhaustive() { " (Non-exhaustive)" } else { "" } } fn document_non_exhaustive(w: &mut Buffer, item: &clean::Item) { @@ -2636,7 +2628,7 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait, write!(w, "{}Loading content...", 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(); info!("Documenting {} on {}", name, t.name.as_deref().unwrap_or_default()); let item_type = m.type_(); @@ -2645,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); write!(w, ""); render_stability_since(w, m, t); + write_srclink(cx, m, w, cache); write!(w, ""); document(w, cx, m, Some(t)); } @@ -2656,8 +2649,8 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait, "Associated Types", "
", ); - for t in &types { - trait_item(w, cx, *t, it); + for t in types { + trait_item(w, cx, t, it, cache); } write_loading_content(w, "
"); } @@ -2669,8 +2662,8 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait, "Associated Constants", "
", ); - for t in &consts { - trait_item(w, cx, *t, it); + for t in consts { + trait_item(w, cx, t, it, cache); } write_loading_content(w, "
"); } @@ -2683,8 +2676,8 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait, "Required methods", "
", ); - for m in &required { - trait_item(w, cx, *m, it); + for m in required { + trait_item(w, cx, m, it, cache); } write_loading_content(w, "
"); } @@ -2695,8 +2688,8 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait, "Provided methods", "
", ); - for m in &provided { - trait_item(w, cx, *m, it); + for m in provided { + trait_item(w, cx, m, it, cache); } write_loading_content(w, "
"); } diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css index 7eccb09b07367..7d22913b99de9 100644 --- a/src/librustdoc/html/static/rustdoc.css +++ b/src/librustdoc/html/static/rustdoc.css @@ -659,7 +659,7 @@ a { text-decoration: underline; } -.invisible > .srclink, h4 > code + .srclink { +.invisible > .srclink, h4 > code + .srclink, h3 > code + .srclink { position: absolute; top: 0; right: 0; @@ -857,25 +857,25 @@ body.blur > :not(#help) { top: 0; } -.impl-items .since, .impl .since { +.impl-items .since, .impl .since, .methods .since { flex-grow: 0; padding-left: 12px; padding-right: 2px; position: initial; } -.impl-items .srclink, .impl .srclink { +.impl-items .srclink, .impl .srclink, .methods .srclink { flex-grow: 0; /* Override header settings otherwise it's too bold */ font-size: 17px; font-weight: normal; } -.impl-items code, .impl code { +.impl-items code, .impl code, .methods code { flex-grow: 1; } -.impl-items h4, h4.impl, h3.impl { +.impl-items h4, h4.impl, h3.impl, .methods h3 { display: flex; flex-basis: 100%; font-size: 16px; diff --git a/src/test/rustdoc/trait-src-link.rs b/src/test/rustdoc/trait-src-link.rs new file mode 100644 index 0000000000000..77116695690fc --- /dev/null +++ b/src/test/rustdoc/trait-src-link.rs @@ -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() {} +}