@@ -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 {
@@ -6096,6 +6096,8 @@ namespace ts {
6096
6096
function getEncodedSyntacticClassifications ( fileName : string , span : TextSpan ) : Classifications {
6097
6097
// doesn't use compiler - no need to synchronize with host
6098
6098
let sourceFile = syntaxTreeCache . getCurrentSourceFile ( fileName ) ;
6099
+ let spanStart = span . start ;
6100
+ let spanLength = span . length ;
6099
6101
6100
6102
// Make a scanner we can get trivia from.
6101
6103
let triviaScanner = createScanner ( ScriptTarget . Latest , /*skipTrivia:*/ false , sourceFile . text ) ;
@@ -6112,48 +6114,55 @@ namespace ts {
6112
6114
result . push ( type ) ;
6113
6115
}
6114
6116
6115
- function classifyLeadingTrivia ( token : Node ) : void {
6116
- let tokenStart = skipTrivia ( sourceFile . text , token . pos , /*stopAfterLineBreak:*/ false ) ;
6117
- if ( tokenStart === token . pos ) {
6118
- return ;
6119
- }
6120
-
6121
- // token has trivia. Classify them appropriately.
6117
+ function classifyLeadingTriviaAndGetTokenStart ( token : Node ) : number {
6122
6118
triviaScanner . setTextPos ( token . pos ) ;
6123
6119
while ( true ) {
6124
6120
let start = triviaScanner . getTextPos ( ) ;
6121
+ // only bother scanning if we have something that could be trivia.
6122
+ if ( ! couldStartTrivia ( sourceFile . text , start ) ) {
6123
+ return start ;
6124
+ }
6125
+
6125
6126
let kind = triviaScanner . scan ( ) ;
6126
6127
let end = triviaScanner . getTextPos ( ) ;
6127
6128
let width = end - start ;
6128
6129
6129
6130
// The moment we get something that isn't trivia, then stop processing.
6130
6131
if ( ! isTrivia ( kind ) ) {
6131
- return ;
6132
+ return start ;
6132
6133
}
6133
6134
6134
- // Only bother with the trivia if it at least intersects the span of interest.
6135
- if ( textSpanIntersectsWith ( span , start , width ) ) {
6136
- if ( isComment ( kind ) ) {
6137
- classifyComment ( token , kind , start , width ) ;
6138
- continue ;
6139
- }
6135
+ // Don't bother with newlines/whitespace.
6136
+ if ( kind === SyntaxKind . NewLineTrivia || kind === SyntaxKind . WhitespaceTrivia ) {
6137
+ continue ;
6138
+ }
6140
6139
6141
- if ( kind === SyntaxKind . ConflictMarkerTrivia ) {
6142
- let text = sourceFile . text ;
6143
- let ch = text . charCodeAt ( start ) ;
6140
+ // Only bother with the trivia if it at least intersects the span of interest.
6141
+ if ( isComment ( kind ) ) {
6142
+ classifyComment ( token , kind , start , width ) ;
6143
+
6144
+ // Classifying a comment might cause us to reuse the trivia scanner
6145
+ // (because of jsdoc comments). So after we classify the comment make
6146
+ // sure we set the scanner position back to where it needs to be.
6147
+ triviaScanner . setTextPos ( end ) ;
6148
+ continue ;
6149
+ }
6144
6150
6145
- // for the <<<<<<< and >>>>>>> markers, we just add them in as comments
6146
- // in the classification stream.
6147
- if ( ch === CharacterCodes . lessThan || ch === CharacterCodes . greaterThan ) {
6148
- pushClassification ( start , width , ClassificationType . comment ) ;
6149
- continue ;
6150
- }
6151
+ if ( kind === SyntaxKind . ConflictMarkerTrivia ) {
6152
+ let text = sourceFile . text ;
6153
+ let ch = text . charCodeAt ( start ) ;
6151
6154
6152
- // for the ======== add a comment for the first line, and then lex all
6153
- // subsequent lines up until the end of the conflict marker.
6154
- Debug . assert ( ch === CharacterCodes . equals ) ;
6155
- classifyDisabledMergeCode ( text , start , end ) ;
6155
+ // for the <<<<<<< and >>>>>>> markers, we just add them in as comments
6156
+ // in the classification stream.
6157
+ if ( ch === CharacterCodes . lessThan || ch === CharacterCodes . greaterThan ) {
6158
+ pushClassification ( start , width , ClassificationType . comment ) ;
6159
+ continue ;
6156
6160
}
6161
+
6162
+ // for the ======== add a comment for the first line, and then lex all
6163
+ // subsequent lines up until the end of the conflict marker.
6164
+ Debug . assert ( ch === CharacterCodes . equals ) ;
6165
+ classifyDisabledMergeCode ( text , start , end ) ;
6157
6166
}
6158
6167
}
6159
6168
}
@@ -6272,12 +6281,14 @@ namespace ts {
6272
6281
}
6273
6282
6274
6283
function classifyToken ( token : Node ) : void {
6275
- classifyLeadingTrivia ( token ) ;
6284
+ let tokenStart = classifyLeadingTriviaAndGetTokenStart ( token ) ;
6276
6285
6277
- if ( token . getWidth ( ) > 0 ) {
6286
+ let tokenWidth = token . end - tokenStart ;
6287
+ Debug . assert ( tokenWidth >= 0 ) ;
6288
+ if ( tokenWidth > 0 ) {
6278
6289
let type = classifyTokenType ( token . kind , token ) ;
6279
6290
if ( type ) {
6280
- pushClassification ( token . getStart ( ) , token . getWidth ( ) , type ) ;
6291
+ pushClassification ( tokenStart , tokenWidth , type ) ;
6281
6292
}
6282
6293
}
6283
6294
}
@@ -6382,9 +6393,10 @@ namespace ts {
6382
6393
}
6383
6394
6384
6395
// Ignore nodes that don't intersect the original span to classify.
6385
- if ( textSpanIntersectsWith ( span , element . getFullStart ( ) , element . getFullWidth ( ) ) ) {
6396
+ if ( decodedTextSpanIntersectsWith ( spanStart , spanLength , element . pos , element . getFullWidth ( ) ) ) {
6386
6397
let children = element . getChildren ( sourceFile ) ;
6387
- for ( let child of children ) {
6398
+ for ( let i = 0 , n = children . length ; i < n ; i ++ ) {
6399
+ let child = children [ i ] ;
6388
6400
if ( isToken ( child ) ) {
6389
6401
classifyToken ( child ) ;
6390
6402
}
0 commit comments