@@ -42,4 +42,61 @@ public class CodeGenerationFormat: BasicFormat {
42
42
return super. visit ( node)
43
43
}
44
44
}
45
+
46
+ public override func visit( _ node: TupleExprElementListSyntax ) -> TupleExprElementListSyntax {
47
+ let children = node. children ( viewMode: . all)
48
+ // If the function only takes a single argument, display it on the same line
49
+ if let callee = node. parent? . as ( FunctionCallExprSyntax . self) ? . calledExpression. as ( MemberAccessExprSyntax . self) , callee. base == nil {
50
+ // This is a constructor for tokens. Write them on a single line
51
+ return super. visit ( node)
52
+ }
53
+ if children. count > 3 {
54
+ return TupleExprElementListSyntax ( formatChildrenSeparatedByNewline ( children: children, elementType: TupleExprElementSyntax . self) )
55
+ } else {
56
+ return super. visit ( node)
57
+ }
58
+ }
59
+
60
+ public override func visit( _ node: ArrayElementListSyntax ) -> ArrayElementListSyntax {
61
+ let children = node. children ( viewMode: . all)
62
+ // Short array literals are presented on one line, list each element on a different line.
63
+ if children. count > 2 {
64
+ return ArrayElementListSyntax ( formatChildrenSeparatedByNewline ( children: children, elementType: ArrayElementSyntax . self) )
65
+ } else {
66
+ return super. visit ( node)
67
+ }
68
+ }
69
+
70
+ public override func visit( _ node: DictionaryElementListSyntax ) -> DictionaryElementListSyntax {
71
+ let children = node. children ( viewMode: . all)
72
+ // Short array literals are presented on one line, list each element on a different line.
73
+ if children. count > 2 {
74
+ return DictionaryElementListSyntax ( formatChildrenSeparatedByNewline ( children: children, elementType: DictionaryElementSyntax . self) )
75
+ } else {
76
+ return super. visit ( node)
77
+ }
78
+ }
79
+ }
80
+
81
+ // MARK: - Private helpers
82
+
83
+ fileprivate extension CodeGenerationFormat {
84
+ func formatChildrenSeparatedByNewline< SyntaxType: SyntaxProtocol > ( children: SyntaxChildren , elementType: SyntaxType . Type ) -> [ SyntaxType ] {
85
+ indentationLevel += 1
86
+ var formattedChildren = children. map {
87
+ self . visit ( $0) . as ( SyntaxType . self) !
88
+ }
89
+ formattedChildren = formattedChildren. map {
90
+ if $0. leadingTrivia? . first? . isNewline == true {
91
+ return $0
92
+ } else {
93
+ return $0. with ( \. leadingTrivia, indentedNewline + ( $0. leadingTrivia ?? [ ] ) )
94
+ }
95
+ }
96
+ indentationLevel -= 1
97
+ if !formattedChildren. isEmpty {
98
+ formattedChildren [ formattedChildren. count - 1 ] = formattedChildren [ formattedChildren. count - 1 ] . with ( \. trailingTrivia, indentedNewline)
99
+ }
100
+ return formattedChildren
101
+ }
45
102
}
0 commit comments