1
Fork 0

rustdoc: Clean up html::format::print_where_clause

This commit is contained in:
Roc Yu 2022-04-20 20:45:30 -04:00
parent 51ea9bb29b
commit 5d59c16c2d
No known key found for this signature in database
GPG key ID: 5068CE514A79D27F

View file

@ -267,6 +267,8 @@ crate fn print_where_clause<'a, 'tcx: 'a>(
indent: usize, indent: usize,
end_newline: bool, end_newline: bool,
) -> impl fmt::Display + 'a + Captures<'tcx> { ) -> impl fmt::Display + 'a + Captures<'tcx> {
use fmt::Write;
display_fn(move |f| { display_fn(move |f| {
let mut where_predicates = gens.where_predicates.iter().filter(|pred| { let mut where_predicates = gens.where_predicates.iter().filter(|pred| {
!matches!(pred, clean::WherePredicate::BoundPredicate { bounds, .. } if bounds.is_empty()) !matches!(pred, clean::WherePredicate::BoundPredicate { bounds, .. } if bounds.is_empty())
@ -280,56 +282,44 @@ crate fn print_where_clause<'a, 'tcx: 'a>(
match pred { match pred {
clean::WherePredicate::BoundPredicate { ty, bounds, bound_params } => { clean::WherePredicate::BoundPredicate { ty, bounds, bound_params } => {
let bounds = bounds; let ty_cx = ty.print(cx);
let for_prefix = if bound_params.is_empty() { let generic_bounds = print_generic_bounds(bounds, cx);
String::new()
} else if f.alternate() {
format!(
"for&lt;{:#}&gt; ",
comma_sep(bound_params.iter().map(|lt| lt.print()), true)
)
} else {
format!(
"for&lt;{}&gt; ",
comma_sep(bound_params.iter().map(|lt| lt.print()), true)
)
};
if f.alternate() { if bound_params.is_empty() {
write!( if f.alternate() {
f, write!(f, "{ty_cx:#}: {generic_bounds:#}")
"{}{:#}: {:#}", } else {
for_prefix, write!(f, "{ty_cx}: {generic_bounds}")
ty.print(cx), }
print_generic_bounds(bounds, cx)
)
} else { } else {
write!( if f.alternate() {
f, write!(
"{}{}: {}", f,
for_prefix, "for<{:#}> {ty_cx:#}: {generic_bounds:#}",
ty.print(cx), comma_sep(bound_params.iter().map(|lt| lt.print()), true)
print_generic_bounds(bounds, cx) )
) } else {
write!(
f,
"for&lt;{}&gt; {ty_cx}: {generic_bounds}",
comma_sep(bound_params.iter().map(|lt| lt.print()), true)
)
}
} }
} }
clean::WherePredicate::RegionPredicate { lifetime, bounds } => { clean::WherePredicate::RegionPredicate { lifetime, bounds } => {
write!( let mut bounds_display = String::new();
f, for bound in bounds.iter().map(|b| b.print(cx)) {
"{}: {}", write!(bounds_display, "{bound} + ")?;
lifetime.print(), }
bounds bounds_display.truncate(bounds_display.len() - " + ".len());
.iter() write!(f, "{}: {bounds_display}", lifetime.print())
.map(|b| b.print(cx).to_string())
.collect::<Vec<_>>()
.join(" + ")
)
} }
clean::WherePredicate::EqPredicate { lhs, rhs } => { clean::WherePredicate::EqPredicate { lhs, rhs } => {
if f.alternate() { if f.alternate() {
write!(f, "{:#} == {:#}", lhs.print(cx), rhs.print(cx),) write!(f, "{:#} == {:#}", lhs.print(cx), rhs.print(cx))
} else { } else {
write!(f, "{} == {}", lhs.print(cx), rhs.print(cx),) write!(f, "{} == {}", lhs.print(cx), rhs.print(cx))
} }
} }
} }
@ -340,41 +330,43 @@ crate fn print_where_clause<'a, 'tcx: 'a>(
return Ok(()); return Ok(());
} }
let mut clause = String::new(); let where_preds = comma_sep(where_predicates, false);
let clause = if f.alternate() {
if f.alternate() {
clause.push_str(" where");
} else {
if end_newline { if end_newline {
clause.push_str(" <span class=\"where fmt-newline\">where"); // add a space so stripping <br> tags and breaking spaces still renders properly
format!(" where{where_preds}, ")
} else { } else {
clause.push_str(" <span class=\"where\">where"); format!(" where{where_preds}")
} }
} } else {
let mut br_with_padding = String::with_capacity(6 * indent + 28);
br_with_padding.push_str("<br>");
for _ in 0..indent + 4 {
br_with_padding.push_str("&nbsp;");
}
let where_preds = where_preds.to_string().replace("<br>", &br_with_padding);
clause.push_str(&comma_sep(where_predicates, false).to_string()); if end_newline {
let mut clause = "&nbsp;".repeat(indent.saturating_sub(1));
if end_newline { // add a space so stripping <br> tags and breaking spaces still renders properly
clause.push(','); write!(
// add a space so stripping <br> tags and breaking spaces still renders properly clause,
if f.alternate() { " <span class=\"where fmt-newline\">where{where_preds},&nbsp;</span>"
clause.push(' '); )?;
clause
} else { } else {
clause.push_str("&nbsp;"); // insert a <br> tag after a single space but before multiple spaces at the start
if indent == 0 {
format!(" <br><span class=\"where\">where{where_preds}</span>")
} else {
let mut clause = br_with_padding;
clause.truncate(clause.len() - 5 * "&nbsp;".len());
write!(clause, " <span class=\"where\">where{where_preds}</span>")?;
clause
}
} }
} };
write!(f, "{clause}")
if !f.alternate() {
clause.push_str("</span>");
let padding = "&nbsp;".repeat(indent + 4);
clause = clause.replace("<br>", &format!("<br>{}", padding));
clause.insert_str(0, &"&nbsp;".repeat(indent.saturating_sub(1)));
if !end_newline {
// we insert the <br> after a single space but before multiple spaces at the start
clause.insert_str(if indent == 0 { 1 } else { 0 }, "<br>");
}
}
write!(f, "{}", clause)
}) })
} }