Skip to content

Restore some discretionary newlines from PR #143 #158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
/// - Parameter node: The tuple expression element to be arranged.
private func arrangeAsTupleExprElement(_ node: TupleExprElementSyntax) {
before(node.firstToken, tokens: .open)
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
after(node.colon, tokens: .break)
after(node.lastToken, tokens: .close)
if let trailingComma = node.trailingComma {
closingDelimiterTokens.insert(trailingComma)
Expand Down Expand Up @@ -764,13 +764,13 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}

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

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

after(node.colon, tokens: tokenAfterColon)

Expand Down Expand Up @@ -1049,7 +1046,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
before(
node.secondName,
tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
after(node.colon, tokens: .break)

if let trailingComma = node.trailingComma {
after(trailingComma, tokens: .close, .break(.same))
Expand Down Expand Up @@ -1274,7 +1271,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
before(
node.secondName,
tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
after(node.colon, tokens: .break)

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

override func visit(_ node: GenericParameterSyntax) -> SyntaxVisitorContinueKind {
before(node.firstToken, tokens: .open)
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
after(node.colon, tokens: .break)
if let trailingComma = node.trailingComma {
after(trailingComma, tokens: .close, .break(.same))
} else {
Expand Down
25 changes: 22 additions & 3 deletions Tests/SwiftFormatPrettyPrintTests/DictionaryDeclTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ final class DictionaryDeclTests: PrettyPrintTestCase {
XCTAssertDiagnosed(.addTrailingComma, line: 4, column: 17)
}

func testIgnoresDiscretionaryNewlineAfterColon() {
func testDiscretionaryNewlineAfterColon() {
let input =
"""
let a = [
Expand All @@ -106,6 +106,13 @@ final class DictionaryDeclTests: PrettyPrintTestCase {
"shortKey":
value
]
let a = [
"shortKey": Very.Deeply.Nested.Member
]
let a = [
"shortKey":
Very.Deeply.Nested.Member
]
let a:
[ReallyLongKeySoTheValueWillWrap:
Value]
Expand All @@ -121,13 +128,25 @@ final class DictionaryDeclTests: PrettyPrintTestCase {
value
]
let a = [
"shortKey": value
"shortKey":
value
]
let a = [
"shortKey": Very
.Deeply.Nested
.Member
]
let a = [
"shortKey":
Very.Deeply
.Nested.Member
]
let a:
[ReallyLongKeySoTheValueWillWrap:
Value]
let a:
[ShortKey: Value]
[ShortKey:
Value]

"""

Expand Down
19 changes: 14 additions & 5 deletions Tests/SwiftFormatPrettyPrintTests/FunctionCallTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,23 +226,32 @@ final class FunctionCallTests: PrettyPrintTestCase {
assertPrettyPrintEqual(input: input, expected: expected, linelength: 60)
}

func testIgnoresDiscretionaryLineBreakAfterColon() {
func testDiscretionaryLineBreakAfterColon() {
let input =
"""
myFunc(
a:
foo,
b:
bar + baz + quux
bar + baz + qux,
c: Very.Deeply.Nested.Member,
d:
Very.Deeply.Nested.Member
)
"""

let expected =
"""
myFunc(
a: foo,
b: bar + baz
+ quux
a:
foo,
b:
bar + baz + qux,
c: Very.Deeply
.Nested.Member,
d:
Very.Deeply
.Nested.Member
)

"""
Expand Down
44 changes: 38 additions & 6 deletions Tests/SwiftFormatPrettyPrintTests/FunctionDeclTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ final class FunctionDeclTests: PrettyPrintTestCase {
assertPrettyPrintEqual(input: input, expected: expected, linelength: 14)
}

func testIgnoresDiscretionaryLineBreakAfterColonAndInout() {
func testDiscretionaryLineBreakAfterColonAndInout() {
let input =
"""
func foo(
Expand All @@ -1075,6 +1075,18 @@ final class FunctionDeclTests: PrettyPrintTestCase {
reallyLongLabel
reallyLongArg: E
) {}
func foo(
a: Very.Deeply.Nested.InnerMember,
b:
Also.Deeply.Nested.InnerMember,
) {}
func foo(
cmp: @escaping (R) -> ()
) {}
func foo(
cmp:
@escaping (R) -> ()
) {}
func foo<
A:
ReallyLongType,
Expand All @@ -1089,15 +1101,35 @@ final class FunctionDeclTests: PrettyPrintTestCase {
func foo(
a:
ReallyLongTypeName,
b: ShortType,
c: inout C,
labeled d: D,
b:
ShortType,
c:
inout C,
labeled d:
D,
reallyLongLabel
reallyLongArg: E
) {}
func foo(
a: Very.Deeply.Nested
.InnerMember,
b:
Also.Deeply.Nested
.InnerMember,
) {}
func foo(
cmp: @escaping (R) ->
()
) {}
func foo(
cmp:
@escaping (R) -> ()
) {}
func foo<
A: ReallyLongType,
B: ShortType
A:
ReallyLongType,
B:
ShortType
>(a: A, b: B) {}

"""
Expand Down
18 changes: 14 additions & 4 deletions Tests/SwiftFormatPrettyPrintTests/TupleDeclTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final class TupleDeclTests: PrettyPrintTestCase {
assertPrettyPrintEqual(input: input, expected: expected, linelength: 30)
}

func testIgnoresDiscretionaryNewlineAfterColon() {
func testDiscretionaryNewlineAfterColon() {
let input =
"""
let a = (
Expand All @@ -58,7 +58,10 @@ final class TupleDeclTests: PrettyPrintTestCase {
shortKey:
value,
b:
c
c,
label: Deeply.Nested.InnerMember,
label2:
Deeply.Nested.InnerMember
)
"""

Expand All @@ -70,8 +73,15 @@ final class TupleDeclTests: PrettyPrintTestCase {
b: c
)
let a = (
shortKey: value,
b: c
shortKey:
value,
b:
c,
label: Deeply.Nested
.InnerMember,
label2:
Deeply.Nested
.InnerMember
)

"""
Expand Down
8 changes: 4 additions & 4 deletions Tests/SwiftFormatPrettyPrintTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ extension DictionaryDeclTests {
// to regenerate.
static let __allTests__DictionaryDeclTests = [
("testBasicDictionaries", testBasicDictionaries),
("testDiscretionaryNewlineAfterColon", testDiscretionaryNewlineAfterColon),
("testGroupsTrailingComma", testGroupsTrailingComma),
("testIgnoresDiscretionaryNewlineAfterColon", testIgnoresDiscretionaryNewlineAfterColon),
("testNoTrailingCommasInTypes", testNoTrailingCommasInTypes),
("testTrailingCommaDiagnostics", testTrailingCommaDiagnostics),
("testWhitespaceOnlyDoesNotChangeTrailingComma", testWhitespaceOnlyDoesNotChangeTrailingComma),
Expand Down Expand Up @@ -270,10 +270,10 @@ extension FunctionCallTests {
("testArgumentStartsWithOpenDelimiter", testArgumentStartsWithOpenDelimiter),
("testBasicFunctionCalls_noPackArguments", testBasicFunctionCalls_noPackArguments),
("testBasicFunctionCalls_packArguments", testBasicFunctionCalls_packArguments),
("testDiscretionaryLineBreakAfterColon", testDiscretionaryLineBreakAfterColon),
("testDiscretionaryLineBreakBeforeClosingParenthesis", testDiscretionaryLineBreakBeforeClosingParenthesis),
("testDiscretionaryLineBreaksAreSelfCorrecting", testDiscretionaryLineBreaksAreSelfCorrecting),
("testGroupsTrailingComma", testGroupsTrailingComma),
("testIgnoresDiscretionaryLineBreakAfterColon", testIgnoresDiscretionaryLineBreakAfterColon),
("testNestedFunctionCallExprSequences", testNestedFunctionCallExprSequences),
("testSingleUnlabeledArgumentWithDelimiters", testSingleUnlabeledArgumentWithDelimiters),
]
Expand All @@ -294,6 +294,7 @@ extension FunctionDeclTests {
("testBreaksBeforeOrInsideOutputWithAttributes_prioritizingKeepingOutputTogether", testBreaksBeforeOrInsideOutputWithAttributes_prioritizingKeepingOutputTogether),
("testBreaksBeforeOrInsideOutputWithWhereClause", testBreaksBeforeOrInsideOutputWithWhereClause),
("testBreaksBeforeOrInsideOutputWithWhereClause_prioritizingKeepingOutputTogether", testBreaksBeforeOrInsideOutputWithWhereClause_prioritizingKeepingOutputTogether),
("testDiscretionaryLineBreakAfterColonAndInout", testDiscretionaryLineBreakAfterColonAndInout),
("testDoesNotBreakInsideEmptyParens", testDoesNotBreakInsideEmptyParens),
("testDoesNotCollapseFunctionParameterAttributes", testDoesNotCollapseFunctionParameterAttributes),
("testDoesNotCollapseStackedFunctionParameterAttributes", testDoesNotCollapseStackedFunctionParameterAttributes),
Expand All @@ -308,7 +309,6 @@ extension FunctionDeclTests {
("testFunctionWhereClause", testFunctionWhereClause),
("testFunctionWhereClause_lineBreakBeforeEachGenericRequirement", testFunctionWhereClause_lineBreakBeforeEachGenericRequirement),
("testFunctionWithDefer", testFunctionWithDefer),
("testIgnoresDiscretionaryLineBreakAfterColonAndInout", testIgnoresDiscretionaryLineBreakAfterColonAndInout),
("testOperatorOverloads", testOperatorOverloads),
("testRemovesLineBreakBeforeOpenBraceUnlessAbsolutelyNecessary", testRemovesLineBreakBeforeOpenBraceUnlessAbsolutelyNecessary),
]
Expand Down Expand Up @@ -723,8 +723,8 @@ extension TupleDeclTests {
// to regenerate.
static let __allTests__TupleDeclTests = [
("testBasicTuples", testBasicTuples),
("testDiscretionaryNewlineAfterColon", testDiscretionaryNewlineAfterColon),
("testGroupsTrailingComma", testGroupsTrailingComma),
("testIgnoresDiscretionaryNewlineAfterColon", testIgnoresDiscretionaryNewlineAfterColon),
("testLabeledTuples", testLabeledTuples),
]
}
Expand Down