respect alternate flag when formatting impl trait

This commit is contained in:
Andy Russell 2019-02-06 11:46:41 -05:00
parent 57d7cfc3cf
commit e827c9ab3c
No known key found for this signature in database
GPG Key ID: BE2221033EDBC374
3 changed files with 43 additions and 30 deletions

View File

@ -49,8 +49,11 @@ pub struct AbiSpace(pub Abi);
pub struct Function<'a> { pub struct Function<'a> {
/// The declaration to emit. /// The declaration to emit.
pub decl: &'a clean::FnDecl, pub decl: &'a clean::FnDecl,
/// The length of the function's "name", used to determine line-wrapping. /// The length of the function header and name. In other words, the number of characters in the
pub name_len: usize, /// function declaration up to but not including the parentheses.
///
/// Used to determine line-wrapping.
pub header_len: usize,
/// The number of spaces to indent each successive line with, if line-wrapping is necessary. /// The number of spaces to indent each successive line with, if line-wrapping is necessary.
pub indent: usize, pub indent: usize,
/// Whether the function is async or not. /// Whether the function is async or not.
@ -665,7 +668,11 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
} }
} }
clean::ImplTrait(ref bounds) => { clean::ImplTrait(ref bounds) => {
write!(f, "impl {}", GenericBounds(bounds)) if f.alternate() {
write!(f, "impl {:#}", GenericBounds(bounds))
} else {
write!(f, "impl {}", GenericBounds(bounds))
}
} }
clean::QPath { ref name, ref self_type, ref trait_ } => { clean::QPath { ref name, ref self_type, ref trait_ } => {
let should_show_cast = match *trait_ { let should_show_cast = match *trait_ {
@ -834,7 +841,7 @@ impl fmt::Display for clean::FnDecl {
impl<'a> fmt::Display for Function<'a> { impl<'a> fmt::Display for Function<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let &Function { decl, name_len, indent, asyncness } = self; let &Function { decl, header_len, indent, asyncness } = self;
let amp = if f.alternate() { "&" } else { "&amp;" }; let amp = if f.alternate() { "&" } else { "&amp;" };
let mut args = String::new(); let mut args = String::new();
let mut args_plain = String::new(); let mut args_plain = String::new();
@ -889,6 +896,8 @@ impl<'a> fmt::Display for Function<'a> {
} }
} }
let mut args_plain = format!("({})", args_plain);
if decl.variadic { if decl.variadic {
args.push_str(",<br> ..."); args.push_str(",<br> ...");
args_plain.push_str(", ..."); args_plain.push_str(", ...");
@ -907,13 +916,8 @@ impl<'a> fmt::Display for Function<'a> {
output.to_string() output.to_string()
}; };
let pad = " ".repeat(name_len); let declaration_len = header_len + args_plain.len() + arrow_plain.len();
let plain = format!("{pad}({args}){arrow}", let output = if declaration_len > 80 {
pad = pad,
args = args_plain,
arrow = arrow_plain);
let output = if plain.len() > 80 {
let full_pad = format!("<br>{}", "&nbsp;".repeat(indent + 4)); let full_pad = format!("<br>{}", "&nbsp;".repeat(indent + 4));
let close_pad = format!("<br>{}", "&nbsp;".repeat(indent)); let close_pad = format!("<br>{}", "&nbsp;".repeat(indent));
format!("({args}{close}){arrow}", format!("({args}{close}){arrow}",

View File

@ -2962,14 +2962,16 @@ fn item_static(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
f: &clean::Function) -> fmt::Result { f: &clean::Function) -> fmt::Result {
let name_len = format!("{}{}{}{}{:#}fn {}{:#}", let header_len = format!(
VisSpace(&it.visibility), "{}{}{}{}{:#}fn {}{:#}",
ConstnessSpace(f.header.constness), VisSpace(&it.visibility),
UnsafetySpace(f.header.unsafety), ConstnessSpace(f.header.constness),
AsyncSpace(f.header.asyncness), UnsafetySpace(f.header.unsafety),
AbiSpace(f.header.abi), AsyncSpace(f.header.asyncness),
it.name.as_ref().unwrap(), AbiSpace(f.header.abi),
f.generics).len(); it.name.as_ref().unwrap(),
f.generics
).len();
write!(w, "{}<pre class='rust fn'>", render_spotlight_traits(it)?)?; write!(w, "{}<pre class='rust fn'>", render_spotlight_traits(it)?)?;
render_attributes(w, it)?; render_attributes(w, it)?;
write!(w, write!(w,
@ -2985,7 +2987,7 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
where_clause = WhereClause { gens: &f.generics, indent: 0, end_newline: true }, where_clause = WhereClause { gens: &f.generics, indent: 0, end_newline: true },
decl = Function { decl = Function {
decl: &f.decl, decl: &f.decl,
name_len, header_len,
indent: 0, indent: 0,
asyncness: f.header.asyncness, asyncness: f.header.asyncness,
})?; })?;
@ -3400,16 +3402,18 @@ fn render_assoc_item(w: &mut fmt::Formatter,
href(did).map(|p| format!("{}#{}.{}", p.0, ty, name)).unwrap_or(anchor) href(did).map(|p| format!("{}#{}.{}", p.0, ty, name)).unwrap_or(anchor)
} }
}; };
let mut head_len = format!("{}{}{}{}{:#}fn {}{:#}", let mut header_len = format!(
VisSpace(&meth.visibility), "{}{}{}{}{:#}fn {}{:#}",
ConstnessSpace(header.constness), VisSpace(&meth.visibility),
UnsafetySpace(header.unsafety), ConstnessSpace(header.constness),
AsyncSpace(header.asyncness), UnsafetySpace(header.unsafety),
AbiSpace(header.abi), AsyncSpace(header.asyncness),
name, AbiSpace(header.abi),
*g).len(); name,
*g
).len();
let (indent, end_newline) = if parent == ItemType::Trait { let (indent, end_newline) = if parent == ItemType::Trait {
head_len += 4; header_len += 4;
(4, false) (4, false)
} else { } else {
(0, true) (0, true)
@ -3427,7 +3431,7 @@ fn render_assoc_item(w: &mut fmt::Formatter,
generics = *g, generics = *g,
decl = Function { decl = Function {
decl: d, decl: d,
name_len: head_len, header_len,
indent, indent,
asyncness: header.asyncness, asyncness: header.asyncness,
}, },

View File

@ -0,0 +1,5 @@
use std::fmt::Debug;
// @has 'wrapping/fn.foo.html' '//pre[@class="rust fn"]' 'pub fn foo() -> impl Debug'
// @count - '//pre[@class="rust fn"]/br' 0
pub fn foo() -> impl Debug {}