Skip to content

Commit 996858b

Browse files
committed
Minor improvements to BasicFormat
1 parent 4d29ad9 commit 996858b

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

Sources/SwiftBasicFormat/BasicFormat.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ open class BasicFormat: SyntaxRewriter {
4949

5050
public func increaseIndentationLevel(to userDefinedIndentation: Trivia? = nil) {
5151
if let userDefinedIndentation = userDefinedIndentation {
52-
indentationStack.append((indentation: userDefinedIndentation, isUserDefined: false))
52+
indentationStack.append((indentation: userDefinedIndentation, isUserDefined: true))
5353
} else {
5454
indentationStack.append((indentation: currentIndentationLevel + indentationWidth, isUserDefined: false))
5555
}
@@ -229,6 +229,14 @@ open class BasicFormat: SyntaxRewriter {
229229
return false
230230
}()
231231

232+
lazy var nextTokenWillStartWithWhitespace: Bool = {
233+
guard let nextToken = nextToken else {
234+
return false
235+
}
236+
return nextToken.leadingTrivia.startsWithWhitespace
237+
|| requiresLeadingNewline(nextToken)
238+
}()
239+
232240
lazy var nextTokenWillStartWithNewline: Bool = {
233241
guard let nextToken = nextToken else {
234242
return false
@@ -278,7 +286,7 @@ open class BasicFormat: SyntaxRewriter {
278286
// because newlines should be preferred to spaces as a whitespace
279287
if requiresWhitespace(between: token, and: nextToken)
280288
&& !trailingTrivia.endsWithWhitespace
281-
&& !nextTokenWillStartWithNewline
289+
&& !nextTokenWillStartWithWhitespace
282290
{
283291
trailingTrivia += .space
284292
}

Tests/SwiftBasicFormatTest/BasicFormatTests.swift

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,30 @@ import SwiftSyntax
1818
import XCTest
1919
import _SwiftSyntaxTestSupport
2020

21+
fileprivate func assertFormatted<T: SyntaxProtocol>(
22+
tree: T,
23+
expected: String,
24+
using format: BasicFormat = BasicFormat(indentationWidth: .spaces(4)),
25+
file: StaticString = #file,
26+
line: UInt = #line
27+
) {
28+
assertStringsEqualWithDiff(tree.formatted(using: format).description, expected, file: file, line: line)
29+
}
30+
2131
fileprivate func assertFormatted(
2232
source: String,
2333
expected: String,
34+
using format: BasicFormat = BasicFormat(indentationWidth: .spaces(4)),
2435
file: StaticString = #file,
2536
line: UInt = #line
2637
) {
27-
assertStringsEqualWithDiff(Parser.parse(source: source).formatted().description, expected, file: file, line: line)
38+
assertFormatted(
39+
tree: Parser.parse(source: source),
40+
expected: expected,
41+
using: format,
42+
file: file,
43+
line: line
44+
)
2845
}
2946

3047
final class BasicFormatTest: XCTestCase {
@@ -190,4 +207,57 @@ final class BasicFormatTest: XCTestCase {
190207
"""
191208
)
192209
}
210+
211+
func testDontInsertTrailingWhitespaceIfNextTokenStartsWithLeadingWhitespace() {
212+
let tree = VariableDeclSyntax(
213+
bindingKeyword: .keyword(.var),
214+
bindings: PatternBindingListSyntax([
215+
PatternBindingSyntax(
216+
pattern: PatternSyntax(IdentifierPatternSyntax(identifier: .identifier("x"))),
217+
typeAnnotation: TypeAnnotationSyntax(
218+
colon: .colonToken(trailingTrivia: .space),
219+
type: TypeSyntax(SimpleTypeIdentifierSyntax(name: .identifier("Int")))
220+
),
221+
accessor: PatternBindingSyntax.Accessor(
222+
AccessorBlockSyntax(
223+
leftBrace: .leftBraceToken(leadingTrivia: .space),
224+
accessors: AccessorListSyntax([]),
225+
rightBrace: .rightBraceToken(leadingTrivia: .newline)
226+
)
227+
)
228+
)
229+
])
230+
)
231+
assertFormatted(
232+
tree: tree,
233+
expected: """
234+
var x: Int {
235+
}
236+
"""
237+
)
238+
}
239+
240+
func testAccessor() {
241+
let source = """
242+
struct Point {
243+
var computed: Int {
244+
get { 0 }
245+
}
246+
}
247+
"""
248+
249+
assertFormatted(
250+
source: source,
251+
expected: """
252+
struct Point {
253+
var computed: Int {
254+
get {
255+
0
256+
}
257+
}
258+
}
259+
""",
260+
using: BasicFormat(indentationWidth: .spaces(2))
261+
)
262+
}
193263
}

0 commit comments

Comments
 (0)