Skip to content

Commit bcaad6d

Browse files
authored
Merge pull request #1355 from kimdv/kimdv/update-swift-syntax
Fix leading newline in source files
2 parents 4f1f06c + db7eb73 commit bcaad6d

33 files changed

+17
-1064
lines changed

CodeGeneration/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let package = Package(
1111
.executable(name: "generate-swiftsyntax", targets: ["generate-swiftsyntax"])
1212
],
1313
dependencies: [
14-
.package(url: "https://github.com/apple/swift-syntax.git", revision: "cf413a6fe9a82e3f8e1c022cac6f6d23dfb1a035"),
14+
.package(url: "https://github.com/apple/swift-syntax.git", revision: "4727d3c3f81be056cf7f9636d682ac9879270d12"),
1515
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMinor(from: "1.2.2")),
1616
],
1717
targets: [

CodeGeneration/Sources/Utils/CodeGenerationFormat.swift

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,8 @@ public class CodeGenerationFormat: BasicFormat {
2727
}
2828
}
2929

30-
public override func visit(_ node: MemberDeclBlockSyntax) -> MemberDeclBlockSyntax {
31-
if node.members.count == 0 {
32-
return node.with(\.leftBrace, .leftBraceToken())
33-
} else {
34-
return super.visit(node)
35-
}
36-
}
37-
3830
public override func visit(_ node: CodeBlockItemSyntax) -> CodeBlockItemSyntax {
39-
if node.parent?.parent?.is(SourceFileSyntax.self) == true, !node.item.is(ImportDeclSyntax.self) {
31+
if node.parent?.parent?.is(SourceFileSyntax.self) == true, !shouldBeSeparatedByTwoNewlines(node: node) {
4032
let formatted = super.visit(node)
4133
return ensuringTwoLeadingNewlines(node: formatted)
4234
} else {
@@ -64,9 +56,17 @@ public class CodeGenerationFormat: BasicFormat {
6456
}
6557
}
6658

59+
public override func visit(_ node: MemberDeclBlockSyntax) -> MemberDeclBlockSyntax {
60+
if node.members.count == 0 {
61+
return node.with(\.leftBrace, .leftBraceToken())
62+
} else {
63+
return super.visit(node)
64+
}
65+
}
66+
6767
public override func visit(_ node: MemberDeclListItemSyntax) -> MemberDeclListItemSyntax {
6868
let formatted = super.visit(node)
69-
if node.indexInParent != 0 {
69+
if node.indexInParent != 0 && !node.decl.is(EnumCaseDeclSyntax.self) {
7070
return ensuringTwoLeadingNewlines(node: formatted)
7171
} else {
7272
return formatted
@@ -85,6 +85,12 @@ public class CodeGenerationFormat: BasicFormat {
8585

8686
// MARK: - Private
8787

88+
private func shouldBeSeparatedByTwoNewlines(node: CodeBlockItemSyntax) -> Bool {
89+
// First item in the `CodeBlockItemListSyntax` don't need a newline or identation if the parent is a `SourceFileSyntax`.
90+
// We want to group imports so newline between them should be omitted
91+
return node.parent?.as(CodeBlockItemListSyntax.self)?.first == node || node.item.is(ImportDeclSyntax.self)
92+
}
93+
8894
private func ensuringTwoLeadingNewlines<NodeType: SyntaxProtocol>(node: NodeType) -> NodeType {
8995
if node.leadingTrivia?.first?.isNewline ?? false {
9096
return node.with(\.leadingTrivia, indentedNewline + (node.leadingTrivia ?? []))

Sources/IDEUtils/generated/SyntaxClassification.swift

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
//// Automatically Generated by generate-ideutils
32
//// Do Not Edit Directly!
43
//===----------------------------------------------------------------------===//
@@ -18,58 +17,40 @@
1817
public enum SyntaxClassification {
1918
/// An attribute starting with an `@`.
2019
case attribute
21-
2220
/// A block comment starting with `/**` and ending with `*/.
2321
case blockComment
24-
2522
/// A build configuration directive like `#if`, `#elseif`, `#else`.
2623
case buildConfigId
27-
2824
/// A doc block comment starting with `/**` and ending with `*/.
2925
case docBlockComment
30-
3126
/// A doc line comment starting with `///`.
3227
case docLineComment
33-
3428
/// An identifier starting with `$` like `$0`.
3529
case dollarIdentifier
36-
3730
/// An editor placeholder of the form `<#content#>`
3831
case editorPlaceholder
39-
4032
/// A floating point literal.
4133
case floatingLiteral
42-
4334
/// A generic identifier.
4435
case identifier
45-
4636
/// An integer literal.
4737
case integerLiteral
48-
4938
/// A Swift keyword, including contextual keywords.
5039
case keyword
51-
5240
/// A line comment starting with `//`.
5341
case lineComment
54-
5542
/// The token should not receive syntax coloring.
5643
case none
57-
5844
/// An image, color, etc. literal.
5945
case objectLiteral
60-
6146
/// An identifier referring to an operator.
6247
case operatorIdentifier
63-
6448
/// A `#` keyword like `#warning`.
6549
case poundDirectiveKeyword
66-
6750
/// The opening and closing parenthesis of string interpolation.
6851
case stringInterpolationAnchor
69-
7052
/// A string literal including multiline string literals.
7153
case stringLiteral
72-
7354
/// An identifier referring to a type.
7455
case typeIdentifier
7556
}

Sources/SwiftBasicFormat/generated/BasicFormat.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
//// Automatically Generated by generate-swiftbasicformat
32
//// Do Not Edit Directly!
43
//===----------------------------------------------------------------------===//

Sources/SwiftParser/generated/DeclarationModifier.swift

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
//// Automatically Generated by generate-swiftparser
32
//// Do Not Edit Directly!
43
//===----------------------------------------------------------------------===//
@@ -17,69 +16,37 @@
1716

1817
enum DeclarationModifier: TokenSpecSet {
1918
case `static`
20-
2119
case `class`
22-
2320
case final
24-
2521
case required
26-
2722
case optional
28-
2923
case lazy
30-
3124
case dynamic
32-
3325
case infix
34-
3526
case prefix
36-
3727
case postfix
38-
3928
case __consuming
40-
4129
case mutating
42-
4330
case nonmutating
44-
4531
case convenience
46-
4732
case override
48-
4933
case `private`
50-
5134
case `fileprivate`
52-
5335
case `internal`
54-
5536
case `public`
56-
5737
case package
58-
5938
case open
60-
6139
case __setter_access
62-
6340
case weak
64-
6541
case unowned
66-
6742
case `rethrows`
68-
6943
case indirect
70-
7144
case isolated
72-
7345
case async
74-
7546
case reasync
76-
7747
case nonisolated
78-
7948
case distributed
80-
8149
case _const
82-
8350
case _local
8451

8552
init?(lexeme: Lexer.Lexeme) {

Sources/SwiftParser/generated/Parser+Entry.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
//// Automatically Generated by generate-swiftparser
32
//// Do Not Edit Directly!
43
//===----------------------------------------------------------------------===//

Sources/SwiftParser/generated/TokenSpecStaticMembers.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
//// Automatically Generated by generate-swiftparser
32
//// Do Not Edit Directly!
43
//===----------------------------------------------------------------------===//

Sources/SwiftParser/generated/TypeAttribute.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
//// Automatically Generated by generate-swiftparser
32
//// Do Not Edit Directly!
43
//===----------------------------------------------------------------------===//
@@ -18,27 +17,16 @@
1817
extension Parser {
1918
enum TypeAttribute: TokenSpecSet {
2019
case autoclosure
21-
2220
case convention
23-
2421
case noescape
25-
2622
case escaping
27-
2823
case differentiable
29-
3024
case noDerivative
31-
3225
case async
33-
3426
case Sendable
35-
3627
case unchecked
37-
3828
case _local
39-
4029
case _noMetadata
41-
4230
case _opaqueReturnTypeOf
4331

4432
init?(lexeme: Lexer.Lexeme) {

0 commit comments

Comments
 (0)