diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 9f2d02c14dde0..ffef42bc3d27c 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -41,22 +41,38 @@ 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); /// 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 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, +} + pub struct HRef<'a> { pub did: DefId, pub text: &'a str, @@ -167,24 +183,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 +232,29 @@ impl<'a> fmt::Display for WhereClause<'a> { } } } + + if i < gens.where_predicates.len() - 1 || end_newline { + 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 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.saturating_sub(1)) + .collect::()); + if !end_newline { + clause.insert_str(0, "
"); } } write!(f, "{}", clause) @@ -838,43 +865,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(()) } @@ -939,12 +958,15 @@ 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(); 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 => { @@ -970,7 +992,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() { @@ -986,8 +1008,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 +1025,23 @@ 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); + 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 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 { - output = format!("({args})
{arrow}", args = args, arrow = arrow); - plain = format!("{pad}({args}){arrow}", - pad = pad, - args = args_plain, - arrow = arrow_plain); - } + format!("({args}){arrow}", args = args.replace("
", ""), arrow = arrow) + }; - if plain.len() > 80 { - let pad = repeat(" ").take(indent).collect::(); - let pad = format!("
{}", pad); - output = output.replace("
", &pad); - } else { - output = output.replace("
", ""); - } if f.alternate() { write!(f, "{}", output.replace("
", "\n")) } else { diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 1e1202f04005c..42a18345681f6 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -2016,13 +2016,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 \
@@ -2033,8 +2033,12 @@ 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),
-           decl = Method(&f.decl, indent))?;
+           where_clause = WhereClause { gens: &f.generics, indent: 0, end_newline: true },
+           decl = Method {
+               decl: &f.decl,
+               name_len: name_len,
+               indent: 0,
+           })?;
     document(w, cx, it)
 }
 
@@ -2062,14 +2066,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,
-           // Where clauses in traits are indented nine spaces, per rustdoc.css
-           WhereClause(&t.generics, 9))?;
+           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::>();
@@ -2108,7 +2116,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, "}}")?;
     }
@@ -2342,21 +2357,17 @@ fn render_assoc_item(w: &mut fmt::Formatter,
         } else {
             hir::Constness::NotConst
         };
-        let prefix = format!("{}{}{:#}fn {}{:#}",
-                             ConstnessSpace(vis_constness),
-                             UnsafetySpace(unsafety),
-                             AbiSpace(abi),
-                             name,
-                             *g);
-        let mut indent = prefix.len();
-        let where_indent = if parent == ItemType::Trait {
-            indent += 4;
-            8
-        } else if parent == ItemType::Impl {
-            2
+        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 {
-            let prefix = prefix + &format!("{:#}", Method(d, indent));
-            prefix.lines().last().unwrap().len() + 1
+            (0, true)
         };
         write!(w, "{}{}{}fn {name}\
                    {generics}{decl}{where_clause}",
@@ -2366,8 +2377,16 @@ fn render_assoc_item(w: &mut fmt::Formatter,
                href = href,
                name = name,
                generics = *g,
-               decl = Method(d, indent),
-               where_clause = WhereClause(g, where_indent))
+               decl = Method {
+                   decl: d,
+                   name_len: head_len,
+                   indent: indent,
+               },
+               where_clause = WhereClause {
+                   gens: g,
+                   indent: indent,
+                   end_newline: end_newline,
+               })
     }
     match item.inner {
         clean::StrippedItem(..) => Ok(()),
@@ -2480,15 +2499,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 {
@@ -2662,23 +2677,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, " {{")?;
@@ -2707,35 +2716,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: false })?
             }
             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: false })?
             }
             write!(w, ";")?;
         }
@@ -2748,19 +2752,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)?;
@@ -3059,13 +3057,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 4edf6309346ac..ddaa00aa4fb14 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 +} diff --git a/src/test/rustdoc/impl-parts.rs b/src/test/rustdoc/impl-parts.rs index 89c5e60e34314..48ef4b6be66de 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 9ebd1c448eeb0..960e40b070985 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 d8dc115abf91e..e691f7c5bea01 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);