Repository: YusukeHosonuma/SwiftPrettyPrint (swift)

Issue:
Support line-break style format for struct and class
Support line-break style format for struct or class.

## Related

Should fix # #25 at before this issue.

## Overview

In execute follows:

```swift
struct Owner {
    var name: String
    var age: Int
    var address: String?
}

let owner = Owner(name: "Nanachi", age: 20, address: "4th layer in Abyss")
Debug.prettyPrint(owner)
```

Currently, get like follows:

```swift
Owner(name: "Nanachi",
      age: 20,
      address: "4th layer in Abyss")
```

I purpose following style with option:

```swift
Owner(
    name: "Nanachi",
    age: 20,
    address: "4th layer in Abyss"
)
```

## Detail Design

This format option are specified by `Option#objectFormat` implemented by enum which include `.standard` and `.linebreak`.

```swift
Debug.prettyPrint(dog, option: Debug.Option(indent: 2, objectFormat: .standard))
// => output currently

Debug.prettyPrint(dog, option: Debug.Option(indent: 2, objectFormat: .linebreak))
// => output that is purpose by this issue
```

Pull request:
resolved #49

## As is
### At first glance, it is difficult to understand the hierarchy
If the following is implimented,
```
struct A {
  let b: B
  let string: String
}

struct B {
  let string: String
  let int: Int
}

let target = A(b: B(string: "hoge", int: 0), string: "foo")
Debug.pp >>> target
```
the result will be the following.
```
A(b: B(string: "hoge",
       int: 0),
  string: "foo")
```


## To be
### Using indent and new-line make the hierarchy easier to understand.

the result will be the following.
```
A(
 b: B(
     string: "hoge",
     int: 0
    ),
 string: "foo"
)
```

Patch:
diff --git a/Sources/Core/Formatter/MultilineFormatter.swift b/Sources/Core/Formatter/MultilineFormatter.swift
index a77c65d..ae5b152 100644
--- a/Sources/Core/Formatter/MultilineFormatter.swift
+++ b/Sources/Core/Formatter/MultilineFormatter.swift
@@ -60,24 +60,27 @@ class MultilineFormatter: PrettyFormatter {
     ///
     /// goes to
     ///
-    /// owner: Owner(name: "Nanachi",
-    ///              age: 4))
+    /// owner: Owner(
+    ///            name: "Nanachi",
+    ///            age: 4)
+    ///        )
     ///
     func objectString(
         typeName: String, fields: [(String, String)]
     ) -> String {
-        let prefix = "\(typeName)("
-        let body: String
-
         if fields.count == 1, let field = fields.first {
-            body = "\(field.0): \(field.1)"
+            return "\(typeName)(" + "\(field.0): \(field.1)" + ")"
         } else {
-            body = fields
+            let body = fields
                 .map { label, value in "\(label): \(value.indentTail(size: "\(label): ".count))" }
                 .joined(separator: ",\n")
-                .indentTail(size: prefix.count)
-        }
+                .indent(size: option.indent)
 
-        return prefix + body + ")"
+            return """
+            \(typeName)(
+            \(body)
+            )
+            """
+        }
     }
 }


