10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
- extension TokenSyntax {
14
- /// The `SyntaxClassifiedRange` for the token text, excluding trivia.
15
- public var tokenClassification : SyntaxClassifiedRange {
16
- var contextualClassification : ( SyntaxClassification , Bool ) ? = nil
17
- var curData = self . data
13
+ extension SyntaxData {
14
+ var contextualClassification : ( SyntaxClassification , Bool ) ? {
15
+ var contextualClassif : ( SyntaxClassification , Bool ) ? = nil
16
+ var curData = self
18
17
repeat {
19
18
guard let parent = curData. parent else { break }
20
- contextualClassification = SyntaxClassification . classify ( parentKind: parent. raw. kind,
19
+ contextualClassif = SyntaxClassification . classify ( parentKind: parent. raw. kind,
21
20
indexInParent: curData. indexInParent, childKind: raw. kind)
22
21
curData = parent. data
23
- } while contextualClassification == nil
22
+ } while contextualClassif == nil
23
+ return contextualClassif
24
+ }
25
+ }
24
26
27
+ extension TokenSyntax {
28
+ /// The `SyntaxClassifiedRange` for the token text, excluding trivia.
29
+ public var tokenClassification : SyntaxClassifiedRange {
30
+ let contextualClassification = self . data. contextualClassification
25
31
let relativeOffset = raw. tokenLeadingTriviaLength. utf8Length
26
32
let absoluteOffset = position. utf8Offset + relativeOffset
27
33
let classifiedRange = raw. withUnsafeTokenText (
@@ -85,10 +91,14 @@ fileprivate struct AbsoluteNode {
85
91
let classification : ( SyntaxClassification , Bool ) ?
86
92
87
93
/// Use this constructor for the root node.
88
- init ( raw: RawSyntax , position: AbsoluteSyntaxPosition ) {
94
+ init (
95
+ raw: RawSyntax ,
96
+ position: AbsoluteSyntaxPosition ,
97
+ contextualClassification: ( SyntaxClassification , Bool ) ?
98
+ ) {
89
99
self . raw = raw
90
100
self . position = position
91
- self . classification = nil
101
+ self . classification = contextualClassification
92
102
}
93
103
94
104
init ( raw: RawSyntax , position: AbsoluteSyntaxPosition , parent: AbsoluteNode ) {
@@ -137,10 +147,16 @@ fileprivate struct SyntaxCursor {
137
147
let absRange : ByteSourceRange
138
148
var finished : Bool
139
149
140
- init ( root: RawSyntax , offset: Int , in absRange: ByteSourceRange ) {
150
+ init (
151
+ root: RawSyntax ,
152
+ offset: Int ,
153
+ in absRange: ByteSourceRange ,
154
+ contextualClassification: ( SyntaxClassification , Bool ) ?
155
+ ) {
141
156
self . absRange = absRange
142
157
self . node = AbsoluteNode ( raw: root,
143
- position: . init( offset: UInt32 ( truncatingIfNeeded: offset) , indexInParent: 0 ) )
158
+ position: . init( offset: UInt32 ( truncatingIfNeeded: offset) , indexInParent: 0 ) ,
159
+ contextualClassification: contextualClassification)
144
160
self . parents = [ ]
145
161
self . finished = false
146
162
}
@@ -214,8 +230,14 @@ fileprivate struct FastTokenSequence: Sequence {
214
230
struct Iterator : IteratorProtocol {
215
231
var cursor : SyntaxCursor
216
232
217
- init ( root: RawSyntax , offset: Int , in absRange: ByteSourceRange ) {
218
- self . cursor = SyntaxCursor ( root: root, offset: offset, in: absRange)
233
+ init (
234
+ root: RawSyntax ,
235
+ offset: Int ,
236
+ in absRange: ByteSourceRange ,
237
+ contextualClassification: ( SyntaxClassification , Bool ) ?
238
+ ) {
239
+ self . cursor = SyntaxCursor ( root: root, offset: offset, in: absRange,
240
+ contextualClassification: contextualClassification)
219
241
_ = self . cursor. advanceToFirstToken ( )
220
242
}
221
243
@@ -231,15 +253,23 @@ fileprivate struct FastTokenSequence: Sequence {
231
253
private let root : RawSyntax
232
254
private let offset : Int
233
255
private let absRange : ByteSourceRange
234
-
235
- init ( _ root: RawSyntax , offset: Int , in absRange: ByteSourceRange ) {
256
+ private let contextualClassification : ( SyntaxClassification , Bool ) ?
257
+
258
+ init (
259
+ _ root: RawSyntax ,
260
+ offset: Int ,
261
+ in absRange: ByteSourceRange ,
262
+ contextualClassification: ( SyntaxClassification , Bool ) ?
263
+ ) {
236
264
self . root = root
237
265
self . offset = offset
238
266
self . absRange = absRange
267
+ self . contextualClassification = contextualClassification
239
268
}
240
269
241
270
func makeIterator( ) -> Iterator {
242
- return . init( root: root, offset: offset, in: absRange)
271
+ return . init( root: root, offset: offset, in: absRange,
272
+ contextualClassification: contextualClassification)
243
273
}
244
274
}
245
275
@@ -335,9 +365,15 @@ public struct SyntaxClassifications: Sequence {
335
365
fileprivate var curOffset : Int
336
366
fileprivate var curTokenEndOffset : Int
337
367
338
- init ( root: RawSyntax , offset: Int , in absRange: ByteSourceRange ) {
368
+ init (
369
+ root: RawSyntax ,
370
+ offset: Int ,
371
+ in absRange: ByteSourceRange ,
372
+ contextualClassification: ( SyntaxClassification , Bool ) ?
373
+ ) {
339
374
self . absRange = absRange
340
- self . tokenIterator = . init( root: root, offset: offset, in: absRange)
375
+ self . tokenIterator = . init( root: root, offset: offset, in: absRange,
376
+ contextualClassification: contextualClassification)
341
377
self . pendingClassifiedRange = . init( kind: . none,
342
378
range: ByteSourceRange ( offset: absRange. offset, length: 0 ) )
343
379
self . curOffset = absRange. offset
@@ -431,6 +467,7 @@ public struct SyntaxClassifications: Sequence {
431
467
let nodeOffset = node. position. utf8Offset
432
468
let absRange = ByteSourceRange ( offset: nodeOffset + relRange. offset,
433
469
length: relRange. length)
434
- return . init( root: node. raw, offset: nodeOffset, in: absRange)
470
+ return . init( root: node. raw, offset: nodeOffset, in: absRange,
471
+ contextualClassification: node. data. contextualClassification)
435
472
}
436
473
}
0 commit comments