@@ -160,17 +160,24 @@ private struct ClassificationVisitor {
160160 classifications. append ( range)
161161 }
162162
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+
163174 // Report classification ranges in `descriptor.node` that is a token.
164175 private mutating func handleToken( _ descriptor: Descriptor ) -> VisitResult {
165176 let tokenView = descriptor. node. tokenView!
166177 var byteOffset = descriptor. byteOffset
167178
168179 // 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)
174181 // Token text.
175182 do {
176183 let range = TokenKindAndText ( kind: tokenView. rawKind, text: tokenView. rawText)
@@ -179,11 +186,7 @@ private struct ClassificationVisitor {
179186 byteOffset += tokenView. rawText. count
180187 }
181188 // 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)
187190
188191 precondition ( byteOffset == descriptor. byteOffset + descriptor. node. byteLength)
189192 return . continue
@@ -196,12 +199,37 @@ private struct ClassificationVisitor {
196199
197200 for case (let index, let child? ) in children. enumerated ( ) {
198201
199- let classification : ( SyntaxClassification , Bool ) ?
202+ let classification : ( classification : SyntaxClassification , force : Bool ) ?
200203 if case . layout( let layout) = descriptor. node. kind. syntaxNodeType. structure {
201204 classification = SyntaxClassification . classify ( layout [ index] )
202205 } else {
203206 classification = nil
204207 }
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+
205233 let result = visit (
206234 . init(
207235 node: child,
0 commit comments