Skip to content

Commit db55127

Browse files
committed
Avoid reporting false-positive changes
1 parent 4b700ca commit db55127

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

Sources/SwiftFormat.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,19 @@ private func applyRules(
519519
}
520520
}
521521

522+
// Split tokens into lines
523+
func lines(in tokens: [Token]) -> [Int: ArraySlice<Token>?] {
524+
var lines: [Int: ArraySlice<Token>?] = [:]
525+
var start = 0
526+
for (i, token) in tokens.enumerated() {
527+
if case let .linebreak(_, line) = token {
528+
lines[line] = tokens[start ..< i]
529+
start = i + 1
530+
}
531+
}
532+
return lines
533+
}
534+
522535
// Recursively apply rules until no changes are detected
523536
let group = DispatchGroup()
524537
let queue = DispatchQueue(label: "swiftformat.formatting", qos: .userInteractive)
@@ -541,20 +554,29 @@ private func applyRules(
541554
}
542555
changes += formatter.changes
543556
if tokens == formatter.tokens {
557+
if changes.isEmpty {
558+
return (tokens, [])
559+
}
544560
// Sort changes
545561
changes.sort(by: {
546562
if $0.line == $1.line {
547563
return $0.rule.name < $1.rule.name
548564
}
549565
return $0.line < $1.line
550566
})
551-
// Filter out duplicates
567+
// Get lines
568+
let oldLines = lines(in: originalTokens)
569+
let newLines = lines(in: tokens)
570+
// Filter out duplicates and lines that haven't changed
552571
var last: Formatter.Change?
553572
return (tokens, changes.filter { change in
554573
if last == change {
555574
return false
556575
}
557576
last = change
577+
if newLines[change.line] == oldLines[change.line] {
578+
return false
579+
}
558580
return true
559581
})
560582
}

0 commit comments

Comments
 (0)