From 6bc3d65948c3606c29beb8da359d2a45a36e5c15 Mon Sep 17 00:00:00 2001 From: QuietMisdreavus Date: Tue, 4 Apr 2017 10:31:57 -0500 Subject: [PATCH] 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,
                })
     }