Test patch:
diff --git a/Tests/SwiftPrettyPrintTests/Core/Formatter/MultilineFormatterTests.swift b/Tests/SwiftPrettyPrintTests/Core/Formatter/MultilineFormatterTests.swift
index 7590551..6d34280 100644
--- a/Tests/SwiftPrettyPrintTests/Core/Formatter/MultilineFormatterTests.swift
+++ b/Tests/SwiftPrettyPrintTests/Core/Formatter/MultilineFormatterTests.swift
@@ -104,21 +104,54 @@ class MultilineFormatterTests: XCTestCase {
     }
 
     func testObjectString() {
-        let fields: [(String, String)] = [
+        var fields: [(String, String)] = [
             ("name", #""pochi""#),
             ("owner", """
-            Owner(name: "Nanachi",
-                  age: 4)
+            Owner(
+              name: "Nanachi",
+              age: 4
+            )
             """),
         ]
 
+        var expected =
+        """
+        Dog(
+          name: "pochi",
+          owner: Owner(
+                   name: "Nanachi",
+                   age: 4
+                 )
+        )
+        """
+
         formatter = MultilineFormatter(option: option(indent: 2))
-        assertEqualLines(formatter.objectString(typeName: "Dog", fields: fields),
-                         """
-                         Dog(name: "pochi",
-                             owner: Owner(name: "Nanachi",
-                                          age: 4))
-                         """)
+        assertEqualLines(formatter.objectString(typeName: "Dog", fields: fields), expected)
+
+
+        fields = [
+            ("name", #""pochi""#),
+            ("owner", """
+            Owner(
+                name: "Nanachi",
+                age: 4
+            )
+            """),
+        ]
+
+        expected =
+        """
+        Dog(
+            name: "pochi",
+            owner: Owner(
+                       name: "Nanachi",
+                       age: 4
+                   )
+        )
+        """
+
+        formatter = MultilineFormatter(option: option(indent: 4))
+        assertEqualLines(formatter.objectString(typeName: "Dog", fields: fields), expected)
     }
     
     // MARK: - Helper
diff --git a/Tests/SwiftPrettyPrintTests/Public/DebugTests.swift b/Tests/SwiftPrettyPrintTests/Public/DebugTests.swift
index 00ddf91..26b1688 100644
--- a/Tests/SwiftPrettyPrintTests/Public/DebugTests.swift
+++ b/Tests/SwiftPrettyPrintTests/Public/DebugTests.swift
@@ -96,22 +96,26 @@ class DebugTests: XCTestCase {
         Debug.prettyPrint(dog, to: &result)
         assertEqualLines(result,
                          """
-                         Dog(id: "pochi",
+                         Dog(
+                             id: "pochi",
                              name: "ポチ",
                              nickname: nil,
                              age: 3,
-                             homepage: https://www.google.com/)
+                             homepage: https://www.google.com/
+                         )
                          """ + "\n")
 
         result = ""
         Debug.debugPrettyPrint(dog, to: &result)
         assertEqualLines(result,
                          """
-                         Dog(id: DogId(rawValue: "pochi"),
+                         Dog(
+                             id: DogId(rawValue: "pochi"),
                              name: Optional("ポチ"),
                              nickname: nil,
                              age: 3,
-                             homepage: Optional(https://www.google.com/))
+                             homepage: Optional(https://www.google.com/)
+                         )
                          """ + "\n")
 
         result = ""
@@ -119,16 +123,20 @@ class DebugTests: XCTestCase {
         assertEqualLines(result,
                          """
                          [
-                             Dog(id: "pochi",
+                             Dog(
+                                 id: "pochi",
                                  name: "ポチ",
                                  nickname: nil,
                                  age: 3,
-                                 homepage: https://www.google.com/),
-                             Dog(id: "pochi",
+                                 homepage: https://www.google.com/
+                             ),
+                             Dog(
+                                 id: "pochi",
                                  name: "ポチ",
                                  nickname: nil,
                                  age: 3,
-                                 homepage: https://www.google.com/)
+                                 homepage: https://www.google.com/
+                             )
                          ]
                          """ + "\n")
 
@@ -137,16 +145,20 @@ class DebugTests: XCTestCase {
         assertEqualLines(result,
                          """
                          [
-                             Dog(id: DogId(rawValue: "pochi"),
+                             Dog(
+                                 id: DogId(rawValue: "pochi"),
                                  name: Optional("ポチ"),
                                  nickname: nil,
                                  age: 3,
-                                 homepage: Optional(https://www.google.com/)),
-                             Dog(id: DogId(rawValue: "pochi"),
+                                 homepage: Optional(https://www.google.com/)
+                             ),
+                             Dog(
+                                 id: DogId(rawValue: "pochi"),
                                  name: Optional("ポチ"),
                                  nickname: nil,
                                  age: 3,
-                                 homepage: Optional(https://www.google.com/))
+                                 homepage: Optional(https://www.google.com/)
+                             )
                          ]
                          """ + "\n")
 
@@ -160,16 +172,20 @@ class DebugTests: XCTestCase {
         assertEqualLines(result,
                          """
                          [
-                             "dog-1": Dog(id: "pochi",
+                             "dog-1": Dog(
+                                          id: "pochi",
                                           name: "ポチ",
                                           nickname: nil,
                                           age: 3,
-                                          homepage: https://www.google.com/),
-                             "dog-2": Dog(id: "pochi",
+                                          homepage: https://www.google.com/
+                                      ),
+                             "dog-2": Dog(
+                                          id: "pochi",
                                           name: "ポチ",
                                           nickname: nil,
                                           age: 3,
-                                          homepage: https://www.google.com/)
+                                          homepage: https://www.google.com/
+                                      )
                          ]
                          """ + "\n")
 
@@ -178,16 +194,20 @@ class DebugTests: XCTestCase {
         assertEqualLines(result,
                          """
                          [
-                             "dog-1": Dog(id: DogId(rawValue: "pochi"),
+                             "dog-1": Dog(
+                                          id: DogId(rawValue: "pochi"),
                                           name: Optional("ポチ"),
                                           nickname: nil,
                                           age: 3,
-                                          homepage: Optional(https://www.google.com/)),
-                             "dog-2": Dog(id: DogId(rawValue: "pochi"),
+                                          homepage: Optional(https://www.google.com/)
+                                      ),
+                             "dog-2": Dog(
+                                          id: DogId(rawValue: "pochi"),
                                           name: Optional("ポチ"),
                                           nickname: nil,
                                           age: 3,
-                                          homepage: Optional(https://www.google.com/))
+                                          homepage: Optional(https://www.google.com/)
+                                      )
                          ]
                          """ + "\n")
     }