Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup some nits from #44238 #44329

Merged
merged 1 commit into from
Sep 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 22 additions & 80 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,12 +606,20 @@ pub fn run(mut krate: clean::Crate,
}

// A short, single-line view of `s`.
fn concise_str(s: &str) -> String {
fn concise_str(mut s: &str) -> String {
if s.contains('\n') {
return format!("{}...", s.lines().next().expect("Impossible! We just found a newline"));
s = s.lines().next().expect("Impossible! We just found a newline");
}
if s.len() > 70 {
return format!("{} ... {}", &s[..50], &s[s.len()-20..]);
let mut lo = 50;
let mut hi = s.len() - 20;
while !s.is_char_boundary(lo) {
lo -= 1;
}
while !s.is_char_boundary(hi) {
hi += 1;
}
return format!("{} ... {}", &s[..lo], &s[hi..]);
}
s.to_owned()
}
Expand Down Expand Up @@ -1756,18 +1764,18 @@ fn render_markdown(w: &mut fmt::Formatter,
// We only emit warnings if the user has opted-in to Pulldown rendering.
let output = if render_type == RenderType::Pulldown {
let pulldown_output = format!("{}", Markdown(md_text, RenderType::Pulldown));
let differences = html_diff::get_differences(&pulldown_output, &hoedown_output);
let differences = differences.into_iter()
.filter(|s| {
match *s {
html_diff::Difference::NodeText { ref elem_text,
ref opposite_elem_text,
.. }
if match_non_whitespace(elem_text, opposite_elem_text) => false,
_ => true,
let mut differences = html_diff::get_differences(&pulldown_output, &hoedown_output);
differences.retain(|s| {
match *s {
html_diff::Difference::NodeText { ref elem_text,
ref opposite_elem_text,
.. }
if elem_text.split_whitespace().eq(opposite_elem_text.split_whitespace()) => {
false
}
})
.collect::<Vec<_>>();
_ => true,
}
});

if !differences.is_empty() {
scx.markdown_warnings.borrow_mut().push((span, md_text.to_owned(), differences));
Expand All @@ -1781,40 +1789,6 @@ fn render_markdown(w: &mut fmt::Formatter,
write!(w, "<div class='docblock'>{}{}</div>", prefix, output)
}

// Returns true iff s1 and s2 match, ignoring whitespace.
fn match_non_whitespace(s1: &str, s2: &str) -> bool {
let s1 = s1.trim();
let s2 = s2.trim();
let mut cs1 = s1.chars();
let mut cs2 = s2.chars();
while let Some(c1) = cs1.next() {
if c1.is_whitespace() {
continue;
}

loop {
if let Some(c2) = cs2.next() {
if !c2.is_whitespace() {
if c1 != c2 {
return false;
}
break;
}
} else {
return false;
}
}
}

while let Some(c2) = cs2.next() {
if !c2.is_whitespace() {
return false;
}
}

true
}

fn document_short(w: &mut fmt::Formatter, item: &clean::Item, link: AssocItemLink,
cx: &Context, prefix: &str) -> fmt::Result {
if let Some(s) = item.doc_value() {
Expand Down Expand Up @@ -3729,35 +3703,3 @@ fn test_name_sorting() {
sorted.sort_by_key(|&s| name_key(s));
assert_eq!(names, sorted);
}

#[cfg(test)]
#[test]
fn test_match_non_whitespace() {
assert!(match_non_whitespace("", ""));
assert!(match_non_whitespace(" ", ""));
assert!(match_non_whitespace("", " "));

assert!(match_non_whitespace("a", "a"));
assert!(match_non_whitespace(" a ", "a"));
assert!(match_non_whitespace("a", " a"));
assert!(match_non_whitespace("abc", "abc"));
assert!(match_non_whitespace("abc", " abc "));
assert!(match_non_whitespace("abc ", "abc"));
assert!(match_non_whitespace("abc xyz", "abc xyz"));
assert!(match_non_whitespace("abc xyz", "abc\nxyz"));
assert!(match_non_whitespace("abc xyz", "abcxyz"));
assert!(match_non_whitespace("abcxyz", "abc xyz"));
assert!(match_non_whitespace("abc xyz ", " abc xyz\n"));

assert!(!match_non_whitespace("a", "b"));
assert!(!match_non_whitespace(" a ", "c"));
assert!(!match_non_whitespace("a", " aa"));
assert!(!match_non_whitespace("abc", "ac"));
assert!(!match_non_whitespace("abc", " adc "));
assert!(!match_non_whitespace("abc ", "abca"));
assert!(!match_non_whitespace("abc xyz", "abc xy"));
assert!(!match_non_whitespace("abc xyz", "bc\nxyz"));
assert!(!match_non_whitespace("abc xyz", "abc.xyz"));
assert!(!match_non_whitespace("abcxyz", "abc.xyz"));
assert!(!match_non_whitespace("abc xyz ", " abc xyz w"));
}