rustdoc: properly indent fn signatures in traits
This commit is contained in:
parent
3643d81659
commit
6bc3d65948
2 changed files with 48 additions and 30 deletions
|
@ -41,8 +41,6 @@ pub struct UnsafetySpace(pub hir::Unsafety);
|
||||||
/// with a space after it.
|
/// with a space after it.
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct ConstnessSpace(pub hir::Constness);
|
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
|
/// Similar to VisSpace, but used for mutability
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct MutableSpace(pub clean::Mutability);
|
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 CommaSep<'a, T: 'a>(pub &'a [T]);
|
||||||
pub struct AbiSpace(pub Abi);
|
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.
|
/// Wrapper struct for emitting a where clause from Generics.
|
||||||
pub struct WhereClause<'a>{
|
pub struct WhereClause<'a>{
|
||||||
|
/// The Generics from which to emit a where clause.
|
||||||
pub gens: &'a clean::Generics,
|
pub gens: &'a clean::Generics,
|
||||||
|
/// The number of spaces to indent each line with.
|
||||||
pub indent: usize,
|
pub indent: usize,
|
||||||
|
/// Whether the where clause needs to add a comma and newline after the last bound.
|
||||||
pub end_newline: bool,
|
pub end_newline: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -936,8 +947,7 @@ impl fmt::Display for clean::FnDecl {
|
||||||
|
|
||||||
impl<'a> fmt::Display for Method<'a> {
|
impl<'a> fmt::Display for Method<'a> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let decl = self.0;
|
let &Method { decl, name_len, indent } = self;
|
||||||
let indent = self.1;
|
|
||||||
let amp = if f.alternate() { "&" } else { "&" };
|
let amp = if f.alternate() { "&" } else { "&" };
|
||||||
let mut args = String::new();
|
let mut args = String::new();
|
||||||
let mut args_plain = String::new();
|
let mut args_plain = String::new();
|
||||||
|
@ -1004,15 +1014,19 @@ impl<'a> fmt::Display for Method<'a> {
|
||||||
format!("{}", decl.output)
|
format!("{}", decl.output)
|
||||||
};
|
};
|
||||||
|
|
||||||
let pad = repeat(" ").take(indent).collect::<String>();
|
let pad = repeat(" ").take(name_len).collect::<String>();
|
||||||
let plain = format!("{pad}({args}){arrow}",
|
let plain = format!("{pad}({args}){arrow}",
|
||||||
pad = pad,
|
pad = pad,
|
||||||
args = args_plain,
|
args = args_plain,
|
||||||
arrow = arrow_plain);
|
arrow = arrow_plain);
|
||||||
|
|
||||||
let output = if plain.len() > 80 {
|
let output = if plain.len() > 80 {
|
||||||
let pad = "<br> ";
|
let full_pad = format!("<br>{}", repeat(" ").take(indent + 4).collect::<String>());
|
||||||
format!("({args}<br>){arrow}", args = args.replace("<br>", pad), arrow = arrow)
|
let close_pad = format!("<br>{}", repeat(" ").take(indent).collect::<String>());
|
||||||
|
format!("({args}{close}){arrow}",
|
||||||
|
args = args.replace("<br>", &full_pad),
|
||||||
|
close = close_pad,
|
||||||
|
arrow = arrow)
|
||||||
} else {
|
} else {
|
||||||
format!("({args}){arrow}", args = args.replace("<br>", ""), arrow = arrow)
|
format!("({args}){arrow}", args = args.replace("<br>", ""), arrow = arrow)
|
||||||
};
|
};
|
||||||
|
|
|
@ -1995,13 +1995,13 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
|
||||||
UnstableFeatures::Allow => f.constness,
|
UnstableFeatures::Allow => f.constness,
|
||||||
_ => hir::Constness::NotConst
|
_ => hir::Constness::NotConst
|
||||||
};
|
};
|
||||||
let indent = format!("{}{}{}{:#}fn {}{:#}",
|
let name_len = format!("{}{}{}{:#}fn {}{:#}",
|
||||||
VisSpace(&it.visibility),
|
VisSpace(&it.visibility),
|
||||||
ConstnessSpace(vis_constness),
|
ConstnessSpace(vis_constness),
|
||||||
UnsafetySpace(f.unsafety),
|
UnsafetySpace(f.unsafety),
|
||||||
AbiSpace(f.abi),
|
AbiSpace(f.abi),
|
||||||
it.name.as_ref().unwrap(),
|
it.name.as_ref().unwrap(),
|
||||||
f.generics).len();
|
f.generics).len();
|
||||||
write!(w, "<pre class='rust fn'>")?;
|
write!(w, "<pre class='rust fn'>")?;
|
||||||
render_attributes(w, it)?;
|
render_attributes(w, it)?;
|
||||||
write!(w, "{vis}{constness}{unsafety}{abi}fn \
|
write!(w, "{vis}{constness}{unsafety}{abi}fn \
|
||||||
|
@ -2013,7 +2013,11 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
|
||||||
name = it.name.as_ref().unwrap(),
|
name = it.name.as_ref().unwrap(),
|
||||||
generics = f.generics,
|
generics = f.generics,
|
||||||
where_clause = WhereClause { gens: &f.generics, indent: 0, end_newline: true },
|
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)
|
document(w, cx, it)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2326,21 +2330,17 @@ fn render_assoc_item(w: &mut fmt::Formatter,
|
||||||
UnstableFeatures::Allow => constness,
|
UnstableFeatures::Allow => constness,
|
||||||
_ => hir::Constness::NotConst
|
_ => hir::Constness::NotConst
|
||||||
};
|
};
|
||||||
let prefix = format!("{}{}{:#}fn {}{:#}",
|
let mut head_len = format!("{}{}{:#}fn {}{:#}",
|
||||||
ConstnessSpace(vis_constness),
|
ConstnessSpace(vis_constness),
|
||||||
UnsafetySpace(unsafety),
|
UnsafetySpace(unsafety),
|
||||||
AbiSpace(abi),
|
AbiSpace(abi),
|
||||||
name,
|
name,
|
||||||
*g);
|
*g).len();
|
||||||
let mut indent = prefix.len();
|
let (indent, end_newline) = if parent == ItemType::Trait {
|
||||||
let (where_indent, end_newline) = if parent == ItemType::Trait {
|
head_len += 4;
|
||||||
indent += 4;
|
|
||||||
(4, false)
|
(4, false)
|
||||||
} else if parent == ItemType::Impl {
|
|
||||||
(0, true)
|
|
||||||
} else {
|
} else {
|
||||||
let prefix = prefix + &format!("{:#}", Method(d, indent));
|
(0, true)
|
||||||
(prefix.lines().last().unwrap().len() + 1, true)
|
|
||||||
};
|
};
|
||||||
write!(w, "{}{}{}fn <a href='{href}' class='fnname'>{name}</a>\
|
write!(w, "{}{}{}fn <a href='{href}' class='fnname'>{name}</a>\
|
||||||
{generics}{decl}{where_clause}",
|
{generics}{decl}{where_clause}",
|
||||||
|
@ -2350,10 +2350,14 @@ fn render_assoc_item(w: &mut fmt::Formatter,
|
||||||
href = href,
|
href = href,
|
||||||
name = name,
|
name = name,
|
||||||
generics = *g,
|
generics = *g,
|
||||||
decl = Method(d, indent),
|
decl = Method {
|
||||||
|
decl: d,
|
||||||
|
name_len: head_len,
|
||||||
|
indent: indent,
|
||||||
|
},
|
||||||
where_clause = WhereClause {
|
where_clause = WhereClause {
|
||||||
gens: g,
|
gens: g,
|
||||||
indent: where_indent,
|
indent: indent,
|
||||||
end_newline: end_newline,
|
end_newline: end_newline,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue