Skip to content

Commit 410c9e0

Browse files
committed
Fix indentation when there is a leading new line
1 parent 00e010a commit 410c9e0

File tree

12 files changed

+729
-636
lines changed

12 files changed

+729
-636
lines changed

Sources/SwiftSyntax/Trivia.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ extension TriviaPiece: CustomDebugStringConvertible {
8080
/// A collection of leading or trailing trivia. This is the main data structure
8181
/// for thinking about trivia.
8282
public struct Trivia {
83-
let pieces: [TriviaPiece]
83+
public let pieces: [TriviaPiece]
8484

8585
/// Creates Trivia with the provided underlying pieces.
8686
public init(pieces: [TriviaPiece]) {

Sources/SwiftSyntax/gyb_generated/Trivia.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ extension TriviaPiece: CustomDebugStringConvertible {
126126
/// A collection of leading or trailing trivia. This is the main data structure
127127
/// for thinking about trivia.
128128
public struct Trivia {
129-
let pieces: [TriviaPiece]
129+
public let pieces: [TriviaPiece]
130130

131131
/// Creates Trivia with the provided underlying pieces.
132132
public init(pieces: [TriviaPiece]) {

Sources/SwiftSyntaxBuilder/BuildableCollectionNodes.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public struct ${type.buildable()}: ExpressibleByArrayLiteral, SyntaxBuildable, $
6565
})
6666
% end
6767
if let leadingTrivia = leadingTrivia {
68-
return result.withLeadingTrivia(leadingTrivia + (result.leadingTrivia ?? []))
68+
return result.withLeadingTrivia((leadingTrivia + (result.leadingTrivia ?? [])).addSpacingAfterNewlinesIfNeeded())
6969
} else {
7070
return result
7171
}

Sources/SwiftSyntaxBuilder/BuildableNodes.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public struct ${type.buildable()}${conformance_clause(conformances)} {
120120
${',\n '.join(['%s: %s' % (child.name(), child.generate_expr_build_syntax_node(child.name(), 'format')) for child in children])}
121121
)
122122
let combinedLeadingTrivia = leadingTrivia + (additionalLeadingTrivia ?? []) + (result.leadingTrivia ?? [])
123-
return result.withLeadingTrivia(combinedLeadingTrivia)
123+
return result.withLeadingTrivia(combinedLeadingTrivia.addSpacingAfterNewlinesIfNeeded())
124124
}
125125

126126
/// Conformance to `${base_type.buildable()}`.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 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+
extension Trivia {
16+
/// Creates a new `Trivia` and adds spacing if needed.
17+
///
18+
/// If there is 3 or more, then we see if the first two is a newline and a space
19+
/// If so, we need to replace add the correct amount of space after each newline
20+
/// comming after to have the right indentation of the code.
21+
func addSpacingAfterNewlinesIfNeeded() -> Trivia {
22+
var updatedTriviaPieces = self.pieces
23+
if updatedTriviaPieces.count > 2,
24+
isNewline(trivia: updatedTriviaPieces[0]),
25+
let toInset = self.makeIndentation(trivia: updatedTriviaPieces[1]) {
26+
27+
for i in (2..<updatedTriviaPieces.count).reversed() where isNewline(trivia: updatedTriviaPieces[i]) {
28+
updatedTriviaPieces.insert(toInset, at: i + 1)
29+
}
30+
}
31+
32+
return Trivia(pieces: updatedTriviaPieces)
33+
}
34+
35+
private func isNewline(trivia: TriviaPiece) -> Bool {
36+
switch trivia {
37+
case .newlines,
38+
.carriageReturns,
39+
.carriageReturnLineFeeds:
40+
return true
41+
default:
42+
return false
43+
}
44+
}
45+
46+
private func makeIndentation(trivia: TriviaPiece) -> TriviaPiece? {
47+
switch trivia {
48+
case .spaces(let spaces):
49+
return .spaces(spaces)
50+
case .tabs(let tabs):
51+
return .tabs(tabs)
52+
default:
53+
return nil
54+
}
55+
}
56+
}

Sources/SwiftSyntaxBuilder/generated/BuildableBaseProtocols.swift

Lines changed: 102 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -16,202 +16,202 @@
1616
import SwiftSyntax
1717
public protocol DeclListBuildable: SyntaxListBuildable {
1818
/// Builds list of `DeclSyntax`s.
19-
/// - Parameter format: The `Format` to use.
20-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
21-
func buildDeclList(format: Format, leadingTrivia: Trivia?)-> [DeclSyntax]
19+
/// - Parameter format: The `Format` to use.
20+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
21+
func buildDeclList(format: Format, leadingTrivia: Trivia?)-> [DeclSyntax]
2222
}
2323
public protocol DeclBuildable: ExpressibleAsDeclBuildable, DeclListBuildable, SyntaxBuildable {
2424
/// Builds list of `DeclSyntax`s.
25-
/// - Parameter format: The `Format` to use.
26-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
27-
func buildDecl(format: Format, leadingTrivia: Trivia?)-> DeclSyntax
25+
/// - Parameter format: The `Format` to use.
26+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
27+
func buildDecl(format: Format, leadingTrivia: Trivia?)-> DeclSyntax
2828
}
2929
public extension DeclBuildable {
3030
/// Satisfies conformance to `ExpressibleAsDeclBuildable`.
31-
func createDeclBuildable()-> DeclBuildable {
31+
func createDeclBuildable()-> DeclBuildable {
3232
return self
3333
}
3434
/// Builds list of `DeclSyntax`s.
35-
/// - Parameter format: The `Format` to use.
36-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
37-
///
38-
/// Satisfies conformance to `DeclListBuildable`
39-
func buildDeclList(format: Format, leadingTrivia: Trivia? = nil)-> [DeclSyntax] {
35+
/// - Parameter format: The `Format` to use.
36+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
37+
///
38+
/// Satisfies conformance to `DeclListBuildable`
39+
func buildDeclList(format: Format, leadingTrivia: Trivia? = nil)-> [DeclSyntax] {
4040
return [buildDecl(format: format, leadingTrivia: leadingTrivia)]
4141
}
4242
/// Builds a `DeclSyntax`.
43-
/// - Parameter format: The `Format` to use.
44-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
45-
/// - Returns: A new `Syntax` with the built `DeclSyntax`.
46-
///
47-
/// Satisfies conformance to `SyntaxBuildable`.
48-
func buildSyntax(format: Format, leadingTrivia: Trivia? = nil)-> Syntax {
43+
/// - Parameter format: The `Format` to use.
44+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
45+
/// - Returns: A new `Syntax` with the built `DeclSyntax`.
46+
///
47+
/// Satisfies conformance to `SyntaxBuildable`.
48+
func buildSyntax(format: Format, leadingTrivia: Trivia? = nil)-> Syntax {
4949
return Syntax(buildDecl(format: format, leadingTrivia: leadingTrivia))
5050
}
5151
}
5252
public protocol ExprListBuildable: SyntaxListBuildable {
5353
/// Builds list of `ExprSyntax`s.
54-
/// - Parameter format: The `Format` to use.
55-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
56-
func buildExprList(format: Format, leadingTrivia: Trivia?)-> [ExprSyntax]
54+
/// - Parameter format: The `Format` to use.
55+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
56+
func buildExprList(format: Format, leadingTrivia: Trivia?)-> [ExprSyntax]
5757
}
5858
public protocol ExprBuildable: ExpressibleAsExprBuildable, ExprListBuildable, SyntaxBuildable {
5959
/// Builds list of `ExprSyntax`s.
60-
/// - Parameter format: The `Format` to use.
61-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
62-
func buildExpr(format: Format, leadingTrivia: Trivia?)-> ExprSyntax
60+
/// - Parameter format: The `Format` to use.
61+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
62+
func buildExpr(format: Format, leadingTrivia: Trivia?)-> ExprSyntax
6363
}
6464
public extension ExprBuildable {
6565
/// Satisfies conformance to `ExpressibleAsExprBuildable`.
66-
func createExprBuildable()-> ExprBuildable {
66+
func createExprBuildable()-> ExprBuildable {
6767
return self
6868
}
6969
/// Builds list of `ExprSyntax`s.
70-
/// - Parameter format: The `Format` to use.
71-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
72-
///
73-
/// Satisfies conformance to `ExprListBuildable`
74-
func buildExprList(format: Format, leadingTrivia: Trivia? = nil)-> [ExprSyntax] {
70+
/// - Parameter format: The `Format` to use.
71+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
72+
///
73+
/// Satisfies conformance to `ExprListBuildable`
74+
func buildExprList(format: Format, leadingTrivia: Trivia? = nil)-> [ExprSyntax] {
7575
return [buildExpr(format: format, leadingTrivia: leadingTrivia)]
7676
}
7777
/// Builds a `ExprSyntax`.
78-
/// - Parameter format: The `Format` to use.
79-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
80-
/// - Returns: A new `Syntax` with the built `ExprSyntax`.
81-
///
82-
/// Satisfies conformance to `SyntaxBuildable`.
83-
func buildSyntax(format: Format, leadingTrivia: Trivia? = nil)-> Syntax {
78+
/// - Parameter format: The `Format` to use.
79+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
80+
/// - Returns: A new `Syntax` with the built `ExprSyntax`.
81+
///
82+
/// Satisfies conformance to `SyntaxBuildable`.
83+
func buildSyntax(format: Format, leadingTrivia: Trivia? = nil)-> Syntax {
8484
return Syntax(buildExpr(format: format, leadingTrivia: leadingTrivia))
8585
}
8686
}
8787
public protocol PatternListBuildable: SyntaxListBuildable {
8888
/// Builds list of `PatternSyntax`s.
89-
/// - Parameter format: The `Format` to use.
90-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
91-
func buildPatternList(format: Format, leadingTrivia: Trivia?)-> [PatternSyntax]
89+
/// - Parameter format: The `Format` to use.
90+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
91+
func buildPatternList(format: Format, leadingTrivia: Trivia?)-> [PatternSyntax]
9292
}
9393
public protocol PatternBuildable: ExpressibleAsPatternBuildable, PatternListBuildable, SyntaxBuildable {
9494
/// Builds list of `PatternSyntax`s.
95-
/// - Parameter format: The `Format` to use.
96-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
97-
func buildPattern(format: Format, leadingTrivia: Trivia?)-> PatternSyntax
95+
/// - Parameter format: The `Format` to use.
96+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
97+
func buildPattern(format: Format, leadingTrivia: Trivia?)-> PatternSyntax
9898
}
9999
public extension PatternBuildable {
100100
/// Satisfies conformance to `ExpressibleAsPatternBuildable`.
101-
func createPatternBuildable()-> PatternBuildable {
101+
func createPatternBuildable()-> PatternBuildable {
102102
return self
103103
}
104104
/// Builds list of `PatternSyntax`s.
105-
/// - Parameter format: The `Format` to use.
106-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
107-
///
108-
/// Satisfies conformance to `PatternListBuildable`
109-
func buildPatternList(format: Format, leadingTrivia: Trivia? = nil)-> [PatternSyntax] {
105+
/// - Parameter format: The `Format` to use.
106+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
107+
///
108+
/// Satisfies conformance to `PatternListBuildable`
109+
func buildPatternList(format: Format, leadingTrivia: Trivia? = nil)-> [PatternSyntax] {
110110
return [buildPattern(format: format, leadingTrivia: leadingTrivia)]
111111
}
112112
/// Builds a `PatternSyntax`.
113-
/// - Parameter format: The `Format` to use.
114-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
115-
/// - Returns: A new `Syntax` with the built `PatternSyntax`.
116-
///
117-
/// Satisfies conformance to `SyntaxBuildable`.
118-
func buildSyntax(format: Format, leadingTrivia: Trivia? = nil)-> Syntax {
113+
/// - Parameter format: The `Format` to use.
114+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
115+
/// - Returns: A new `Syntax` with the built `PatternSyntax`.
116+
///
117+
/// Satisfies conformance to `SyntaxBuildable`.
118+
func buildSyntax(format: Format, leadingTrivia: Trivia? = nil)-> Syntax {
119119
return Syntax(buildPattern(format: format, leadingTrivia: leadingTrivia))
120120
}
121121
}
122122
public protocol StmtListBuildable: SyntaxListBuildable {
123123
/// Builds list of `StmtSyntax`s.
124-
/// - Parameter format: The `Format` to use.
125-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
126-
func buildStmtList(format: Format, leadingTrivia: Trivia?)-> [StmtSyntax]
124+
/// - Parameter format: The `Format` to use.
125+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
126+
func buildStmtList(format: Format, leadingTrivia: Trivia?)-> [StmtSyntax]
127127
}
128128
public protocol StmtBuildable: ExpressibleAsStmtBuildable, StmtListBuildable, SyntaxBuildable {
129129
/// Builds list of `StmtSyntax`s.
130-
/// - Parameter format: The `Format` to use.
131-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
132-
func buildStmt(format: Format, leadingTrivia: Trivia?)-> StmtSyntax
130+
/// - Parameter format: The `Format` to use.
131+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
132+
func buildStmt(format: Format, leadingTrivia: Trivia?)-> StmtSyntax
133133
}
134134
public extension StmtBuildable {
135135
/// Satisfies conformance to `ExpressibleAsStmtBuildable`.
136-
func createStmtBuildable()-> StmtBuildable {
136+
func createStmtBuildable()-> StmtBuildable {
137137
return self
138138
}
139139
/// Builds list of `StmtSyntax`s.
140-
/// - Parameter format: The `Format` to use.
141-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
142-
///
143-
/// Satisfies conformance to `StmtListBuildable`
144-
func buildStmtList(format: Format, leadingTrivia: Trivia? = nil)-> [StmtSyntax] {
140+
/// - Parameter format: The `Format` to use.
141+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
142+
///
143+
/// Satisfies conformance to `StmtListBuildable`
144+
func buildStmtList(format: Format, leadingTrivia: Trivia? = nil)-> [StmtSyntax] {
145145
return [buildStmt(format: format, leadingTrivia: leadingTrivia)]
146146
}
147147
/// Builds a `StmtSyntax`.
148-
/// - Parameter format: The `Format` to use.
149-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
150-
/// - Returns: A new `Syntax` with the built `StmtSyntax`.
151-
///
152-
/// Satisfies conformance to `SyntaxBuildable`.
153-
func buildSyntax(format: Format, leadingTrivia: Trivia? = nil)-> Syntax {
148+
/// - Parameter format: The `Format` to use.
149+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
150+
/// - Returns: A new `Syntax` with the built `StmtSyntax`.
151+
///
152+
/// Satisfies conformance to `SyntaxBuildable`.
153+
func buildSyntax(format: Format, leadingTrivia: Trivia? = nil)-> Syntax {
154154
return Syntax(buildStmt(format: format, leadingTrivia: leadingTrivia))
155155
}
156156
}
157157
public protocol SyntaxListBuildable {
158158
/// Builds list of `Syntax`s.
159-
/// - Parameter format: The `Format` to use.
160-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
161-
func buildSyntaxList(format: Format, leadingTrivia: Trivia?)-> [Syntax]
159+
/// - Parameter format: The `Format` to use.
160+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
161+
func buildSyntaxList(format: Format, leadingTrivia: Trivia?)-> [Syntax]
162162
}
163163
public protocol SyntaxBuildable: ExpressibleAsSyntaxBuildable, SyntaxListBuildable {
164164
/// Builds list of `Syntax`s.
165-
/// - Parameter format: The `Format` to use.
166-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
167-
func buildSyntax(format: Format, leadingTrivia: Trivia?)-> Syntax
165+
/// - Parameter format: The `Format` to use.
166+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
167+
func buildSyntax(format: Format, leadingTrivia: Trivia?)-> Syntax
168168
}
169169
public extension SyntaxBuildable {
170170
/// Satisfies conformance to `ExpressibleAsSyntaxBuildable`.
171-
func createSyntaxBuildable()-> SyntaxBuildable {
171+
func createSyntaxBuildable()-> SyntaxBuildable {
172172
return self
173173
}
174174
/// Builds list of `Syntax`s.
175-
/// - Parameter format: The `Format` to use.
176-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
177-
///
178-
/// Satisfies conformance to `SyntaxListBuildable`
179-
func buildSyntaxList(format: Format, leadingTrivia: Trivia? = nil)-> [Syntax] {
175+
/// - Parameter format: The `Format` to use.
176+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
177+
///
178+
/// Satisfies conformance to `SyntaxListBuildable`
179+
func buildSyntaxList(format: Format, leadingTrivia: Trivia? = nil)-> [Syntax] {
180180
return [buildSyntax(format: format, leadingTrivia: leadingTrivia)]
181181
}
182182
}
183183
public protocol TypeListBuildable: SyntaxListBuildable {
184184
/// Builds list of `TypeSyntax`s.
185-
/// - Parameter format: The `Format` to use.
186-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
187-
func buildTypeList(format: Format, leadingTrivia: Trivia?)-> [TypeSyntax]
185+
/// - Parameter format: The `Format` to use.
186+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
187+
func buildTypeList(format: Format, leadingTrivia: Trivia?)-> [TypeSyntax]
188188
}
189189
public protocol TypeBuildable: ExpressibleAsTypeBuildable, TypeListBuildable, SyntaxBuildable {
190190
/// Builds list of `TypeSyntax`s.
191-
/// - Parameter format: The `Format` to use.
192-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
193-
func buildType(format: Format, leadingTrivia: Trivia?)-> TypeSyntax
191+
/// - Parameter format: The `Format` to use.
192+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
193+
func buildType(format: Format, leadingTrivia: Trivia?)-> TypeSyntax
194194
}
195195
public extension TypeBuildable {
196196
/// Satisfies conformance to `ExpressibleAsTypeBuildable`.
197-
func createTypeBuildable()-> TypeBuildable {
197+
func createTypeBuildable()-> TypeBuildable {
198198
return self
199199
}
200200
/// Builds list of `TypeSyntax`s.
201-
/// - Parameter format: The `Format` to use.
202-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
203-
///
204-
/// Satisfies conformance to `TypeListBuildable`
205-
func buildTypeList(format: Format, leadingTrivia: Trivia? = nil)-> [TypeSyntax] {
201+
/// - Parameter format: The `Format` to use.
202+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
203+
///
204+
/// Satisfies conformance to `TypeListBuildable`
205+
func buildTypeList(format: Format, leadingTrivia: Trivia? = nil)-> [TypeSyntax] {
206206
return [buildType(format: format, leadingTrivia: leadingTrivia)]
207207
}
208208
/// Builds a `TypeSyntax`.
209-
/// - Parameter format: The `Format` to use.
210-
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
211-
/// - Returns: A new `Syntax` with the built `TypeSyntax`.
212-
///
213-
/// Satisfies conformance to `SyntaxBuildable`.
214-
func buildSyntax(format: Format, leadingTrivia: Trivia? = nil)-> Syntax {
209+
/// - Parameter format: The `Format` to use.
210+
/// - Parameter leadingTrivia: Replaces the last leading trivia if not nil.
211+
/// - Returns: A new `Syntax` with the built `TypeSyntax`.
212+
///
213+
/// Satisfies conformance to `SyntaxBuildable`.
214+
func buildSyntax(format: Format, leadingTrivia: Trivia? = nil)-> Syntax {
215215
return Syntax(buildType(format: format, leadingTrivia: leadingTrivia))
216216
}
217217
}

0 commit comments

Comments
 (0)