@@ -50,10 +50,9 @@ public final class IncrementalParseTransition {
5050 fileprivate var edits : ConcurrentEdits ?
5151 fileprivate var reusedDelegate : IncrementalParseReusedNodeDelegate ?
5252
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 ] = [ : ]
5756
5857 /// - Parameters:
5958 /// - previousTree: The previous tree to do lookups on.
@@ -75,12 +74,15 @@ public final class IncrementalParseTransition {
7574 self . previousTree = tree
7675 self . edits = edits
7776 self . reusedDelegate = delegate
78- self . previousLookaheadRange = cursorLookaheadRange
79- self . cursorLookaheadRange = [ : ]
8077 }
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
8486 }
8587
8688 public func isValidTransition( ) -> Bool {
@@ -106,6 +108,10 @@ public struct IncrementalParseLookup {
106108 fileprivate var reusedDelegate : IncrementalParseReusedNodeDelegate ? {
107109 return transition. reusedDelegate
108110 }
111+
112+ fileprivate var lookaheadBytes : [ UnsafeRawPointer : Int ] {
113+ return transition. lookaheadBytes
114+ }
109115
110116 /// Does a lookup to see if the current source `offset` should be associated
111117 /// with a known ``Syntax`` node and its region skipped during parsing.
@@ -175,35 +181,14 @@ public struct IncrementalParseLookup {
175181 return true
176182 }
177183
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
197187 }
198188
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-
204189 let nodeAffectRange = ByteSourceRange (
205190 offset: node. position. utf8Offset,
206- length: nodeAffectLength
191+ length: max ( lookaheadRange , node . byteSize )
207192 )
208193
209194 for edit in edits. edits {
@@ -239,21 +224,6 @@ public struct IncrementalParseLookup {
239224 }
240225 return offset
241226 }
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- }
257227}
258228
259229/// Functions as an iterator that walks the tree looking for nodes with a
0 commit comments