From 3bb7be96fa1b048e7c2ef4409611046c046c4a25 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Tue, 16 Jun 2015 16:03:45 -0700 Subject: [PATCH] Scan less during classification. --- src/harness/runner.ts | 1 - src/services/services.ts | 28 +++++++++++-------- .../syntacticClassificationsDocComment3.ts | 1 - 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/harness/runner.ts b/src/harness/runner.ts index 42c2d5667c4..1e9c6470421 100644 --- a/src/harness/runner.ts +++ b/src/harness/runner.ts @@ -49,7 +49,6 @@ if (testConfigFile !== '') { if (!option) { continue; } - ts.sys.write("Option: " + option + "\r\n"); switch (option) { case 'compiler': runners.push(new CompilerBaselineRunner(CompilerTestType.Conformance)); diff --git a/src/services/services.ts b/src/services/services.ts index 3939e62cfd2..cebe79630d0 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -6132,13 +6132,7 @@ namespace ts { result.push(type); } - function classifyLeadingTrivia(token: Node): void { - let tokenStart = skipTrivia(sourceFile.text, token.pos, /*stopAfterLineBreak:*/ false); - if (tokenStart === token.pos) { - return; - } - - // token has trivia. Classify them appropriately. + function classifyLeadingTriviaAndGetTokenStart(token: Node): number { triviaScanner.setTextPos(token.pos); while (true) { let start = triviaScanner.getTextPos(); @@ -6148,13 +6142,23 @@ namespace ts { // The moment we get something that isn't trivia, then stop processing. if (!isTrivia(kind)) { - return; + return start; + } + + // Don't bother with newlines/whitespace. + if (kind === SyntaxKind.NewLineTrivia || kind === SyntaxKind.WhitespaceTrivia) { + continue; } // Only bother with the trivia if it at least intersects the span of interest. if (textSpanIntersectsWith(span, start, width)) { if (isComment(kind)) { classifyComment(token, kind, start, width); + + // Classifying a comment might cause us to reuse the trivia scanner + // (because of jsdoc comments). So after we classify the comment make + // sure we set the scanner position back to where it needs to be. + triviaScanner.setTextPos(end); continue; } @@ -6292,12 +6296,14 @@ namespace ts { } function classifyToken(token: Node): void { - classifyLeadingTrivia(token); + let tokenStart = classifyLeadingTriviaAndGetTokenStart(token); - if (token.getWidth() > 0) { + let tokenWidth = token.getEnd() - tokenStart; + Debug.assert(tokenWidth >= 0); + if (tokenWidth > 0) { let type = classifyTokenType(token.kind, token); if (type) { - pushClassification(token.getStart(), token.getWidth(), type); + pushClassification(tokenStart, tokenWidth, type); } } } diff --git a/tests/cases/fourslash/syntacticClassificationsDocComment3.ts b/tests/cases/fourslash/syntacticClassificationsDocComment3.ts index bdc7faa470e..6aca8cc415f 100644 --- a/tests/cases/fourslash/syntacticClassificationsDocComment3.ts +++ b/tests/cases/fourslash/syntacticClassificationsDocComment3.ts @@ -14,7 +14,6 @@ verify.syntacticClassificationsAre( c.punctuation("{"), c.keyword("number"), c.comment(" /* } */"), - c.comment("/* } */"), c.keyword("var"), c.text("v"), c.punctuation(";"));