Skip to content

Commit 4495871

Browse files
committed
Add source file builder
1 parent 1687304 commit 4495871

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 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 SwiftSyntax
14+
15+
public struct SourceFile: SyntaxBuildable {
16+
let builder: SyntaxListBuildable
17+
18+
public init(@SyntaxListBuilder makeBuilder: () -> SyntaxListBuildable) {
19+
self.builder = makeBuilder()
20+
}
21+
22+
public func buildSyntax(format: Format, leadingTrivia: Trivia) -> Syntax {
23+
let syntaxList = builder.buildSyntaxList(format: format, leadingTrivia: leadingTrivia)
24+
25+
return SourceFileSyntax {
26+
for syntax in syntaxList {
27+
$0.addStatement(CodeBlockItemSyntax {
28+
$0.useItem(syntax)
29+
})
30+
}
31+
}
32+
}
33+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import XCTest
2+
import SwiftSyntax
3+
4+
@testable import SwiftSyntaxBuilder
5+
6+
final class SourceFileTests: XCTestCase {
7+
let format = Format(indentWidth: 2).indented()
8+
9+
func testSourceFile() {
10+
let sourceFile = SourceFile {
11+
Import("SwiftSyntax")
12+
13+
Struct("ExampleStruct") {
14+
Let("syntax", of: "Syntax")
15+
}
16+
}
17+
18+
let syntax = sourceFile.buildSyntax(format: format, leadingTrivia: .zero)
19+
20+
var text = ""
21+
syntax.write(to: &text)
22+
XCTAssertEqual(text, """
23+
import SwiftSyntax
24+
struct ExampleStruct {
25+
let syntax: Syntax
26+
}
27+
""")
28+
}
29+
}

0 commit comments

Comments
 (0)