Skip to content

Commit 61d7c47

Browse files
committed
Fix indent logic for closing braces
1 parent 4e2b8e9 commit 61d7c47

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

Sources/Rules.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,11 +1113,11 @@ public struct _FormatRules {
11131113
stringBodyIndentStack.removeLast()
11141114
stringBodyIndentStack.append(stringBodyIndentStack.last ?? "")
11151115
}
1116-
// Check if line on which scope ends should be unindented
1116+
// Don't reduce indent if line doesn't start with end of scope
1117+
// or starts with a multiline string delimiter
11171118
let start = formatter.startOfLine(at: i)
1118-
guard !formatter.isCommentedCode(at: start),
1119-
// Don't reduce indent if end of scope is not first token in line
1120-
formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: start - 1) == i else {
1119+
guard let firstToken = formatter.next(.nonSpaceOrComment, after: start - 1),
1120+
firstToken.isEndOfScope, !firstToken.isMultilineStringDelimiter else {
11211121
break
11221122
}
11231123
// Reduce indent for closing scope of guard else back to normal

Tests/RulesTests.swift

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,7 @@ class RulesTests: XCTestCase {
15011501
Foo(bar:
15021502
Bar(
15031503
baz: quux
1504-
))
1504+
))
15051505
"""
15061506
testFormatting(for: input, rule: FormatRules.indent)
15071507
}
@@ -1551,6 +1551,20 @@ class RulesTests: XCTestCase {
15511551
testFormatting(for: input, output, rule: FormatRules.indent, exclude: ["redundantParens"])
15521552
}
15531553

1554+
func testUnindentClosingParenAroundBraces() {
1555+
let input = """
1556+
foo(success: {
1557+
self.bar()
1558+
})
1559+
"""
1560+
let output = """
1561+
foo(success: {
1562+
self.bar()
1563+
})
1564+
"""
1565+
testFormatting(for: input, output, rule: FormatRules.indent)
1566+
}
1567+
15541568
// indent switch/case
15551569

15561570
func testSwitchCaseIndenting() {
@@ -1899,8 +1913,18 @@ class RulesTests: XCTestCase {
18991913
}
19001914

19011915
func testDoubleIndentWhenScopesSeparatedByWrap() {
1902-
let input = "(foo\nas Bar {\nbaz\n})"
1903-
let output = "(foo\n as Bar {\n baz\n})"
1916+
let input = """
1917+
(foo
1918+
as Bar {
1919+
baz
1920+
})
1921+
"""
1922+
let output = """
1923+
(foo
1924+
as Bar {
1925+
baz
1926+
})
1927+
"""
19041928
testFormatting(for: input, output, rule: FormatRules.indent, exclude: ["redundantParens"])
19051929
}
19061930

@@ -2527,12 +2551,21 @@ class RulesTests: XCTestCase {
25272551
}
25282552

25292553
func testIndentMultilineStringWrappedAfter() {
2530-
let input = "foo(baz:\n \"\"\"\n baz\n \"\"\")"
2554+
let input = """
2555+
foo(baz:
2556+
\"""
2557+
baz
2558+
\""")
2559+
"""
25312560
testFormatting(for: input, rule: FormatRules.indent)
25322561
}
25332562

25342563
func testIndentMultilineStringInNestedCalls() {
2535-
let input = "foo(bar(\"\"\"\nbaz\n\"\"\"))"
2564+
let input = """
2565+
foo(bar(\"""
2566+
baz
2567+
\"""))
2568+
"""
25362569
testFormatting(for: input, rule: FormatRules.indent)
25372570
}
25382571

Tests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,7 @@ extension RulesTests {
17211721
("testUnhoistIfArgIsUnderscore", testUnhoistIfArgIsUnderscore),
17221722
("testUnhoistLabelledCaseLet", testUnhoistLabelledCaseLet),
17231723
("testUnhoistSingleCaseLet", testUnhoistSingleCaseLet),
1724+
("testUnindentClosingParenAroundBraces", testUnindentClosingParenAroundBraces),
17241725
("testUnterminatedFragment", testUnterminatedFragment),
17251726
("testUnusedInoutClosureArgumentsNotMangled", testUnusedInoutClosureArgumentsNotMangled),
17261727
("testUnusedInoutFunctionArgumentIsNotMangled", testUnusedInoutFunctionArgumentIsNotMangled),

0 commit comments

Comments
 (0)