From ca329a3a1c5ed14a57c49195940a04cb9008aee0 Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Wed, 24 Aug 2022 23:17:44 +0100 Subject: [PATCH] Fix indent regression in xcode indent mode --- Snapshots/Issues/1254.swift | 22 ++++++++++++++++++++++ Sources/Rules.swift | 22 ++++++++++++++++++---- Tests/RulesTests+Indentation.swift | 14 ++++++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 Snapshots/Issues/1254.swift diff --git a/Snapshots/Issues/1254.swift b/Snapshots/Issues/1254.swift new file mode 100644 index 00000000..67dd171c --- /dev/null +++ b/Snapshots/Issues/1254.swift @@ -0,0 +1,22 @@ +// swiftformat:disable blankLinesBetweenScopes +// swiftformat:disable wrapMultilineStatementBraces +// swiftformat:options --commas false +// swiftformat:options --xcodeindentation enabled +// swiftformat:options --extensionacl on-declarations +// swiftformat:options --modifierorder public,override +// swiftformat:options --patternlet inline +// swiftformat:options --trimwhitespace nonblank-lines + +func testing() { + firstly { + doSomething() + } + // then do something else + .then { + doSomethingElse() + } + // convert the thing + .map { + transform($0) + } +} diff --git a/Sources/Rules.swift b/Sources/Rules.swift index c4d47287..a671ad7f 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -1635,10 +1635,24 @@ public struct _FormatRules { linewrapStack[linewrapStack.count - 1] = false indent = indentStack.last! } else { - let shouldIndentLeadingDotStatement = formatter.options.xcodeIndentation - || (formatter.startOfConditionalStatement(at: i) != nil - && formatter.options.wrapConditions == .beforeFirst) - + let shouldIndentLeadingDotStatement: Bool + if formatter.options.xcodeIndentation { + if let prevIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, before: i), + formatter.token(at: formatter.startOfLine( + at: prevIndex, excludingIndent: true + )) == .endOfScope("}"), + formatter.index(of: .linebreak, in: prevIndex + 1 ..< i) != nil + { + shouldIndentLeadingDotStatement = false + } else { + shouldIndentLeadingDotStatement = true + } + } else { + shouldIndentLeadingDotStatement = ( + formatter.startOfConditionalStatement(at: i) != nil + && formatter.options.wrapConditions == .beforeFirst + ) + } if shouldIndentLeadingDotStatement, formatter.next(.nonSpace, after: i) == .operator(".", .infix), let prevIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, before: i), diff --git a/Tests/RulesTests+Indentation.swift b/Tests/RulesTests+Indentation.swift index 71f0283f..2f7c80d5 100644 --- a/Tests/RulesTests+Indentation.swift +++ b/Tests/RulesTests+Indentation.swift @@ -1790,6 +1790,20 @@ class IndentTests: RulesTests { testFormatting(for: input, rule: FormatRules.indent, options: options) } + func testCommentSeparatedChainedFunctionAfterBraceWithXcodeIndentation() { + let input = """ + func foo() { + bar { + doSomething() + } + // baz + .baz() + } + """ + let options = FormatOptions(xcodeIndentation: true) + testFormatting(for: input, rule: FormatRules.indent, options: options) + } + func testChainedFunctionsInPropertySetterOnNewLineWithXcodeIndentation() { let input = """ private let foo =