Skip to content

Commit d58272d

Browse files
committed
move getTodoComments to the new tree
1 parent fdc9b9d commit d58272d

File tree

1 file changed

+49
-31
lines changed

1 file changed

+49
-31
lines changed

src/services/services.ts

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,6 +1977,24 @@ module ts {
19771977
}
19781978
}
19791979

1980+
function getTokenAtPosition(sourceFile: SourceFile, position: number) {
1981+
var current: Node = sourceFile;
1982+
outer: while (true) {
1983+
// find the child that has this
1984+
for (var i = 0, n = current.getChildCount(); i < n; i++) {
1985+
var child = current.getChildAt(i);
1986+
if (child.getFullStart() <= position && position < child.getEnd()) {
1987+
current = child;
1988+
continue outer;
1989+
}
1990+
if (child.end > position) {
1991+
break;
1992+
}
1993+
}
1994+
return current;
1995+
}
1996+
}
1997+
19801998
function getContainerNode(node: Node): Node {
19811999
while (true) {
19822000
node = node.parent;
@@ -3569,19 +3587,19 @@ module ts {
35693587
//
35703588
// 'i' is for case insensitivity (We do this to match C# TODO comment code).
35713589
//
3572-
// 'm' is so we can find matches in a multiline input.
3590+
// 'm' is so we can find matches in a multi-line input.
35733591
return new RegExp(regExpString, "gim");
35743592
}
35753593

3576-
function getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[] {
3577-
fileName = TypeScript.switchToForwardSlashes(fileName);
3594+
function getTodoComments(filename: string, descriptors: TodoCommentDescriptor[]): TodoComment[] {
3595+
filename = TypeScript.switchToForwardSlashes(filename);
3596+
3597+
var sourceFile = getCurrentSourceFile(filename);
35783598

3579-
var sourceFile = getCurrentSourceFile(fileName);
3580-
var syntaxTree = sourceFile.getSyntaxTree();
35813599
cancellationToken.throwIfCancellationRequested();
35823600

3583-
var text = syntaxTree.text;
3584-
var fileContents = text.substr(0, text.length());
3601+
var fileContents = sourceFile.text;
3602+
35853603
cancellationToken.throwIfCancellationRequested();
35863604

35873605
var result: TodoComment[] = [];
@@ -3602,7 +3620,7 @@ module ts {
36023620
// ["// hack 1", "// ", "hack 1", undefined, "hack"]
36033621
//
36043622
// Here are the relevant capture groups:
3605-
// 0) The full match for the entire regex.
3623+
// 0) The full match for the entire regexp.
36063624
// 1) The preamble to the message portion.
36073625
// 2) The message portion.
36083626
// 3...N) The descriptor that was matched - by index. 'undefined' for each
@@ -3616,20 +3634,18 @@ module ts {
36163634
var preamble = matchArray[1];
36173635
var matchPosition = matchArray.index + preamble.length;
36183636

3619-
// Ok, we have found a match in the file. This is only an acceptable match if
3637+
// OK, we have found a match in the file. This is only an acceptable match if
36203638
// it is contained within a comment.
3621-
var token = TypeScript.findToken(syntaxTree.sourceUnit(), matchPosition);
3639+
var token = getTokenAtPosition(sourceFile, matchPosition);
36223640

3623-
if (matchPosition >= TypeScript.start(token) && matchPosition < TypeScript.end(token)) {
3624-
// match was within the token itself. Not in the comment. Keep searching
3625-
// descriptor.
3641+
if (token.getStart() <= matchPosition && matchPosition < token.getEnd()) {
3642+
// match was within the token itself. Not in the comment. Keep searching // descriptor.
36263643
continue;
36273644
}
36283645

3629-
// Looks to be within the trivia. See if we can find the comment containing it.
3630-
var triviaList = matchPosition < TypeScript.start(token) ? token.leadingTrivia(syntaxTree.text) : token.trailingTrivia(syntaxTree.text);
3631-
var trivia = findContainingComment(triviaList, matchPosition);
3632-
if (trivia === null) {
3646+
// Looks to be within the trivia. See if we can find the comment containing it.
3647+
if (!getContainingComment(getTrailingComments(fileContents, token.getFullStart()), matchPosition) &&
3648+
!getContainingComment(getLeadingComments(fileContents, token.getFullStart()), matchPosition)) {
36333649
continue;
36343650
}
36353651

@@ -3653,25 +3669,27 @@ module ts {
36533669
}
36543670

36553671
return result;
3656-
}
36573672

3658-
function isLetterOrDigit(char: number): boolean {
3659-
return (char >= TypeScript.CharacterCodes.a && char <= TypeScript.CharacterCodes.z) ||
3660-
(char >= TypeScript.CharacterCodes.A && char <= TypeScript.CharacterCodes.Z) ||
3661-
(char >= TypeScript.CharacterCodes._0 && char <= TypeScript.CharacterCodes._9);
3662-
}
3663-
3664-
function findContainingComment(triviaList: TypeScript.ISyntaxTriviaList, position: number): TypeScript.ISyntaxTrivia {
3665-
for (var i = 0, n = triviaList.count(); i < n; i++) {
3666-
var trivia = triviaList.syntaxTriviaAt(i);
3667-
var fullEnd = trivia.fullStart() + trivia.fullWidth();
3668-
if (trivia.isComment() && trivia.fullStart() <= position && position < fullEnd) {
3669-
return trivia;
3673+
function getContainingComment(comments: Comment[], position: number): Comment {
3674+
if (comments) {
3675+
for (var i = 0, n = comments.length; i < n; i++) {
3676+
var comment = comments[i];
3677+
if (comment.pos <= position && position < comment.end) {
3678+
return comment;
3679+
}
3680+
}
36703681
}
3682+
3683+
return undefined;
36713684
}
36723685

3673-
return null;
3686+
function isLetterOrDigit(char: number): boolean {
3687+
return (char >= TypeScript.CharacterCodes.a && char <= TypeScript.CharacterCodes.z) ||
3688+
(char >= TypeScript.CharacterCodes.A && char <= TypeScript.CharacterCodes.Z) ||
3689+
(char >= TypeScript.CharacterCodes._0 && char <= TypeScript.CharacterCodes._9);
3690+
}
36743691
}
3692+
36753693

36763694
return {
36773695
dispose: dispose,

0 commit comments

Comments
 (0)