Skip to content

Commit 4f444f5

Browse files
committed
Adjustments for refactoring of representation of Accessors in SwiftSyntax
1 parent d16d7de commit 4f444f5

File tree

3 files changed

+49
-31
lines changed

3 files changed

+49
-31
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ import SwiftFormatCore
1616
import SwiftOperators
1717
import SwiftSyntax
1818

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+
1930
/// Visits the nodes of a syntax tree and constructs a linear stream of formatting tokens that
2031
/// tell the pretty printer how the source text should be laid out.
2132
fileprivate final class TokenStreamCreator: SyntaxVisitor {
@@ -442,12 +453,16 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
442453

443454
before(node.returnClause.firstToken(viewMode: .sourceAccurate), tokens: .break)
444455

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)
451466
}
452467
}
453468

@@ -2160,12 +2175,16 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
21602175
}
21612176
}
21622177

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)
21692188
}
21702189
} else if let trailingComma = node.trailingComma {
21712190
// If this is one of multiple comma-delimited bindings, move any pending close breaks to
@@ -2960,24 +2979,24 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
29602979
/// Applies consistent formatting to the braces and contents of the given node.
29612980
///
29622981
/// - Parameter node: An `AccessorBlockSyntax` node.
2963-
private func arrangeBracesAndContents(of node: AccessorBlockSyntax) {
2982+
private func arrangeBracesAndContents(leftBrace: TokenSyntax, accessors: AccessorDeclListSyntax, rightBrace: TokenSyntax) {
29642983
// If the collection is empty, then any comments that might be present in the block must be
29652984
// leading trivia of the right brace.
2966-
let commentPrecedesRightBrace = node.rightBrace.leadingTrivia.numberOfComments > 0
2985+
let commentPrecedesRightBrace = rightBrace.leadingTrivia.numberOfComments > 0
29672986
// We can't use `count` here because it also includes missing children. Instead, we get an
29682987
// iterator and check if it returns `nil` immediately.
2969-
var accessorsIterator = node.accessors.makeIterator()
2988+
var accessorsIterator = accessors.makeIterator()
29702989
let areAccessorsEmpty = accessorsIterator.next() == nil
29712990
let bracesAreCompletelyEmpty = areAccessorsEmpty && !commentPrecedesRightBrace
29722991

2973-
before(node.leftBrace, tokens: .break(.reset, size: 1))
2992+
before(leftBrace, tokens: .break(.reset, size: 1))
29742993

29752994
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)
29782997
} 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))
29813000
}
29823001
}
29833002

Sources/SwiftFormatRules/UseShorthandTypeNames.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,19 +499,18 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
499499
/// Returns true if the given pattern binding represents a stored property/variable (as opposed to
500500
/// a computed property/variable).
501501
private func isStoredProperty(_ node: PatternBindingSyntax) -> Bool {
502-
guard let accessor = node.accessors else {
502+
guard let accessor = node.accessorBlock else {
503503
// If it has no accessors at all, it is definitely a stored property.
504504
return true
505505
}
506506

507-
guard let accessorBlock = accessor.as(AccessorBlockSyntax.self) else {
507+
guard case .accessors(let accessors) = accessor.accessors else {
508508
// If the accessor isn't an `AccessorBlockSyntax`, then it is a `CodeBlockSyntax`; i.e., the
509509
// accessor an implicit `get`. So, it is definitely not a stored property.
510-
assert(accessor.is(CodeBlockSyntax.self))
511510
return false
512511
}
513512

514-
for accessorDecl in accessorBlock.accessors {
513+
for accessorDecl in accessors {
515514
// Look for accessors that indicate that this is a computed property. If none are found, then
516515
// it is a stored property (e.g., having only observers like `willSet/didSet`).
517516
switch accessorDecl.accessorSpecifier.tokenKind {

Sources/SwiftFormatRules/UseSingleLinePropertyGetter.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ public final class UseSingleLinePropertyGetter: SyntaxFormatRule {
2222

2323
public override func visit(_ node: PatternBindingSyntax) -> PatternBindingSyntax {
2424
guard
25-
let accessorBlock = node.accessors?.as(AccessorBlockSyntax.self),
26-
let acc = accessorBlock.accessors.first,
25+
let accessorBlock = node.accessorBlock,
26+
case .accessors(let accessors) = accessorBlock.accessors,
27+
let acc = accessors.first,
2728
let body = acc.body,
28-
accessorBlock.accessors.count == 1,
29+
accessors.count == 1,
2930
acc.accessorSpecifier.tokenKind == .keyword(.get),
3031
acc.attributes == nil,
3132
acc.modifier == nil,
@@ -34,10 +35,9 @@ public final class UseSingleLinePropertyGetter: SyntaxFormatRule {
3435

3536
diagnose(.removeExtraneousGetBlock, on: acc)
3637

37-
let newBlock = CodeBlockSyntax(
38-
leftBrace: accessorBlock.leftBrace, statements: body.statements,
39-
rightBrace: accessorBlock.rightBrace)
40-
return node.with(\.accessors, .getter(newBlock))
38+
var result = node
39+
result.accessorBlock?.accessors = .getter(body.statements)
40+
return result
4141
}
4242
}
4343

0 commit comments

Comments
 (0)