@@ -35,61 +35,39 @@ extension Parser {
35
35
}
36
36
}
37
37
38
- /// Accepts the re-used ``Syntax`` nodes that `IncrementalParseTransition`
38
+ /// Accepts a re-used ``Syntax`` node that `IncrementalParseTransition`
39
39
/// determined they should be re-used for a parse invocation.
40
40
///
41
41
/// The client can use this information to potentially avoid unnecessary work
42
42
/// for regions of the source that have not changed.
43
43
///
44
44
/// This is also used for testing purposes to ensure incremental reparsing
45
45
/// worked as expected.
46
- public protocol IncrementalParseReusedNodeDelegate {
47
- /// Accepts ``Syntax`` node of skipped source region.
48
- ///
49
- /// - Parameters:
50
- /// - previousNode: The node from the previous tree that is associated with
51
- /// the skipped source region.
52
- func parserReusedNode( previousNode: Syntax )
53
- }
54
-
55
- /// An implementation of `IncrementalParseReusedNodeDelegate` that just collects
56
- /// the range and re-used node into an array.
57
- public final class IncrementalParseReusedNodeCollector :
58
- IncrementalParseReusedNodeDelegate
59
- {
60
- public var nodes : [ Syntax ] = [ ]
61
-
62
- public init ( ) { }
63
-
64
- public func parserReusedNode( previousNode: Syntax ) {
65
- nodes. append ( previousNode)
66
- }
67
- }
46
+ public typealias ReusedNodeCallback = ( _ node: Syntax ) -> ( )
68
47
69
48
/// Keeps track of a previously parsed syntax tree and the source edits that
70
49
/// occurred since it was created.
71
50
public final class IncrementalParseTransition {
72
51
fileprivate let previousTree : SourceFileSyntax
73
52
fileprivate let edits : ConcurrentEdits
74
53
fileprivate let lookaheadRanges : LookaheadRanges
75
- fileprivate let reusedDelegate : IncrementalParseReusedNodeDelegate ?
54
+ fileprivate let reusedNodeCallback : ReusedNodeCallback ?
76
55
77
56
/// - Parameters:
78
57
/// - previousTree: The previous tree to do lookups on.
79
58
/// - edits: The edits that have occurred since the last parse that resulted
80
59
/// in the new source that is about to be parsed.
81
- /// - reusedNodeDelegate: Optional delegate to accept information about the
82
- /// reused regions and nodes.
60
+ /// - reusedNodeCallback: Optional closure to accept information about the re-used node. For each node that gets re-used `reusedNodeCallback` is called.
83
61
public init (
84
62
previousTree: SourceFileSyntax ,
85
63
edits: ConcurrentEdits ,
86
64
lookaheadRanges: LookaheadRanges ,
87
- reusedNodeDelegate : IncrementalParseReusedNodeDelegate ? = nil
65
+ reusedNodeCallback : ReusedNodeCallback ? = nil
88
66
) {
89
67
self . previousTree = previousTree
90
68
self . edits = edits
91
69
self . lookaheadRanges = lookaheadRanges
92
- self . reusedDelegate = reusedNodeDelegate
70
+ self . reusedNodeCallback = reusedNodeCallback
93
71
}
94
72
}
95
73
@@ -110,8 +88,8 @@ struct IncrementalParseLookup {
110
88
return transition. edits
111
89
}
112
90
113
- fileprivate var reusedDelegate : IncrementalParseReusedNodeDelegate ? {
114
- return transition. reusedDelegate
91
+ fileprivate var reusedCallback : ReusedNodeCallback ? {
92
+ return transition. reusedNodeCallback
115
93
}
116
94
117
95
/// Does a lookup to see if the current source `offset` should be associated
@@ -132,10 +110,8 @@ struct IncrementalParseLookup {
132
110
}
133
111
let prevPosition = AbsolutePosition ( utf8Offset: prevOffset)
134
112
let node = cursorLookup ( prevPosition: prevPosition, kind: kind)
135
- if let delegate = reusedDelegate, let node {
136
- delegate. parserReusedNode (
137
- previousNode: node
138
- )
113
+ if let node {
114
+ reusedCallback ? ( node)
139
115
}
140
116
return node
141
117
}
0 commit comments