Skip to content

Commit c6948b5

Browse files
committed
Further simplify print_where_clause
This commit (or one of the prior ones) mostly fixes rust-lang#112901, though it causes one case (item-decl for trait assoc ty) to regress in that issue.
1 parent 4b7aed8 commit c6948b5

File tree

1 file changed

+37
-27
lines changed

1 file changed

+37
-27
lines changed

src/librustdoc/html/format.rs

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -276,32 +276,39 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
276276
indent: usize,
277277
ending: Ending,
278278
) -> impl fmt::Display + 'a + Captures<'tcx> {
279+
let where_indent = 3;
280+
let padding_amount = if ending == Ending::Newline {
281+
indent + 4
282+
} else if indent == 0 {
283+
4
284+
} else {
285+
indent + where_indent + "where ".len()
286+
};
287+
279288
display_fn(move |f| {
280-
let mut where_predicates = gens.where_predicates.iter().filter(|pred| {
281-
!matches!(pred, clean::WherePredicate::BoundPredicate { bounds, .. } if bounds.is_empty())
282-
}).map(|pred|
283-
print_where_pred(cx,pred)
284-
).peekable();
289+
let mut where_predicates = gens
290+
.where_predicates
291+
.iter()
292+
.filter(|pred| {
293+
!matches!(pred, clean::WherePredicate::BoundPredicate { bounds, .. } if bounds.is_empty())
294+
})
295+
.enumerate()
296+
.map(|(i, pred)| {
297+
let (padding_amount, add_newline) = if i == 0 && ending == Ending::NoNewline && indent > 0 {
298+
// put the first one on the same line as the 'where' keyword
299+
(1, false)
300+
} else {
301+
(padding_amount, true)
302+
};
303+
print_where_pred_helper(cx, pred, padding_amount, add_newline)
304+
})
305+
.peekable();
285306

286307
if where_predicates.peek().is_none() {
287308
return Ok(());
288309
}
289310

290-
let where_preds = comma_sep(where_predicates, false);
291-
let mut br_with_padding = String::with_capacity(6 * indent + 28);
292-
br_with_padding.push('\n');
293-
let where_indent = 3;
294-
let padding_amount = if ending == Ending::Newline {
295-
indent + 4
296-
} else if indent == 0 {
297-
4
298-
} else {
299-
indent + where_indent + "where ".len()
300-
};
301-
for _ in 0..padding_amount {
302-
br_with_padding.push(' ');
303-
}
304-
let where_preds = where_preds.to_string().replace('\n', &br_with_padding);
311+
let where_preds = comma_sep(where_predicates, false).to_string();
305312
let clause = if ending == Ending::Newline {
306313
let mut clause = " ".repeat(indent.saturating_sub(1));
307314
write!(clause, "<span class=\"where fmt-newline\">where{where_preds},</span>")?;
@@ -311,23 +318,26 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
311318
if indent == 0 {
312319
format!("\n<span class=\"where\">where{where_preds}</span>")
313320
} else {
314-
// put the first one on the same line as the 'where' keyword
315-
let where_preds = where_preds.replacen(&br_with_padding, " ", 1);
316-
317-
write!(br_with_padding, "<span class=\"where\">where{where_preds}</span>")?;
318-
br_with_padding
321+
let padding_str: String =
322+
iter::once("\n").chain(iter::repeat(" ").take(padding_amount)).collect();
323+
format!("\n{padding_str}<span class=\"where\">where{where_preds}</span>")
319324
}
320325
};
321326
write!(f, "{clause}")
322327
})
323328
}
324329

325-
fn print_where_pred<'a, 'tcx: 'a>(
330+
fn print_where_pred_helper<'a, 'tcx: 'a>(
326331
cx: &'a Context<'tcx>,
327332
pred: &'a clean::WherePredicate,
333+
indent_len: usize,
334+
add_newline: bool,
328335
) -> impl Display + Captures<'a> + Captures<'tcx> {
329336
display_fn(move |f| {
330-
f.write_str("\n")?;
337+
if add_newline {
338+
f.write_str("\n")?;
339+
}
340+
f.write_str(&" ".repeat(indent_len))?;
331341

332342
match pred {
333343
clean::WherePredicate::BoundPredicate { ty, bounds, bound_params } => {

0 commit comments

Comments
 (0)