diff --git a/Sources/Rules.swift b/Sources/Rules.swift index 917cc8e3f..157748268 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -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) { diff --git a/Tests/RulesTests+Indentation.swift b/Tests/RulesTests+Indentation.swift index eb2f1b962..9c4b0923c 100644 --- a/Tests/RulesTests+Indentation.swift +++ b/Tests/RulesTests+Indentation.swift @@ -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 {