Skip to content

Commit 6bc3d65

Browse files
rustdoc: properly indent fn signatures in traits
1 parent 3643d81 commit 6bc3d65

File tree

2 files changed

+48
-30
lines changed

2 files changed

+48
-30
lines changed

src/librustdoc/html/format.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ 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);
@@ -55,10 +53,23 @@ pub struct TyParamBounds<'a>(pub &'a [clean::TyParamBound]);
5553
pub struct CommaSep<'a, T: 'a>(pub &'a [T]);
5654
pub struct AbiSpace(pub Abi);
5755

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+
5866
/// Wrapper struct for emitting a where clause from Generics.
5967
pub struct WhereClause<'a>{
68+
/// The Generics from which to emit a where clause.
6069
pub gens: &'a clean::Generics,
70+
/// The number of spaces to indent each line with.
6171
pub indent: usize,
72+
/// Whether the where clause needs to add a comma and newline after the last bound.
6273
pub end_newline: bool,
6374
}
6475

@@ -936,8 +947,7 @@ impl fmt::Display for clean::FnDecl {
936947

937948
impl<'a> fmt::Display for Method<'a> {
938949
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
939-
let decl = self.0;
940-
let indent = self.1;
950+
let &Method { decl, name_len, indent } = self;
941951
let amp = if f.alternate() { "&" } else { "&amp;" };
942952
let mut args = String::new();
943953
let mut args_plain = String::new();
@@ -1004,15 +1014,19 @@ impl<'a> fmt::Display for Method<'a> {
10041014
format!("{}", decl.output)
10051015
};
10061016

1007-
let pad = repeat(" ").take(indent).collect::<String>();
1017+
let pad = repeat(" ").take(name_len).collect::<String>();
10081018
let plain = format!("{pad}({args}){arrow}",
10091019
pad = pad,
10101020
args = args_plain,
10111021
arrow = arrow_plain);
10121022

10131023
let output = if plain.len() > 80 {
1014-
let pad = "<br>&nbsp;&nbsp;&nbsp;&nbsp;";
1015-
format!("({args}<br>){arrow}", args = args.replace("<br>", pad), arrow = arrow)
1024+
let full_pad = format!("<br>{}", repeat("&nbsp;").take(indent + 4).collect::<String>());
1025+
let close_pad = format!("<br>{}", repeat("&nbsp;").take(indent).collect::<String>());
1026+
format!("({args}{close}){arrow}",
1027+
args = args.replace("<br>", &full_pad),
1028+
close = close_pad,
1029+
arrow = arrow)
10161030
} else {
10171031
format!("({args}){arrow}", args = args.replace("<br>", ""), arrow = arrow)
10181032
};

src/librustdoc/html/render.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,13 +1995,13 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
19951995
UnstableFeatures::Allow => f.constness,
19961996
_ => hir::Constness::NotConst
19971997
};
1998-
let indent = format!("{}{}{}{:#}fn {}{:#}",
1999-
VisSpace(&it.visibility),
2000-
ConstnessSpace(vis_constness),
2001-
UnsafetySpace(f.unsafety),
2002-
AbiSpace(f.abi),
2003-
it.name.as_ref().unwrap(),
2004-
f.generics).len();
1998+
let name_len = format!("{}{}{}{:#}fn {}{:#}",
1999+
VisSpace(&it.visibility),
2000+
ConstnessSpace(vis_constness),
2001+
UnsafetySpace(f.unsafety),
2002+
AbiSpace(f.abi),
2003+
it.name.as_ref().unwrap(),
2004+
f.generics).len();
20052005
write!(w, "<pre class='rust fn'>")?;
20062006
render_attributes(w, it)?;
20072007
write!(w, "{vis}{constness}{unsafety}{abi}fn \
@@ -2013,7 +2013,11 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
20132013
name = it.name.as_ref().unwrap(),
20142014
generics = f.generics,
20152015
where_clause = WhereClause { gens: &f.generics, indent: 0, end_newline: true },
2016-
decl = Method(&f.decl, indent))?;
2016+
decl = Method {
2017+
decl: &f.decl,
2018+
name_len: name_len,
2019+
indent: 0,
2020+
})?;
20172021
document(w, cx, it)
20182022
}
20192023

@@ -2326,21 +2330,17 @@ fn render_assoc_item(w: &mut fmt::Formatter,
23262330
UnstableFeatures::Allow => constness,
23272331
_ => hir::Constness::NotConst
23282332
};
2329-
let prefix = format!("{}{}{:#}fn {}{:#}",
2330-
ConstnessSpace(vis_constness),
2331-
UnsafetySpace(unsafety),
2332-
AbiSpace(abi),
2333-
name,
2334-
*g);
2335-
let mut indent = prefix.len();
2336-
let (where_indent, end_newline) = if parent == ItemType::Trait {
2337-
indent += 4;
2333+
let mut head_len = format!("{}{}{:#}fn {}{:#}",
2334+
ConstnessSpace(vis_constness),
2335+
UnsafetySpace(unsafety),
2336+
AbiSpace(abi),
2337+
name,
2338+
*g).len();
2339+
let (indent, end_newline) = if parent == ItemType::Trait {
2340+
head_len += 4;
23382341
(4, false)
2339-
} else if parent == ItemType::Impl {
2340-
(0, true)
23412342
} else {
2342-
let prefix = prefix + &format!("{:#}", Method(d, indent));
2343-
(prefix.lines().last().unwrap().len() + 1, true)
2343+
(0, true)
23442344
};
23452345
write!(w, "{}{}{}fn <a href='{href}' class='fnname'>{name}</a>\
23462346
{generics}{decl}{where_clause}",
@@ -2350,10 +2350,14 @@ fn render_assoc_item(w: &mut fmt::Formatter,
23502350
href = href,
23512351
name = name,
23522352
generics = *g,
2353-
decl = Method(d, indent),
2353+
decl = Method {
2354+
decl: d,
2355+
name_len: head_len,
2356+
indent: indent,
2357+
},
23542358
where_clause = WhereClause {
23552359
gens: g,
2356-
indent: where_indent,
2360+
indent: indent,
23572361
end_newline: end_newline,
23582362
})
23592363
}

0 commit comments

Comments
 (0)