@@ -160,17 +160,24 @@ private struct ClassificationVisitor {
160
160
classifications. append ( range)
161
161
}
162
162
163
+ /// Classifies `triviaPieces` starting from `offset` and returns the number of bytes the trivia took up in the source
164
+ private mutating func classify( triviaPieces: [ RawTriviaPiece ] , at offset: Int ) -> Int {
165
+ var classifiedBytes = 0
166
+ for triviaPiece in triviaPieces {
167
+ let range = triviaPiece. classify ( offset: offset + classifiedBytes)
168
+ report ( range: range)
169
+ classifiedBytes += triviaPiece. byteLength
170
+ }
171
+ return classifiedBytes
172
+ }
173
+
163
174
// Report classification ranges in `descriptor.node` that is a token.
164
175
private mutating func handleToken( _ descriptor: Descriptor ) -> VisitResult {
165
176
let tokenView = descriptor. node. tokenView!
166
177
var byteOffset = descriptor. byteOffset
167
178
168
179
// Leading trivia.
169
- for piece in tokenView. leadingRawTriviaPieces {
170
- let range = piece. classify ( offset: byteOffset)
171
- report ( range: range)
172
- byteOffset += piece. byteLength
173
- }
180
+ byteOffset += classify ( triviaPieces: tokenView. leadingRawTriviaPieces, at: byteOffset)
174
181
// Token text.
175
182
do {
176
183
let range = TokenKindAndText ( kind: tokenView. rawKind, text: tokenView. rawText)
@@ -179,11 +186,7 @@ private struct ClassificationVisitor {
179
186
byteOffset += tokenView. rawText. count
180
187
}
181
188
// Trailing trivia.
182
- for piece in tokenView. trailingRawTriviaPieces {
183
- let range = piece. classify ( offset: byteOffset)
184
- report ( range: range)
185
- byteOffset += piece. byteLength
186
- }
189
+ byteOffset += classify ( triviaPieces: tokenView. trailingRawTriviaPieces, at: byteOffset)
187
190
188
191
precondition ( byteOffset == descriptor. byteOffset + descriptor. node. byteLength)
189
192
return . continue
@@ -196,12 +199,37 @@ private struct ClassificationVisitor {
196
199
197
200
for case (let index, let child? ) in children. enumerated ( ) {
198
201
199
- let classification : ( SyntaxClassification , Bool ) ?
202
+ let classification : ( classification : SyntaxClassification , force : Bool ) ?
200
203
if case . layout( let layout) = descriptor. node. kind. syntaxNodeType. structure {
201
204
classification = SyntaxClassification . classify ( layout [ index] )
202
205
} else {
203
206
classification = nil
204
207
}
208
+
209
+ if let classification, classification. force {
210
+ // Leading trivia.
211
+ if let leadingTriviaPieces = child. leadingTriviaPieces {
212
+ byteOffset += classify ( triviaPieces: leadingTriviaPieces, at: byteOffset)
213
+ }
214
+ // Layout node text.
215
+ let layoutNodeTextLength = child. byteLength - child. leadingTriviaByteLength - child. trailingTriviaByteLength
216
+ let range = SyntaxClassifiedRange (
217
+ kind: classification. classification,
218
+ range: ByteSourceRange (
219
+ offset: byteOffset,
220
+ length: layoutNodeTextLength
221
+ )
222
+ )
223
+ report ( range: range)
224
+ byteOffset += layoutNodeTextLength
225
+
226
+ // Trailing trivia.
227
+ if let trailingTriviaPieces = child. trailingTriviaPieces {
228
+ byteOffset += classify ( triviaPieces: trailingTriviaPieces, at: byteOffset)
229
+ }
230
+ continue
231
+ }
232
+
205
233
let result = visit (
206
234
. init(
207
235
node: child,
0 commit comments