From 8206d0c54e149609617a1de57cad480bbad73970 Mon Sep 17 00:00:00 2001 From: QuietMisdreavus Date: Tue, 28 Mar 2017 16:49:05 -0500 Subject: [PATCH 1/8] rustdoc: format fns like format rfc 39 --- src/librustdoc/html/format.rs | 36 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index fc5507d4d55..3589cc8fac6 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -945,6 +945,10 @@ impl<'a> fmt::Display for Method<'a> { let mut args = String::new(); let mut args_plain = String::new(); for (i, input) in decl.inputs.values.iter().enumerate() { + if i == 0 { + args.push_str("
"); + } + if let Some(selfty) = input.to_self() { match selfty { clean::SelfValue => { @@ -986,8 +990,8 @@ impl<'a> fmt::Display for Method<'a> { args_plain.push_str(&format!("{:#}", input.type_)); } if i + 1 < decl.inputs.values.len() { - args.push_str(","); - args_plain.push_str(","); + args.push(','); + args_plain.push(','); } } @@ -1003,27 +1007,19 @@ impl<'a> fmt::Display for Method<'a> { format!("{}", decl.output) }; - let mut output: String; - let plain: String; let pad = repeat(" ").take(indent).collect::(); - if arrow.is_empty() { - output = format!("({})", args); - plain = format!("{}({})", pad, args_plain); - } else { - output = format!("({args})
{arrow}", args = args, arrow = arrow); - plain = format!("{pad}({args}){arrow}", - pad = pad, - args = args_plain, - arrow = arrow_plain); - } + let plain = format!("{pad}({args}){arrow}", + pad = pad, + args = args_plain, + arrow = arrow_plain); - if plain.len() > 80 { - let pad = repeat(" ").take(indent).collect::(); - let pad = format!("
{}", pad); - output = output.replace("
", &pad); + let output = if plain.len() > 80 { + let pad = "
    "; + format!("({args}
){arrow}", args = args.replace("
", pad), arrow = arrow) } else { - output = output.replace("
", ""); - } + format!("({args}){arrow}", args = args.replace("
", ""), arrow = arrow) + }; + if f.alternate() { write!(f, "{}", output.replace("
", "\n")) } else { From 80bff6b59654cde4c36c7ae77ea06fb254ffe148 Mon Sep 17 00:00:00 2001 From: QuietMisdreavus Date: Fri, 31 Mar 2017 18:04:42 -0500 Subject: [PATCH 2/8] rustdoc: format where clauses like rust-lang-nursery/fmt-rfcs#38 --- src/librustdoc/html/format.rs | 61 ++++++++++++------------- src/librustdoc/html/render.rs | 62 +++++++++++--------------- src/librustdoc/html/static/rustdoc.css | 13 +----- 3 files changed, 55 insertions(+), 81 deletions(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 3589cc8fac6..3fb90f407eb 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -49,14 +49,19 @@ pub struct MutableSpace(pub clean::Mutability); /// Similar to VisSpace, but used for mutability #[derive(Copy, Clone)] pub struct RawMutableSpace(pub clean::Mutability); -/// Wrapper struct for emitting a where clause from Generics. -pub struct WhereClause<'a>(pub &'a clean::Generics, pub usize); /// Wrapper struct for emitting type parameter bounds. pub struct TyParamBounds<'a>(pub &'a [clean::TyParamBound]); /// Wrapper struct for emitting a comma-separated list of items pub struct CommaSep<'a, T: 'a>(pub &'a [T]); pub struct AbiSpace(pub Abi); +/// Wrapper struct for emitting a where clause from Generics. +pub struct WhereClause<'a>{ + pub gens: &'a clean::Generics, + pub indent: usize, + pub end_newline: bool, +} + pub struct HRef<'a> { pub did: DefId, pub text: &'a str, @@ -167,24 +172,27 @@ impl fmt::Display for clean::Generics { impl<'a> fmt::Display for WhereClause<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let &WhereClause(gens, pad) = self; + let &WhereClause{ gens, indent, end_newline } = self; if gens.where_predicates.is_empty() { return Ok(()); } let mut clause = String::new(); if f.alternate() { - clause.push_str(" where "); + clause.push_str(" where"); } else { - clause.push_str(" where "); + if end_newline { + clause.push_str("where"); + } else { + clause.push_str("where"); + } } for (i, pred) in gens.where_predicates.iter().enumerate() { - if i > 0 { - if f.alternate() { - clause.push_str(", "); - } else { - clause.push_str(",
"); - } + if f.alternate() { + clause.push(' '); + } else { + clause.push_str("
"); } + match pred { &clean::WherePredicate::BoundPredicate { ref ty, ref bounds } => { let bounds = bounds; @@ -213,21 +221,18 @@ impl<'a> fmt::Display for WhereClause<'a> { } } } + + if i < gens.where_predicates.len() - 1 || end_newline { + clause.push(','); + } } if !f.alternate() { clause.push_str("
"); - let plain = format!("{:#}", self); - if plain.len() + pad > 80 { - // break it onto its own line regardless, but make sure method impls and trait - // blocks keep their fixed padding (2 and 9, respectively) - let padding = if pad > 10 { - repeat(" ").take(8).collect::() - } else { - repeat(" ").take(pad + 6).collect::() - }; - clause = clause.replace("
", &format!("
{}", padding)); - } else { - clause = clause.replace("
", " "); + let padding = repeat(" ").take(indent + 4).collect::(); + clause = clause.replace("
", &format!("
{}", padding)); + clause.insert_str(0, &repeat(" ").take(indent).collect::()); + if !end_newline { + clause.insert_str(0, "
"); } } write!(f, "{}", clause) @@ -838,43 +843,35 @@ fn fmt_impl(i: &clean::Impl, f: &mut fmt::Formatter, link_trait: bool, use_absolute: bool) -> fmt::Result { - let mut plain = String::new(); - if f.alternate() { write!(f, "impl{:#} ", i.generics)?; } else { write!(f, "impl{} ", i.generics)?; } - plain.push_str(&format!("impl{:#} ", i.generics)); if let Some(ref ty) = i.trait_ { if i.polarity == Some(clean::ImplPolarity::Negative) { write!(f, "!")?; - plain.push_str("!"); } if link_trait { fmt::Display::fmt(ty, f)?; - plain.push_str(&format!("{:#}", ty)); } else { match *ty { clean::ResolvedPath { typarams: None, ref path, is_generic: false, .. } => { let last = path.segments.last().unwrap(); fmt::Display::fmt(&last.name, f)?; fmt::Display::fmt(&last.params, f)?; - plain.push_str(&format!("{:#}{:#}", last.name, last.params)); } _ => unreachable!(), } } write!(f, " for ")?; - plain.push_str(" for "); } fmt_type(&i.for_, f, use_absolute, true)?; - plain.push_str(&format!("{:#}", i.for_)); - fmt::Display::fmt(&WhereClause(&i.generics, plain.len() + 1), f)?; + fmt::Display::fmt(&WhereClause{ gens: &i.generics, indent: 0, end_newline: true }, f)?; Ok(()) } diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index c571bcb08e4..4eb228ce68f 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -2012,7 +2012,7 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, abi = AbiSpace(f.abi), name = it.name.as_ref().unwrap(), generics = f.generics, - where_clause = WhereClause(&f.generics, 2), + where_clause = WhereClause { gens: &f.generics, indent: 0, end_newline: true }, decl = Method(&f.decl, indent))?; document(w, cx, it) } @@ -2047,8 +2047,7 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, it.name.as_ref().unwrap(), t.generics, bounds, - // Where clauses in traits are indented nine spaces, per rustdoc.css - WhereClause(&t.generics, 9))?; + WhereClause { gens: &t.generics, indent: 0, end_newline: true })?; let types = t.items.iter().filter(|m| m.is_associated_type()).collect::>(); let consts = t.items.iter().filter(|m| m.is_associated_const()).collect::>(); @@ -2087,7 +2086,14 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, for m in &provided { write!(w, " ")?; render_assoc_item(w, m, AssocItemLink::Anchor(None), ItemType::Trait)?; - write!(w, " {{ ... }}\n")?; + match m.inner { + clean::MethodItem(ref inner) if !inner.generics.where_predicates.is_empty() => { + write!(w, ",\n {{ ... }}\n")?; + }, + _ => { + write!(w, " {{ ... }}\n")?; + }, + } } write!(w, "}}")?; } @@ -2327,14 +2333,14 @@ fn render_assoc_item(w: &mut fmt::Formatter, name, *g); let mut indent = prefix.len(); - let where_indent = if parent == ItemType::Trait { + let (where_indent, end_newline) = if parent == ItemType::Trait { indent += 4; - 8 + (4, false) } else if parent == ItemType::Impl { - 2 + (0, true) } else { let prefix = prefix + &format!("{:#}", Method(d, indent)); - prefix.lines().last().unwrap().len() + 1 + (prefix.lines().last().unwrap().len() + 1, true) }; write!(w, "{}{}{}fn {name}\ {generics}{decl}{where_clause}", @@ -2345,7 +2351,11 @@ fn render_assoc_item(w: &mut fmt::Formatter, name = name, generics = *g, decl = Method(d, indent), - where_clause = WhereClause(g, where_indent)) + where_clause = WhereClause { + gens: g, + indent: where_indent, + end_newline: end_newline, + }) } match item.inner { clean::StrippedItem(..) => Ok(()), @@ -2458,15 +2468,11 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, e: &clean::Enum) -> fmt::Result { write!(w, "
")?;
     render_attributes(w, it)?;
-    let padding = format!("{}enum {}{:#} ",
-                          VisSpace(&it.visibility),
-                          it.name.as_ref().unwrap(),
-                          e.generics).len();
     write!(w, "{}enum {}{}{}",
            VisSpace(&it.visibility),
            it.name.as_ref().unwrap(),
            e.generics,
-           WhereClause(&e.generics, padding))?;
+           WhereClause { gens: &e.generics, indent: 0, end_newline: true })?;
     if e.variants.is_empty() && !e.variants_stripped {
         write!(w, " {{}}")?;
     } else {
@@ -2640,23 +2646,17 @@ fn render_struct(w: &mut fmt::Formatter, it: &clean::Item,
                  fields: &[clean::Item],
                  tab: &str,
                  structhead: bool) -> fmt::Result {
-    let mut plain = String::new();
     write!(w, "{}{}{}",
            VisSpace(&it.visibility),
            if structhead {"struct "} else {""},
            it.name.as_ref().unwrap())?;
-    plain.push_str(&format!("{}{}{}",
-                            VisSpace(&it.visibility),
-                            if structhead {"struct "} else {""},
-                            it.name.as_ref().unwrap()));
     if let Some(g) = g {
-        plain.push_str(&format!("{:#}", g));
         write!(w, "{}", g)?
     }
     match ty {
         doctree::Plain => {
             if let Some(g) = g {
-                write!(w, "{}", WhereClause(g, plain.len() + 1))?
+                write!(w, "{}", WhereClause { gens: g, indent: 0, end_newline: true })?
             }
             let mut has_visible_fields = false;
             write!(w, " {{")?;
@@ -2685,35 +2685,30 @@ fn render_struct(w: &mut fmt::Formatter, it: &clean::Item,
         }
         doctree::Tuple => {
             write!(w, "(")?;
-            plain.push_str("(");
             for (i, field) in fields.iter().enumerate() {
                 if i > 0 {
                     write!(w, ", ")?;
-                    plain.push_str(", ");
                 }
                 match field.inner {
                     clean::StrippedItem(box clean::StructFieldItem(..)) => {
-                        plain.push_str("_");
                         write!(w, "_")?
                     }
                     clean::StructFieldItem(ref ty) => {
-                        plain.push_str(&format!("{}{:#}", VisSpace(&field.visibility), *ty));
                         write!(w, "{}{}", VisSpace(&field.visibility), *ty)?
                     }
                     _ => unreachable!()
                 }
             }
             write!(w, ")")?;
-            plain.push_str(")");
             if let Some(g) = g {
-                write!(w, "{}", WhereClause(g, plain.len() + 1))?
+                write!(w, "{}", WhereClause { gens: g, indent: 0, end_newline: true })?
             }
             write!(w, ";")?;
         }
         doctree::Unit => {
             // Needed for PhantomData.
             if let Some(g) = g {
-                write!(w, "{}", WhereClause(g, plain.len() + 1))?
+                write!(w, "{}", WhereClause { gens: g, indent: 0, end_newline: true })?
             }
             write!(w, ";")?;
         }
@@ -2726,19 +2721,13 @@ fn render_union(w: &mut fmt::Formatter, it: &clean::Item,
                 fields: &[clean::Item],
                 tab: &str,
                 structhead: bool) -> fmt::Result {
-    let mut plain = String::new();
     write!(w, "{}{}{}",
            VisSpace(&it.visibility),
            if structhead {"union "} else {""},
            it.name.as_ref().unwrap())?;
-    plain.push_str(&format!("{}{}{}",
-                            VisSpace(&it.visibility),
-                            if structhead {"union "} else {""},
-                            it.name.as_ref().unwrap()));
     if let Some(g) = g {
         write!(w, "{}", g)?;
-        plain.push_str(&format!("{:#}", g));
-        write!(w, "{}", WhereClause(g, plain.len() + 1))?;
+        write!(w, "{}", WhereClause { gens: g, indent: 0, end_newline: true })?;
     }
 
     write!(w, " {{\n{}", tab)?;
@@ -3037,13 +3026,12 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
 
 fn item_typedef(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
                 t: &clean::Typedef) -> fmt::Result {
-    let indent = format!("type {}{:#} ", it.name.as_ref().unwrap(), t.generics).len();
     write!(w, "
")?;
     render_attributes(w, it)?;
     write!(w, "type {}{}{where_clause} = {type_};
", it.name.as_ref().unwrap(), t.generics, - where_clause = WhereClause(&t.generics, indent), + where_clause = WhereClause { gens: &t.generics, indent: 0, end_newline: true }, type_ = t.type_)?; document(w, cx, it) diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css index 4047f6045bc..77ae2b58704 100644 --- a/src/librustdoc/html/static/rustdoc.css +++ b/src/librustdoc/html/static/rustdoc.css @@ -379,12 +379,6 @@ h4 > code, h3 > code, .invisible > code { .content .where.fmt-newline { display: block; } -/* Bit of whitespace to indent it */ -.content .method .where::before, -.content .fn .where::before, -.content .where.fmt-newline::before { - content: ' '; -} .content .methods > div { margin-left: 40px; } @@ -399,11 +393,6 @@ h4 > code, h3 > code, .invisible > code { font-size: 90%; } -/* Shift where in trait listing down a line */ -pre.trait .where::before { - content: '\a '; -} - nav { border-bottom: 1px solid; padding-bottom: 10px; @@ -772,4 +761,4 @@ span.since { nav.sub, .content .out-of-band, .collapse-toggle { display: none; } -} \ No newline at end of file +} From 3643d8165909c4a7959ed6cf9f3d1bd9cf3661b4 Mon Sep 17 00:00:00 2001 From: QuietMisdreavus Date: Fri, 31 Mar 2017 19:02:00 -0500 Subject: [PATCH 3/8] rustdoc: fix alignment of fn arguments when on multiple lines --- src/librustdoc/html/format.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 3fb90f407eb..fd069ed7e07 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -971,7 +971,7 @@ impl<'a> fmt::Display for Method<'a> { } } else { if i > 0 { - args.push_str("
"); + args.push_str("
"); args_plain.push_str(" "); } if !input.name.is_empty() { From 6bc3d65948c3606c29beb8da359d2a45a36e5c15 Mon Sep 17 00:00:00 2001 From: QuietMisdreavus Date: Tue, 4 Apr 2017 10:31:57 -0500 Subject: [PATCH 4/8] rustdoc: properly indent fn signatures in traits --- src/librustdoc/html/format.rs | 28 +++++++++++++++----- src/librustdoc/html/render.rs | 50 +++++++++++++++++++---------------- 2 files changed, 48 insertions(+), 30 deletions(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index fd069ed7e07..9e45ccaff32 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -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 { "&" }; 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::(); + let pad = repeat(" ").take(name_len).collect::(); let plain = format!("{pad}({args}){arrow}", pad = pad, args = args_plain, arrow = arrow_plain); let output = if plain.len() > 80 { - let pad = "
    "; - format!("({args}
){arrow}", args = args.replace("
", pad), arrow = arrow) + let full_pad = format!("
{}", repeat(" ").take(indent + 4).collect::()); + let close_pad = format!("
{}", repeat(" ").take(indent).collect::()); + format!("({args}{close}){arrow}", + args = args.replace("
", &full_pad), + close = close_pad, + arrow = arrow) } else { format!("({args}){arrow}", args = args.replace("
", ""), arrow = arrow) }; diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 4eb228ce68f..be69f6b8ec2 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -1995,13 +1995,13 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, UnstableFeatures::Allow => f.constness, _ => hir::Constness::NotConst }; - let indent = format!("{}{}{}{:#}fn {}{:#}", - VisSpace(&it.visibility), - ConstnessSpace(vis_constness), - UnsafetySpace(f.unsafety), - AbiSpace(f.abi), - it.name.as_ref().unwrap(), - f.generics).len(); + let name_len = format!("{}{}{}{:#}fn {}{:#}", + VisSpace(&it.visibility), + ConstnessSpace(vis_constness), + UnsafetySpace(f.unsafety), + AbiSpace(f.abi), + it.name.as_ref().unwrap(), + f.generics).len(); write!(w, "
")?;
     render_attributes(w, it)?;
     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(),
            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 {}{:#}",
-                             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;
+        let mut head_len = format!("{}{}{:#}fn {}{:#}",
+                                   ConstnessSpace(vis_constness),
+                                   UnsafetySpace(unsafety),
+                                   AbiSpace(abi),
+                                   name,
+                                   *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 {name}\
                    {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,
                })
     }

From 5bffa0aa50f7456249e4ffdd3e7cbf72f743a56d Mon Sep 17 00:00:00 2001
From: QuietMisdreavus 
Date: Tue, 4 Apr 2017 11:16:16 -0500
Subject: [PATCH 5/8] rustdoc: don't add a space before `{` on traits with
 where clauses

cc #41025
---
 src/librustdoc/html/render.rs | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs
index be69f6b8ec2..b1ac6a5127f 100644
--- a/src/librustdoc/html/render.rs
+++ b/src/librustdoc/html/render.rs
@@ -2045,13 +2045,18 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
     // Output the trait definition
     write!(w, "
")?;
     render_attributes(w, it)?;
-    write!(w, "{}{}trait {}{}{}{} ",
+    write!(w, "{}{}trait {}{}{}",
            VisSpace(&it.visibility),
            UnsafetySpace(t.unsafety),
            it.name.as_ref().unwrap(),
            t.generics,
-           bounds,
-           WhereClause { gens: &t.generics, indent: 0, end_newline: true })?;
+           bounds)?;
+
+    if !t.generics.where_predicates.is_empty() {
+        write!(w, "{}", WhereClause { gens: &t.generics, indent: 0, end_newline: true })?;
+    } else {
+        write!(w, " ")?;
+    }
 
     let types = t.items.iter().filter(|m| m.is_associated_type()).collect::>();
     let consts = t.items.iter().filter(|m| m.is_associated_const()).collect::>();

From 36bc448c00a9fb5397d84b94485dd0279eb2af45 Mon Sep 17 00:00:00 2001
From: QuietMisdreavus 
Date: Wed, 5 Apr 2017 10:02:37 -0500
Subject: [PATCH 6/8] style: space between struct name and opening brace

---
 src/librustdoc/html/format.rs | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs
index 9e45ccaff32..7c1139d7135 100644
--- a/src/librustdoc/html/format.rs
+++ b/src/librustdoc/html/format.rs
@@ -183,7 +183,7 @@ impl fmt::Display for clean::Generics {
 
 impl<'a> fmt::Display for WhereClause<'a> {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        let &WhereClause{ gens, indent, end_newline } = self;
+        let &WhereClause { gens, indent, end_newline } = self;
         if gens.where_predicates.is_empty() {
             return Ok(());
         }
@@ -882,7 +882,7 @@ fn fmt_impl(i: &clean::Impl,
 
     fmt_type(&i.for_, f, use_absolute, true)?;
 
-    fmt::Display::fmt(&WhereClause{ gens: &i.generics, indent: 0, end_newline: true }, f)?;
+    fmt::Display::fmt(&WhereClause { gens: &i.generics, indent: 0, end_newline: true }, f)?;
     Ok(())
 }
 

From ae0e45c02808551ec27ed940fad9a05cd9bcbaed Mon Sep 17 00:00:00 2001
From: QuietMisdreavus 
Date: Thu, 6 Apr 2017 14:19:45 -0500
Subject: [PATCH 7/8] rustdoc: where clause adjustment to fix tests

- add spaces to output so stripping lines and breaking spaces renders
  the same
- add commas to where clauses in rustdoc tests to match the new output
---
 src/librustdoc/html/format.rs     | 11 +++++++----
 src/librustdoc/html/render.rs     |  4 ++--
 src/test/rustdoc/impl-parts.rs    |  4 ++--
 src/test/rustdoc/issue-20727-4.rs |  4 ++--
 src/test/rustdoc/where.rs         |  2 +-
 5 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs
index 7c1139d7135..13a31c55702 100644
--- a/src/librustdoc/html/format.rs
+++ b/src/librustdoc/html/format.rs
@@ -192,9 +192,9 @@ impl<'a> fmt::Display for WhereClause<'a> {
             clause.push_str(" where");
         } else {
             if end_newline {
-                clause.push_str("where");
+                clause.push_str(" where");
             } else {
-                clause.push_str("where");
+                clause.push_str(" where");
             }
         }
         for (i, pred) in gens.where_predicates.iter().enumerate() {
@@ -241,8 +241,11 @@ impl<'a> fmt::Display for WhereClause<'a> {
             clause.push_str("");
             let padding = repeat(" ").take(indent + 4).collect::();
             clause = clause.replace("
", &format!("
{}", padding)); - clause.insert_str(0, &repeat(" ").take(indent).collect::()); - if !end_newline { + clause.insert_str(0, &repeat(" ").take(indent.saturating_sub(1)) + .collect::()); + if end_newline { + clause.push(' '); + } else { clause.insert_str(0, "
"); } } diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index b1ac6a5127f..af2beacf58a 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -2710,14 +2710,14 @@ fn render_struct(w: &mut fmt::Formatter, it: &clean::Item, } write!(w, ")")?; if let Some(g) = g { - write!(w, "{}", WhereClause { gens: g, indent: 0, end_newline: true })? + write!(w, "{}", WhereClause { gens: g, indent: 0, end_newline: false })? } write!(w, ";")?; } doctree::Unit => { // Needed for PhantomData. if let Some(g) = g { - write!(w, "{}", WhereClause { gens: g, indent: 0, end_newline: true })? + write!(w, "{}", WhereClause { gens: g, indent: 0, end_newline: false })? } write!(w, ";")?; } diff --git a/src/test/rustdoc/impl-parts.rs b/src/test/rustdoc/impl-parts.rs index 89c5e60e343..48ef4b6be66 100644 --- a/src/test/rustdoc/impl-parts.rs +++ b/src/test/rustdoc/impl-parts.rs @@ -17,7 +17,7 @@ impl AnOibit for .. {} pub struct Foo { field: T } // @has impl_parts/struct.Foo.html '//*[@class="impl"]//code' \ -// "impl !AnOibit for Foo where T: Sync" +// "impl !AnOibit for Foo where T: Sync," // @has impl_parts/trait.AnOibit.html '//*[@class="item-list"]//code' \ -// "impl !AnOibit for Foo where T: Sync" +// "impl !AnOibit for Foo where T: Sync," impl !AnOibit for Foo where T: Sync {} diff --git a/src/test/rustdoc/issue-20727-4.rs b/src/test/rustdoc/issue-20727-4.rs index 9ebd1c448ee..960e40b0709 100644 --- a/src/test/rustdoc/issue-20727-4.rs +++ b/src/test/rustdoc/issue-20727-4.rs @@ -35,7 +35,7 @@ pub trait IndexMut: Index { pub mod reexport { // @has issue_20727_4/reexport/trait.Index.html - // @has - '//*[@class="rust trait"]' 'trait Index where Idx: ?Sized {' + // @has - '//*[@class="rust trait"]' 'trait Index where Idx: ?Sized, {' // @has - '//*[@class="rust trait"]' 'type Output: ?Sized' // @has - '//*[@class="rust trait"]' \ // 'fn index(&self, index: Idx) -> &Self::Output' @@ -43,7 +43,7 @@ pub mod reexport { // @has issue_20727_4/reexport/trait.IndexMut.html // @has - '//*[@class="rust trait"]' \ - // 'trait IndexMut: Index where Idx: ?Sized {' + // 'trait IndexMut: Index where Idx: ?Sized, {' // @has - '//*[@class="rust trait"]' \ // 'fn index_mut(&mut self, index: Idx) -> &mut Self::Output;' pub use issue_20727::IndexMut; diff --git a/src/test/rustdoc/where.rs b/src/test/rustdoc/where.rs index d8dc115abf9..e691f7c5bea 100644 --- a/src/test/rustdoc/where.rs +++ b/src/test/rustdoc/where.rs @@ -44,5 +44,5 @@ pub enum Foxtrot { Foxtrot1(F) } impl MyTrait for Foxtrot where F: MyTrait {} // @has foo/type.Golf.html '//pre[@class="rust typedef"]' \ -// "type Golf where T: Clone = (T, T)" +// "type Golf where T: Clone, = (T, T)" pub type Golf where T: Clone = (T, T); From bfd01b7f40ae2cbfe9acbc1d10e79ffe16870df8 Mon Sep 17 00:00:00 2001 From: QuietMisdreavus Date: Thu, 6 Apr 2017 18:36:14 -0500 Subject: [PATCH 8/8] rustdoc: move the space at the end of where clauses ...so that we don't indent the next line by one extra space --- src/librustdoc/html/format.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 13a31c55702..d9bbc957c8a 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -237,15 +237,23 @@ impl<'a> fmt::Display for WhereClause<'a> { clause.push(','); } } + + if end_newline { + //add a space so stripping
tags and breaking spaces still renders properly + if f.alternate() { + clause.push(' '); + } else { + clause.push_str(" "); + } + } + if !f.alternate() { clause.push_str("
"); let padding = repeat(" ").take(indent + 4).collect::(); clause = clause.replace("
", &format!("
{}", padding)); clause.insert_str(0, &repeat(" ").take(indent.saturating_sub(1)) .collect::()); - if end_newline { - clause.push(' '); - } else { + if !end_newline { clause.insert_str(0, "
"); } }