@@ -17,11 +17,42 @@ import SwiftSyntax
17
17
public class CodeGenerationFormat : BasicFormat {
18
18
public override var indentation : TriviaPiece { . spaces( indentationLevel * 2 ) }
19
19
20
- public func ensuringTwoLeadingNewlines< NodeType: SyntaxProtocol > ( node: NodeType ) -> NodeType {
21
- if node. leadingTrivia? . first? . isNewline ?? false {
22
- return node. with ( \. leadingTrivia, indentedNewline + ( node. leadingTrivia ?? [ ] ) )
20
+ public override func visit( _ node: ArrayElementListSyntax ) -> ArrayElementListSyntax {
21
+ let children = node. children ( viewMode: . all)
22
+ // Short array literals are presented on one line, list each element on a different line.
23
+ if children. count > 3 {
24
+ return ArrayElementListSyntax ( formatChildrenSeparatedByNewline ( children: children, elementType: ArrayElementSyntax . self) )
23
25
} else {
24
- return node. with ( \. leadingTrivia, indentedNewline + indentedNewline + ( node. leadingTrivia ?? [ ] ) )
26
+ return super. visit ( node)
27
+ }
28
+ }
29
+
30
+ public override func visit( _ node: CodeBlockItemSyntax ) -> CodeBlockItemSyntax {
31
+ if node. parent? . parent? . is ( SourceFileSyntax . self) == true , !node. item. is ( ImportDeclSyntax . self) {
32
+ let formatted = super. visit ( node)
33
+ return ensuringTwoLeadingNewlines ( node: formatted)
34
+ } else {
35
+ return super. visit ( node)
36
+ }
37
+ }
38
+
39
+ public override func visit( _ node: DictionaryElementListSyntax ) -> DictionaryElementListSyntax {
40
+ let children = node. children ( viewMode: . all)
41
+ // Short dictionary literals are presented on one line, list each element on a different line.
42
+ if children. count > 3 {
43
+ return DictionaryElementListSyntax ( formatChildrenSeparatedByNewline ( children: children, elementType: DictionaryElementSyntax . self) )
44
+ } else {
45
+ return super. visit ( node)
46
+ }
47
+ }
48
+
49
+ public override func visit( _ node: FunctionParameterListSyntax ) -> FunctionParameterListSyntax {
50
+ let children = node. children ( viewMode: . all)
51
+ // Short function parameter literals are presented on one line, list each element on a different line.
52
+ if children. count > 3 {
53
+ return FunctionParameterListSyntax ( formatChildrenSeparatedByNewline ( children: children, elementType: FunctionParameterSyntax . self) )
54
+ } else {
55
+ return super. visit ( node)
25
56
}
26
57
}
27
58
@@ -34,12 +65,42 @@ public class CodeGenerationFormat: BasicFormat {
34
65
}
35
66
}
36
67
37
- public override func visit( _ node: CodeBlockItemSyntax ) -> CodeBlockItemSyntax {
38
- if node. parent? . parent? . is ( SourceFileSyntax . self) == true , !node. item. is ( ImportDeclSyntax . self) {
39
- let formatted = super. visit ( node)
40
- return ensuringTwoLeadingNewlines ( node: formatted)
68
+ public override func visit( _ node: TupleExprElementListSyntax ) -> TupleExprElementListSyntax {
69
+ let children = node. children ( viewMode: . all)
70
+ // Short tuple element list literals are presented on one line, list each element on a different line.
71
+ if children. count > 3 {
72
+ return TupleExprElementListSyntax ( formatChildrenSeparatedByNewline ( children: children, elementType: TupleExprElementSyntax . self) )
41
73
} else {
42
74
return super. visit ( node)
43
75
}
44
76
}
77
+
78
+ // MARK: - Private
79
+
80
+ private func ensuringTwoLeadingNewlines< NodeType: SyntaxProtocol > ( node: NodeType ) -> NodeType {
81
+ if node. leadingTrivia? . first? . isNewline ?? false {
82
+ return node. with ( \. leadingTrivia, indentedNewline + ( node. leadingTrivia ?? [ ] ) )
83
+ } else {
84
+ return node. with ( \. leadingTrivia, indentedNewline + indentedNewline + ( node. leadingTrivia ?? [ ] ) )
85
+ }
86
+ }
87
+
88
+ private func formatChildrenSeparatedByNewline< SyntaxType: SyntaxProtocol > ( children: SyntaxChildren , elementType: SyntaxType . Type ) -> [ SyntaxType ] {
89
+ indentationLevel += 1
90
+ var formattedChildren = children. map {
91
+ self . visit ( $0) . as ( SyntaxType . self) !
92
+ }
93
+ formattedChildren = formattedChildren. map {
94
+ if $0. leadingTrivia? . first? . isNewline == true {
95
+ return $0
96
+ } else {
97
+ return $0. with ( \. leadingTrivia, indentedNewline + ( $0. leadingTrivia ?? [ ] ) )
98
+ }
99
+ }
100
+ indentationLevel -= 1
101
+ if !formattedChildren. isEmpty {
102
+ formattedChildren [ formattedChildren. count - 1 ] = formattedChildren [ formattedChildren. count - 1 ] . with ( \. trailingTrivia, indentedNewline)
103
+ }
104
+ return formattedChildren
105
+ }
45
106
}
0 commit comments