@@ -167,7 +167,7 @@ namespace ts {
167
167
}
168
168
169
169
public getFullWidth ( ) : number {
170
- return this . end - this . getFullStart ( ) ;
170
+ return this . end - this . pos ;
171
171
}
172
172
173
173
public getLeadingTriviaWidth ( sourceFile ?: SourceFile ) : number {
@@ -6115,6 +6115,8 @@ namespace ts {
6115
6115
function getEncodedSyntacticClassifications ( fileName : string , span : TextSpan ) : Classifications {
6116
6116
// doesn't use compiler - no need to synchronize with host
6117
6117
let sourceFile = syntaxTreeCache . getCurrentSourceFile ( fileName ) ;
6118
+ let spanStart = span . start ;
6119
+ let spanLength = span . length ;
6118
6120
6119
6121
// Make a scanner we can get trivia from.
6120
6122
let triviaScanner = createScanner ( ScriptTarget . Latest , /*skipTrivia:*/ false , sourceFile . text ) ;
@@ -6131,48 +6133,55 @@ namespace ts {
6131
6133
result . push ( type ) ;
6132
6134
}
6133
6135
6134
- function classifyLeadingTrivia ( token : Node ) : void {
6135
- let tokenStart = skipTrivia ( sourceFile . text , token . pos , /*stopAfterLineBreak:*/ false ) ;
6136
- if ( tokenStart === token . pos ) {
6137
- return ;
6138
- }
6139
-
6140
- // token has trivia. Classify them appropriately.
6136
+ function classifyLeadingTriviaAndGetTokenStart ( token : Node ) : number {
6141
6137
triviaScanner . setTextPos ( token . pos ) ;
6142
6138
while ( true ) {
6143
6139
let start = triviaScanner . getTextPos ( ) ;
6140
+ // only bother scanning if we have something that could be trivia.
6141
+ if ( ! couldStartTrivia ( sourceFile . text , start ) ) {
6142
+ return start ;
6143
+ }
6144
+
6144
6145
let kind = triviaScanner . scan ( ) ;
6145
6146
let end = triviaScanner . getTextPos ( ) ;
6146
6147
let width = end - start ;
6147
6148
6148
6149
// The moment we get something that isn't trivia, then stop processing.
6149
6150
if ( ! isTrivia ( kind ) ) {
6150
- return ;
6151
+ return start ;
6151
6152
}
6152
6153
6153
- // Only bother with the trivia if it at least intersects the span of interest.
6154
- if ( textSpanIntersectsWith ( span , start , width ) ) {
6155
- if ( isComment ( kind ) ) {
6156
- classifyComment ( token , kind , start , width ) ;
6157
- continue ;
6158
- }
6154
+ // Don't bother with newlines/whitespace.
6155
+ if ( kind === SyntaxKind . NewLineTrivia || kind === SyntaxKind . WhitespaceTrivia ) {
6156
+ continue ;
6157
+ }
6159
6158
6160
- if ( kind === SyntaxKind . ConflictMarkerTrivia ) {
6161
- let text = sourceFile . text ;
6162
- let ch = text . charCodeAt ( start ) ;
6159
+ // Only bother with the trivia if it at least intersects the span of interest.
6160
+ if ( isComment ( kind ) ) {
6161
+ classifyComment ( token , kind , start , width ) ;
6162
+
6163
+ // Classifying a comment might cause us to reuse the trivia scanner
6164
+ // (because of jsdoc comments). So after we classify the comment make
6165
+ // sure we set the scanner position back to where it needs to be.
6166
+ triviaScanner . setTextPos ( end ) ;
6167
+ continue ;
6168
+ }
6163
6169
6164
- // for the <<<<<<< and >>>>>>> markers, we just add them in as comments
6165
- // in the classification stream.
6166
- if ( ch === CharacterCodes . lessThan || ch === CharacterCodes . greaterThan ) {
6167
- pushClassification ( start , width , ClassificationType . comment ) ;
6168
- continue ;
6169
- }
6170
+ if ( kind === SyntaxKind . ConflictMarkerTrivia ) {
6171
+ let text = sourceFile . text ;
6172
+ let ch = text . charCodeAt ( start ) ;
6170
6173
6171
- // for the ======== add a comment for the first line, and then lex all
6172
- // subsequent lines up until the end of the conflict marker.
6173
- Debug . assert ( ch === CharacterCodes . equals ) ;
6174
- classifyDisabledMergeCode ( text , start , end ) ;
6174
+ // for the <<<<<<< and >>>>>>> markers, we just add them in as comments
6175
+ // in the classification stream.
6176
+ if ( ch === CharacterCodes . lessThan || ch === CharacterCodes . greaterThan ) {
6177
+ pushClassification ( start , width , ClassificationType . comment ) ;
6178
+ continue ;
6175
6179
}
6180
+
6181
+ // for the ======== add a comment for the first line, and then lex all
6182
+ // subsequent lines up until the end of the conflict marker.
6183
+ Debug . assert ( ch === CharacterCodes . equals ) ;
6184
+ classifyDisabledMergeCode ( text , start , end ) ;
6176
6185
}
6177
6186
}
6178
6187
}
@@ -6291,12 +6300,14 @@ namespace ts {
6291
6300
}
6292
6301
6293
6302
function classifyToken ( token : Node ) : void {
6294
- classifyLeadingTrivia ( token ) ;
6303
+ let tokenStart = classifyLeadingTriviaAndGetTokenStart ( token ) ;
6295
6304
6296
- if ( token . getWidth ( ) > 0 ) {
6305
+ let tokenWidth = token . end - tokenStart ;
6306
+ Debug . assert ( tokenWidth >= 0 ) ;
6307
+ if ( tokenWidth > 0 ) {
6297
6308
let type = classifyTokenType ( token . kind , token ) ;
6298
6309
if ( type ) {
6299
- pushClassification ( token . getStart ( ) , token . getWidth ( ) , type ) ;
6310
+ pushClassification ( tokenStart , tokenWidth , type ) ;
6300
6311
}
6301
6312
}
6302
6313
}
@@ -6401,9 +6412,10 @@ namespace ts {
6401
6412
}
6402
6413
6403
6414
// Ignore nodes that don't intersect the original span to classify.
6404
- if ( textSpanIntersectsWith ( span , element . getFullStart ( ) , element . getFullWidth ( ) ) ) {
6415
+ if ( decodedTextSpanIntersectsWith ( spanStart , spanLength , element . pos , element . getFullWidth ( ) ) ) {
6405
6416
let children = element . getChildren ( sourceFile ) ;
6406
- for ( let child of children ) {
6417
+ for ( let i = 0 , n = children . length ; i < n ; i ++ ) {
6418
+ let child = children [ i ] ;
6407
6419
if ( isToken ( child ) ) {
6408
6420
classifyToken ( child ) ;
6409
6421
}
0 commit comments