Skip to content

Commit b9dd67d

Browse files
committed
Update rule to insert blank line in scope
1 parent 9f411b6 commit b9dd67d

3 files changed

Lines changed: 51 additions & 48 deletions

File tree

Sources/Formatter.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,4 +597,32 @@ public extension Formatter {
597597
}
598598
return .linebreak(options.linebreak, lineNumber)
599599
}
600+
601+
func addLinebreakAtStartOfScope(index i: Int, canInsertSpace: Bool) {
602+
guard canInsertSpace else { return }
603+
604+
guard let linebreakToken = self.token(at: i + 2),
605+
!linebreakToken.isLinebreak
606+
else { return }
607+
608+
guard let endOfScopeToken = self.token(at: i + 1),
609+
endOfScopeToken != .endOfScope("{")
610+
else { return }
611+
612+
self.insertLinebreak(at: i + 1)
613+
}
614+
615+
func addLineBreakToEndOfScope(index i: Int, canInsertSpace: Bool) {
616+
guard canInsertSpace,
617+
let endOfScopeTokenIndex = self.endOfScope(at: i)
618+
else { return }
619+
620+
guard let linebreakToken = self.token(at: endOfScopeTokenIndex - 2),
621+
!linebreakToken.isLinebreak
622+
else { return }
623+
624+
guard !self.onSameLine(i, endOfScopeTokenIndex) else { return }
625+
626+
self.insertLinebreak(at: endOfScopeTokenIndex - 1)
627+
}
600628
}

Sources/Rules.swift

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,35 +1043,11 @@ public struct _FormatRules {
10431043
canInsertSpaceAtStart = false
10441044

10451045
case .startOfScope("{"):
1046-
guard canInsertSpaceAtStart else { return }
1047-
startToken = token
1046+
formatter.addLinebreakAtStartOfScope(index: i, canInsertSpace: canInsertSpaceAtStart)
1047+
formatter.addLineBreakToEndOfScope(index: i, canInsertSpace: canInsertSpaceAtStart)
1048+
10481049
canInsertSpaceAtStart = false
10491050

1050-
guard let linebreakToken = formatter.token(at: i + 2),
1051-
!linebreakToken.isLinebreak
1052-
else { return }
1053-
1054-
guard let endOfScopeToken = formatter.token(at: i + 1),
1055-
endOfScopeToken != .endOfScope("{")
1056-
else { return }
1057-
1058-
formatter.insertLinebreak(at: i + 1)
1059-
1060-
case .endOfScope("}"):
1061-
guard let startToken = startToken,
1062-
token.isEndOfScope(startToken)
1063-
else { return }
1064-
1065-
guard let linebreakToken = formatter.token(at: i - 2),
1066-
!linebreakToken.isLinebreak
1067-
else { return }
1068-
1069-
guard let startOfScopeToken = formatter.token(at: i - 1),
1070-
startOfScopeToken != .startOfScope("{")
1071-
else { return }
1072-
1073-
formatter.insertLinebreak(at: i - 1)
1074-
10751051
default:
10761052
break
10771053
}

Tests/RulesTests+BlanklineInScope.swift

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,6 @@ import XCTest
1212
class BlanklineInScopeTests: XCTestCase {
1313
// MARK: - Space in start/end of scope
1414

15-
func testGuardLet() {
16-
let input = """
17-
func setNavigationBarHidden(_ isHidden: Bool, animated: Bool) {
18-
guard let navigationController = navigationController else { return }
19-
20-
navigationController.setNavigationBarHidden(isHidden, animated: animated)
21-
}
22-
"""
23-
24-
let output = """
25-
func setNavigationBarHidden(_ isHidden: Bool, animated: Bool) {
26-
guard let navigationController = navigationController else { return }
27-
28-
navigationController.setNavigationBarHidden(isHidden, animated: animated)
29-
}
30-
"""
31-
32-
let formattedInput = (try? format(input, rules: [FormatRules.insertBlankLinesAtScope])) ?? ""
33-
XCTAssertEqual(output, formattedInput)
34-
}
35-
3615
func testBlankLineAtStartOfScope() {
3716
let input = """
3817
struct Foo {
@@ -355,6 +334,26 @@ class BlanklineInScopeTests: XCTestCase {
355334
XCTAssertEqual(output, formattedInput)
356335
}
357336

337+
func testGuardLet() {
338+
let input = """
339+
func setNavigationBarHidden(_ isHidden: Bool, animated: Bool) {
340+
guard let navigationController = navigationController else { return }
341+
342+
navigationController.setNavigationBarHidden(isHidden, animated: animated)
343+
}
344+
"""
345+
346+
let output = """
347+
func setNavigationBarHidden(_ isHidden: Bool, animated: Bool) {
348+
guard let navigationController = navigationController else { return }
349+
350+
navigationController.setNavigationBarHidden(isHidden, animated: animated)
351+
}
352+
"""
353+
354+
let formattedInput = (try? format(input, rules: [FormatRules.insertBlankLinesAtScope])) ?? ""
355+
XCTAssertEqual(output, formattedInput)
356+
}
358357
// func testBlanklineAfterCallSuper() {
359358
// let input = """
360359
// override func foo() {

0 commit comments

Comments
 (0)