Skip to content

Generate Format using SwiftSyntaxBuilder #611

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 2 commits into from
Aug 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import SwiftSyntax
extension VariableDecl {
public init(
leadingTrivia: Trivia = [],
modifiers: ModifierList? = nil,
_ letOrVarKeyword: TokenSyntax,
name: ExpressibleAsIdentifierPattern,
type: ExpressibleAsTypeAnnotation? = nil,
initializer: ExpressibleAsInitializerClause? = nil
) {
self.init(leadingTrivia: leadingTrivia, letOrVarKeyword: letOrVarKeyword) {
self.init(leadingTrivia: leadingTrivia, modifiers: modifiers, letOrVarKeyword: letOrVarKeyword) {
PatternBinding(
pattern: name,
typeAnnotation: type,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

//// Automatically Generated by generate-swift-syntax-builder
//// Do Not Edit Directly!
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
Expand All @@ -11,24 +14,19 @@
//===----------------------------------------------------------------------===//

import SwiftSyntax

public struct Format {
public let indentWidth: Int

private var indents: Int = 0

public init(indentWidth: Int = 4) {
public init (indentWidth: Int = 4) {
self.indentWidth = indentWidth
}
}

extension Format {
public func _indented() -> Format {
var copy = self
copy.indents += 1
return copy
}

public func _makeIndent() -> Trivia {
return indents == 0 ? .zero : Trivia.spaces(indents * indentWidth)
}
Expand Down
106 changes: 106 additions & 0 deletions Sources/generate-swift-syntax-builder/Templates/FormatFile.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//===----------------------------------------------------------------------===//
//
// 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 Foundation
import SwiftSyntax
import SwiftSyntaxBuilder

let formatFile = SourceFile {
ImportDecl(
leadingTrivia: .docLineComment(copyrightHeader),
path: "SwiftSyntax"
)

StructDecl(modifiers: [TokenSyntax.public], identifier: "Format") {
VariableDecl(
modifiers: [TokenSyntax.public],
.let,
name: "indentWidth",
type: "Int"
)

VariableDecl(
modifiers: [TokenSyntax.private],
.var,
name: "indents",
type: "Int",
initializer: IntegerLiteralExpr(0)
)

InitializerDecl(
modifiers: [TokenSyntax.public],
signature: FunctionSignature(
input: ParameterClause {
FunctionParameter(
firstName: .identifier("indentWidth"),
colon: .colon,
type: "Int",
defaultArgument: IntegerLiteralExpr(4)
)
}
)
) {
SequenceExpr {
MemberAccessExpr(base: "self", name: "indentWidth")
AssignmentExpr()
"indentWidth"
}
}
}

ExtensionDecl(extendedType: "Format") {
FunctionDecl(
modifiers: [TokenSyntax.public],
identifier: .identifier("_indented"),
signature: FunctionSignature(
input: ParameterClause(),
output: "Format"
)
) {
VariableDecl(.var, name: "copy", initializer: "self")
SequenceExpr {
MemberAccessExpr(base: "copy", name: "indents")
BinaryOperatorExpr("+=")
IntegerLiteralExpr(1)
}
ReturnStmt(expression: "copy")
}

FunctionDecl(
modifiers: [TokenSyntax.public],
identifier: .identifier("_makeIndent"),
signature: FunctionSignature(
input: ParameterClause(),
output: "Trivia"
)
) {
// TODO: Use sugared TernaryExpr once https://github.com/apple/swift-syntax/pull/610 is merged
ReturnStmt(expression: TernaryExpr(
conditionExpression: SequenceExpr {
"indents"
BinaryOperatorExpr("==")
IntegerLiteralExpr(0)
},
questionMark: .infixQuestionMark.withLeadingTrivia(.space).withTrailingTrivia(.space),
firstChoice: MemberAccessExpr(name: "zero"),
colonMark: .colon.withLeadingTrivia(.space).withTrailingTrivia(.space),
secondChoice: FunctionCallExpr(MemberAccessExpr(base: "Trivia", name: "spaces")) {
TupleExprElement(expression: SequenceExpr {
"indents"
BinaryOperatorExpr("*")
"indentWidth"
})
}
))
}
}
}
1 change: 1 addition & 0 deletions Sources/generate-swift-syntax-builder/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ let sourceTemplates = [
(buildableCollectionNodesFile, "BuildableCollectionNodes.swift"),
(buildableNodesFile, "BuildableNodes.swift"),
(expressibleAsProtocolsFile, "ExpressibleAsProtocols.swift"),
(formatFile, "Format.swift"),
(tokensFile, "Tokens.swift"),
(tokenSyntaxFile, "TokenSyntax.swift"),
]
Expand Down