Skip to content

Commit 21654ab

Browse files
committed
Minor improvements to BasicFormat
1 parent 7dc1f04 commit 21654ab

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

Sources/SwiftBasicFormat/BasicFormat.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,14 @@ open class BasicFormat: SyntaxRewriter {
310310
return false
311311
}()
312312

313+
lazy var nextTokenWillStartWithWhitespace: Bool = {
314+
guard let nextToken = nextToken else {
315+
return false
316+
}
317+
return nextToken.leadingTrivia.startsWithWhitespace
318+
|| requiresLeadingNewline(nextToken)
319+
}()
320+
313321
lazy var nextTokenWillStartWithNewline: Bool = {
314322
guard let nextToken = nextToken else {
315323
return false
@@ -359,7 +367,7 @@ open class BasicFormat: SyntaxRewriter {
359367
// because newlines should be preferred to spaces as a whitespace
360368
if requiresWhitespace(between: token, and: nextToken)
361369
&& !trailingTrivia.endsWithWhitespace
362-
&& !nextTokenWillStartWithNewline
370+
&& !nextTokenWillStartWithWhitespace
363371
{
364372
trailingTrivia += .space
365373
}

Tests/SwiftBasicFormatTest/BasicFormatTests.swift

Lines changed: 72 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 {
@@ -257,4 +274,58 @@ final class BasicFormatTest: XCTestCase {
257274
"""
258275
)
259276
}
277+
278+
279+
func testDontInsertTrailingWhitespaceIfNextTokenStartsWithLeadingWhitespace() {
280+
let tree = VariableDeclSyntax(
281+
bindingKeyword: .keyword(.var),
282+
bindings: PatternBindingListSyntax([
283+
PatternBindingSyntax(
284+
pattern: PatternSyntax(IdentifierPatternSyntax(identifier: .identifier("x"))),
285+
typeAnnotation: TypeAnnotationSyntax(
286+
colon: .colonToken(trailingTrivia: .space),
287+
type: TypeSyntax(SimpleTypeIdentifierSyntax(name: .identifier("Int")))
288+
),
289+
accessor: PatternBindingSyntax.Accessor(
290+
AccessorBlockSyntax(
291+
leftBrace: .leftBraceToken(leadingTrivia: .space),
292+
accessors: AccessorListSyntax([]),
293+
rightBrace: .rightBraceToken(leadingTrivia: .newline)
294+
)
295+
)
296+
)
297+
])
298+
)
299+
assertFormatted(
300+
tree: tree,
301+
expected: """
302+
var x: Int {
303+
}
304+
"""
305+
)
306+
}
307+
308+
func testAccessor() {
309+
let source = """
310+
struct Point {
311+
var computed: Int {
312+
get { 0 }
313+
}
314+
}
315+
"""
316+
317+
assertFormatted(
318+
source: source,
319+
expected: """
320+
struct Point {
321+
var computed: Int {
322+
get {
323+
0
324+
}
325+
}
326+
}
327+
""",
328+
using: BasicFormat(indentationWidth: .spaces(2))
329+
)
330+
}
260331
}

0 commit comments

Comments
 (0)