@@ -50,10 +50,9 @@ public final class IncrementalParseTransition {
50
50
fileprivate var edits : ConcurrentEdits ?
51
51
fileprivate var reusedDelegate : IncrementalParseReusedNodeDelegate ?
52
52
53
- fileprivate var previousLookaheadRange : [ Int : Int ] = [ : ]
54
- /// Keep track of how far we would look when calling ``Lookahead``
55
- /// Key is offset to buffer start and value is the length of we lookahead
56
- fileprivate var cursorLookaheadRange : [ Int : Int ] = [ : ]
53
+ public var furthestOffset : Int ?
54
+
55
+ fileprivate var lookaheadBytes : [ UnsafeRawPointer : Int ] = [ : ]
57
56
58
57
/// - Parameters:
59
58
/// - previousTree: The previous tree to do lookups on.
@@ -75,12 +74,15 @@ public final class IncrementalParseTransition {
75
74
self . previousTree = tree
76
75
self . edits = edits
77
76
self . reusedDelegate = delegate
78
- self . previousLookaheadRange = cursorLookaheadRange
79
- self . cursorLookaheadRange = [ : ]
80
77
}
81
-
82
- public func registerAffectRange( at offset: Int , length: Int ) {
83
- self . cursorLookaheadRange [ offset] = length
78
+
79
+ @_spi ( RawSyntax)
80
+ public func registerNodeForIncrementalParse( offset: Int , node: RawSyntax ) {
81
+
82
+ guard let furthestOffset,
83
+ furthestOffset > offset else { return }
84
+
85
+ self . lookaheadBytes [ node. id] = furthestOffset - offset
84
86
}
85
87
86
88
public func isValidTransition( ) -> Bool {
@@ -106,6 +108,10 @@ public struct IncrementalParseLookup {
106
108
fileprivate var reusedDelegate : IncrementalParseReusedNodeDelegate ? {
107
109
return transition. reusedDelegate
108
110
}
111
+
112
+ fileprivate var lookaheadBytes : [ UnsafeRawPointer : Int ] {
113
+ return transition. lookaheadBytes
114
+ }
109
115
110
116
/// Does a lookup to see if the current source `offset` should be associated
111
117
/// with a known ``Syntax`` node and its region skipped during parsing.
@@ -175,35 +181,14 @@ public struct IncrementalParseLookup {
175
181
return true
176
182
}
177
183
178
- // Node can also not be reused if an edit has been made in the next token's
179
- // text, e.g. because `private struct Foo {}` parses as a CodeBlockItem with
180
- // a StructDecl inside and `private struc Foo {}` parses as two
181
- // CodeBlockItems one for `private` and one for `struc Foo {}`
182
- var nextLeafNodeLength : SourceLength = . zero
183
- if let nextSibling = cursor. nextSibling {
184
- // Fast path check: if next sibling is before all the edits then we can
185
- // re-use the node.
186
- if !edits. edits. isEmpty && edits. edits. first!. range. offset > nextSibling. endPosition. utf8Offset {
187
- return true
188
- }
189
- if let nextToken = nextSibling. firstToken ( viewMode: . sourceAccurate) {
190
- nextLeafNodeLength = nextToken. leadingTriviaLength + nextToken. contentLength
191
- }
192
- }
193
-
194
- var underlyingRaw = node. raw
195
- if let codeBlockItem = underlyingRaw. as ( RawCodeBlockItemSyntax . self) {
196
- underlyingRaw = codeBlockItem. item
184
+
185
+ guard let lookaheadRange = self . lookaheadBytes [ node. raw. id] else {
186
+ return false
197
187
}
198
188
199
- let nodeAffectLength =
200
- underlyingRaw. isSelfCompleted
201
- ? ( node. totalLength + nextLeafNodeLength) . utf8Length
202
- : max ( mergeLookaheadRange ( at: node. position. utf8Offset, length: node. totalLength. utf8Length) , ( node. totalLength + nextLeafNodeLength) . utf8Length)
203
-
204
189
let nodeAffectRange = ByteSourceRange (
205
190
offset: node. position. utf8Offset,
206
- length: nodeAffectLength
191
+ length: max ( lookaheadRange , node . byteSize )
207
192
)
208
193
209
194
for edit in edits. edits {
@@ -239,21 +224,6 @@ public struct IncrementalParseLookup {
239
224
}
240
225
return offset
241
226
}
242
-
243
- fileprivate func mergeLookaheadRange( at start: Int , length: Int ) -> Int {
244
- var totalLength = start + length
245
-
246
- let targetRanges = transition. previousLookaheadRange. filter { $0. key >= totalLength } . sorted ( by: { $0. key < $1. key } )
247
-
248
- for targetRange in targetRanges {
249
- if targetRange. key != totalLength {
250
- break
251
- }
252
- totalLength += targetRange. value
253
- }
254
-
255
- return totalLength
256
- }
257
227
}
258
228
259
229
/// Functions as an iterator that walks the tree looking for nodes with a
0 commit comments