@@ -246,18 +246,29 @@ private final class TokenStreamCreator: SyntaxVisitor {
246
246
// MARK: - Function and function-like declaration nodes (initializers, deinitializers, subscripts)
247
247
248
248
func visit( _ node: FunctionDeclSyntax ) -> SyntaxVisitorContinueKind {
249
- // Prioritize keeping "<modifiers> func <name>" together.
250
- let firstTokenAfterAttributes = node. modifiers? . firstToken ?? node. funcKeyword
251
- before ( firstTokenAfterAttributes, tokens: . open)
252
- after ( node. funcKeyword, tokens: . break)
253
- after ( node. identifier, tokens: . close)
249
+ let hasArguments = !node. signature. input. parameterList. isEmpty
254
250
255
- // Prioritize keeping ") throws -> <return_type>" together.
256
- if config. prioritizeKeepingFunctionOutputTogether {
251
+ // Prioritize keeping ") throws -> <return_type>" together. We can only do this if the function
252
+ // has arguments.
253
+ if hasArguments && config. prioritizeKeepingFunctionOutputTogether {
257
254
// Due to visitation order, the matching .open break is added in ParameterClauseSyntax.
258
255
after ( node. signature. lastToken, tokens: . close)
259
256
}
260
257
258
+ let mustBreak = node. body != nil || node. signature. output != nil
259
+ arrangeParameterClause ( node. signature. input, forcesBreakBeforeRightParen: mustBreak)
260
+
261
+ // Prioritize keeping "<modifiers> func <name>(" together. Also include the ")" if the parameter
262
+ // list is empty.
263
+ let firstTokenAfterAttributes = node. modifiers? . firstToken ?? node. funcKeyword
264
+ before ( firstTokenAfterAttributes, tokens: . open)
265
+ after ( node. funcKeyword, tokens: . break)
266
+ if hasArguments || node. genericParameterClause != nil {
267
+ after ( node. signature. input. leftParen, tokens: . close)
268
+ } else {
269
+ after ( node. signature. input. rightParen, tokens: . close)
270
+ }
271
+
261
272
// Add a non-breaking space after the identifier if it's an operator, to separate it visually
262
273
// from the following parenthesis or generic argument list. Note that even if the function is
263
274
// defining a prefix or postfix operator, or even if the operator isn't originally followed by a
@@ -266,9 +277,6 @@ private final class TokenStreamCreator: SyntaxVisitor {
266
277
after ( node. identifier. lastToken, tokens: . space)
267
278
}
268
279
269
- let mustBreak = node. body != nil || node. signature. output != nil
270
- arrangeParameterClause ( node. signature. input, forcesBreakBeforeRightParen: mustBreak)
271
-
272
280
arrangeFunctionLikeDecl (
273
281
node,
274
282
attributes: node. attributes,
@@ -280,18 +288,22 @@ private final class TokenStreamCreator: SyntaxVisitor {
280
288
}
281
289
282
290
func visit( _ node: InitializerDeclSyntax ) -> SyntaxVisitorContinueKind {
291
+ let hasArguments = !node. parameters. parameterList. isEmpty
292
+
293
+ arrangeParameterClause ( node. parameters, forcesBreakBeforeRightParen: node. body != nil )
294
+
283
295
// Prioritize keeping "<modifiers> init<punctuation>" together.
284
296
let firstTokenAfterAttributes = node. modifiers? . firstToken ?? node. initKeyword
285
- let lastTokenOfName = node. optionalMark ?? node. initKeyword
286
- if firstTokenAfterAttributes != lastTokenOfName {
287
- before ( firstTokenAfterAttributes, tokens: . open)
288
- after ( lastTokenOfName, tokens: . close)
297
+ before ( firstTokenAfterAttributes, tokens: . open)
298
+
299
+ if hasArguments || node. genericParameterClause != nil {
300
+ after ( node. parameters. leftParen, tokens: . close)
301
+ } else {
302
+ after ( node. parameters. rightParen, tokens: . close)
289
303
}
290
304
291
305
before ( node. throwsOrRethrowsKeyword, tokens: . break)
292
306
293
- arrangeParameterClause ( node. parameters, forcesBreakBeforeRightParen: node. body != nil )
294
-
295
307
arrangeFunctionLikeDecl (
296
308
node,
297
309
attributes: node. attributes,
@@ -313,16 +325,24 @@ private final class TokenStreamCreator: SyntaxVisitor {
313
325
}
314
326
315
327
func visit( _ node: SubscriptDeclSyntax ) -> SyntaxVisitorContinueKind {
328
+ let hasArguments = !node. indices. parameterList. isEmpty
329
+
316
330
before ( node. firstToken, tokens: . open)
317
331
318
332
// Prioritize keeping "<modifiers> subscript" together.
319
333
if let firstModifierToken = node. modifiers? . firstToken {
320
334
before ( firstModifierToken, tokens: . open)
321
- after ( node. subscriptKeyword, tokens: . close)
335
+
336
+ if hasArguments || node. genericParameterClause != nil {
337
+ after ( node. indices. leftParen, tokens: . close)
338
+ } else {
339
+ after ( node. indices. rightParen, tokens: . close)
340
+ }
322
341
}
323
342
324
- // Prioritize keeping ") -> <return_type>" together.
325
- if config. prioritizeKeepingFunctionOutputTogether {
343
+ // Prioritize keeping ") -> <return_type>" together. We can only do this if the subscript has
344
+ // arguments.
345
+ if hasArguments && config. prioritizeKeepingFunctionOutputTogether {
326
346
// Due to visitation order, the matching .open break is added in ParameterClauseSyntax.
327
347
after ( node. result. lastToken, tokens: . close)
328
348
}
@@ -892,8 +912,9 @@ private final class TokenStreamCreator: SyntaxVisitor {
892
912
}
893
913
894
914
func visit( _ node: ParameterClauseSyntax ) -> SyntaxVisitorContinueKind {
895
- // Prioritize keeping ") throws -> <return_type>" together.
896
- if config. prioritizeKeepingFunctionOutputTogether {
915
+ // Prioritize keeping ") throws -> <return_type>" together. We can only do this if the function
916
+ // has arguments.
917
+ if !node. parameterList. isEmpty && config. prioritizeKeepingFunctionOutputTogether {
897
918
// Due to visitation order, this .open corresponds to a .close added in FunctionDeclSyntax
898
919
// or SubscriptDeclSyntax.
899
920
before ( node. rightParen, tokens: . open)
@@ -2042,6 +2063,8 @@ private final class TokenStreamCreator: SyntaxVisitor {
2042
2063
private func arrangeParameterClause(
2043
2064
_ parameters: ParameterClauseSyntax , forcesBreakBeforeRightParen: Bool
2044
2065
) {
2066
+ guard !parameters. parameterList. isEmpty else { return }
2067
+
2045
2068
after ( parameters. leftParen, tokens: . break( . open, size: 0 ) , . open( argumentListConsistency ( ) ) )
2046
2069
before (
2047
2070
parameters. rightParen,
0 commit comments