Skip to content

Commit c7087ad

Browse files
authored
Merge pull request #1558 from ahoppen/ahoppen/update-basic-format
2 parents 13b1e47 + 2e4761e commit c7087ad

28 files changed

+1169
-681
lines changed

CodeGeneration/Sources/SyntaxSupport/Trivia.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public class Trivia {
1515
public let comment: String
1616
public let characters: [Character]
1717
public let swiftCharacters: [Character]
18-
public let isNewLine: Bool
1918
public let isComment: Bool
2019

2120
public var lowerName: String { lowercaseFirstWord(name: name) }
@@ -36,17 +35,23 @@ public class Trivia {
3635

3736
public var isCollection: Bool { charactersLen > 0 }
3837

38+
public var isBlank: Bool {
39+
characters.contains { $0.isWhitespace }
40+
}
41+
42+
public var isNewLine: Bool {
43+
characters.contains { $0.isNewline }
44+
}
45+
3946
init(
4047
name: String,
4148
comment: String,
4249
characters: [Character] = [],
4350
swiftCharacters: [Character] = [],
44-
isNewLine: Bool = false,
4551
isComment: Bool = false
4652
) {
4753
self.name = name
4854
self.comment = comment
49-
self.isNewLine = isNewLine
5055
self.isComment = isComment
5156
self.characters = characters
5257

@@ -86,8 +91,7 @@ public let TRIVIAS: [Trivia] = [
8691
],
8792
swiftCharacters: [
8893
Character("\r")
89-
],
90-
isNewLine: true
94+
]
9195
),
9296

9397
Trivia(
@@ -100,8 +104,7 @@ public let TRIVIAS: [Trivia] = [
100104
swiftCharacters: [
101105
Character("\r"),
102106
Character("\n"),
103-
],
104-
isNewLine: true
107+
]
105108
),
106109

107110
Trivia(
@@ -142,8 +145,7 @@ public let TRIVIAS: [Trivia] = [
142145
],
143146
swiftCharacters: [
144147
Character("\n")
145-
],
146-
isNewLine: true
148+
]
147149
),
148150

149151
Trivia(

CodeGeneration/Sources/Utils/CodeGenerationFormat.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ import SwiftSyntax
1515

1616
/// A format style for files generated by CodeGeneration.
1717
public class CodeGenerationFormat: BasicFormat {
18-
public override var indentation: TriviaPiece { .spaces(indentationLevel * 2) }
18+
public init() {
19+
super.init(indentationWidth: .spaces(2))
20+
}
21+
22+
var indentedNewline: Trivia {
23+
.newline + currentIndentationLevel
24+
}
1925

2026
public override func visit(_ node: ArrayElementListSyntax) -> ArrayElementListSyntax {
2127
let children = node.children(viewMode: .all)
@@ -100,7 +106,7 @@ public class CodeGenerationFormat: BasicFormat {
100106
}
101107

102108
private func formatChildrenSeparatedByNewline<SyntaxType: SyntaxProtocol>(children: SyntaxChildren, elementType: SyntaxType.Type) -> [SyntaxType] {
103-
indentationLevel += 1
109+
increaseIndentationLevel()
104110
var formattedChildren = children.map {
105111
self.visit($0).as(SyntaxType.self)!
106112
}
@@ -111,7 +117,7 @@ public class CodeGenerationFormat: BasicFormat {
111117
return $0.with(\.leadingTrivia, indentedNewline + $0.leadingTrivia)
112118
}
113119
}
114-
indentationLevel -= 1
120+
decreaseIndentationLevel()
115121
if !formattedChildren.isEmpty {
116122
formattedChildren[formattedChildren.count - 1] = formattedChildren[formattedChildren.count - 1].with(\.trailingTrivia, indentedNewline)
117123
}

CodeGeneration/Sources/generate-swiftsyntax/GenerateSwiftSyntax.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ struct GenerateSwiftSyntax: ParsableCommand {
8080
let fileSpecs: [GeneratedFileSpec] =
8181
[
8282
// SwiftBasicFormat
83-
GeneratedFileSpec(swiftBasicFormatGeneratedDir + ["BasicFormat.swift"], basicFormatFile),
83+
GeneratedFileSpec(swiftBasicFormatGeneratedDir + ["BasicFormat+Extensions.swift"], basicFormatExtensionsFile),
8484

8585
// IDEUtils
8686
GeneratedFileSpec(ideUtilsGeneratedDir + ["SyntaxClassification.swift"], syntaxClassificationFile),

0 commit comments

Comments
 (0)