Fix indent regression in xcode indent mode

This commit is contained in:
Nick Lockwood
2022-08-24 23:17:44 +01:00
parent 9b0215b684
commit ca329a3a1c
3 changed files with 54 additions and 4 deletions
+22
View File
@@ -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)
}
}
+18 -4
View File
@@ -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),
+14
View File
@@ -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 =