Skip to content

Commit 70c38a2

Browse files
authored
Merge pull request swiftlang#158 from dylansturg/experimental_remove_ignores_discretionary
Restore some discretionary newlines from PR swiftlang#143
2 parents 97fe585 + 0a2cac0 commit 70c38a2

File tree

6 files changed

+99
-32
lines changed

6 files changed

+99
-32
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
697697
/// - Parameter node: The tuple expression element to be arranged.
698698
private func arrangeAsTupleExprElement(_ node: TupleExprElementSyntax) {
699699
before(node.firstToken, tokens: .open)
700-
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
700+
after(node.colon, tokens: .break)
701701
after(node.lastToken, tokens: .close)
702702
if let trailingComma = node.trailingComma {
703703
closingDelimiterTokens.insert(trailingComma)
@@ -764,13 +764,13 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
764764
}
765765

766766
override func visit(_ node: DictionaryTypeSyntax) -> SyntaxVisitorContinueKind {
767-
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
767+
after(node.colon, tokens: .break)
768768
return .visitChildren
769769
}
770770

771771
override func visit(_ node: DictionaryElementSyntax) -> SyntaxVisitorContinueKind {
772772
before(node.firstToken, tokens: .open)
773-
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
773+
after(node.colon, tokens: .break)
774774
after(node.lastToken, tokens: .close)
775775
if let trailingComma = node.trailingComma {
776776
closingDelimiterTokens.insert(trailingComma)
@@ -869,10 +869,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
869869
// If we have an open delimiter following the colon, use a space instead of a continuation
870870
// break so that we don't awkwardly shift the delimiter down and indent it further if it
871871
// wraps.
872-
let tokenAfterColon: Token =
873-
startsWithOpenDelimiter(Syntax(node.expression))
874-
? .space
875-
: .break(.continue, newlines: .elective(ignoresDiscretionary: true))
872+
let tokenAfterColon: Token = startsWithOpenDelimiter(Syntax(node.expression)) ? .space : .break
876873

877874
after(node.colon, tokens: tokenAfterColon)
878875

@@ -1049,7 +1046,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
10491046
before(
10501047
node.secondName,
10511048
tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
1052-
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
1049+
after(node.colon, tokens: .break)
10531050

10541051
if let trailingComma = node.trailingComma {
10551052
after(trailingComma, tokens: .close, .break(.same))
@@ -1274,7 +1271,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
12741271
before(
12751272
node.secondName,
12761273
tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
1277-
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
1274+
after(node.colon, tokens: .break)
12781275

12791276
if let trailingComma = node.trailingComma {
12801277
after(trailingComma, tokens: .close, .break(.same))
@@ -1788,7 +1785,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
17881785

17891786
override func visit(_ node: GenericParameterSyntax) -> SyntaxVisitorContinueKind {
17901787
before(node.firstToken, tokens: .open)
1791-
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
1788+
after(node.colon, tokens: .break)
17921789
if let trailingComma = node.trailingComma {
17931790
after(trailingComma, tokens: .close, .break(.same))
17941791
} else {

Tests/SwiftFormatPrettyPrintTests/DictionaryDeclTests.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ final class DictionaryDeclTests: PrettyPrintTestCase {
9595
XCTAssertDiagnosed(.addTrailingComma, line: 4, column: 17)
9696
}
9797

98-
func testIgnoresDiscretionaryNewlineAfterColon() {
98+
func testDiscretionaryNewlineAfterColon() {
9999
let input =
100100
"""
101101
let a = [
@@ -106,6 +106,13 @@ final class DictionaryDeclTests: PrettyPrintTestCase {
106106
"shortKey":
107107
value
108108
]
109+
let a = [
110+
"shortKey": Very.Deeply.Nested.Member
111+
]
112+
let a = [
113+
"shortKey":
114+
Very.Deeply.Nested.Member
115+
]
109116
let a:
110117
[ReallyLongKeySoTheValueWillWrap:
111118
Value]
@@ -121,13 +128,25 @@ final class DictionaryDeclTests: PrettyPrintTestCase {
121128
value
122129
]
123130
let a = [
124-
"shortKey": value
131+
"shortKey":
132+
value
133+
]
134+
let a = [
135+
"shortKey": Very
136+
.Deeply.Nested
137+
.Member
138+
]
139+
let a = [
140+
"shortKey":
141+
Very.Deeply
142+
.Nested.Member
125143
]
126144
let a:
127145
[ReallyLongKeySoTheValueWillWrap:
128146
Value]
129147
let a:
130-
[ShortKey: Value]
148+
[ShortKey:
149+
Value]
131150
132151
"""
133152

Tests/SwiftFormatPrettyPrintTests/FunctionCallTests.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,23 +226,32 @@ final class FunctionCallTests: PrettyPrintTestCase {
226226
assertPrettyPrintEqual(input: input, expected: expected, linelength: 60)
227227
}
228228

229-
func testIgnoresDiscretionaryLineBreakAfterColon() {
229+
func testDiscretionaryLineBreakAfterColon() {
230230
let input =
231231
"""
232232
myFunc(
233233
a:
234234
foo,
235235
b:
236-
bar + baz + quux
236+
bar + baz + qux,
237+
c: Very.Deeply.Nested.Member,
238+
d:
239+
Very.Deeply.Nested.Member
237240
)
238241
"""
239242

240243
let expected =
241244
"""
242245
myFunc(
243-
a: foo,
244-
b: bar + baz
245-
+ quux
246+
a:
247+
foo,
248+
b:
249+
bar + baz + qux,
250+
c: Very.Deeply
251+
.Nested.Member,
252+
d:
253+
Very.Deeply
254+
.Nested.Member
246255
)
247256
248257
"""

Tests/SwiftFormatPrettyPrintTests/FunctionDeclTests.swift

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ final class FunctionDeclTests: PrettyPrintTestCase {
10581058
assertPrettyPrintEqual(input: input, expected: expected, linelength: 14)
10591059
}
10601060

1061-
func testIgnoresDiscretionaryLineBreakAfterColonAndInout() {
1061+
func testDiscretionaryLineBreakAfterColonAndInout() {
10621062
let input =
10631063
"""
10641064
func foo(
@@ -1075,6 +1075,18 @@ final class FunctionDeclTests: PrettyPrintTestCase {
10751075
reallyLongLabel
10761076
reallyLongArg: E
10771077
) {}
1078+
func foo(
1079+
a: Very.Deeply.Nested.InnerMember,
1080+
b:
1081+
Also.Deeply.Nested.InnerMember,
1082+
) {}
1083+
func foo(
1084+
cmp: @escaping (R) -> ()
1085+
) {}
1086+
func foo(
1087+
cmp:
1088+
@escaping (R) -> ()
1089+
) {}
10781090
func foo<
10791091
A:
10801092
ReallyLongType,
@@ -1089,15 +1101,35 @@ final class FunctionDeclTests: PrettyPrintTestCase {
10891101
func foo(
10901102
a:
10911103
ReallyLongTypeName,
1092-
b: ShortType,
1093-
c: inout C,
1094-
labeled d: D,
1104+
b:
1105+
ShortType,
1106+
c:
1107+
inout C,
1108+
labeled d:
1109+
D,
10951110
reallyLongLabel
10961111
reallyLongArg: E
10971112
) {}
1113+
func foo(
1114+
a: Very.Deeply.Nested
1115+
.InnerMember,
1116+
b:
1117+
Also.Deeply.Nested
1118+
.InnerMember,
1119+
) {}
1120+
func foo(
1121+
cmp: @escaping (R) ->
1122+
()
1123+
) {}
1124+
func foo(
1125+
cmp:
1126+
@escaping (R) -> ()
1127+
) {}
10981128
func foo<
1099-
A: ReallyLongType,
1100-
B: ShortType
1129+
A:
1130+
ReallyLongType,
1131+
B:
1132+
ShortType
11011133
>(a: A, b: B) {}
11021134
11031135
"""

Tests/SwiftFormatPrettyPrintTests/TupleDeclTests.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ final class TupleDeclTests: PrettyPrintTestCase {
4646
assertPrettyPrintEqual(input: input, expected: expected, linelength: 30)
4747
}
4848

49-
func testIgnoresDiscretionaryNewlineAfterColon() {
49+
func testDiscretionaryNewlineAfterColon() {
5050
let input =
5151
"""
5252
let a = (
@@ -58,7 +58,10 @@ final class TupleDeclTests: PrettyPrintTestCase {
5858
shortKey:
5959
value,
6060
b:
61-
c
61+
c,
62+
label: Deeply.Nested.InnerMember,
63+
label2:
64+
Deeply.Nested.InnerMember
6265
)
6366
"""
6467

@@ -70,8 +73,15 @@ final class TupleDeclTests: PrettyPrintTestCase {
7073
b: c
7174
)
7275
let a = (
73-
shortKey: value,
74-
b: c
76+
shortKey:
77+
value,
78+
b:
79+
c,
80+
label: Deeply.Nested
81+
.InnerMember,
82+
label2:
83+
Deeply.Nested
84+
.InnerMember
7585
)
7686
7787
"""

Tests/SwiftFormatPrettyPrintTests/XCTestManifests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ extension DictionaryDeclTests {
182182
// to regenerate.
183183
static let __allTests__DictionaryDeclTests = [
184184
("testBasicDictionaries", testBasicDictionaries),
185+
("testDiscretionaryNewlineAfterColon", testDiscretionaryNewlineAfterColon),
185186
("testGroupsTrailingComma", testGroupsTrailingComma),
186-
("testIgnoresDiscretionaryNewlineAfterColon", testIgnoresDiscretionaryNewlineAfterColon),
187187
("testNoTrailingCommasInTypes", testNoTrailingCommasInTypes),
188188
("testTrailingCommaDiagnostics", testTrailingCommaDiagnostics),
189189
("testWhitespaceOnlyDoesNotChangeTrailingComma", testWhitespaceOnlyDoesNotChangeTrailingComma),
@@ -270,10 +270,10 @@ extension FunctionCallTests {
270270
("testArgumentStartsWithOpenDelimiter", testArgumentStartsWithOpenDelimiter),
271271
("testBasicFunctionCalls_noPackArguments", testBasicFunctionCalls_noPackArguments),
272272
("testBasicFunctionCalls_packArguments", testBasicFunctionCalls_packArguments),
273+
("testDiscretionaryLineBreakAfterColon", testDiscretionaryLineBreakAfterColon),
273274
("testDiscretionaryLineBreakBeforeClosingParenthesis", testDiscretionaryLineBreakBeforeClosingParenthesis),
274275
("testDiscretionaryLineBreaksAreSelfCorrecting", testDiscretionaryLineBreaksAreSelfCorrecting),
275276
("testGroupsTrailingComma", testGroupsTrailingComma),
276-
("testIgnoresDiscretionaryLineBreakAfterColon", testIgnoresDiscretionaryLineBreakAfterColon),
277277
("testNestedFunctionCallExprSequences", testNestedFunctionCallExprSequences),
278278
("testSingleUnlabeledArgumentWithDelimiters", testSingleUnlabeledArgumentWithDelimiters),
279279
]
@@ -294,6 +294,7 @@ extension FunctionDeclTests {
294294
("testBreaksBeforeOrInsideOutputWithAttributes_prioritizingKeepingOutputTogether", testBreaksBeforeOrInsideOutputWithAttributes_prioritizingKeepingOutputTogether),
295295
("testBreaksBeforeOrInsideOutputWithWhereClause", testBreaksBeforeOrInsideOutputWithWhereClause),
296296
("testBreaksBeforeOrInsideOutputWithWhereClause_prioritizingKeepingOutputTogether", testBreaksBeforeOrInsideOutputWithWhereClause_prioritizingKeepingOutputTogether),
297+
("testDiscretionaryLineBreakAfterColonAndInout", testDiscretionaryLineBreakAfterColonAndInout),
297298
("testDoesNotBreakInsideEmptyParens", testDoesNotBreakInsideEmptyParens),
298299
("testDoesNotCollapseFunctionParameterAttributes", testDoesNotCollapseFunctionParameterAttributes),
299300
("testDoesNotCollapseStackedFunctionParameterAttributes", testDoesNotCollapseStackedFunctionParameterAttributes),
@@ -308,7 +309,6 @@ extension FunctionDeclTests {
308309
("testFunctionWhereClause", testFunctionWhereClause),
309310
("testFunctionWhereClause_lineBreakBeforeEachGenericRequirement", testFunctionWhereClause_lineBreakBeforeEachGenericRequirement),
310311
("testFunctionWithDefer", testFunctionWithDefer),
311-
("testIgnoresDiscretionaryLineBreakAfterColonAndInout", testIgnoresDiscretionaryLineBreakAfterColonAndInout),
312312
("testOperatorOverloads", testOperatorOverloads),
313313
("testRemovesLineBreakBeforeOpenBraceUnlessAbsolutelyNecessary", testRemovesLineBreakBeforeOpenBraceUnlessAbsolutelyNecessary),
314314
]
@@ -723,8 +723,8 @@ extension TupleDeclTests {
723723
// to regenerate.
724724
static let __allTests__TupleDeclTests = [
725725
("testBasicTuples", testBasicTuples),
726+
("testDiscretionaryNewlineAfterColon", testDiscretionaryNewlineAfterColon),
726727
("testGroupsTrailingComma", testGroupsTrailingComma),
727-
("testIgnoresDiscretionaryNewlineAfterColon", testIgnoresDiscretionaryNewlineAfterColon),
728728
("testLabeledTuples", testLabeledTuples),
729729
]
730730
}

0 commit comments

Comments
 (0)