Skip to content

Commit 3f4d915

Browse files
committed
Rollup merge of rust-lang#33679 - Manishearth:rustdoc-readmore-impls, r=alexcrichton
rustdoc: Add doc snippets for trait impls, with a read more link The read more link only appears if the documentation is more than one line long. ![screenshot from 2016-05-17 06 54 14](https://cloud.githubusercontent.com/assets/1617736/15308544/4c2ba0ce-1bfc-11e6-9add-29de8dc7ac6e.png) It currently does not appear on non-defaulted methods, since you can document them directly. I could make it so that default documentation gets forwarded if regular docs don't exist. Fixes rust-lang#33672 r? @alexcrichton cc @steveklabnik
2 parents 87a2288 + 9ce018b commit 3f4d915

File tree

2 files changed

+62
-18
lines changed

2 files changed

+62
-18
lines changed

src/librustdoc/html/render.rs

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,19 @@ fn document(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item) -> fmt::Re
16581658
Ok(())
16591659
}
16601660

1661+
fn document_short(w: &mut fmt::Formatter, item: &clean::Item, link: AssocItemLink) -> fmt::Result {
1662+
if let Some(s) = item.doc_value() {
1663+
let markdown = if s.contains('\n') {
1664+
format!("{} [Read more]({})",
1665+
&plain_summary_line(Some(s)), naive_assoc_href(item, link))
1666+
} else {
1667+
format!("{}", &plain_summary_line(Some(s)))
1668+
};
1669+
write!(w, "<div class='docblock'>{}</div>", Markdown(&markdown))?;
1670+
}
1671+
Ok(())
1672+
}
1673+
16611674
fn item_module(w: &mut fmt::Formatter, cx: &Context,
16621675
item: &clean::Item, items: &[clean::Item]) -> fmt::Result {
16631676
document(w, cx, item)?;
@@ -2555,8 +2568,9 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
25552568
}
25562569

25572570
fn doctraititem(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item,
2558-
link: AssocItemLink, render_static: bool, is_default_item: bool,
2559-
outer_version: Option<&str>) -> fmt::Result {
2571+
link: AssocItemLink, render_static: bool,
2572+
is_default_item: bool, outer_version: Option<&str>,
2573+
trait_: Option<&clean::Trait>) -> fmt::Result {
25602574
let shortty = shortty(item);
25612575
let name = item.name.as_ref().unwrap();
25622576

@@ -2607,16 +2621,35 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
26072621
_ => panic!("can't make docs for trait item with name {:?}", item.name)
26082622
}
26092623

2610-
if !is_default_item && (!is_static || render_static) {
2611-
document(w, cx, item)
2612-
} else {
2613-
Ok(())
2624+
if !is_static || render_static {
2625+
if !is_default_item {
2626+
2627+
if item.doc_value().is_some() {
2628+
document(w, cx, item)?;
2629+
} else {
2630+
// In case the item isn't documented,
2631+
// provide short documentation from the trait
2632+
if let Some(t) = trait_ {
2633+
if let Some(it) = t.items.iter()
2634+
.find(|i| i.name == item.name) {
2635+
document_short(w, it, link)?;
2636+
}
2637+
}
2638+
}
2639+
} else {
2640+
document_short(w, item, link)?;
2641+
}
26142642
}
2643+
Ok(())
26152644
}
26162645

2646+
let traits = &cache().traits;
2647+
let trait_ = i.trait_did().and_then(|did| traits.get(&did));
2648+
26172649
write!(w, "<div class='impl-items'>")?;
26182650
for trait_item in &i.inner_impl().items {
2619-
doctraititem(w, cx, trait_item, link, render_header, false, outer_version)?;
2651+
doctraititem(w, cx, trait_item, link, render_header,
2652+
false, outer_version, trait_)?;
26202653
}
26212654

26222655
fn render_default_items(w: &mut fmt::Formatter,
@@ -2634,17 +2667,15 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
26342667
let assoc_link = AssocItemLink::GotoSource(did, &i.provided_trait_methods);
26352668

26362669
doctraititem(w, cx, trait_item, assoc_link, render_static, true,
2637-
outer_version)?;
2670+
outer_version, None)?;
26382671
}
26392672
Ok(())
26402673
}
26412674

26422675
// If we've implemented a trait, then also emit documentation for all
26432676
// default items which weren't overridden in the implementation block.
2644-
if let Some(did) = i.trait_did() {
2645-
if let Some(t) = cache().traits.get(&did) {
2646-
render_default_items(w, cx, t, &i.inner_impl(), render_header, outer_version)?;
2647-
}
2677+
if let Some(t) = trait_ {
2678+
render_default_items(w, cx, t, &i.inner_impl(), render_header, outer_version)?;
26482679
}
26492680
write!(w, "</div>")?;
26502681
Ok(())

src/test/rustdoc/manual_impl.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,24 @@ pub trait T {
2121
fn b_method(&self) -> usize {
2222
self.a_method()
2323
}
24+
25+
/// Docs associated with the trait c_method definition.
26+
///
27+
/// There is another line
28+
fn c_method(&self) -> usize {
29+
self.a_method()
30+
}
2431
}
2532

2633
// @has manual_impl/struct.S1.html '//*[@class="trait"]' 'T'
2734
// @has - '//*[@class="docblock"]' 'Docs associated with the S1 trait implementation.'
2835
// @has - '//*[@class="docblock"]' 'Docs associated with the S1 trait a_method implementation.'
2936
// @!has - '//*[@class="docblock"]' 'Docs associated with the trait a_method definition.'
30-
// @!has - '//*[@class="docblock"]' 'Docs associated with the trait b_method definition.'
37+
// @has - '//*[@class="docblock"]' 'Docs associated with the trait b_method definition.'
38+
// @has - '//*[@class="docblock"]' 'Docs associated with the trait b_method definition.'
39+
// @has - '//*[@class="docblock"]' 'Docs associated with the trait c_method definition.'
40+
// @!has - '//*[@class="docblock"]' 'There is another line'
41+
// @has - '//*[@class="docblock"]' 'Read more'
3142
pub struct S1(usize);
3243

3344
/// Docs associated with the S1 trait implementation.
@@ -41,9 +52,11 @@ impl T for S1 {
4152
// @has manual_impl/struct.S2.html '//*[@class="trait"]' 'T'
4253
// @has - '//*[@class="docblock"]' 'Docs associated with the S2 trait implementation.'
4354
// @has - '//*[@class="docblock"]' 'Docs associated with the S2 trait a_method implementation.'
44-
// @has - '//*[@class="docblock"]' 'Docs associated with the S2 trait b_method implementation.'
55+
// @has - '//*[@class="docblock"]' 'Docs associated with the S2 trait c_method implementation.'
4556
// @!has - '//*[@class="docblock"]' 'Docs associated with the trait a_method definition.'
46-
// @!has - '//*[@class="docblock"]' 'Docs associated with the trait b_method definition.'
57+
// @!has - '//*[@class="docblock"]' 'Docs associated with the trait c_method definition.'
58+
// @has - '//*[@class="docblock"]' 'Docs associated with the trait b_method definition.'
59+
// @!has - '//*[@class="docblock"]' 'Read more'
4760
pub struct S2(usize);
4861

4962
/// Docs associated with the S2 trait implementation.
@@ -53,16 +66,16 @@ impl T for S2 {
5366
self.0
5467
}
5568

56-
/// Docs associated with the S2 trait b_method implementation.
57-
fn b_method(&self) -> usize {
69+
/// Docs associated with the S2 trait c_method implementation.
70+
fn c_method(&self) -> usize {
5871
5
5972
}
6073
}
6174

6275
// @has manual_impl/struct.S3.html '//*[@class="trait"]' 'T'
6376
// @has - '//*[@class="docblock"]' 'Docs associated with the S3 trait implementation.'
6477
// @has - '//*[@class="docblock"]' 'Docs associated with the S3 trait b_method implementation.'
65-
// @!has - '//*[@class="docblock"]' 'Docs associated with the trait a_method definition.'
78+
// @has - '//*[@class="docblock"]' 'Docs associated with the trait a_method definition.'
6679
pub struct S3(usize);
6780

6881
/// Docs associated with the S3 trait implementation.

0 commit comments

Comments
 (0)