Skip to content

Commit

Permalink
Merge pull request #21 from allevato/sr-11169
Browse files Browse the repository at this point in the history
Improve method chaining, especially involving trailing closures.
  • Loading branch information
allevato committed Jul 24, 2019
2 parents 0654d1a + 53ae01e commit 79bb590
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
19 changes: 15 additions & 4 deletions Sources/SwiftFormatPrettyPrint/PrettyPrint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ public class PrettyPrinter {
switch kind {
case .open:
continuationStack.append(currentLineIsContinuation)
isContinuationIfBreakFires = false
openDelimiterBreakStack.append(lineNumber)

// If an open break occurs on a continuation line, we must push that continuation
Expand All @@ -222,16 +221,22 @@ public class PrettyPrinter {
}

indentationStack.append(configuration.indentation)

// Once we've reached an open break and preserved the continuation state, the "scope" we now
// enter is *not* a continuation scope. If it was one, we'll re-enter it when we reach the
// corresponding close.
currentLineIsContinuation = false

case .close(let closeMustBreak):
guard let matchingOpenLineNumber = openDelimiterBreakStack.popLast() else {
fatalError("Unmatched closing break")
}

indentationStack.removeLast()
isContinuationIfBreakFires = continuationStack.popLast() ?? false
let wasContinuationWhenOpened = continuationStack.popLast() ?? false

// Un-persist any continuation indentation that may have applied to the open/close scope.
if isContinuationIfBreakFires {
if wasContinuationWhenOpened {
indentationStack.removeLast()
}

Expand Down Expand Up @@ -268,12 +273,18 @@ public class PrettyPrinter {
//
// Likewise, we need to do this if we popped an old continuation state off the stack,
// even if the break *doesn't* fire.
currentLineIsContinuation = isContinuationIfBreakFires || isDifferentLine
currentLineIsContinuation = isDifferentLine
}

// Restore the continuation state of the scope we were in before the open break occurred.
currentLineIsContinuation = currentLineIsContinuation || wasContinuationWhenOpened

case .continue:
isContinuationIfBreakFires = true

case .same:
break

case .reset:
mustBreak = currentLineIsContinuation
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ private final class TokenStreamCreator: SyntaxVisitor {
before(
node.rightParen, tokens: .break(.close(mustBreak: breakBeforeRightParen), size: 0), .close)
}
before(node.trailingClosure?.leftBrace, tokens: .break(.reset))
before(node.trailingClosure?.leftBrace, tokens: .break(.same))
return .visitChildren
}

Expand Down
38 changes: 38 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/MemberAccessExprTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,42 @@ public class MemberAccessExprTests: PrettyPrintTestCase {

assertPrettyPrintEqual(input: input, expected: expected, linelength: 15)
}

public func testMethodChainingWithClosures() {
let input =
"""
let result = [1, 2, 3, 4, 5]
.filter{$0 % 2 == 0}
.map{$0 * $0}
"""

let expected =
"""
let result = [1, 2, 3, 4, 5]
.filter { $0 % 2 == 0 }
.map { $0 * $0 }
"""

assertPrettyPrintEqual(input: input, expected: expected, linelength: 30)
}

public func testMethodChainingWithClosuresFullWrap() {
let input =
"""
let result = [1, 2, 3, 4, 5].filter { $0 % 2 == 0 }.map { $0 * $0 }
"""

let expected =
"""
let result = [
1, 2, 3, 4, 5
].filter {
$0 % 2 == 0
}.map { $0 * $0 }
"""

assertPrettyPrintEqual(input: input, expected: expected, linelength: 20)
}
}
2 changes: 2 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ extension MemberAccessExprTests {
static let __allTests__MemberAccessExprTests = [
("testImplicitMemberAccess", testImplicitMemberAccess),
("testMemberAccess", testMemberAccess),
("testMethodChainingWithClosures", testMethodChainingWithClosures),
("testMethodChainingWithClosuresFullWrap", testMethodChainingWithClosuresFullWrap),
]
}

Expand Down

0 comments on commit 79bb590

Please sign in to comment.