Skip to content

Commit

Permalink
Fix target highlighting in rustdoc.
Browse files Browse the repository at this point in the history
Also factor out outer_version and const_outer_version into
render_rightside.
  • Loading branch information
jsha committed Jun 17, 2021
1 parent c4fa6d5 commit 2ac5c17
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 48 deletions.
47 changes: 19 additions & 28 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,8 @@ fn short_item_info(
extra_info
}

// Render the list of items inside one of the sections "Trait Implementations",
// "Auto Trait Implementations," "Blanket Trait Implementations" (on struct/enum pages).
fn render_impls(
cx: &Context<'_>,
w: &mut Buffer,
Expand All @@ -745,8 +747,6 @@ fn render_impls(
containing_item,
assoc_link,
RenderMode::Normal,
containing_item.stable_since(tcx).as_deref(),
containing_item.const_stable_since(tcx).as_deref(),
true,
None,
false,
Expand Down Expand Up @@ -1024,7 +1024,6 @@ fn render_assoc_items(
Some(v) => v,
None => return,
};
let tcx = cx.tcx();
let cache = cx.cache();
let (non_trait, traits): (Vec<_>, _) = v.iter().partition(|i| i.inner_impl().trait_.is_none());
if !non_trait.is_empty() {
Expand Down Expand Up @@ -1058,8 +1057,6 @@ fn render_assoc_items(
containing_item,
AssocItemLink::Anchor(None),
render_mode,
containing_item.stable_since(tcx).as_deref(),
containing_item.const_stable_since(tcx).as_deref(),
true,
None,
false,
Expand Down Expand Up @@ -1260,8 +1257,6 @@ fn render_impl(
parent: &clean::Item,
link: AssocItemLink<'_>,
render_mode: RenderMode,
outer_version: Option<&str>,
outer_const_version: Option<&str>,
show_def_docs: bool,
use_absolute: Option<bool>,
is_on_foreign_type: bool,
Expand All @@ -1278,17 +1273,18 @@ fn render_impl(
// For trait implementations, the `interesting` output contains all methods that have doc
// comments, and the `boring` output contains all methods that do not. The distinction is
// used to allow hiding the boring methods.
// `containing_item` is used for rendering stability info. If the parent is a trait impl,
// `containing_item` will the grandparent, since trait impls can't have stability attached.
fn doc_impl_item(
boring: &mut Buffer,
interesting: &mut Buffer,
cx: &Context<'_>,
item: &clean::Item,
parent: &clean::Item,
containing_item: &clean::Item,
link: AssocItemLink<'_>,
render_mode: RenderMode,
is_default_item: bool,
outer_version: Option<&str>,
outer_const_version: Option<&str>,
trait_: Option<&clean::Trait>,
show_def_docs: bool,
) {
Expand Down Expand Up @@ -1362,7 +1358,7 @@ fn render_impl(
"<div id=\"{}\" class=\"{}{} has-srclink\">",
id, item_type, in_trait_class,
);
render_rightside(w, cx, item, outer_version, outer_const_version);
render_rightside(w, cx, item, containing_item);
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
w.write_str("<code>");
render_assoc_item(
Expand Down Expand Up @@ -1406,7 +1402,7 @@ fn render_impl(
"<div id=\"{}\" class=\"{}{} has-srclink\">",
id, item_type, in_trait_class
);
render_rightside(w, cx, item, outer_version, outer_const_version);
render_rightside(w, cx, item, containing_item);
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
w.write_str("<code>");
assoc_const(
Expand Down Expand Up @@ -1461,11 +1457,10 @@ fn render_impl(
cx,
trait_item,
if trait_.is_some() { &i.impl_item } else { parent },
parent,
link,
render_mode,
false,
outer_version,
outer_const_version,
trait_.map(|t| &t.trait_),
show_def_docs,
);
Expand All @@ -1478,9 +1473,8 @@ fn render_impl(
t: &clean::Trait,
i: &clean::Impl,
parent: &clean::Item,
containing_item: &clean::Item,
render_mode: RenderMode,
outer_version: Option<&str>,
outer_const_version: Option<&str>,
show_def_docs: bool,
) {
for trait_item in &t.items {
Expand All @@ -1498,11 +1492,10 @@ fn render_impl(
cx,
trait_item,
parent,
containing_item,
assoc_link,
render_mode,
true,
outer_version,
outer_const_version,
Some(t),
show_def_docs,
);
Expand All @@ -1522,9 +1515,8 @@ fn render_impl(
&t.trait_,
&i.inner_impl(),
&i.impl_item,
parent,
render_mode,
outer_version,
outer_const_version,
show_def_docs,
);
}
Expand All @@ -1541,8 +1533,7 @@ fn render_impl(
cx,
i,
parent,
outer_version,
outer_const_version,
parent,
show_def_docs,
use_absolute,
is_on_foreign_type,
Expand Down Expand Up @@ -1578,12 +1569,13 @@ fn render_impl(
w.write_str(&close_tags);
}

// Render the items that appear on the right side of methods, impls, and
// associated types. For example "1.0.0 (const: 1.39.0) [src]".
fn render_rightside(
w: &mut Buffer,
cx: &Context<'_>,
item: &clean::Item,
outer_version: Option<&str>,
outer_const_version: Option<&str>,
containing_item: &clean::Item,
) {
let tcx = cx.tcx();

Expand All @@ -1592,8 +1584,8 @@ fn render_rightside(
w,
item.stable_since(tcx).as_deref(),
item.const_stable_since(tcx).as_deref(),
outer_version,
outer_const_version,
containing_item.stable_since(tcx).as_deref(),
containing_item.const_stable_since(tcx).as_deref(),
);

write_srclink(cx, item, w);
Expand All @@ -1605,8 +1597,7 @@ pub(crate) fn render_impl_summary(
cx: &Context<'_>,
i: &Impl,
parent: &clean::Item,
outer_version: Option<&str>,
outer_const_version: Option<&str>,
containing_item: &clean::Item,
show_def_docs: bool,
use_absolute: Option<bool>,
is_on_foreign_type: bool,
Expand All @@ -1630,7 +1621,7 @@ pub(crate) fn render_impl_summary(
format!(" data-aliases=\"{}\"", aliases.join(","))
};
write!(w, "<div id=\"{}\" class=\"impl has-srclink\"{}>", id, aliases);
render_rightside(w, cx, &i.impl_item, outer_version, outer_const_version);
render_rightside(w, cx, &i.impl_item, containing_item);
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
write!(w, "<code class=\"in-band\">");

Expand Down
10 changes: 2 additions & 8 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,15 +694,12 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
write_small_section_header(w, "foreign-impls", "Implementations on Foreign Types", "");

for implementor in foreign {
let outer_version = implementor.impl_item.stable_since(cx.tcx());
let outer_const_version = implementor.impl_item.const_stable_since(cx.tcx());
render_impl_summary(
w,
cx,
&implementor,
it,
outer_version.as_deref(),
outer_const_version.as_deref(),
&implementor.impl_item,
false,
None,
true,
Expand Down Expand Up @@ -1319,15 +1316,12 @@ fn render_implementor(
} => implementor_dups[&path.last()].1,
_ => false,
};
let outer_version = trait_.stable_since(cx.tcx());
let outer_const_version = trait_.const_stable_since(cx.tcx());
render_impl_summary(
w,
cx,
implementor,
trait_,
outer_version.as_deref(),
outer_const_version.as_deref(),
trait_,
false,
Some(use_absolute),
false,
Expand Down
4 changes: 4 additions & 0 deletions src/librustdoc/html/static/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,10 @@ a.test-arrow:hover{
opacity: 1;
}

:target {
padding-right: 3px;
}

.information {
position: absolute;
left: -25px;
Expand Down
5 changes: 4 additions & 1 deletion src/librustdoc/html/static/themes/ayu.css
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,11 @@ a.test-arrow:hover {
color: #999;
}

:target > code, :target > .in-band {
:target, :target * {
background: rgba(255, 236, 164, 0.06);
}

:target {
border-right: 3px solid rgba(255, 180, 76, 0.85);
}

Expand Down
5 changes: 4 additions & 1 deletion src/librustdoc/html/static/themes/dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,11 @@ a.test-arrow:hover{
color: #999;
}

:target > code, :target > .in-band {
:target, :target * {
background-color: #494a3d;
}

:target {
border-right: 3px solid #bb7410;
}

Expand Down
5 changes: 4 additions & 1 deletion src/librustdoc/html/static/themes/light.css
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,11 @@ a.test-arrow:hover{
color: #999;
}

:target > code, :target > .in-band {
:target, :target * {
background: #FDFFD3;
}

:target {
border-right: 3px solid #ffb44c;
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/rustdoc/ensure-src-link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

// This test ensures that the [src] link is present on traits items.

// @has foo/trait.Iterator.html '//div[@id="method.zip"]/a[@class="srclink"]' "[src]"
// @has foo/trait.Iterator.html '//div[@id="method.zip"]//a[@class="srclink"]' "[src]"
pub use std::iter::Iterator;
8 changes: 0 additions & 8 deletions src/test/rustdoc/trait-impl-items-links-and-anchors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,15 @@ impl MyTrait for Vec<u8> {
}

impl MyTrait for MyStruct {
// @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="associatedtype.Assoc-3"]//a[@class="type"]/@href' #associatedtype.Assoc
// @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="associatedtype.Assoc-3"]//a[@class="anchor"]/@href' #associatedtype.Assoc-3
// @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="associatedtype.Assoc"]//a[@class="type"]/@href' trait.MyTrait.html#associatedtype.Assoc
// @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="associatedtype.Assoc"]//a[@class="anchor"]/@href' #associatedtype.Assoc
type Assoc = bool;
// @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="associatedconstant.VALUE-3"]//a[@class="constant"]/@href' #associatedconstant.VALUE
// @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="associatedconstant.VALUE-3"]//a[@class="anchor"]/@href' #associatedconstant.VALUE-3
// @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="associatedconstant.VALUE"]//a[@class="constant"]/@href' trait.MyTrait.html#associatedconstant.VALUE
// @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="associatedconstant.VALUE"]//a[@class="anchor"]/@href' #associatedconstant.VALUE
const VALUE: u32 = 20;
// @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="method.trait_function-2"]//a[@class="fnname"]/@href' #tymethod.trait_function
// @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="method.trait_function-2"]//a[@class="anchor"]/@href' #method.trait_function-2
// @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="method.trait_function"]//a[@class="fnname"]/@href' trait.MyTrait.html#tymethod.trait_function
// @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="method.trait_function"]//a[@class="anchor"]/@href' #method.trait_function
fn trait_function(&self) {}
// @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="method.defaulted_override-3"]//a[@class="fnname"]/@href' #method.defaulted_override
// @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="method.defaulted_override-3"]//a[@class="anchor"]/@href' #method.defaulted_override-3
// @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="method.defaulted_override"]//a[@class="fnname"]/@href' trait.MyTrait.html#method.defaulted_override
// @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="method.defaulted_override"]//a[@class="anchor"]/@href' #method.defaulted_override
fn defaulted_override(&self) {}
Expand Down

0 comments on commit 2ac5c17

Please sign in to comment.