@@ -23,90 +23,45 @@ let formatFile = SourceFile {
23
23
)
24
24
25
25
StructDecl ( modifiers: [ Token . public] , identifier: " Format " ) {
26
- VariableDecl (
27
- modifiers: [ Token . public] ,
28
- . let,
29
- name: " indentWidth " ,
30
- type: " Int "
31
- )
26
+ VariableDecl ( " public let indentWidth: Int " )
32
27
33
- VariableDecl (
34
- modifiers: [ Token . private] ,
35
- . var,
36
- name: " indents " ,
37
- type: " Int " ,
38
- initializer: IntegerLiteralExpr ( 0 )
39
- )
28
+ VariableDecl ( " private var indents: Int = 0 " )
40
29
41
30
InitializerDecl (
42
- modifiers: [ Token . public] ,
43
- signature: FunctionSignature (
44
- input: ParameterClause {
45
- FunctionParameter (
46
- firstName: . identifier( " indentWidth " ) ,
47
- colon: . colon,
48
- type: " Int " ,
49
- defaultArgument: IntegerLiteralExpr ( 4 )
50
- )
51
- }
52
- )
53
- ) {
54
- SequenceExpr {
55
- MemberAccessExpr ( base: " self " , name: " indentWidth " )
56
- AssignmentExpr ( )
57
- " indentWidth "
31
+ """
32
+ public init(indentWidth: Int = 4) {
33
+ self.indentWidth = indentWidth
58
34
}
59
- }
35
+ """
36
+ )
60
37
}
61
38
62
39
ExtensionDecl ( extendedType: " Format " ) {
63
40
VariableDecl (
64
- modifiers: [ Token . public] ,
65
- name: " _indented " ,
66
- type: " Self "
67
- ) {
68
- VariableDecl ( . var, name: " copy " , initializer: " self " )
69
- SequenceExpr {
70
- MemberAccessExpr ( base: " copy " , name: " indents " )
71
- BinaryOperatorExpr ( " += " )
72
- IntegerLiteralExpr ( 1 )
41
+ """
42
+ public var _indented: Self {
43
+ var copy = self
44
+ copy.indents += 1
45
+ return copy
73
46
}
74
- ReturnStmt ( expression : " copy " )
75
- }
47
+ """
48
+ )
76
49
77
50
VariableDecl (
78
- modifiers: [ Token . public] ,
79
- name: " indentTrivia " ,
80
- type: " Trivia "
81
- ) {
82
- TernaryExpr (
83
- if: SequenceExpr {
84
- " indents "
85
- BinaryOperatorExpr ( " == " )
86
- IntegerLiteralExpr ( 0 )
87
- } ,
88
- then: MemberAccessExpr ( name: " zero " ) ,
89
- else: FunctionCallExpr ( MemberAccessExpr ( name: " spaces " ) ) {
90
- TupleExprElement ( expression: SequenceExpr {
91
- " indents "
92
- BinaryOperatorExpr ( " * " )
93
- " indentWidth "
94
- } )
95
- }
96
- )
97
- }
51
+ """
52
+ public var indentTrivia: Trivia {
53
+ indents == 0 ? .zero : .spaces(indents * indentWidth)
54
+ }
55
+ """
56
+ )
98
57
99
58
VariableDecl (
100
- modifiers: [ Token . private] ,
101
- name: " indentedNewline " ,
102
- type: " Trivia "
103
- ) {
104
- SequenceExpr {
105
- MemberAccessExpr ( name: " newline " )
106
- BinaryOperatorExpr ( " + " )
107
- " indentTrivia "
59
+ """
60
+ private var indentedNewline: Trivia {
61
+ .newline + indentTrivia
108
62
}
109
- }
63
+ """
64
+ )
110
65
}
111
66
112
67
ExtensionDecl ( extendedType: " Format " ) {
@@ -136,57 +91,22 @@ private func createFormatFunctionSignature(type: SyntaxBuildableType) -> Functio
136
91
137
92
/// Generate the format implementation for a buildable node.
138
93
private func createBuildableNodeFormatFunction( node: Node ) -> FunctionDecl {
139
- FunctionDecl (
140
- modifiers: Token . public,
141
- identifier: . identifier( " format " ) ,
142
- signature: createFormatFunctionSignature ( type: node. type)
143
- ) {
144
- VariableDecl (
145
- . var,
146
- name: " result " ,
147
- initializer: node. children
148
- . filter ( \. requiresLeadingNewline)
149
- . reduce ( " syntax " ) { base, child in
150
- FunctionCallExpr ( MemberAccessExpr ( base: base, name: " with \( child. name) " ) ) {
151
- let childExpr = MemberAccessExpr ( base: " syntax " , name: child. swiftName)
152
- TupleExprElement ( expression: FunctionCallExpr ( MemberAccessExpr ( base: childExpr, name: " withLeadingTrivia " ) ) {
153
- TupleExprElement ( expression: SequenceExpr {
154
- " indentedNewline "
155
- BinaryOperatorExpr ( " + " )
156
- TupleExpr {
157
- SequenceExpr {
158
- MemberAccessExpr ( base: childExpr, name: " leadingTrivia " )
159
- BinaryOperatorExpr ( " ?? " )
160
- ArrayExpr ( )
161
- }
162
- }
163
- } )
164
- } )
165
- }
166
- }
167
- )
168
- VariableDecl (
169
- . let,
170
- name: " leadingTrivia " ,
171
- initializer: SequenceExpr {
172
- MemberAccessExpr ( base: " result " , name: " leadingTrivia " )
173
- BinaryOperatorExpr ( " ?? " )
174
- ArrayExpr ( )
175
- }
176
- )
177
- IfStmt ( conditions: ExprList {
178
- PrefixOperatorExpr ( " ! " , MemberAccessExpr ( base: " leadingTrivia " , name: " isEmpty " ) )
179
- } ) {
180
- SequenceExpr {
181
- " result "
182
- AssignmentExpr ( )
183
- FunctionCallExpr ( MemberAccessExpr ( base: " result " , name: " withLeadingTrivia " ) ) {
184
- TupleExprElement ( expression: " leadingTrivia " )
185
- }
94
+ var initializerExpr : ExprBuildable = IdentifierExpr ( " syntax " )
95
+ for child in node. children where child. requiresLeadingNewline {
96
+ initializerExpr = FunctionCallExpr ( " \( initializerExpr) .with \( child. name) (syntax. \( child. swiftName) .withLeadingTrivia(indentedNewline + (syntax. \( child. swiftName) .leadingTrivia ?? []))) " )
97
+ }
98
+ return FunctionDecl (
99
+ """
100
+ public func format(syntax: \( node. type. syntaxBaseName) ) -> \( node. type. syntaxBaseName) {
101
+ var result = \( initializerExpr)
102
+ let leadingTrivia = result.leadingTrivia ?? []
103
+ if !leadingTrivia.isEmpty {
104
+ result = result.withLeadingTrivia(leadingTrivia)
186
105
}
106
+ return result
187
107
}
188
- ReturnStmt ( expression : " result " )
189
- }
108
+ """
109
+ )
190
110
}
191
111
192
112
/// Generate the format implementation for a collection node.
@@ -198,29 +118,13 @@ private func createBuildableCollectionNodeFormatFunction(node: Node) -> Function
198
118
signature: createFormatFunctionSignature ( type: node. type)
199
119
) {
200
120
if node. elementsSeparatedByNewline {
201
- FunctionCallExpr ( node. type. syntaxBaseName) {
202
- TupleExprElement ( expression: FunctionCallExpr (
203
- MemberAccessExpr ( base: " syntax " , name: " map " ) ,
204
- trailingClosure: ClosureExpr {
205
- FunctionCallExpr ( MemberAccessExpr ( base: " $0 " , name: " withLeadingTrivia " ) ) {
206
- TupleExprElement ( expression: TupleExpr {
207
- SequenceExpr {
208
- " indentedNewline "
209
- BinaryOperatorExpr ( " + " )
210
- TupleExpr {
211
- SequenceExpr {
212
- MemberAccessExpr ( base: " $0 " , name: " leadingTrivia " )
213
- BinaryOperatorExpr ( " ?? " )
214
- ArrayExpr ( )
215
- }
216
- }
217
- }
218
- }
219
- )
220
- }
221
- }
222
- ) )
223
- }
121
+ FunctionCallExpr (
122
+ """
123
+ \( node. type. syntaxBaseName) (syntax.map {
124
+ $0.withLeadingTrivia((indentedNewline + ($0.leadingTrivia ?? [])))
125
+ })
126
+ """
127
+ )
224
128
} else {
225
129
" syntax "
226
130
}
@@ -250,14 +154,10 @@ private func createTokenFormatFunction() -> FunctionDecl {
250
154
private func createWithLeadingWithTrailingTriviaCall( token: TokenSpec ) -> CodeBlockItem {
251
155
var res : ExprBuildable = IdentifierExpr ( " syntax " )
252
156
if token. requiresLeadingSpace {
253
- res = FunctionCallExpr ( MemberAccessExpr ( base: res, name: " withLeadingTrivia " ) ) {
254
- TupleExprElement ( expression: MemberAccessExpr ( name: " space " ) )
255
- }
157
+ res = FunctionCallExpr ( " \( res) .withLeadingTrivia(.space) " )
256
158
}
257
159
if token. requiresTrailingSpace {
258
- res = FunctionCallExpr ( MemberAccessExpr ( base: res, name: " withTrailingTrivia " ) ) {
259
- TupleExprElement ( expression: MemberAccessExpr ( name: " space " ) )
260
- }
160
+ res = FunctionCallExpr ( " \( res) .withTrailingTrivia(.space) " )
261
161
}
262
162
return CodeBlockItem ( item: ReturnStmt ( expression: res) )
263
163
}
0 commit comments