Skip to content

Commit

Permalink
Fix indent after wrapped closure in
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Jul 13, 2022
1 parent 0123354 commit a51a305
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
20 changes: 9 additions & 11 deletions Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1407,18 +1407,16 @@ public struct _FormatRules {
let stringIndent = stringBodyIndentStack.last!
i += formatter.insertSpaceIfEnabled(stringIndent + indent, at: start)
case .keyword("in") where scopeStack.last == .startOfScope("{"):
guard let startIndex = formatter.index(of: .startOfScope, before: i),
!formatter.tokens[startIndex ..< i].contains(.keyword("for")),
let scopeEnd = formatter.lastIndex(in: startIndex ..< i, where: {
[.endOfScope(")"), .endOfScope("]")].contains($0)
}),
formatter.tokens[startIndex ..< scopeEnd].contains(where: {
if case .linebreak = $0 { return true } else { return false }
})
else {
break
if let startIndex = formatter.index(of: .startOfScope("{"), before: i),
let paramsIndex = formatter.index(of: .startOfScope, in: startIndex + 1 ..< i),
!formatter.tokens[startIndex + 1 ..< paramsIndex].contains(where: {
$0.isLinebreak
}), formatter.tokens[paramsIndex + 1 ..< i].contains(where: {
$0.isLinebreak
})
{
indentStack[indentStack.count - 1] += formatter.options.indent
}
indentStack[indentStack.count - 1] += formatter.options.indent
default:
// Handle end of scope
if let scope = scopeStack.last, token.isEndOfScope(scope) {
Expand Down
20 changes: 20 additions & 0 deletions Tests/RulesTests+Indentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,26 @@ class IndentTests: RulesTests {
testFormatting(for: input, rule: FormatRules.indent)
}

func testNoDoubleIndentInInsideClosure3() {
let input = """
foo {
[weak self] _ in
self?.bar()
}
"""
testFormatting(for: input, rule: FormatRules.indent)
}

func testNoDoubleIndentInInsideClosure4() {
let input = """
foo {
(baz: Int) in
self?.bar(baz)
}
"""
testFormatting(for: input, rule: FormatRules.indent)
}

func testNoUnindentTrailingClosure() {
let input = """
private final class Foo {
Expand Down

0 comments on commit a51a305

Please sign in to comment.