Skip to content

Commit 1313c36

Browse files
authored
Merge pull request #611 from fwcd/generate-format-file
Generate `Format` using `SwiftSyntaxBuilder`
2 parents ebdd796 + 36fb63b commit 1313c36

File tree

3 files changed

+111
-6
lines changed

3 files changed

+111
-6
lines changed

Sources/SwiftSyntaxBuilder/Format.swift renamed to Sources/SwiftSyntaxBuilder/generated/Format.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2+
//// Automatically Generated by generate-swift-syntax-builder
3+
//// Do Not Edit Directly!
14
//===----------------------------------------------------------------------===//
25
//
36
// This source file is part of the Swift.org open source project
@@ -11,24 +14,19 @@
1114
//===----------------------------------------------------------------------===//
1215

1316
import SwiftSyntax
14-
1517
public struct Format {
1618
public let indentWidth: Int
17-
1819
private var indents: Int = 0
19-
20-
public init(indentWidth: Int = 4) {
20+
public init (indentWidth: Int = 4) {
2121
self.indentWidth = indentWidth
2222
}
2323
}
24-
2524
extension Format {
2625
public func _indented() -> Format {
2726
var copy = self
2827
copy.indents += 1
2928
return copy
3029
}
31-
3230
public func _makeIndent() -> Trivia {
3331
return indents == 0 ? .zero : Trivia.spaces(indents * indentWidth)
3432
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Foundation
14+
import SwiftSyntax
15+
import SwiftSyntaxBuilder
16+
17+
let formatFile = SourceFile {
18+
ImportDecl(
19+
leadingTrivia: .docLineComment(copyrightHeader),
20+
path: "SwiftSyntax"
21+
)
22+
23+
StructDecl(modifiers: [TokenSyntax.public], identifier: "Format") {
24+
VariableDecl(
25+
modifiers: [TokenSyntax.public],
26+
.let,
27+
name: "indentWidth",
28+
type: "Int"
29+
)
30+
31+
VariableDecl(
32+
modifiers: [TokenSyntax.private],
33+
.var,
34+
name: "indents",
35+
type: "Int",
36+
initializer: IntegerLiteralExpr(0)
37+
)
38+
39+
InitializerDecl(
40+
modifiers: [TokenSyntax.public],
41+
signature: FunctionSignature(
42+
input: ParameterClause {
43+
FunctionParameter(
44+
firstName: .identifier("indentWidth"),
45+
colon: .colon,
46+
type: "Int",
47+
defaultArgument: IntegerLiteralExpr(4)
48+
)
49+
}
50+
)
51+
) {
52+
SequenceExpr {
53+
MemberAccessExpr(base: "self", name: "indentWidth")
54+
AssignmentExpr()
55+
"indentWidth"
56+
}
57+
}
58+
}
59+
60+
ExtensionDecl(extendedType: "Format") {
61+
FunctionDecl(
62+
modifiers: [TokenSyntax.public],
63+
identifier: .identifier("_indented"),
64+
signature: FunctionSignature(
65+
input: ParameterClause(),
66+
output: "Format"
67+
)
68+
) {
69+
VariableDecl(.var, name: "copy", initializer: "self")
70+
SequenceExpr {
71+
MemberAccessExpr(base: "copy", name: "indents")
72+
BinaryOperatorExpr("+=")
73+
IntegerLiteralExpr(1)
74+
}
75+
ReturnStmt(expression: "copy")
76+
}
77+
78+
FunctionDecl(
79+
modifiers: [TokenSyntax.public],
80+
identifier: .identifier("_makeIndent"),
81+
signature: FunctionSignature(
82+
input: ParameterClause(),
83+
output: "Trivia"
84+
)
85+
) {
86+
// TODO: Use sugared TernaryExpr once https://github.com/apple/swift-syntax/pull/610 is merged
87+
ReturnStmt(expression: TernaryExpr(
88+
conditionExpression: SequenceExpr {
89+
"indents"
90+
BinaryOperatorExpr("==")
91+
IntegerLiteralExpr(0)
92+
},
93+
questionMark: .infixQuestionMark.withLeadingTrivia(.space).withTrailingTrivia(.space),
94+
firstChoice: MemberAccessExpr(name: "zero"),
95+
colonMark: .colon.withLeadingTrivia(.space).withTrailingTrivia(.space),
96+
secondChoice: FunctionCallExpr(MemberAccessExpr(base: "Trivia", name: "spaces")) {
97+
TupleExprElement(expression: SequenceExpr {
98+
"indents"
99+
BinaryOperatorExpr("*")
100+
"indentWidth"
101+
})
102+
}
103+
))
104+
}
105+
}
106+
}

Sources/generate-swift-syntax-builder/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ let sourceTemplates = [
2020
(buildableCollectionNodesFile, "BuildableCollectionNodes.swift"),
2121
(buildableNodesFile, "BuildableNodes.swift"),
2222
(expressibleAsProtocolsFile, "ExpressibleAsProtocols.swift"),
23+
(formatFile, "Format.swift"),
2324
(tokensFile, "Tokens.swift"),
2425
(tokenSyntaxFile, "TokenSyntax.swift"),
2526
]

0 commit comments

Comments
 (0)