rustdoc: properly indent fn signatures in traits

This commit is contained in:
QuietMisdreavus 2017-04-04 10:31:57 -05:00
parent 3643d81659
commit 6bc3d65948
2 changed files with 48 additions and 30 deletions

View File

@ -41,8 +41,6 @@ pub struct UnsafetySpace(pub hir::Unsafety);
/// with a space after it.
#[derive(Copy, Clone)]
pub struct ConstnessSpace(pub hir::Constness);
/// Wrapper struct for properly emitting a method declaration.
pub struct Method<'a>(pub &'a clean::FnDecl, pub usize);
/// Similar to VisSpace, but used for mutability
#[derive(Copy, Clone)]
pub struct MutableSpace(pub clean::Mutability);
@ -55,10 +53,23 @@ pub struct TyParamBounds<'a>(pub &'a [clean::TyParamBound]);
pub struct CommaSep<'a, T: 'a>(pub &'a [T]);
pub struct AbiSpace(pub Abi);
/// Wrapper struct for properly emitting a method declaration.
pub struct Method<'a> {
/// The declaration to emit.
pub decl: &'a clean::FnDecl,
/// The length of the function's "name", used to determine line-wrapping.
pub name_len: usize,
/// The number of spaces to indent each successive line with, if line-wrapping is necessary.
pub indent: usize,
}
/// Wrapper struct for emitting a where clause from Generics.
pub struct WhereClause<'a>{
/// The Generics from which to emit a where clause.
pub gens: &'a clean::Generics,
/// The number of spaces to indent each line with.
pub indent: usize,
/// Whether the where clause needs to add a comma and newline after the last bound.
pub end_newline: bool,
}
@ -936,8 +947,7 @@ impl fmt::Display for clean::FnDecl {
impl<'a> fmt::Display for Method<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let decl = self.0;
let indent = self.1;
let &Method { decl, name_len, indent } = self;
let amp = if f.alternate() { "&" } else { "&amp;" };
let mut args = String::new();
let mut args_plain = String::new();
@ -1004,15 +1014,19 @@ impl<'a> fmt::Display for Method<'a> {
format!("{}", decl.output)
};
let pad = repeat(" ").take(indent).collect::<String>();
let pad = repeat(" ").take(name_len).collect::<String>();
let plain = format!("{pad}({args}){arrow}",
pad = pad,
args = args_plain,
arrow = arrow_plain);
let output = if plain.len() > 80 {
let pad = "<br>&nbsp;&nbsp;&nbsp;&nbsp;";
format!("({args}<br>){arrow}", args = args.replace("<br>", pad), arrow = arrow)
let full_pad = format!("<br>{}", repeat("&nbsp;").take(indent + 4).collect::<String>());
let close_pad = format!("<br>{}", repeat("&nbsp;").take(indent).collect::<String>());
format!("({args}{close}){arrow}",
args = args.replace("<br>", &full_pad),
close = close_pad,
arrow = arrow)
} else {
format!("({args}){arrow}", args = args.replace("<br>", ""), arrow = arrow)
};

View File

@ -1995,7 +1995,7 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
UnstableFeatures::Allow => f.constness,
_ => hir::Constness::NotConst
};
let indent = format!("{}{}{}{:#}fn {}{:#}",
let name_len = format!("{}{}{}{:#}fn {}{:#}",
VisSpace(&it.visibility),
ConstnessSpace(vis_constness),
UnsafetySpace(f.unsafety),
@ -2013,7 +2013,11 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
name = it.name.as_ref().unwrap(),
generics = f.generics,
where_clause = WhereClause { gens: &f.generics, indent: 0, end_newline: true },
decl = Method(&f.decl, indent))?;
decl = Method {
decl: &f.decl,
name_len: name_len,
indent: 0,
})?;
document(w, cx, it)
}
@ -2326,21 +2330,17 @@ fn render_assoc_item(w: &mut fmt::Formatter,
UnstableFeatures::Allow => constness,
_ => hir::Constness::NotConst
};
let prefix = format!("{}{}{:#}fn {}{:#}",
let mut head_len = format!("{}{}{:#}fn {}{:#}",
ConstnessSpace(vis_constness),
UnsafetySpace(unsafety),
AbiSpace(abi),
name,
*g);
let mut indent = prefix.len();
let (where_indent, end_newline) = if parent == ItemType::Trait {
indent += 4;
*g).len();
let (indent, end_newline) = if parent == ItemType::Trait {
head_len += 4;
(4, false)
} else if parent == ItemType::Impl {
(0, true)
} else {
let prefix = prefix + &format!("{:#}", Method(d, indent));
(prefix.lines().last().unwrap().len() + 1, true)
(0, true)
};
write!(w, "{}{}{}fn <a href='{href}' class='fnname'>{name}</a>\
{generics}{decl}{where_clause}",
@ -2350,10 +2350,14 @@ fn render_assoc_item(w: &mut fmt::Formatter,
href = href,
name = name,
generics = *g,
decl = Method(d, indent),
decl = Method {
decl: d,
name_len: head_len,
indent: indent,
},
where_clause = WhereClause {
gens: g,
indent: where_indent,
indent: indent,
end_newline: end_newline,
})
}