@@ -59,29 +59,41 @@ public enum FixItApplier {
59
59
var edits = edits
60
60
var source = tree. description
61
61
62
- while let edit = edits. first {
63
- edits = Array ( edits. dropFirst ( ) )
62
+ for var editIndex in edits. indices {
63
+ let edit = edits [ editIndex ]
64
64
65
65
// Empty edits do nothing.
66
66
guard !edit. isEmpty else {
67
67
continue
68
68
}
69
69
70
- let startIndex = source. utf8. index ( source. utf8. startIndex, offsetBy: edit. startUtf8Offset)
71
- let endIndex = source. utf8. index ( source. utf8. startIndex, offsetBy: edit. endUtf8Offset)
70
+ do {
71
+ let startIndex = source. utf8. index ( source. utf8. startIndex, offsetBy: edit. startUtf8Offset)
72
+ let endIndex = source. utf8. index ( source. utf8. startIndex, offsetBy: edit. endUtf8Offset)
72
73
73
- source. replaceSubrange ( startIndex..< endIndex, with: edit. replacement)
74
+ source. replaceSubrange ( startIndex..< endIndex, with: edit. replacement)
75
+ }
74
76
75
77
// Drop any subsequent edits that conflict with one we just applied, and
76
78
// adjust the range of the rest.
77
- for i in edits. indices {
78
- let remainingEdit = edits [ i]
79
+ while true {
80
+ editIndex += 1
81
+ guard editIndex != edits. endIndex else {
82
+ break
83
+ }
84
+
85
+ let remainingEdit = edits [ editIndex]
86
+
87
+ // Empty edits do nothing.
88
+ guard !remainingEdit. isEmpty else {
89
+ continue
90
+ }
79
91
80
92
guard !remainingEdit. range. overlaps ( edit. range) else {
81
93
// The edit overlaps with the previous edit. We can't apply both
82
94
// without conflicts. Drop this one by swapping it for a no-op
83
95
// edit.
84
- edits [ i ] = SourceEdit ( )
96
+ edits [ editIndex ] = SourceEdit ( )
85
97
continue
86
98
}
87
99
@@ -92,7 +104,7 @@ public enum FixItApplier {
92
104
let startPosition = AbsolutePosition ( utf8Offset: remainingEdit. startUtf8Offset + shift)
93
105
let endPosition = AbsolutePosition ( utf8Offset: remainingEdit. endUtf8Offset + shift)
94
106
95
- edits [ i ] = SourceEdit ( range: startPosition..< endPosition, replacement: remainingEdit. replacement)
107
+ edits [ editIndex ] = SourceEdit ( range: startPosition..< endPosition, replacement: remainingEdit. replacement)
96
108
}
97
109
}
98
110
}
0 commit comments