Skip to content

Commit c5b5a32

Browse files
committed
Fix swift-format In Response to SwiftSyntax Changes
1 parent f30e81e commit c5b5a32

File tree

6 files changed

+38
-21
lines changed

6 files changed

+38
-21
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,21 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
155155
Syntax(node),
156156
attributes: node.attributes,
157157
modifiers: node.modifiers,
158-
typeKeyword: node.classOrActorKeyword,
158+
typeKeyword: node.classKeyword,
159+
identifier: node.identifier,
160+
genericParameterClause: node.genericParameterClause,
161+
inheritanceClause: node.inheritanceClause,
162+
genericWhereClause: node.genericWhereClause,
163+
members: node.members)
164+
return .visitChildren
165+
}
166+
167+
override func visit(_ node: ActorDeclSyntax) -> SyntaxVisitorContinueKind {
168+
arrangeTypeDeclBlock(
169+
Syntax(node),
170+
attributes: node.attributes,
171+
modifiers: node.modifiers,
172+
typeKeyword: node.actorKeyword,
159173
identifier: node.identifier,
160174
genericParameterClause: node.genericParameterClause,
161175
inheritanceClause: node.inheritanceClause,
@@ -305,22 +319,27 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
305319
}
306320

307321
override func visit(_ node: InitializerDeclSyntax) -> SyntaxVisitorContinueKind {
308-
let hasArguments = !node.parameters.parameterList.isEmpty
322+
let hasArguments = !node.signature.input.parameterList.isEmpty
309323

310-
arrangeParameterClause(node.parameters, forcesBreakBeforeRightParen: node.body != nil)
324+
// Prioritize keeping ") throws" together. We can only do this if the function
325+
// has arguments.
326+
if hasArguments && config.prioritizeKeepingFunctionOutputTogether {
327+
// Due to visitation order, the matching .open break is added in ParameterClauseSyntax.
328+
after(node.signature.lastToken, tokens: .close)
329+
}
330+
331+
arrangeParameterClause(node.signature.input, forcesBreakBeforeRightParen: node.body != nil)
311332

312333
// Prioritize keeping "<modifiers> init<punctuation>" together.
313334
let firstTokenAfterAttributes = node.modifiers?.firstToken ?? node.initKeyword
314335
before(firstTokenAfterAttributes, tokens: .open)
315336

316337
if hasArguments || node.genericParameterClause != nil {
317-
after(node.parameters.leftParen, tokens: .close)
338+
after(node.signature.input.leftParen, tokens: .close)
318339
} else {
319-
after(node.parameters.rightParen, tokens: .close)
340+
after(node.signature.input.rightParen, tokens: .close)
320341
}
321342

322-
before(node.throwsOrRethrowsKeyword, tokens: .break)
323-
324343
arrangeFunctionLikeDecl(
325344
Syntax(node),
326345
attributes: node.attributes,
@@ -2486,7 +2505,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
24862505
var verbatimText = ""
24872506
for piece in trailingTrivia[...lastGarbageIndex] {
24882507
switch piece {
2489-
case .garbageText, .spaces, .tabs, .formfeeds, .verticalTabs:
2508+
case .shebang, .garbageText, .spaces, .tabs, .formfeeds, .verticalTabs:
24902509
piece.write(to: &verbatimText)
24912510
default:
24922511
// The implementation of the lexer today ensures that newlines, carriage returns, and
@@ -2937,7 +2956,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
29372956
}
29382957
}
29392958

2940-
case .garbageText(let text):
2959+
case .shebang(let text), .garbageText(let text):
29412960
// Garbage text in leading trivia might be something meaningful that would be disruptive to
29422961
// throw away when formatting the file, like a hashbang line or Unicode byte-order marker at
29432962
// the beginning of a file, or source control conflict markers. Keep it as verbatim text so

Sources/SwiftFormatRules/FullyIndirectEnum.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ public final class FullyIndirectEnum: SyntaxFormatRule {
6868

6969
let newModifier = SyntaxFactory.makeDeclModifier(
7070
name: SyntaxFactory.makeIdentifier(
71-
"indirect", leadingTrivia: leadingTrivia, trailingTrivia: .spaces(1)), detailLeftParen: nil,
72-
detail: nil, detailRightParen: nil)
71+
"indirect", leadingTrivia: leadingTrivia, trailingTrivia: .spaces(1)), detail: nil)
7372

7473
let newMemberBlock = node.members.withMembers(SyntaxFactory.makeMemberDeclList(newMembers))
7574
return DeclSyntax(newEnumDecl.addModifier(newModifier).withMembers(newMemberBlock))

Sources/SwiftFormatRules/ModifierListSyntax+Convenience.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ extension ModifierListSyntax {
5050
func createModifierToken(name: String) -> DeclModifierSyntax {
5151
let id = SyntaxFactory.makeIdentifier(name, trailingTrivia: .spaces(1))
5252
let newModifier = SyntaxFactory.makeDeclModifier(
53-
name: id, detailLeftParen: nil, detail: nil, detailRightParen: nil)
53+
name: id, detail: nil)
5454
return newModifier
5555
}
5656

Sources/SwiftFormatRules/ReturnVoidInsteadOfEmptyTuple.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public final class ReturnVoidInsteadOfEmptyTuple: SyntaxFormatRule {
8282
private func hasNonWhitespaceLeadingTrivia(_ token: TokenSyntax) -> Bool {
8383
for piece in token.leadingTrivia {
8484
switch piece {
85-
case .blockComment, .docBlockComment, .docLineComment, .garbageText, .lineComment:
85+
case .blockComment, .docBlockComment, .docLineComment, .garbageText, .lineComment, .shebang:
8686
return true
8787
default:
8888
break

Sources/SwiftFormatRules/UseSynthesizedInitializer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public final class UseSynthesizedInitializer: SyntaxLintRule {
4040
// Collect any possible redundant initializers into a list
4141
} else if let initDecl = member.as(InitializerDeclSyntax.self) {
4242
guard initDecl.optionalMark == nil else { continue }
43-
guard initDecl.throwsOrRethrowsKeyword == nil else { continue }
43+
guard initDecl.signature.throwsOrRethrowsKeyword == nil else { continue }
4444
initializers.append(initDecl)
4545
}
4646
}
@@ -51,7 +51,7 @@ public final class UseSynthesizedInitializer: SyntaxLintRule {
5151
for initializer in initializers {
5252
guard
5353
matchesPropertyList(
54-
parameters: initializer.parameters.parameterList,
54+
parameters: initializer.signature.input.parameterList,
5555
properties: storedProperties)
5656
else { continue }
5757
guard

Sources/SwiftFormatRules/ValidateDocumentationComments.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,19 @@ public final class ValidateDocumentationComments: SyntaxLintRule {
2929

3030
public override func visit(_ node: InitializerDeclSyntax) -> SyntaxVisitorContinueKind {
3131
return checkFunctionLikeDocumentation(
32-
DeclSyntax(node), name: "init", parameters: node.parameters.parameterList, throwsOrRethrowsKeyword: node.throwsOrRethrowsKeyword)
32+
DeclSyntax(node), name: "init", signature: node.signature)
3333
}
3434

3535
public override func visit(_ node: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
3636
return checkFunctionLikeDocumentation(
37-
DeclSyntax(node), name: node.identifier.text, parameters: node.signature.input.parameterList, throwsOrRethrowsKeyword: node.signature.throwsOrRethrowsKeyword,
37+
DeclSyntax(node), name: node.identifier.text, signature: node.signature,
3838
returnClause: node.signature.output)
3939
}
4040

4141
private func checkFunctionLikeDocumentation(
4242
_ node: DeclSyntax,
4343
name: String,
44-
parameters: FunctionParameterListSyntax,
45-
throwsOrRethrowsKeyword: TokenSyntax?,
44+
signature: FunctionSignatureSyntax,
4645
returnClause: ReturnClauseSyntax? = nil
4746
) -> SyntaxVisitorContinueKind {
4847
guard let declComment = node.docComment else { return .skipChildren }
@@ -64,10 +63,10 @@ public final class ValidateDocumentationComments: SyntaxLintRule {
6463
}
6564

6665
validateThrows(
67-
throwsOrRethrowsKeyword, name: name, throwsDesc: commentInfo.throwsDescription, node: node)
66+
signature.throwsOrRethrowsKeyword, name: name, throwsDesc: commentInfo.throwsDescription, node: node)
6867
validateReturn(
6968
returnClause, name: name, returnDesc: commentInfo.returnsDescription, node: node)
70-
let funcParameters = funcParametersIdentifiers(in: parameters)
69+
let funcParameters = funcParametersIdentifiers(in: signature.input.parameterList)
7170

7271
// If the documentation of the parameters is wrong 'docCommentInfo' won't
7372
// parse the parameters correctly. First the documentation has to be fix

0 commit comments

Comments
 (0)