Skip to content

Commit 4f32783

Browse files
committed
Minor improvements to BasicFormat
1 parent c5079a2 commit 4f32783

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

Sources/SwiftBasicFormat/BasicFormat.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ open class BasicFormat: SyntaxRewriter {
216216
(.regexLiteralPattern, _),
217217
(.regexSlash, .extendedRegexDelimiter), // closing extended regex delimiter should never be separate by a space
218218
(.rightAngle, .leftParen), // func foo<T>(x: T)
219+
(.rightBrace, .leftParen), // { return 1 }()
219220
(.rightParen, .leftParen), // returnsClosure()()
220221
(.rightParen, .period), // foo().bar
221222
(.rightSquareBracket, .period), // myArray[1].someProperty
@@ -310,6 +311,14 @@ open class BasicFormat: SyntaxRewriter {
310311
return false
311312
}()
312313

314+
lazy var nextTokenWillStartWithWhitespace: Bool = {
315+
guard let nextToken = nextToken else {
316+
return false
317+
}
318+
return nextToken.leadingTrivia.startsWithWhitespace
319+
|| (requiresLeadingNewline(nextToken) && isMutable(nextToken))
320+
}()
321+
313322
lazy var nextTokenWillStartWithNewline: Bool = {
314323
guard let nextToken = nextToken else {
315324
return false
@@ -359,7 +368,7 @@ open class BasicFormat: SyntaxRewriter {
359368
// because newlines should be preferred to spaces as a whitespace
360369
if requiresWhitespace(between: token, and: nextToken)
361370
&& !trailingTrivia.endsWithWhitespace
362-
&& !nextTokenWillStartWithNewline
371+
&& !nextTokenWillStartWithWhitespace
363372
{
364373
trailingTrivia += .space
365374
}

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

0 commit comments

Comments
 (0)