@@ -16,6 +16,17 @@ import SwiftFormatCore
16
16
import SwiftOperators
17
17
import SwiftSyntax
18
18
19
+ fileprivate extension AccessorBlockSyntax {
20
+ /// Assuming that the accessor only contains an implicit getter (i.e. no
21
+ /// `get` or `set`), return the code block items in that getter.
22
+ var getterCodeBlockItems : CodeBlockItemListSyntax {
23
+ guard case . getter( let codeBlockItemList) = self . accessors else {
24
+ preconditionFailure ( " AccessorBlock has an accessor list and not just a getter " )
25
+ }
26
+ return codeBlockItemList
27
+ }
28
+ }
29
+
19
30
/// Visits the nodes of a syntax tree and constructs a linear stream of formatting tokens that
20
31
/// tell the pretty printer how the source text should be laid out.
21
32
fileprivate final class TokenStreamCreator : SyntaxVisitor {
@@ -442,12 +453,16 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
442
453
443
454
before ( node. returnClause. firstToken ( viewMode: . sourceAccurate) , tokens: . break)
444
455
445
- if let accessorOrCodeBlock = node. accessors {
446
- switch accessorOrCodeBlock {
447
- case . accessors( let accessorBlock) :
448
- arrangeBracesAndContents ( of: accessorBlock)
449
- case . getter( let codeBlock) :
450
- arrangeBracesAndContents ( of: codeBlock, contentsKeyPath: \. statements)
456
+ if let accessorBlock = node. accessorBlock {
457
+ switch accessorBlock. accessors {
458
+ case . accessors( let accessors) :
459
+ arrangeBracesAndContents (
460
+ leftBrace: accessorBlock. leftBrace,
461
+ accessors: accessors,
462
+ rightBrace: accessorBlock. rightBrace
463
+ )
464
+ case . getter:
465
+ arrangeBracesAndContents ( of: accessorBlock, contentsKeyPath: \. getterCodeBlockItems)
451
466
}
452
467
}
453
468
@@ -2160,12 +2175,16 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
2160
2175
}
2161
2176
}
2162
2177
2163
- if let accessorOrCodeBlock = node. accessors {
2164
- switch accessorOrCodeBlock {
2165
- case . accessors( let accessorBlock) :
2166
- arrangeBracesAndContents ( of: accessorBlock)
2167
- case . getter( let codeBlock) :
2168
- arrangeBracesAndContents ( of: codeBlock, contentsKeyPath: \. statements)
2178
+ if let accessorBlock = node. accessorBlock {
2179
+ switch accessorBlock. accessors {
2180
+ case . accessors( let accessors) :
2181
+ arrangeBracesAndContents (
2182
+ leftBrace: accessorBlock. leftBrace,
2183
+ accessors: accessors,
2184
+ rightBrace: accessorBlock. rightBrace
2185
+ )
2186
+ case . getter:
2187
+ arrangeBracesAndContents ( of: accessorBlock, contentsKeyPath: \. getterCodeBlockItems)
2169
2188
}
2170
2189
} else if let trailingComma = node. trailingComma {
2171
2190
// If this is one of multiple comma-delimited bindings, move any pending close breaks to
@@ -2960,24 +2979,24 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
2960
2979
/// Applies consistent formatting to the braces and contents of the given node.
2961
2980
///
2962
2981
/// - Parameter node: An `AccessorBlockSyntax` node.
2963
- private func arrangeBracesAndContents( of node : AccessorBlockSyntax ) {
2982
+ private func arrangeBracesAndContents( leftBrace : TokenSyntax , accessors : AccessorDeclListSyntax , rightBrace : TokenSyntax ) {
2964
2983
// If the collection is empty, then any comments that might be present in the block must be
2965
2984
// leading trivia of the right brace.
2966
- let commentPrecedesRightBrace = node . rightBrace. leadingTrivia. numberOfComments > 0
2985
+ let commentPrecedesRightBrace = rightBrace. leadingTrivia. numberOfComments > 0
2967
2986
// We can't use `count` here because it also includes missing children. Instead, we get an
2968
2987
// iterator and check if it returns `nil` immediately.
2969
- var accessorsIterator = node . accessors. makeIterator ( )
2988
+ var accessorsIterator = accessors. makeIterator ( )
2970
2989
let areAccessorsEmpty = accessorsIterator. next ( ) == nil
2971
2990
let bracesAreCompletelyEmpty = areAccessorsEmpty && !commentPrecedesRightBrace
2972
2991
2973
- before ( node . leftBrace, tokens: . break( . reset, size: 1 ) )
2992
+ before ( leftBrace, tokens: . break( . reset, size: 1 ) )
2974
2993
2975
2994
if !bracesAreCompletelyEmpty {
2976
- after ( node . leftBrace, tokens: . break( . open, size: 1 ) , . open)
2977
- before ( node . rightBrace, tokens: . break( . close, size: 1 ) , . close)
2995
+ after ( leftBrace, tokens: . break( . open, size: 1 ) , . open)
2996
+ before ( rightBrace, tokens: . break( . close, size: 1 ) , . close)
2978
2997
} else {
2979
- after ( node . leftBrace, tokens: . break( . open, size: 0 ) )
2980
- before ( node . rightBrace, tokens: . break( . close, size: 0 ) )
2998
+ after ( leftBrace, tokens: . break( . open, size: 0 ) )
2999
+ before ( rightBrace, tokens: . break( . close, size: 0 ) )
2981
3000
}
2982
3001
}
2983
3002
0 commit comments