Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc: respect alternate flag when formatting impl trait #58409

Merged
merged 1 commit into from Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/librustdoc/html/format.rs
Expand Up @@ -49,8 +49,11 @@ pub struct AbiSpace(pub Abi);
pub struct Function<'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 length of the function header and name. In other words, the number of characters in the
/// 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.
pub indent: usize,
/// Whether the function is async or not.
Expand Down Expand Up @@ -665,7 +668,11 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
}
}
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_ } => {
let should_show_cast = match *trait_ {
Expand Down Expand Up @@ -834,7 +841,7 @@ impl fmt::Display for clean::FnDecl {

impl<'a> fmt::Display for Function<'a> {
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 mut args = String::new();
let mut args_plain = String::new();
Expand Down Expand Up @@ -889,6 +896,8 @@ impl<'a> fmt::Display for Function<'a> {
}
}

let mut args_plain = format!("({})", args_plain);

if decl.variadic {
args.push_str(",<br> ...");
args_plain.push_str(", ...");
Expand All @@ -907,13 +916,8 @@ impl<'a> fmt::Display for Function<'a> {
output.to_string()
};

let pad = " ".repeat(name_len);
let plain = format!("{pad}({args}){arrow}",
pad = pad,
args = args_plain,
arrow = arrow_plain);

let output = if plain.len() > 80 {
let declaration_len = header_len + args_plain.len() + arrow_plain.len();
let output = if declaration_len > 80 {
let full_pad = format!("<br>{}", "&nbsp;".repeat(indent + 4));
let close_pad = format!("<br>{}", "&nbsp;".repeat(indent));
format!("({args}{close}){arrow}",
Expand Down
42 changes: 23 additions & 19 deletions src/librustdoc/html/render.rs
Expand Up @@ -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,
f: &clean::Function) -> fmt::Result {
let name_len = format!("{}{}{}{}{:#}fn {}{:#}",
VisSpace(&it.visibility),
ConstnessSpace(f.header.constness),
UnsafetySpace(f.header.unsafety),
AsyncSpace(f.header.asyncness),
AbiSpace(f.header.abi),
it.name.as_ref().unwrap(),
f.generics).len();
let header_len = format!(
"{}{}{}{}{:#}fn {}{:#}",
VisSpace(&it.visibility),
ConstnessSpace(f.header.constness),
UnsafetySpace(f.header.unsafety),
AsyncSpace(f.header.asyncness),
AbiSpace(f.header.abi),
it.name.as_ref().unwrap(),
f.generics
).len();
write!(w, "{}<pre class='rust fn'>", render_spotlight_traits(it)?)?;
render_attributes(w, it)?;
write!(w,
Expand All @@ -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 },
decl = Function {
decl: &f.decl,
name_len,
header_len,
indent: 0,
asyncness: f.header.asyncness,
})?;
Expand Down Expand Up @@ -3400,16 +3402,18 @@ fn render_assoc_item(w: &mut fmt::Formatter,
href(did).map(|p| format!("{}#{}.{}", p.0, ty, name)).unwrap_or(anchor)
}
};
let mut head_len = format!("{}{}{}{}{:#}fn {}{:#}",
VisSpace(&meth.visibility),
ConstnessSpace(header.constness),
UnsafetySpace(header.unsafety),
AsyncSpace(header.asyncness),
AbiSpace(header.abi),
name,
*g).len();
let mut header_len = format!(
"{}{}{}{}{:#}fn {}{:#}",
VisSpace(&meth.visibility),
ConstnessSpace(header.constness),
UnsafetySpace(header.unsafety),
AsyncSpace(header.asyncness),
AbiSpace(header.abi),
name,
*g
).len();
let (indent, end_newline) = if parent == ItemType::Trait {
head_len += 4;
header_len += 4;
(4, false)
} else {
(0, true)
Expand All @@ -3427,7 +3431,7 @@ fn render_assoc_item(w: &mut fmt::Formatter,
generics = *g,
decl = Function {
decl: d,
name_len: head_len,
header_len,
indent,
asyncness: header.asyncness,
},
Expand Down
5 changes: 5 additions & 0 deletions src/test/rustdoc/wrapping.rs
@@ -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 {}