Skip to content

Commit 2ac5c17

Browse files
committed
Fix target highlighting in rustdoc.
Also factor out outer_version and const_outer_version into render_rightside.
1 parent c4fa6d5 commit 2ac5c17

File tree

8 files changed

+38
-48
lines changed

8 files changed

+38
-48
lines changed

src/librustdoc/html/render/mod.rs

+19-28
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,8 @@ fn short_item_info(
723723
extra_info
724724
}
725725

726+
// Render the list of items inside one of the sections "Trait Implementations",
727+
// "Auto Trait Implementations," "Blanket Trait Implementations" (on struct/enum pages).
726728
fn render_impls(
727729
cx: &Context<'_>,
728730
w: &mut Buffer,
@@ -745,8 +747,6 @@ fn render_impls(
745747
containing_item,
746748
assoc_link,
747749
RenderMode::Normal,
748-
containing_item.stable_since(tcx).as_deref(),
749-
containing_item.const_stable_since(tcx).as_deref(),
750750
true,
751751
None,
752752
false,
@@ -1024,7 +1024,6 @@ fn render_assoc_items(
10241024
Some(v) => v,
10251025
None => return,
10261026
};
1027-
let tcx = cx.tcx();
10281027
let cache = cx.cache();
10291028
let (non_trait, traits): (Vec<_>, _) = v.iter().partition(|i| i.inner_impl().trait_.is_none());
10301029
if !non_trait.is_empty() {
@@ -1058,8 +1057,6 @@ fn render_assoc_items(
10581057
containing_item,
10591058
AssocItemLink::Anchor(None),
10601059
render_mode,
1061-
containing_item.stable_since(tcx).as_deref(),
1062-
containing_item.const_stable_since(tcx).as_deref(),
10631060
true,
10641061
None,
10651062
false,
@@ -1260,8 +1257,6 @@ fn render_impl(
12601257
parent: &clean::Item,
12611258
link: AssocItemLink<'_>,
12621259
render_mode: RenderMode,
1263-
outer_version: Option<&str>,
1264-
outer_const_version: Option<&str>,
12651260
show_def_docs: bool,
12661261
use_absolute: Option<bool>,
12671262
is_on_foreign_type: bool,
@@ -1278,17 +1273,18 @@ fn render_impl(
12781273
// For trait implementations, the `interesting` output contains all methods that have doc
12791274
// comments, and the `boring` output contains all methods that do not. The distinction is
12801275
// used to allow hiding the boring methods.
1276+
// `containing_item` is used for rendering stability info. If the parent is a trait impl,
1277+
// `containing_item` will the grandparent, since trait impls can't have stability attached.
12811278
fn doc_impl_item(
12821279
boring: &mut Buffer,
12831280
interesting: &mut Buffer,
12841281
cx: &Context<'_>,
12851282
item: &clean::Item,
12861283
parent: &clean::Item,
1284+
containing_item: &clean::Item,
12871285
link: AssocItemLink<'_>,
12881286
render_mode: RenderMode,
12891287
is_default_item: bool,
1290-
outer_version: Option<&str>,
1291-
outer_const_version: Option<&str>,
12921288
trait_: Option<&clean::Trait>,
12931289
show_def_docs: bool,
12941290
) {
@@ -1362,7 +1358,7 @@ fn render_impl(
13621358
"<div id=\"{}\" class=\"{}{} has-srclink\">",
13631359
id, item_type, in_trait_class,
13641360
);
1365-
render_rightside(w, cx, item, outer_version, outer_const_version);
1361+
render_rightside(w, cx, item, containing_item);
13661362
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
13671363
w.write_str("<code>");
13681364
render_assoc_item(
@@ -1406,7 +1402,7 @@ fn render_impl(
14061402
"<div id=\"{}\" class=\"{}{} has-srclink\">",
14071403
id, item_type, in_trait_class
14081404
);
1409-
render_rightside(w, cx, item, outer_version, outer_const_version);
1405+
render_rightside(w, cx, item, containing_item);
14101406
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
14111407
w.write_str("<code>");
14121408
assoc_const(
@@ -1461,11 +1457,10 @@ fn render_impl(
14611457
cx,
14621458
trait_item,
14631459
if trait_.is_some() { &i.impl_item } else { parent },
1460+
parent,
14641461
link,
14651462
render_mode,
14661463
false,
1467-
outer_version,
1468-
outer_const_version,
14691464
trait_.map(|t| &t.trait_),
14701465
show_def_docs,
14711466
);
@@ -1478,9 +1473,8 @@ fn render_impl(
14781473
t: &clean::Trait,
14791474
i: &clean::Impl,
14801475
parent: &clean::Item,
1476+
containing_item: &clean::Item,
14811477
render_mode: RenderMode,
1482-
outer_version: Option<&str>,
1483-
outer_const_version: Option<&str>,
14841478
show_def_docs: bool,
14851479
) {
14861480
for trait_item in &t.items {
@@ -1498,11 +1492,10 @@ fn render_impl(
14981492
cx,
14991493
trait_item,
15001494
parent,
1495+
containing_item,
15011496
assoc_link,
15021497
render_mode,
15031498
true,
1504-
outer_version,
1505-
outer_const_version,
15061499
Some(t),
15071500
show_def_docs,
15081501
);
@@ -1522,9 +1515,8 @@ fn render_impl(
15221515
&t.trait_,
15231516
&i.inner_impl(),
15241517
&i.impl_item,
1518+
parent,
15251519
render_mode,
1526-
outer_version,
1527-
outer_const_version,
15281520
show_def_docs,
15291521
);
15301522
}
@@ -1541,8 +1533,7 @@ fn render_impl(
15411533
cx,
15421534
i,
15431535
parent,
1544-
outer_version,
1545-
outer_const_version,
1536+
parent,
15461537
show_def_docs,
15471538
use_absolute,
15481539
is_on_foreign_type,
@@ -1578,12 +1569,13 @@ fn render_impl(
15781569
w.write_str(&close_tags);
15791570
}
15801571

1572+
// Render the items that appear on the right side of methods, impls, and
1573+
// associated types. For example "1.0.0 (const: 1.39.0) [src]".
15811574
fn render_rightside(
15821575
w: &mut Buffer,
15831576
cx: &Context<'_>,
15841577
item: &clean::Item,
1585-
outer_version: Option<&str>,
1586-
outer_const_version: Option<&str>,
1578+
containing_item: &clean::Item,
15871579
) {
15881580
let tcx = cx.tcx();
15891581

@@ -1592,8 +1584,8 @@ fn render_rightside(
15921584
w,
15931585
item.stable_since(tcx).as_deref(),
15941586
item.const_stable_since(tcx).as_deref(),
1595-
outer_version,
1596-
outer_const_version,
1587+
containing_item.stable_since(tcx).as_deref(),
1588+
containing_item.const_stable_since(tcx).as_deref(),
15971589
);
15981590

15991591
write_srclink(cx, item, w);
@@ -1605,8 +1597,7 @@ pub(crate) fn render_impl_summary(
16051597
cx: &Context<'_>,
16061598
i: &Impl,
16071599
parent: &clean::Item,
1608-
outer_version: Option<&str>,
1609-
outer_const_version: Option<&str>,
1600+
containing_item: &clean::Item,
16101601
show_def_docs: bool,
16111602
use_absolute: Option<bool>,
16121603
is_on_foreign_type: bool,
@@ -1630,7 +1621,7 @@ pub(crate) fn render_impl_summary(
16301621
format!(" data-aliases=\"{}\"", aliases.join(","))
16311622
};
16321623
write!(w, "<div id=\"{}\" class=\"impl has-srclink\"{}>", id, aliases);
1633-
render_rightside(w, cx, &i.impl_item, outer_version, outer_const_version);
1624+
render_rightside(w, cx, &i.impl_item, containing_item);
16341625
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
16351626
write!(w, "<code class=\"in-band\">");
16361627

src/librustdoc/html/render/print_item.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -694,15 +694,12 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
694694
write_small_section_header(w, "foreign-impls", "Implementations on Foreign Types", "");
695695

696696
for implementor in foreign {
697-
let outer_version = implementor.impl_item.stable_since(cx.tcx());
698-
let outer_const_version = implementor.impl_item.const_stable_since(cx.tcx());
699697
render_impl_summary(
700698
w,
701699
cx,
702700
&implementor,
703701
it,
704-
outer_version.as_deref(),
705-
outer_const_version.as_deref(),
702+
&implementor.impl_item,
706703
false,
707704
None,
708705
true,
@@ -1319,15 +1316,12 @@ fn render_implementor(
13191316
} => implementor_dups[&path.last()].1,
13201317
_ => false,
13211318
};
1322-
let outer_version = trait_.stable_since(cx.tcx());
1323-
let outer_const_version = trait_.const_stable_since(cx.tcx());
13241319
render_impl_summary(
13251320
w,
13261321
cx,
13271322
implementor,
13281323
trait_,
1329-
outer_version.as_deref(),
1330-
outer_const_version.as_deref(),
1324+
trait_,
13311325
false,
13321326
Some(use_absolute),
13331327
false,

src/librustdoc/html/static/rustdoc.css

+4
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,10 @@ a.test-arrow:hover{
10371037
opacity: 1;
10381038
}
10391039

1040+
:target {
1041+
padding-right: 3px;
1042+
}
1043+
10401044
.information {
10411045
position: absolute;
10421046
left: -25px;

src/librustdoc/html/static/themes/ayu.css

+4-1
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,11 @@ a.test-arrow:hover {
334334
color: #999;
335335
}
336336

337-
:target > code, :target > .in-band {
337+
:target, :target * {
338338
background: rgba(255, 236, 164, 0.06);
339+
}
340+
341+
:target {
339342
border-right: 3px solid rgba(255, 180, 76, 0.85);
340343
}
341344

src/librustdoc/html/static/themes/dark.css

+4-1
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,11 @@ a.test-arrow:hover{
282282
color: #999;
283283
}
284284

285-
:target > code, :target > .in-band {
285+
:target, :target * {
286286
background-color: #494a3d;
287+
}
288+
289+
:target {
287290
border-right: 3px solid #bb7410;
288291
}
289292

src/librustdoc/html/static/themes/light.css

+4-1
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,11 @@ a.test-arrow:hover{
275275
color: #999;
276276
}
277277

278-
:target > code, :target > .in-band {
278+
:target, :target * {
279279
background: #FDFFD3;
280+
}
281+
282+
:target {
280283
border-right: 3px solid #ffb44c;
281284
}
282285

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

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

5-
// @has foo/trait.Iterator.html '//div[@id="method.zip"]/a[@class="srclink"]' "[src]"
5+
// @has foo/trait.Iterator.html '//div[@id="method.zip"]//a[@class="srclink"]' "[src]"
66
pub use std::iter::Iterator;

src/test/rustdoc/trait-impl-items-links-and-anchors.rs

-8
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,15 @@ impl MyTrait for Vec<u8> {
3838
}
3939

4040
impl MyTrait for MyStruct {
41-
// @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="associatedtype.Assoc-3"]//a[@class="type"]/@href' #associatedtype.Assoc
42-
// @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="associatedtype.Assoc-3"]//a[@class="anchor"]/@href' #associatedtype.Assoc-3
4341
// @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="associatedtype.Assoc"]//a[@class="type"]/@href' trait.MyTrait.html#associatedtype.Assoc
4442
// @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="associatedtype.Assoc"]//a[@class="anchor"]/@href' #associatedtype.Assoc
4543
type Assoc = bool;
46-
// @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="associatedconstant.VALUE-3"]//a[@class="constant"]/@href' #associatedconstant.VALUE
47-
// @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="associatedconstant.VALUE-3"]//a[@class="anchor"]/@href' #associatedconstant.VALUE-3
4844
// @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="associatedconstant.VALUE"]//a[@class="constant"]/@href' trait.MyTrait.html#associatedconstant.VALUE
4945
// @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="associatedconstant.VALUE"]//a[@class="anchor"]/@href' #associatedconstant.VALUE
5046
const VALUE: u32 = 20;
51-
// @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="method.trait_function-2"]//a[@class="fnname"]/@href' #tymethod.trait_function
52-
// @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="method.trait_function-2"]//a[@class="anchor"]/@href' #method.trait_function-2
5347
// @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
5448
// @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="method.trait_function"]//a[@class="anchor"]/@href' #method.trait_function
5549
fn trait_function(&self) {}
56-
// @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="method.defaulted_override-3"]//a[@class="fnname"]/@href' #method.defaulted_override
57-
// @has trait_impl_items_links_and_anchors/trait.MyTrait.html '//div[@id="method.defaulted_override-3"]//a[@class="anchor"]/@href' #method.defaulted_override-3
5850
// @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
5951
// @has trait_impl_items_links_and_anchors/struct.MyStruct.html '//div[@id="method.defaulted_override"]//a[@class="anchor"]/@href' #method.defaulted_override
6052
fn defaulted_override(&self) {}

0 commit comments

Comments
 (0)