@@ -42,4 +42,71 @@ 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: FunctionParameterListSyntax ) -> FunctionParameterListSyntax {
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 > 3 {
64
+ return FunctionParameterListSyntax ( formatChildrenSeparatedByNewline ( children: children, elementType: FunctionParameterSyntax . self) )
65
+ } else {
66
+ return super. visit ( node)
67
+ }
68
+ }
69
+
70
+ public override func visit( _ node: ArrayElementListSyntax ) -> ArrayElementListSyntax {
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 ArrayElementListSyntax ( formatChildrenSeparatedByNewline ( children: children, elementType: ArrayElementSyntax . self) )
75
+ } else {
76
+ return super. visit ( node)
77
+ }
78
+ }
79
+
80
+ public override func visit( _ node: DictionaryElementListSyntax ) -> DictionaryElementListSyntax {
81
+ let children = node. children ( viewMode: . all)
82
+ // Short array literals are presented on one line, list each element on a different line.
83
+ if children. count > 2 {
84
+ return DictionaryElementListSyntax ( formatChildrenSeparatedByNewline ( children: children, elementType: DictionaryElementSyntax . self) )
85
+ } else {
86
+ return super. visit ( node)
87
+ }
88
+ }
89
+ }
90
+
91
+ // MARK: - Private helpers
92
+
93
+ fileprivate extension CodeGenerationFormat {
94
+ func formatChildrenSeparatedByNewline< SyntaxType: SyntaxProtocol > ( children: SyntaxChildren , elementType: SyntaxType . Type ) -> [ SyntaxType ] {
95
+ indentationLevel += 1
96
+ var formattedChildren = children. map {
97
+ self . visit ( $0) . as ( SyntaxType . self) !
98
+ }
99
+ formattedChildren = formattedChildren. map {
100
+ if $0. leadingTrivia? . first? . isNewline == true {
101
+ return $0
102
+ } else {
103
+ return $0. with ( \. leadingTrivia, indentedNewline + ( $0. leadingTrivia ?? [ ] ) )
104
+ }
105
+ }
106
+ indentationLevel -= 1
107
+ if !formattedChildren. isEmpty {
108
+ formattedChildren [ formattedChildren. count - 1 ] = formattedChildren [ formattedChildren. count - 1 ] . with ( \. trailingTrivia, indentedNewline)
109
+ }
110
+ return formattedChildren
111
+ }
45
112
}
0 commit comments