Skip to content

Add result builder to decl blocks #305

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
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
58 changes: 55 additions & 3 deletions Sources/SwiftSyntaxBuilder/Buildables.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,19 @@ public protocol ${kind}ListBuildable: SyntaxListBuildable {
func build${kind}List(format: Format, leadingTrivia: Trivia?) -> [${build_kind}]
}

% buildable_type = kind + 'Buildable'
public protocol ExpressibleAs${buildable_type} {
func create${buildable_type}() -> ${buildable_type}
}

% if kind == 'Syntax':
public protocol ${kind}Buildable: ${kind}ListBuildable {
public protocol ${buildable_type}: ExpressibleAs${buildable_type}, ${kind}ListBuildable {
% elif kind == 'Decl':
public protocol ${buildable_type}: ExpressibleAs${buildable_type}, SyntaxBuildable, ${kind}ListBuildable, ExpressibleAsMemberDeclListItem, ExpressibleAsCodeBlockItem {
% elif kind == 'Stmt':
public protocol ${buildable_type}: ExpressibleAs${buildable_type}, SyntaxBuildable, ${kind}ListBuildable, ExpressibleAsCodeBlockItem {
% else:
public protocol ${kind}Buildable: SyntaxBuildable, ${kind}ListBuildable {
public protocol ${buildable_type}: ExpressibleAs${buildable_type}, SyntaxBuildable, ${kind}ListBuildable {
% end
/// Builds a `${build_kind}`.
/// - Parameter format: The `Format` to use.
Expand All @@ -51,7 +60,13 @@ public protocol ${kind}Buildable: SyntaxBuildable, ${kind}ListBuildable {
func build${kind}(format: Format, leadingTrivia: Trivia?) -> ${build_kind}
}

extension ${kind}Buildable {
extension ${buildable_type} {
public func create${buildable_type}() -> ${buildable_type} {
self
}
}

extension ${buildable_type} {
% if kind != 'Syntax':
/// Builds a `${build_kind}`.
/// - Returns: A `${build_kind}`.
Expand Down Expand Up @@ -82,6 +97,22 @@ extension ${kind}Buildable {
[build${kind}(format: format, leadingTrivia: leadingTrivia)]
}
}
% if kind == 'Decl':

extension DeclBuildable {
public func createMemberDeclListItem() -> MemberDeclListItem {
MemberDeclListItem(decl: self)
}
}
% end
% if kind in ['Decl', 'Stmt']:

extension ${kind}Buildable {
public func createCodeBlockItem() -> CodeBlockItem {
CodeBlockItem(item: self)
}
}
% end

% end
% end
Expand Down Expand Up @@ -196,5 +227,26 @@ public struct ${node.syntax_kind}: SyntaxBuildable {
}
}

% end
% if node.is_buildable() or node.is_syntax_collection():
public protocol ExpressibleAs${node.syntax_kind} {
func create${node.syntax_kind}() -> ${node.syntax_kind}
}

extension ${node.syntax_kind}: ExpressibleAs${node.syntax_kind} {
public func create${node.syntax_kind}() -> ${node.syntax_kind} {
self
}
}

% end
% end
public protocol ExpressibleAsTokenSyntax {
func createTokenSyntax() -> TokenSyntax
}

extension TokenSyntax: ExpressibleAsTokenSyntax {
public func createTokenSyntax() -> TokenSyntax {
self
}
}
6 changes: 3 additions & 3 deletions Sources/SwiftSyntaxBuilder/ResultBuilders.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public struct ${node.syntax_kind}Builder {

/// The type of individual statement expressions in the transformed function,
/// which defaults to Component if buildExpression() is not provided.
public typealias Expression = ${element_type}
public typealias Expression = ExpressibleAs${element_type}

/// The type of a partial result, which will be carried through all of the
/// build methods.
public typealias Component = [${element_type}]
public typealias Component = [ExpressibleAs${element_type}]

/// The type of the final returned result, which defaults to Component if
/// buildFinalResult() is not provided.
Expand Down Expand Up @@ -84,7 +84,7 @@ public struct ${node.syntax_kind}Builder {
/// If declared, this will be called on the partial result from the outermost
/// block statement to produce the final returned result.
public static func buildFinalResult(_ component: Component) -> FinalResult {
.init(component)
.init(component.map { $0.create${element_type}() })
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
import SwiftSyntax

extension StringLiteralExpr {
public init(_ value: String) {
public init(_ value: String, openQuote: TokenSyntax = .stringQuote, closeQuote: TokenSyntax = .stringQuote) {
let content = SyntaxFactory.makeToken(TokenKind.stringSegment(value), presence: .present)
let segment = StringSegment(content: content)
let segments = StringLiteralSegments([segment])

self.init(openQuote: .stringQuote,
self.init(openQuote: openQuote,
segments: segments,
closeQuote: .stringQuote)
closeQuote: closeQuote)
}
}

Expand Down
Loading