Skip to content

Commit 4a6921e

Browse files
author
QuietMisdreavus
committed
rustdoc: add line breaks to where clauses a la rustfmt
1 parent 0b2c356 commit 4a6921e

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

src/librustdoc/html/format.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,48 +161,60 @@ impl<'a> fmt::Display for WhereClause<'a> {
161161
if gens.where_predicates.is_empty() {
162162
return Ok(());
163163
}
164+
let mut clause = String::new();
164165
if f.alternate() {
165-
f.write_str(" ")?;
166+
clause.push_str(" where ");
166167
} else {
167-
f.write_str(" <span class='where'>where ")?;
168+
clause.push_str(" <span class='where'>where ");
168169
}
169170
for (i, pred) in gens.where_predicates.iter().enumerate() {
170171
if i > 0 {
171-
f.write_str(", ")?;
172+
if f.alternate() {
173+
clause.push_str(", ");
174+
} else {
175+
clause.push_str(",<br>");
176+
}
172177
}
173178
match pred {
174179
&clean::WherePredicate::BoundPredicate { ref ty, ref bounds } => {
175180
let bounds = bounds;
176181
if f.alternate() {
177-
write!(f, "{:#}: {:#}", ty, TyParamBounds(bounds))?;
182+
clause.push_str(&format!("{:#}: {:#}", ty, TyParamBounds(bounds)));
178183
} else {
179-
write!(f, "{}: {}", ty, TyParamBounds(bounds))?;
184+
clause.push_str(&format!("{}: {}", ty, TyParamBounds(bounds)));
180185
}
181186
}
182187
&clean::WherePredicate::RegionPredicate { ref lifetime,
183188
ref bounds } => {
184-
write!(f, "{}: ", lifetime)?;
189+
clause.push_str(&format!("{}: ", lifetime));
185190
for (i, lifetime) in bounds.iter().enumerate() {
186191
if i > 0 {
187-
f.write_str(" + ")?;
192+
clause.push_str(" + ");
188193
}
189194

190-
write!(f, "{}", lifetime)?;
195+
clause.push_str(&format!("{}", lifetime));
191196
}
192197
}
193198
&clean::WherePredicate::EqPredicate { ref lhs, ref rhs } => {
194199
if f.alternate() {
195-
write!(f, "{:#} == {:#}", lhs, rhs)?;
200+
clause.push_str(&format!("{:#} == {:#}", lhs, rhs));
196201
} else {
197-
write!(f, "{} == {}", lhs, rhs)?;
202+
clause.push_str(&format!("{} == {}", lhs, rhs));
198203
}
199204
}
200205
}
201206
}
202207
if !f.alternate() {
203208
f.write_str("</span>")?;
209+
let plain = format!("{:#}", self);
210+
if plain.len() > 80 {
211+
let padding = repeat("&nbsp;").take(8).collect::<String>();
212+
clause = clause.replace("<br>", &format!("<br>{}", padding));
213+
} else {
214+
clause = clause.replace("<br>", " ");
215+
}
204216
}
205-
Ok(())
217+
write!(f, "{}", clause)
206218
}
207219
}
208220

src/test/rustdoc/line-breaks.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
#![crate_name = "foo"]
1212

13+
use std::ops::Add;
14+
use std::fmt::Display;
15+
1316
//@count foo/fn.function_with_a_really_long_name.html //pre/br 2
1417
pub fn function_with_a_really_long_name(parameter_one: i32,
1518
parameter_two: i32)
@@ -19,3 +22,19 @@ pub fn function_with_a_really_long_name(parameter_one: i32,
1922

2023
//@count foo/fn.short_name.html //pre/br 0
2124
pub fn short_name(param: i32) -> i32 { param + 1 }
25+
26+
//@count foo/fn.where_clause.html //pre/br 4
27+
pub fn where_clause<T, U>(param_one: T,
28+
param_two: U)
29+
where T: Add<U> + Display + Copy,
30+
U: Add<T> + Display + Copy,
31+
T::Output: Display + Add<U::Output> + Copy,
32+
<T::Output as Add<U::Output>>::Output: Display,
33+
U::Output: Display + Copy
34+
{
35+
let x = param_one + param_two;
36+
println!("{} + {} = {}", param_one, param_two, x);
37+
let y = param_two + param_one;
38+
println!("{} + {} = {}", param_two, param_one, y);
39+
println!("{} + {} = {}", x, y, x + y);
40+
}

0 commit comments

Comments
 (0)