Skip to content

Update CodeGeneration to the latest SwiftSyntax commit #993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CodeGeneration/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let package = Package(
.executable(name: "generate-swiftsyntaxbuilder", targets: ["generate-swiftsyntaxbuilder"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-syntax.git", revision: "108a09851eb34b855bd0f9ff3e52ea396b531b58"),
.package(url: "https://github.com/apple/swift-syntax.git", revision: "0663a530c4e51066343c3c2fbd95f8e626b8d631"),
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMinor(from: "1.1.4")),
],
targets: [
Expand Down
24 changes: 24 additions & 0 deletions CodeGeneration/Sources/Utils/CodeGenerationFormat.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftBasicFormat
import SwiftSyntax

/// A format style for files generated by CodeGeneration.
public class CodeGenerationFormat: BasicFormat {
public override var indentation: TriviaPiece { .spaces(indentationLevel * 2) }

public override func visit(_ node: MemberDeclListItemSyntax) -> Syntax {
let formatted = super.visit(node)
return formatted.withLeadingTrivia(indentedNewline + (formatted.leadingTrivia ?? []))
}
}
4 changes: 1 addition & 3 deletions CodeGeneration/Sources/Utils/GenerateTemplates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import SwiftSyntaxBuilder
import SwiftBasicFormat

public func generateTemplates(templates: [(SourceFile, String)], destination: URL, verbose: Bool) throws {
let format = Format(indentWidth: 2)

try FileManager.default.createDirectory(
atPath: destination.path,
withIntermediateDirectories: true,
Expand All @@ -28,7 +26,7 @@ public func generateTemplates(templates: [(SourceFile, String)], destination: UR
if verbose {
print("Generating \(fileURL.path)...")
}
let syntax = sourceFile.buildSyntax(format: format)
let syntax = sourceFile.build(format: CodeGenerationFormat())
try "\(syntax)\n".write(to: fileURL, atomically: true, encoding: .utf8)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private func createBuildFunction(node: Node, trivias: [String]) -> FunctionDecl
createTriviaAttachment(varName: IdentifierExpr("result"), triviaVarName: IdentifierExpr(trivia), trivia: trivia)
}
ReturnStmt("return result")
}.buildSyntax(format: Format(indentWidth: 2))
}.build(format: CodeGenerationFormat())

return FunctionDecl(
"""
Expand Down
106 changes: 55 additions & 51 deletions Sources/SwiftBasicFormat/generated/BasicFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
import SwiftSyntax
open class BasicFormat: SyntaxRewriter {
public var indentationLevel: Int = 0
open var indentation: TriviaPiece { .spaces(indentationLevel * 4) }
public var indentedNewline: Trivia { Trivia(pieces: [.newlines(1), indentation]) }
open var indentation: TriviaPiece {
.spaces(indentationLevel * 4)
}
public var indentedNewline: Trivia {
Trivia(pieces: [.newlines(1), indentation])
}
private var lastRewrittenToken: TokenSyntax?

open override func visit(_ node: UnknownDeclSyntax) -> DeclSyntax {
Expand Down Expand Up @@ -2860,7 +2864,7 @@ open class BasicFormat: SyntaxRewriter {
trailingTrivia += .space
}
case .doKeyword:
break
break
case .repeatKeyword:
if trailingTrivia.isEmpty {
trailingTrivia += .space
Expand Down Expand Up @@ -2906,7 +2910,7 @@ open class BasicFormat: SyntaxRewriter {
trailingTrivia += .space
}
case .defaultKeyword:
break
break
case .whereKeyword:
if leadingTrivia.isEmpty && lastRewrittenToken?.trailingTrivia.isEmpty != false {
leadingTrivia += .space
Expand All @@ -2931,25 +2935,25 @@ open class BasicFormat: SyntaxRewriter {
trailingTrivia += .space
}
case .falseKeyword:
break
break
case .isKeyword:
if trailingTrivia.isEmpty {
trailingTrivia += .space
}
case .nilKeyword:
break
break
case .rethrowsKeyword:
if trailingTrivia.isEmpty {
trailingTrivia += .space
}
case .superKeyword:
break
break
case .selfKeyword:
break
break
case .capitalSelfKeyword:
break
break
case .trueKeyword:
break
break
case .tryKeyword:
if trailingTrivia.isEmpty {
trailingTrivia += .space
Expand Down Expand Up @@ -2983,19 +2987,19 @@ open class BasicFormat: SyntaxRewriter {
trailingTrivia += .space
}
case .leftParen:
break
break
case .rightParen:
break
break
case .leftBrace:
if leadingTrivia.isEmpty && lastRewrittenToken?.trailingTrivia.isEmpty != false {
leadingTrivia += .space
}
case .rightBrace:
break
break
case .leftSquareBracket:
break
break
case .rightSquareBracket:
break
break
case .leftAngle:
if leadingTrivia.isEmpty && lastRewrittenToken?.trailingTrivia.isEmpty != false {
leadingTrivia += .space
Expand All @@ -3011,21 +3015,21 @@ open class BasicFormat: SyntaxRewriter {
trailingTrivia += .space
}
case .period:
break
break
case .prefixPeriod:
break
break
case .comma:
if trailingTrivia.isEmpty {
trailingTrivia += .space
}
case .ellipsis:
break
break
case .colon:
if trailingTrivia.isEmpty {
trailingTrivia += .space
}
case .semicolon:
break
break
case .equal:
if leadingTrivia.isEmpty && lastRewrittenToken?.trailingTrivia.isEmpty != false {
leadingTrivia += .space
Expand All @@ -3034,9 +3038,9 @@ open class BasicFormat: SyntaxRewriter {
trailingTrivia += .space
}
case .atSign:
break
break
case .pound:
break
break
case .prefixAmpersand:
if leadingTrivia.isEmpty && lastRewrittenToken?.trailingTrivia.isEmpty != false {
leadingTrivia += .space
Expand All @@ -3052,21 +3056,21 @@ open class BasicFormat: SyntaxRewriter {
trailingTrivia += .space
}
case .backtick:
break
break
case .backslash:
break
break
case .exclamationMark:
break
break
case .postfixQuestionMark:
break
break
case .infixQuestionMark:
break
break
case .stringQuote:
break
break
case .singleQuote:
break
break
case .multilineStringQuote:
break
break
case .poundKeyPathKeyword:
if trailingTrivia.isEmpty {
trailingTrivia += .space
Expand Down Expand Up @@ -3160,19 +3164,19 @@ open class BasicFormat: SyntaxRewriter {
trailingTrivia += .space
}
case .integerLiteral:
break
break
case .floatingLiteral:
break
break
case .stringLiteral:
break
break
case .regexLiteral:
break
break
case .unknown:
break
break
case .identifier:
break
break
case .unspacedBinaryOperator:
break
break
case .spacedBinaryOperator:
if leadingTrivia.isEmpty && lastRewrittenToken?.trailingTrivia.isEmpty != false {
leadingTrivia += .space
Expand All @@ -3181,38 +3185,38 @@ open class BasicFormat: SyntaxRewriter {
trailingTrivia += .space
}
case .postfixOperator:
break
break
case .prefixOperator:
break
break
case .dollarIdentifier:
break
break
case .rawStringDelimiter:
break
break
case .stringSegment:
break
break
case .stringInterpolationAnchor:
break
break
case .yield:
break
break
case .eof:
break
break
case .contextualKeyword:
switch node.text {
case "async":
case "async":
if trailingTrivia.isEmpty {
trailingTrivia += .space
}
default:
break
default:
break
}
}
leadingTrivia = leadingTrivia.indented(indentation: indentation)
trailingTrivia = trailingTrivia.indented(indentation: indentation)
let rewritten = TokenSyntax(
node.tokenKind,
leadingTrivia: leadingTrivia,
trailingTrivia: trailingTrivia,
presence: node.presence
node.tokenKind,
leadingTrivia: leadingTrivia,
trailingTrivia: trailingTrivia,
presence: node.presence
)
lastRewrittenToken = rewritten
return rewritten
Expand Down
Loading