File tree Expand file tree Collapse file tree 5 files changed +132
-0
lines changed
Sources/SwiftSyntaxBuilder
Tests/SwiftSyntaxBuilderTest/Format Expand file tree Collapse file tree 5 files changed +132
-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 Format {
16
+ public let indentWidth : Int
17
+
18
+ private var indents : Int = 0
19
+
20
+ public init ( indentWidth: Int = 4 ) {
21
+ self . indentWidth = indentWidth
22
+ }
23
+ }
24
+
25
+ extension Format {
26
+ func indented( ) -> Format {
27
+ var copy = self
28
+ copy. indents += 1
29
+ return copy
30
+ }
31
+
32
+ func makeIndent( ) -> Trivia {
33
+ return indents == 0 ? . zero : Trivia . spaces ( indents * indentWidth)
34
+ }
35
+ }
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 protocol SyntaxListBuildable {
16
+ func buildSyntaxList( format: Format , leadingTrivia: Trivia ) -> [ Syntax ]
17
+ }
18
+
19
+ public protocol SyntaxBuildable : SyntaxListBuildable {
20
+ func buildSyntax( format: Format , leadingTrivia: Trivia ) -> Syntax
21
+ }
22
+
23
+ extension SyntaxBuildable {
24
+ public func buildSyntaxList( format: Format , leadingTrivia: Trivia ) -> [ Syntax ] {
25
+ [ buildSyntax ( format: format, leadingTrivia: leadingTrivia) ]
26
+ }
27
+ }
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 SyntaxList : SyntaxListBuildable {
16
+ let builders : [ SyntaxListBuildable ]
17
+
18
+ public func buildSyntaxList( format: Format , leadingTrivia: Trivia ) -> [ Syntax ] {
19
+ // Returns indented newlines to join syntaxes
20
+ func trivia( for index: Int ) -> Trivia {
21
+ leadingTrivia + ( index > builders. startIndex ? . newlines( 1 ) : . zero)
22
+ }
23
+
24
+ return builders
25
+ . enumerated ( )
26
+ . flatMap { index, builder in
27
+ builder. buildSyntaxList ( format: format, leadingTrivia: trivia ( for: index) )
28
+ }
29
+ }
30
+ }
31
+
32
+ extension SyntaxList {
33
+ public static let empty = SyntaxList ( builders: [ ] )
34
+ }
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
+ @_functionBuilder
16
+ public struct SyntaxListBuilder {
17
+ public static func buildBlock( _ builders: SyntaxListBuildable ... ) -> SyntaxListBuildable {
18
+ SyntaxList ( builders: builders)
19
+ }
20
+ }
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 FormatTests : XCTestCase {
7
+ func testMakeIndented( ) {
8
+ for width in 1 ... 4 {
9
+ let format = Format ( indentWidth: width)
10
+
11
+ XCTAssertEqual ( format. makeIndent ( ) , . zero)
12
+ XCTAssertEqual ( format. indented ( ) . makeIndent ( ) , . spaces( width) )
13
+ XCTAssertEqual ( format. indented ( ) . indented ( ) . makeIndent ( ) , . spaces( width * 2 ) )
14
+ }
15
+ }
16
+ }
You can’t perform that action at this time.
0 commit comments