From 5d59c16c2d2eda4089834061a2d6a21a733d9370 Mon Sep 17 00:00:00 2001 From: Roc Yu Date: Wed, 20 Apr 2022 20:45:30 -0400 Subject: [PATCH] rustdoc: Clean up `html::format::print_where_clause` --- src/librustdoc/html/format.rs | 132 ++++++++++++++++------------------ 1 file changed, 62 insertions(+), 70 deletions(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index fd6d675dc8b8c..d3545236e3dfe 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -267,6 +267,8 @@ crate fn print_where_clause<'a, 'tcx: 'a>( indent: usize, end_newline: bool, ) -> impl fmt::Display + 'a + Captures<'tcx> { + use fmt::Write; + display_fn(move |f| { let mut where_predicates = gens.where_predicates.iter().filter(|pred| { !matches!(pred, clean::WherePredicate::BoundPredicate { bounds, .. } if bounds.is_empty()) @@ -280,56 +282,44 @@ crate fn print_where_clause<'a, 'tcx: 'a>( match pred { clean::WherePredicate::BoundPredicate { ty, bounds, bound_params } => { - let bounds = bounds; - let for_prefix = if bound_params.is_empty() { - String::new() - } else if f.alternate() { - format!( - "for<{:#}> ", - comma_sep(bound_params.iter().map(|lt| lt.print()), true) - ) - } else { - format!( - "for<{}> ", - comma_sep(bound_params.iter().map(|lt| lt.print()), true) - ) - }; + let ty_cx = ty.print(cx); + let generic_bounds = print_generic_bounds(bounds, cx); - if f.alternate() { - write!( - f, - "{}{:#}: {:#}", - for_prefix, - ty.print(cx), - print_generic_bounds(bounds, cx) - ) + if bound_params.is_empty() { + if f.alternate() { + write!(f, "{ty_cx:#}: {generic_bounds:#}") + } else { + write!(f, "{ty_cx}: {generic_bounds}") + } } else { - write!( - f, - "{}{}: {}", - for_prefix, - ty.print(cx), - print_generic_bounds(bounds, cx) - ) + if f.alternate() { + write!( + f, + "for<{:#}> {ty_cx:#}: {generic_bounds:#}", + comma_sep(bound_params.iter().map(|lt| lt.print()), true) + ) + } else { + write!( + f, + "for<{}> {ty_cx}: {generic_bounds}", + comma_sep(bound_params.iter().map(|lt| lt.print()), true) + ) + } } } clean::WherePredicate::RegionPredicate { lifetime, bounds } => { - write!( - f, - "{}: {}", - lifetime.print(), - bounds - .iter() - .map(|b| b.print(cx).to_string()) - .collect::>() - .join(" + ") - ) + let mut bounds_display = String::new(); + for bound in bounds.iter().map(|b| b.print(cx)) { + write!(bounds_display, "{bound} + ")?; + } + bounds_display.truncate(bounds_display.len() - " + ".len()); + write!(f, "{}: {bounds_display}", lifetime.print()) } clean::WherePredicate::EqPredicate { lhs, rhs } => { if f.alternate() { - write!(f, "{:#} == {:#}", lhs.print(cx), rhs.print(cx),) + write!(f, "{:#} == {:#}", lhs.print(cx), rhs.print(cx)) } 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(()); } - let mut clause = String::new(); - - if f.alternate() { - clause.push_str(" where"); - } else { + let where_preds = comma_sep(where_predicates, false); + let clause = if f.alternate() { if end_newline { - clause.push_str(" where"); + // add a space so stripping
tags and breaking spaces still renders properly + format!(" where{where_preds}, ") } else { - clause.push_str(" where"); + format!(" where{where_preds}") } - } - - clause.push_str(&comma_sep(where_predicates, false).to_string()); - - if end_newline { - clause.push(','); - // add a space so stripping
tags and breaking spaces still renders properly - if f.alternate() { - clause.push(' '); - } else { - clause.push_str(" "); + } else { + let mut br_with_padding = String::with_capacity(6 * indent + 28); + br_with_padding.push_str("
"); + for _ in 0..indent + 4 { + br_with_padding.push_str(" "); } - } + let where_preds = where_preds.to_string().replace("
", &br_with_padding); - if !f.alternate() { - clause.push_str("
"); - let padding = " ".repeat(indent + 4); - clause = clause.replace("
", &format!("
{}", padding)); - clause.insert_str(0, &" ".repeat(indent.saturating_sub(1))); - if !end_newline { - // we insert the
after a single space but before multiple spaces at the start - clause.insert_str(if indent == 0 { 1 } else { 0 }, "
"); + if end_newline { + let mut clause = " ".repeat(indent.saturating_sub(1)); + // add a space so stripping
tags and breaking spaces still renders properly + write!( + clause, + " where{where_preds}, " + )?; + clause + } else { + // insert a
tag after a single space but before multiple spaces at the start + if indent == 0 { + format!("
where{where_preds}") + } else { + let mut clause = br_with_padding; + clause.truncate(clause.len() - 5 * " ".len()); + write!(clause, " where{where_preds}")?; + clause + } } - } - write!(f, "{}", clause) + }; + write!(f, "{clause}") }) }