Base corrections on bytes to avoid range conversions (#6570)

This commit is contained in:
Danny Mösch
2026-04-07 11:23:58 +02:00
committed by GitHub
parent eac464e199
commit 0316649724
@@ -40,26 +40,26 @@ public extension SwiftSyntaxCorrectableRule {
.filter { $0.areRulesDisabled(ruleIDs: Self.description.allIdentifiers) }
.compactMap { $0.toSourceRange(locationConverter: locationConverter) }
typealias CorrectionRange = (range: NSRange, correction: String)
let correctionRanges = violations
.filter { !$0.position.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) }
.compactMap(\.correction)
.compactMap { correction in
file.stringView.NSRange(start: correction.start, end: correction.end).map { range in
CorrectionRange(range: range, correction: correction.replacement)
}
}
.sorted { (lhs: CorrectionRange, rhs: CorrectionRange) -> Bool in
lhs.range.location > rhs.range.location
(
startByte: correction.start.utf8Offset,
endByte: correction.end.utf8Offset,
replacementBytes: correction.replacement.utf8)
}
.sorted { $0.startByte > $1.startByte }
guard correctionRanges.isNotEmpty else {
return 0
}
var contents = file.contents
var bytes = Array(file.contents.utf8)
for range in correctionRanges {
let contentsNSString = contents.bridge()
contents = contentsNSString.replacingCharacters(in: range.range, with: range.correction)
bytes.replaceSubrange(range.startByte..<range.endByte, with: range.replacementBytes)
}
guard let contents = String(bytes: bytes, encoding: .utf8) else {
return 0
}
file.write(contents)
return correctionRanges.count