Skip to content

Commit 5ed571c

Browse files
author
David Brunow
committed
Add fix and tests for postfix pound ifs
1 parent db62da4 commit 5ed571c

File tree

2 files changed

+190
-2
lines changed

2 files changed

+190
-2
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,15 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
12891289
before(tokenToOpenWith.nextToken, tokens: .break(breakKindClose, newlines: .soft), .close)
12901290
}
12911291

1292-
if let condition = node.condition {
1292+
if Syntax(node).isPostfix {
1293+
before(
1294+
node.firstToken,
1295+
tokens: [
1296+
.printerControl(kind: .enableBreaking),
1297+
.break(.reset),
1298+
]
1299+
)
1300+
} else if let condition = node.condition {
12931301
before(condition.firstToken, tokens: .printerControl(kind: .disableBreaking))
12941302
after(
12951303
condition.lastToken,
@@ -3441,7 +3449,11 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
34413449

34423450
if let calledMemberAccessExpr = calledExpression.as(MemberAccessExprSyntax.self) {
34433451
if calledMemberAccessExpr.base != nil {
3444-
before(calledMemberAccessExpr.dot, tokens: [.break(.contextual, size: 0)])
3452+
if Syntax(calledMemberAccessExpr).isPostfix {
3453+
before(calledMemberAccessExpr.dot, tokens: [.break(.same, size: 0)])
3454+
} else {
3455+
before(calledMemberAccessExpr.dot, tokens: [.break(.contextual, size: 0)])
3456+
}
34453457
}
34463458
before(calledMemberAccessExpr.dot, tokens: beforeTokens)
34473459
after(expr.lastToken, tokens: afterTokens)
@@ -3466,6 +3478,22 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
34663478
}
34673479
}
34683480

3481+
extension Syntax {
3482+
var isPostfix: Bool {
3483+
var this: Syntax? = self
3484+
3485+
while this?.parent != nil {
3486+
if this?.parent?.is(PostfixIfConfigExprSyntax.self) == true {
3487+
return true
3488+
}
3489+
3490+
this = this?.parent
3491+
}
3492+
3493+
return false
3494+
}
3495+
}
3496+
34693497
extension Syntax {
34703498
/// Creates a pretty-printable token stream for the provided Syntax node.
34713499
func makeTokenStream(configuration: Configuration, operatorContext: OperatorContext) -> [Token] {

Tests/SwiftFormatPrettyPrintTests/IfConfigTests.swift

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,164 @@ final class IfConfigTests: PrettyPrintTestCase {
230230

231231
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40)
232232
}
233+
234+
func testPostfixPoundIfAfterParentheses() {
235+
let input =
236+
"""
237+
VStack {
238+
Text("something")
239+
#if os(iOS)
240+
.iOSSpecificModifier()
241+
#endif
242+
.commonModifier()
243+
}
244+
"""
245+
246+
let expected =
247+
"""
248+
VStack {
249+
Text("something")
250+
#if os(iOS)
251+
.iOSSpecificModifier()
252+
#endif
253+
.commonModifier()
254+
}
255+
256+
"""
257+
258+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
259+
}
260+
261+
func testPostfixPoundIfAfterParenthesesMultipleMembers() {
262+
let input =
263+
"""
264+
VStack {
265+
Text("something")
266+
#if os(iOS)
267+
.iOSSpecificModifier()
268+
.anotherModifier()
269+
.anotherAnotherModifier()
270+
#endif
271+
.commonModifier()
272+
.anotherCommonModifier()
273+
}
274+
"""
275+
276+
let expected =
277+
"""
278+
VStack {
279+
Text("something")
280+
#if os(iOS)
281+
.iOSSpecificModifier()
282+
.anotherModifier()
283+
.anotherAnotherModifier()
284+
#endif
285+
.commonModifier()
286+
.anotherCommonModifier()
287+
}
288+
289+
"""
290+
291+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
292+
}
293+
294+
func testPostfixPoundIfNested() {
295+
let input =
296+
"""
297+
VStack {
298+
Text("something")
299+
#if os(iOS) || os(watchOS)
300+
#if os(iOS)
301+
.iOSModifier()
302+
#else
303+
.watchOSModifier()
304+
#endif
305+
.iOSAndWatchOSModifier()
306+
#endif
307+
}
308+
"""
309+
310+
let expected =
311+
"""
312+
VStack {
313+
Text("something")
314+
#if os(iOS) || os(watchOS)
315+
#if os(iOS)
316+
.iOSModifier()
317+
#else
318+
.watchOSModifier()
319+
#endif
320+
.iOSAndWatchOSModifier()
321+
#endif
322+
}
323+
324+
"""
325+
326+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
327+
}
328+
329+
330+
func testPostfixPoundIfAfterVariables() {
331+
let input =
332+
"""
333+
VStack {
334+
textView
335+
#if os(iOS)
336+
.iOSSpecificModifier()
337+
#endif
338+
.commonModifier()
339+
}
340+
"""
341+
342+
let expected =
343+
"""
344+
VStack {
345+
textView
346+
#if os(iOS)
347+
.iOSSpecificModifier()
348+
#endif
349+
.commonModifier()
350+
}
351+
352+
"""
353+
354+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
355+
}
356+
357+
func testPostfixPoundIfAfterClosingBrace() {
358+
let input =
359+
"""
360+
HStack {
361+
Toggle(isOn: binding) {
362+
Text("Some text")
363+
}
364+
#if !os(tvOS)
365+
.toggleStyle(SwitchToggleStyle(tint: Color.blue))
366+
#endif
367+
.accessibilityValue(
368+
binding.wrappedValue == true ? "On" : "Off"
369+
)
370+
}
371+
"""
372+
373+
let expected =
374+
"""
375+
HStack {
376+
Toggle(isOn: binding) {
377+
Text("Some text")
378+
}
379+
#if !os(tvOS)
380+
.toggleStyle(
381+
SwitchToggleStyle(tint: Color.blue))
382+
#endif
383+
.accessibilityValue(
384+
binding.wrappedValue == true
385+
? "On" : "Off"
386+
)
387+
}
388+
389+
"""
390+
391+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
392+
}
233393
}

0 commit comments

Comments
 (0)