@@ -162,7 +162,7 @@ struct IncrementalParseLookup {
162
162
163
163
// Fast path check: if parser is past all the edits then any matching node
164
164
// can be re-used.
165
- if !edits. edits. isEmpty && edits. edits. last!. range. endOffset < node. position. utf8Offset {
165
+ if !edits. edits. isEmpty && edits. edits. last!. range. upperBound . utf8Offset < node. position. utf8Offset {
166
166
return true
167
167
}
168
168
@@ -172,15 +172,15 @@ struct IncrementalParseLookup {
172
172
return false
173
173
}
174
174
175
- let nodeAffectRange = ByteSourceRange (
176
- offset : node . position . utf8Offset ,
177
- length : nodeAffectRangeLength
178
- )
175
+ let nodeAffectRange =
176
+ AbsolutePosition (
177
+ utf8Offset : node . position . utf8Offset
178
+ ) ..< AbsolutePosition ( utf8Offset : node . position . utf8Offset + nodeAffectRangeLength )
179
179
180
180
for edit in edits. edits {
181
181
// Check if this node or the trivia of the next node has been edited. If
182
182
// it has, we cannot reuse it.
183
- if edit. range. offset > nodeAffectRange. endOffset {
183
+ if edit. range. lowerBound . utf8Offset > nodeAffectRange. upperBound . utf8Offset {
184
184
// Remaining edits don't affect the node. (Edits are sorted)
185
185
break
186
186
}
@@ -195,16 +195,16 @@ struct IncrementalParseLookup {
195
195
fileprivate func translateToPreEditOffset( _ postEditOffset: Int ) -> Int ? {
196
196
var offset = postEditOffset
197
197
for edit in edits. edits {
198
- if edit. range. offset > offset {
198
+ if edit. range. lowerBound . utf8Offset > offset {
199
199
// Remaining edits doesn't affect the position. (Edits are sorted)
200
200
break
201
201
}
202
- if edit. range. offset + edit. replacementLength > offset {
202
+ if edit. range. lowerBound . utf8Offset + edit. replacementLength > offset {
203
203
// This is a position inserted by the edit, and thus doesn't exist in
204
204
// the pre-edit version of the file.
205
205
return nil
206
206
}
207
- offset = offset - edit. replacementLength + edit. range. length
207
+ offset = offset - edit. replacementLength + edit. range. length. utf8Length
208
208
}
209
209
return offset
210
210
}
@@ -303,11 +303,11 @@ public struct ConcurrentEdits: Sendable {
303
303
304
304
/// The raw concurrent edits. Are guaranteed to satisfy the requirements
305
305
/// stated above.
306
- public let edits : [ IncrementalEdit ]
306
+ public let edits : [ SourceEdit ]
307
307
308
308
/// Initialize this struct from edits that are already in a concurrent form
309
309
/// and are guaranteed to satisfy the requirements posed above.
310
- public init ( concurrent: [ IncrementalEdit ] ) throws {
310
+ public init ( concurrent: [ SourceEdit ] ) throws {
311
311
if !Self. isValidConcurrentEditArray ( concurrent) {
312
312
throw ConcurrentEditsError . editsNotConcurrent
313
313
}
@@ -323,7 +323,7 @@ public struct ConcurrentEdits: Sendable {
323
323
/// - insert 'z' at offset 2
324
324
/// to '012345' results in 'xyz012345'.
325
325
326
- public init ( fromSequential sequentialEdits: [ IncrementalEdit ] ) {
326
+ public init ( fromSequential sequentialEdits: [ SourceEdit ] ) {
327
327
do {
328
328
try self . init ( concurrent: Self . translateSequentialEditsToConcurrentEdits ( sequentialEdits) )
329
329
} catch {
@@ -336,7 +336,7 @@ public struct ConcurrentEdits: Sendable {
336
336
/// Construct a concurrent edits struct from a single edit. For a single edit,
337
337
/// there is no differentiation between being it being applied concurrently
338
338
/// or sequentially.
339
- public init ( _ single: IncrementalEdit ) {
339
+ public init ( _ single: SourceEdit ) {
340
340
do {
341
341
try self . init ( concurrent: [ single] )
342
342
} catch {
@@ -345,29 +345,31 @@ public struct ConcurrentEdits: Sendable {
345
345
}
346
346
347
347
private static func translateSequentialEditsToConcurrentEdits(
348
- _ edits: [ IncrementalEdit ]
349
- ) -> [ IncrementalEdit ] {
350
- var concurrentEdits : [ IncrementalEdit ] = [ ]
348
+ _ edits: [ SourceEdit ]
349
+ ) -> [ SourceEdit ] {
350
+ var concurrentEdits : [ SourceEdit ] = [ ]
351
351
for editToAdd in edits {
352
352
var editToAdd = editToAdd
353
353
var editIndicesMergedWithNewEdit : [ Int ] = [ ]
354
354
for (index, existingEdit) in concurrentEdits. enumerated ( ) {
355
- if existingEdit. replacementRange . intersectsOrTouches ( editToAdd. range) {
356
- let intersectionLength =
357
- existingEdit . replacementRange . intersected ( editToAdd . range ) . length
355
+ if existingEdit. range . intersectsOrTouches ( editToAdd. range) ,
356
+ let intersectionLength = existingEdit . range . intersecting ( editToAdd . range ) ? . length
357
+ {
358
358
let replacement : [ UInt8 ]
359
359
replacement =
360
- existingEdit. replacement. prefix ( max ( 0 , editToAdd. offset - existingEdit. replacementRange. offset) )
361
- + editToAdd. replacement
362
- + existingEdit. replacement. suffix ( max ( 0 , existingEdit. replacementRange. endOffset - editToAdd. endOffset) )
363
- editToAdd = IncrementalEdit (
360
+ existingEdit. replacementBytes. prefix ( max ( 0 , editToAdd. offset - existingEdit. replacementRange. offset) )
361
+ + editToAdd. replacementBytes
362
+ + existingEdit. replacementBytes. suffix (
363
+ max ( 0 , existingEdit. replacementRange. endOffset - editToAdd. endOffset)
364
+ )
365
+ editToAdd = SourceEdit (
364
366
offset: Swift . min ( existingEdit. offset, editToAdd. offset) ,
365
- length: existingEdit. length + editToAdd. length - intersectionLength,
367
+ length: existingEdit. length + editToAdd. length - intersectionLength. utf8Length ,
366
368
replacement: replacement
367
369
)
368
370
editIndicesMergedWithNewEdit. append ( index)
369
- } else if existingEdit. offset < editToAdd. endOffset {
370
- editToAdd = IncrementalEdit (
371
+ } else if existingEdit. range . lowerBound . utf8Offset < editToAdd. range . upperBound . utf8Offset {
372
+ editToAdd = SourceEdit (
371
373
offset: editToAdd. offset - existingEdit. replacementLength + existingEdit. length,
372
374
length: editToAdd. length,
373
375
replacement: editToAdd. replacement
@@ -380,15 +382,15 @@ public struct ConcurrentEdits: Sendable {
380
382
}
381
383
let insertPos =
382
384
concurrentEdits. firstIndex ( where: { edit in
383
- editToAdd. endOffset <= edit. offset
385
+ editToAdd. range . upperBound . utf8Offset <= edit. range . lowerBound . utf8Offset
384
386
} ) ?? concurrentEdits. count
385
387
concurrentEdits. insert ( editToAdd, at: insertPos)
386
388
precondition ( ConcurrentEdits . isValidConcurrentEditArray ( concurrentEdits) )
387
389
}
388
390
return concurrentEdits
389
391
}
390
392
391
- private static func isValidConcurrentEditArray( _ edits: [ IncrementalEdit ] ) -> Bool {
393
+ private static func isValidConcurrentEditArray( _ edits: [ SourceEdit ] ) -> Bool {
392
394
// Not quite sure if we should disallow creating an `IncrementalParseTransition`
393
395
// object without edits but there doesn't seem to be much benefit if we do,
394
396
// and there are 'lit' tests that want to test incremental re-parsing without edits.
@@ -397,7 +399,7 @@ public struct ConcurrentEdits: Sendable {
397
399
for i in 1 ..< edits. count {
398
400
let prevEdit = edits [ i - 1 ]
399
401
let curEdit = edits [ i]
400
- if curEdit. range. offset < prevEdit. range. endOffset {
402
+ if curEdit. range. lowerBound . utf8Offset < prevEdit. range. upperBound . utf8Offset {
401
403
return false
402
404
}
403
405
if curEdit. intersectsRange ( prevEdit. range) {
@@ -408,7 +410,7 @@ public struct ConcurrentEdits: Sendable {
408
410
}
409
411
410
412
/// **Public for testing purposes only**
411
- public static func _isValidConcurrentEditArray( _ edits: [ IncrementalEdit ] ) -> Bool {
413
+ public static func _isValidConcurrentEditArray( _ edits: [ SourceEdit ] ) -> Bool {
412
414
return isValidConcurrentEditArray ( edits)
413
415
}
414
416
}
0 commit comments