Skip to content

Commit 7f16106

Browse files
committed
Add utility property for adding backticks
1 parent 5bc624d commit 7f16106

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

Sources/SwiftSyntaxBuilderGeneration/String+Extensions.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ import Foundation
1414

1515
extension StringProtocol {
1616
var withFirstCharacterLowercased: String { prefix(1).lowercased() + dropFirst() }
17+
var backticked: String { "`\(self)`" }
1718
}

Sources/SwiftSyntaxBuilderGeneration/Templates/TokensFile.swift

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ let tokensFile = SourceFile {
3535
// We need to use `CodeBlock` here to ensure there is braces around.
3636

3737
let accessor = CodeBlock {
38-
FunctionCallExpr(MemberAccessExpr(base: "TokenSyntax", name: "\(token.swiftKind)"))
38+
FunctionCallExpr(MemberAccessExpr(base: "TokenSyntax", name: token.swiftKind))
3939
}
4040

41-
createTokenSyntaxPatternBinding("`\(token.name.withFirstCharacterLowercased)`", accessor: accessor)
41+
createTokenSyntaxPatternBinding(token.name.withFirstCharacterLowercased.backticked, accessor: accessor)
4242
}
4343
} else if let text = token.text {
4444
VariableDecl(
@@ -51,7 +51,30 @@ let tokensFile = SourceFile {
5151
FunctionCallExpr(MemberAccessExpr(base: "TokenSyntax", name: "\(token.swiftKind)Token"))
5252
}
5353

54-
createTokenSyntaxPatternBinding("`\(token.name.withFirstCharacterLowercased)`", accessor: accessor)
54+
createTokenSyntaxPatternBinding(token.name.withFirstCharacterLowercased.backticked, accessor: accessor)
55+
}
56+
} else {
57+
let signature = FunctionSignature(
58+
input: ParameterClause {
59+
FunctionParameter(
60+
attributes: nil,
61+
firstName: .wildcard,
62+
secondName: .identifier("text"),
63+
colon: .colon,
64+
type: "String"
65+
)
66+
},
67+
output: "TokenSyntax"
68+
)
69+
70+
FunctionDecl(
71+
modifiers: [TokenSyntax.static],
72+
identifier: .identifier(token.name.withFirstCharacterLowercased.backticked),
73+
signature: signature
74+
) {
75+
FunctionCallExpr(MemberAccessExpr(base: "SyntaxFactory", name: "make\(token.name)")) {
76+
TupleExprElement(expression: IdentifierExpr("text"))
77+
}
5578
}
5679
}
5780
// TokenSyntax with custom text already has a static constructor function defined in SwiftSyntax.

0 commit comments

Comments
 (0)