Skip to content

Commit 987bf2e

Browse files
Split on words instead
1 parent 2403146 commit 987bf2e

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,10 @@ impl<'a> fmt::Display for MarkdownSummaryLine<'a> {
806806
}
807807

808808
pub fn plain_summary_line(md: &str) -> String {
809+
plain_summary_line_full(md, false)
810+
}
811+
812+
pub fn plain_summary_line_full(md: &str, limit_length: bool) -> String {
809813
struct ParserWrapper<'a> {
810814
inner: Parser<'a>,
811815
is_in: isize,
@@ -852,8 +856,18 @@ pub fn plain_summary_line(md: &str) -> String {
852856
s.push_str(&t);
853857
}
854858
}
855-
if s.len() > 60 {
856-
s.chars().take(60).collect::<String>()
859+
if limit_length && s.chars().count() > 60 {
860+
let mut len = 0;
861+
let mut ret = s.split_whitespace()
862+
.take_while(|p| {
863+
// + 1 for the added character after the word.
864+
len += p.chars().count() + 1;
865+
len < 60
866+
})
867+
.collect::<Vec<_>>()
868+
.join(" ");
869+
ret.push('…');
870+
ret
857871
} else {
858872
s
859873
}

src/librustdoc/html/render.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
698698
ty: item.type_(),
699699
name: item.name.clone().unwrap(),
700700
path: fqp[..fqp.len() - 1].join("::"),
701-
desc: plain_summary_line(item.doc_value()),
701+
desc: plain_summary_line_short(item.doc_value()),
702702
parent: Some(did),
703703
parent_idx: None,
704704
search_type: get_index_search_type(&item),
@@ -736,7 +736,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
736736
}
737737

738738
let crate_doc = krate.module.as_ref().map(|module| {
739-
plain_summary_line(module.doc_value())
739+
plain_summary_line_short(module.doc_value())
740740
}).unwrap_or(String::new());
741741

742742
let mut crate_data = BTreeMap::new();
@@ -1487,7 +1487,7 @@ impl DocFolder for Cache {
14871487
ty: item.type_(),
14881488
name: s.to_string(),
14891489
path: path.join("::"),
1490-
desc: plain_summary_line(item.doc_value()),
1490+
desc: plain_summary_line_short(item.doc_value()),
14911491
parent,
14921492
parent_idx: None,
14931493
search_type: get_index_search_type(&item),
@@ -1679,7 +1679,7 @@ impl<'a> Cache {
16791679
ty: item.type_(),
16801680
name: item_name.to_string(),
16811681
path: path.clone(),
1682-
desc: plain_summary_line(item.doc_value()),
1682+
desc: plain_summary_line_short(item.doc_value()),
16831683
parent: None,
16841684
parent_idx: None,
16851685
search_type: get_index_search_type(&item),
@@ -2396,7 +2396,13 @@ fn shorter<'a>(s: Option<&'a str>) -> String {
23962396
#[inline]
23972397
fn plain_summary_line(s: Option<&str>) -> String {
23982398
let line = shorter(s).replace("\n", " ");
2399-
markdown::plain_summary_line(&line[..])
2399+
markdown::plain_summary_line_full(&line[..], false)
2400+
}
2401+
2402+
#[inline]
2403+
fn plain_summary_line_short(s: Option<&str>) -> String {
2404+
let line = shorter(s).replace("\n", " ");
2405+
markdown::plain_summary_line_full(&line[..], true)
24002406
}
24012407

24022408
fn document(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item) -> fmt::Result {

0 commit comments

Comments
 (0)