Skip to content

Commit d616f47

Browse files
committed
Auto merge of #41084 - QuietMisdreavus:rustdoc-format-redux, r=frewsxcxv,GuillaumeGomez
rustdoc: update formatting of fn signatures and where clauses to match style rfcs Recent updates to style RFCs ([where clauses](rust-lang/style-team#38), [function definitions](rust-lang/style-team#39)) changed the "canonical" style for these items, so this is a rustdoc update to make it emit that style where necessary. This is mainly a conversion from visual indent to block indent, which helps out in situations where there was excessive indent causing lines to wrap regardless. Samples: ![std::iter::IntoIterator](https://cloud.githubusercontent.com/assets/5217170/24712947/e586604c-19e9-11e7-87ae-4fe64d689dc3.png) ![excerpt from std::iter::Iterator](https://cloud.githubusercontent.com/assets/5217170/24713209/91e65112-19ea-11e7-9ff8-d4cf6b31aae1.png) ![std::iter::FromIterator](https://cloud.githubusercontent.com/assets/5217170/24713138/59f36114-19ea-11e7-9dbb-5f5ba7126e2e.png) ![std::cmp::min](https://cloud.githubusercontent.com/assets/5217170/24713038/1bab88b4-19ea-11e7-935d-defed5648de4.png) ![some trait impls on std::collections::HashMap](https://cloud.githubusercontent.com/assets/5217170/24713251/b7ef69e8-19ea-11e7-94a7-e01fbf89fa31.png) ![`fn extract_code_blocks`, an example given in #40687](https://cloud.githubusercontent.com/assets/5217170/24713159/672717cc-19ea-11e7-9acb-6ac278b90339.png) ![excerpt from itertools::Itertools](https://cloud.githubusercontent.com/assets/5217170/24713323/f06716ea-19ea-11e7-94cc-6ef68d9980ec.png) fixes #41025 and #40687 r? @rust-lang/docs
2 parents fa332c9 + 8dd4c44 commit d616f47

File tree

6 files changed

+137
-133
lines changed

6 files changed

+137
-133
lines changed

src/librustdoc/html/format.rs

+76-58
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,38 @@ pub struct UnsafetySpace(pub hir::Unsafety);
4141
/// with a space after it.
4242
#[derive(Copy, Clone)]
4343
pub struct ConstnessSpace(pub hir::Constness);
44-
/// Wrapper struct for properly emitting a method declaration.
45-
pub struct Method<'a>(pub &'a clean::FnDecl, pub usize);
4644
/// Similar to VisSpace, but used for mutability
4745
#[derive(Copy, Clone)]
4846
pub struct MutableSpace(pub clean::Mutability);
4947
/// Similar to VisSpace, but used for mutability
5048
#[derive(Copy, Clone)]
5149
pub struct RawMutableSpace(pub clean::Mutability);
52-
/// Wrapper struct for emitting a where clause from Generics.
53-
pub struct WhereClause<'a>(pub &'a clean::Generics, pub usize);
5450
/// Wrapper struct for emitting type parameter bounds.
5551
pub struct TyParamBounds<'a>(pub &'a [clean::TyParamBound]);
5652
/// Wrapper struct for emitting a comma-separated list of items
5753
pub struct CommaSep<'a, T: 'a>(pub &'a [T]);
5854
pub struct AbiSpace(pub Abi);
5955

56+
/// Wrapper struct for properly emitting a method declaration.
57+
pub struct Method<'a> {
58+
/// The declaration to emit.
59+
pub decl: &'a clean::FnDecl,
60+
/// The length of the function's "name", used to determine line-wrapping.
61+
pub name_len: usize,
62+
/// The number of spaces to indent each successive line with, if line-wrapping is necessary.
63+
pub indent: usize,
64+
}
65+
66+
/// Wrapper struct for emitting a where clause from Generics.
67+
pub struct WhereClause<'a>{
68+
/// The Generics from which to emit a where clause.
69+
pub gens: &'a clean::Generics,
70+
/// The number of spaces to indent each line with.
71+
pub indent: usize,
72+
/// Whether the where clause needs to add a comma and newline after the last bound.
73+
pub end_newline: bool,
74+
}
75+
6076
pub struct HRef<'a> {
6177
pub did: DefId,
6278
pub text: &'a str,
@@ -167,24 +183,27 @@ impl fmt::Display for clean::Generics {
167183

168184
impl<'a> fmt::Display for WhereClause<'a> {
169185
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
170-
let &WhereClause(gens, pad) = self;
186+
let &WhereClause { gens, indent, end_newline } = self;
171187
if gens.where_predicates.is_empty() {
172188
return Ok(());
173189
}
174190
let mut clause = String::new();
175191
if f.alternate() {
176-
clause.push_str(" where ");
192+
clause.push_str(" where");
177193
} else {
178-
clause.push_str(" <span class=\"where fmt-newline\">where ");
194+
if end_newline {
195+
clause.push_str(" <span class=\"where fmt-newline\">where");
196+
} else {
197+
clause.push_str(" <span class=\"where\">where");
198+
}
179199
}
180200
for (i, pred) in gens.where_predicates.iter().enumerate() {
181-
if i > 0 {
182-
if f.alternate() {
183-
clause.push_str(", ");
184-
} else {
185-
clause.push_str(",<br>");
186-
}
201+
if f.alternate() {
202+
clause.push(' ');
203+
} else {
204+
clause.push_str("<br>");
187205
}
206+
188207
match pred {
189208
&clean::WherePredicate::BoundPredicate { ref ty, ref bounds } => {
190209
let bounds = bounds;
@@ -213,21 +232,29 @@ impl<'a> fmt::Display for WhereClause<'a> {
213232
}
214233
}
215234
}
235+
236+
if i < gens.where_predicates.len() - 1 || end_newline {
237+
clause.push(',');
238+
}
216239
}
240+
241+
if end_newline {
242+
//add a space so stripping <br> tags and breaking spaces still renders properly
243+
if f.alternate() {
244+
clause.push(' ');
245+
} else {
246+
clause.push_str("&nbsp;");
247+
}
248+
}
249+
217250
if !f.alternate() {
218251
clause.push_str("</span>");
219-
let plain = format!("{:#}", self);
220-
if plain.len() + pad > 80 {
221-
// break it onto its own line regardless, but make sure method impls and trait
222-
// blocks keep their fixed padding (2 and 9, respectively)
223-
let padding = if pad > 10 {
224-
repeat("&nbsp;").take(8).collect::<String>()
225-
} else {
226-
repeat("&nbsp;").take(pad + 6).collect::<String>()
227-
};
228-
clause = clause.replace("<br>", &format!("<br>{}", padding));
229-
} else {
230-
clause = clause.replace("<br>", " ");
252+
let padding = repeat("&nbsp;").take(indent + 4).collect::<String>();
253+
clause = clause.replace("<br>", &format!("<br>{}", padding));
254+
clause.insert_str(0, &repeat("&nbsp;").take(indent.saturating_sub(1))
255+
.collect::<String>());
256+
if !end_newline {
257+
clause.insert_str(0, "<br>");
231258
}
232259
}
233260
write!(f, "{}", clause)
@@ -838,43 +865,35 @@ fn fmt_impl(i: &clean::Impl,
838865
f: &mut fmt::Formatter,
839866
link_trait: bool,
840867
use_absolute: bool) -> fmt::Result {
841-
let mut plain = String::new();
842-
843868
if f.alternate() {
844869
write!(f, "impl{:#} ", i.generics)?;
845870
} else {
846871
write!(f, "impl{} ", i.generics)?;
847872
}
848-
plain.push_str(&format!("impl{:#} ", i.generics));
849873

850874
if let Some(ref ty) = i.trait_ {
851875
if i.polarity == Some(clean::ImplPolarity::Negative) {
852876
write!(f, "!")?;
853-
plain.push_str("!");
854877
}
855878

856879
if link_trait {
857880
fmt::Display::fmt(ty, f)?;
858-
plain.push_str(&format!("{:#}", ty));
859881
} else {
860882
match *ty {
861883
clean::ResolvedPath { typarams: None, ref path, is_generic: false, .. } => {
862884
let last = path.segments.last().unwrap();
863885
fmt::Display::fmt(&last.name, f)?;
864886
fmt::Display::fmt(&last.params, f)?;
865-
plain.push_str(&format!("{:#}{:#}", last.name, last.params));
866887
}
867888
_ => unreachable!(),
868889
}
869890
}
870891
write!(f, " for ")?;
871-
plain.push_str(" for ");
872892
}
873893

874894
fmt_type(&i.for_, f, use_absolute, true)?;
875-
plain.push_str(&format!("{:#}", i.for_));
876895

877-
fmt::Display::fmt(&WhereClause(&i.generics, plain.len() + 1), f)?;
896+
fmt::Display::fmt(&WhereClause { gens: &i.generics, indent: 0, end_newline: true }, f)?;
878897
Ok(())
879898
}
880899

@@ -939,12 +958,15 @@ impl fmt::Display for clean::FnDecl {
939958

940959
impl<'a> fmt::Display for Method<'a> {
941960
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
942-
let decl = self.0;
943-
let indent = self.1;
961+
let &Method { decl, name_len, indent } = self;
944962
let amp = if f.alternate() { "&" } else { "&amp;" };
945963
let mut args = String::new();
946964
let mut args_plain = String::new();
947965
for (i, input) in decl.inputs.values.iter().enumerate() {
966+
if i == 0 {
967+
args.push_str("<br>");
968+
}
969+
948970
if let Some(selfty) = input.to_self() {
949971
match selfty {
950972
clean::SelfValue => {
@@ -970,7 +992,7 @@ impl<'a> fmt::Display for Method<'a> {
970992
}
971993
} else {
972994
if i > 0 {
973-
args.push_str("<br> ");
995+
args.push_str(" <br>");
974996
args_plain.push_str(" ");
975997
}
976998
if !input.name.is_empty() {
@@ -986,8 +1008,8 @@ impl<'a> fmt::Display for Method<'a> {
9861008
args_plain.push_str(&format!("{:#}", input.type_));
9871009
}
9881010
if i + 1 < decl.inputs.values.len() {
989-
args.push_str(",");
990-
args_plain.push_str(",");
1011+
args.push(',');
1012+
args_plain.push(',');
9911013
}
9921014
}
9931015

@@ -1003,27 +1025,23 @@ impl<'a> fmt::Display for Method<'a> {
10031025
format!("{}", decl.output)
10041026
};
10051027

1006-
let mut output: String;
1007-
let plain: String;
1008-
let pad = repeat(" ").take(indent).collect::<String>();
1009-
if arrow.is_empty() {
1010-
output = format!("({})", args);
1011-
plain = format!("{}({})", pad, args_plain);
1028+
let pad = repeat(" ").take(name_len).collect::<String>();
1029+
let plain = format!("{pad}({args}){arrow}",
1030+
pad = pad,
1031+
args = args_plain,
1032+
arrow = arrow_plain);
1033+
1034+
let output = if plain.len() > 80 {
1035+
let full_pad = format!("<br>{}", repeat("&nbsp;").take(indent + 4).collect::<String>());
1036+
let close_pad = format!("<br>{}", repeat("&nbsp;").take(indent).collect::<String>());
1037+
format!("({args}{close}){arrow}",
1038+
args = args.replace("<br>", &full_pad),
1039+
close = close_pad,
1040+
arrow = arrow)
10121041
} else {
1013-
output = format!("({args})<br>{arrow}", args = args, arrow = arrow);
1014-
plain = format!("{pad}({args}){arrow}",
1015-
pad = pad,
1016-
args = args_plain,
1017-
arrow = arrow_plain);
1018-
}
1042+
format!("({args}){arrow}", args = args.replace("<br>", ""), arrow = arrow)
1043+
};
10191044

1020-
if plain.len() > 80 {
1021-
let pad = repeat("&nbsp;").take(indent).collect::<String>();
1022-
let pad = format!("<br>{}", pad);
1023-
output = output.replace("<br>", &pad);
1024-
} else {
1025-
output = output.replace("<br>", "");
1026-
}
10271045
if f.alternate() {
10281046
write!(f, "{}", output.replace("<br>", "\n"))
10291047
} else {

0 commit comments

Comments
 (0)