Skip to content

Commit

Permalink
Support comments when removing spaces between chained functions (#1723)
Browse files Browse the repository at this point in the history
  • Loading branch information
mannylopez authored and nicklockwood committed Jun 12, 2024
1 parent e895b0e commit 41dec1d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
10 changes: 6 additions & 4 deletions Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1188,10 +1188,12 @@ public struct _FormatRules {
) { formatter in
formatter.forEach(.operator(".", .infix)) { i, _ in
let endOfLine = formatter.endOfLine(at: i)
if let nextIndex = formatter.index(of: .nonSpaceOrLinebreak, after: endOfLine, if: {
$0 == .operator(".", .infix)
}) {
let startOfLine = formatter.startOfLine(at: nextIndex)
if let nextIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: endOfLine),
formatter.tokens[nextIndex] == .operator(".", .infix),
// Make sure to preserve any code comment between the two lines
let nextTokenOrComment = formatter.index(of: .nonSpaceOrLinebreak, after: endOfLine)
{
let startOfLine = formatter.startOfLine(at: nextTokenOrComment)
formatter.removeTokens(in: endOfLine + 1 ..< startOfLine)
}
}
Expand Down
17 changes: 17 additions & 0 deletions Tests/RulesTests+Linebreaks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,23 @@ class LinebreakTests: RulesTests {
testFormatting(for: input, [output1, output2], rules: [FormatRules.blankLinesBetweenChainedFunctions])
}

func testBlankLinesWithCommentsBetweenChainedFunctions() {
let input = """
[0, 1, 2]
.map { $0 * 2 }
// Multiplies by 3
.map { $0 * 3 }
"""
let output = """
[0, 1, 2]
.map { $0 * 2 }
// Multiplies by 3
.map { $0 * 3 }
"""
testFormatting(for: input, output, rule: FormatRules.blankLinesBetweenChainedFunctions)
}

// MARK: - blankLineAfterImports

func testBlankLineAfterImport() {
Expand Down

0 comments on commit 41dec1d

Please sign in to comment.