Skip to content

Commit 95ad7cf

Browse files
author
David Brunow
committed
Add fix and tests for postfix pound ifs
1 parent e6b8c60 commit 95ad7cf

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,21 @@ 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 let previousToken = node.previousToken,
1293+
node.condition?.nextToken?.text == ".",
1294+
(previousToken.text == ")" || previousToken.text == "}") {
1295+
before(
1296+
node.firstToken,
1297+
tokens: [
1298+
.printerControl(kind: .enableBreaking),
1299+
.break(.reset, size: 0),
1300+
.break(breakKindOpen),
1301+
.open,
1302+
.break(breakKindClose),
1303+
.close
1304+
]
1305+
)
1306+
} else if let condition = node.condition {
12931307
before(condition.firstToken, tokens: .printerControl(kind: .disableBreaking))
12941308
after(
12951309
condition.lastToken,

Tests/SwiftFormatPrettyPrintTests/IfConfigTests.swift

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,68 @@ 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 testPostfixPoundIfAfterClosingBrace() {
262+
let input =
263+
"""
264+
HStack {
265+
Toggle(isOn: binding) {
266+
Text("Some text")
267+
}
268+
#if !os(tvOS)
269+
.toggleStyle(SwitchToggleStyle(tint: Color.blue))
270+
#endif
271+
.accessibilityValue(
272+
binding.wrappedValue == true ? "On" : "Off"
273+
)
274+
}
275+
"""
276+
277+
let expected =
278+
"""
279+
HStack {
280+
Toggle(isOn: binding) {
281+
Text("Some text")
282+
}
283+
#if !os(tvOS)
284+
.toggleStyle(
285+
SwitchToggleStyle(tint: Color.blue))
286+
#endif
287+
.accessibilityValue(
288+
binding.wrappedValue == true
289+
? "On" : "Off"
290+
)
291+
}
292+
293+
"""
294+
295+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
296+
}
233297
}

0 commit comments

Comments
 (0)