@@ -59,29 +59,37 @@ 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 utf8 = source. utf8
72
+ let startIndex = utf8. index ( utf8. startIndex, offsetBy: edit. startUtf8Offset)
73
+ let endIndex = utf8. index ( utf8. startIndex, offsetBy: edit. endUtf8Offset)
72
74
73
- source. replaceSubrange ( startIndex..< endIndex, with: edit. replacement)
75
+ source. replaceSubrange ( startIndex..< endIndex, with: edit. replacement)
76
+ }
74
77
75
78
// Drop any subsequent edits that conflict with one we just applied, and
76
79
// adjust the range of the rest.
77
- for i in edits. indices {
78
- let remainingEdit = edits [ i]
80
+ while edits. formIndex ( after: & editIndex) != edits. endIndex {
81
+ let remainingEdit = edits [ editIndex]
82
+
83
+ // Empty edits do nothing.
84
+ guard !remainingEdit. isEmpty else {
85
+ continue
86
+ }
79
87
80
88
guard !remainingEdit. range. overlaps ( edit. range) else {
81
89
// The edit overlaps with the previous edit. We can't apply both
82
90
// without conflicts. Drop this one by swapping it for a no-op
83
91
// edit.
84
- edits [ i ] = SourceEdit ( )
92
+ edits [ editIndex ] = SourceEdit ( )
85
93
continue
86
94
}
87
95
@@ -92,7 +100,7 @@ public enum FixItApplier {
92
100
let startPosition = AbsolutePosition ( utf8Offset: remainingEdit. startUtf8Offset + shift)
93
101
let endPosition = AbsolutePosition ( utf8Offset: remainingEdit. endUtf8Offset + shift)
94
102
95
- edits [ i ] = SourceEdit ( range: startPosition..< endPosition, replacement: remainingEdit. replacement)
103
+ edits [ editIndex ] = SourceEdit ( range: startPosition..< endPosition, replacement: remainingEdit. replacement)
96
104
}
97
105
}
98
106
}
@@ -101,6 +109,13 @@ public enum FixItApplier {
101
109
}
102
110
}
103
111
112
+ private extension Collection {
113
+ func formIndex( after index: inout Index ) -> Index {
114
+ self . formIndex ( after: & index) as Void
115
+ return index
116
+ }
117
+ }
118
+
104
119
private extension SourceEdit {
105
120
var startUtf8Offset : Int {
106
121
return range. lowerBound. utf8Offset
0 commit comments