File tree Expand file tree Collapse file tree 2 files changed +62
-0
lines changed
Sources/SwiftSyntaxBuilder/Syntax/Buildables
Tests/SwiftSyntaxBuilderTest/Declarations Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments