@@ -84,6 +84,55 @@ public class Node {
84
84
return CollectionNode ( node: self )
85
85
}
86
86
}
87
+
88
+ /// Get the first potential token of a node recursively
89
+ /// This information is used by incremental parse
90
+ public var firstPotentialToken : [ TokenChoice ] {
91
+ if let children = self . layoutNode? . children,
92
+ let firstChild = children. first ( where: { !$0. isUnexpectedNodes} ) {
93
+
94
+ switch firstChild. kind {
95
+ case . node( kind: let nodeKind) :
96
+ guard let nodeSpec = SYNTAX_NODES . first ( where: { $0. kind == nodeKind} ) else {
97
+ return [ ]
98
+ }
99
+ return nodeSpec. firstPotentialToken
100
+
101
+ case . nodeChoices( choices: let children) :
102
+ return children. compactMap { child in
103
+ if let nodeSpec = SYNTAX_NODES . first ( where: { $0. kind == child. syntaxNodeKind} ) {
104
+ return nodeSpec. firstPotentialToken
105
+ }
106
+ return nil
107
+ } . flatMap { $0}
108
+ case . collection( kind: let kind, _) :
109
+ guard let nodeSpec = SYNTAX_NODES . first ( where: { $0. kind == kind} ) ,
110
+ let collection = nodeSpec. collectionNode else {
111
+ return [ ]
112
+ }
113
+ return collection. elementChoices. compactMap { choiceKind in
114
+ if let nodeSpec = SYNTAX_NODES . first ( where: { $0. kind == choiceKind} ) {
115
+ return nodeSpec. firstPotentialToken
116
+ }
117
+ return nil
118
+ } . flatMap { $0}
119
+
120
+ case . token( choices: let choices, _, _) :
121
+ return choices
122
+ }
123
+ }
124
+
125
+ if let collection = self . collectionNode {
126
+ return collection. elementChoices. compactMap { choiceKind in
127
+ if let nodeSpec = SYNTAX_NODES . first ( where: { $0. kind == choiceKind} ) {
128
+ return nodeSpec. firstPotentialToken
129
+ }
130
+ return nil
131
+ } . flatMap { $0}
132
+ }
133
+
134
+ return [ ]
135
+ }
87
136
88
137
/// Construct the specification for a layout syntax node.
89
138
init (
0 commit comments