Skip to content

Commit

Permalink
Split on words instead
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Dec 13, 2018
1 parent 2403146 commit 987bf2e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
18 changes: 16 additions & 2 deletions src/librustdoc/html/markdown.rs
Expand Up @@ -806,6 +806,10 @@ impl<'a> fmt::Display for MarkdownSummaryLine<'a> {
}

pub fn plain_summary_line(md: &str) -> String {
plain_summary_line_full(md, false)
}

pub fn plain_summary_line_full(md: &str, limit_length: bool) -> String {
struct ParserWrapper<'a> {
inner: Parser<'a>,
is_in: isize,
Expand Down Expand Up @@ -852,8 +856,18 @@ pub fn plain_summary_line(md: &str) -> String {
s.push_str(&t);
}
}
if s.len() > 60 {
s.chars().take(60).collect::<String>()
if limit_length && s.chars().count() > 60 {
let mut len = 0;
let mut ret = s.split_whitespace()
.take_while(|p| {
// + 1 for the added character after the word.
len += p.chars().count() + 1;
len < 60
})
.collect::<Vec<_>>()
.join(" ");
ret.push('…');
ret
} else {
s
}
Expand Down
16 changes: 11 additions & 5 deletions src/librustdoc/html/render.rs
Expand Up @@ -698,7 +698,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
ty: item.type_(),
name: item.name.clone().unwrap(),
path: fqp[..fqp.len() - 1].join("::"),
desc: plain_summary_line(item.doc_value()),
desc: plain_summary_line_short(item.doc_value()),
parent: Some(did),
parent_idx: None,
search_type: get_index_search_type(&item),
Expand Down Expand Up @@ -736,7 +736,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
}

let crate_doc = krate.module.as_ref().map(|module| {
plain_summary_line(module.doc_value())
plain_summary_line_short(module.doc_value())
}).unwrap_or(String::new());

let mut crate_data = BTreeMap::new();
Expand Down Expand Up @@ -1487,7 +1487,7 @@ impl DocFolder for Cache {
ty: item.type_(),
name: s.to_string(),
path: path.join("::"),
desc: plain_summary_line(item.doc_value()),
desc: plain_summary_line_short(item.doc_value()),
parent,
parent_idx: None,
search_type: get_index_search_type(&item),
Expand Down Expand Up @@ -1679,7 +1679,7 @@ impl<'a> Cache {
ty: item.type_(),
name: item_name.to_string(),
path: path.clone(),
desc: plain_summary_line(item.doc_value()),
desc: plain_summary_line_short(item.doc_value()),
parent: None,
parent_idx: None,
search_type: get_index_search_type(&item),
Expand Down Expand Up @@ -2396,7 +2396,13 @@ fn shorter<'a>(s: Option<&'a str>) -> String {
#[inline]
fn plain_summary_line(s: Option<&str>) -> String {
let line = shorter(s).replace("\n", " ");
markdown::plain_summary_line(&line[..])
markdown::plain_summary_line_full(&line[..], false)
}

#[inline]
fn plain_summary_line_short(s: Option<&str>) -> String {
let line = shorter(s).replace("\n", " ");
markdown::plain_summary_line_full(&line[..], true)
}

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

0 comments on commit 987bf2e

Please sign in to comment.