refactored isInComment, isInString

This commit is contained in:
Arthur Ozga
2015-07-16 18:28:58 -07:00
parent 0dcb9007a3
commit bf7c073e63
2 changed files with 37 additions and 25 deletions
+8 -25
View File
@@ -5434,7 +5434,7 @@ namespace ts {
symbolToIndex: number[]): void {
let sourceFile = container.getSourceFile();
let tripleSlashDirectivePrefixRegex = /^\/\/\/\s*</
let tripleSlashDirectivePrefixRegex = /^\/\/\/\s*</;
let possiblePositions = getPossibleSymbolReferencePositions(sourceFile, searchText, container.getStart(), container.getEnd());
@@ -5450,8 +5450,8 @@ namespace ts {
// This wasn't the start of a token. Check to see if it might be a
// match in a comment or string if that's what the caller is asking
// for.
if ((findInStrings && isInString(position)) ||
(findInComments && isInComment(position))) {
if ((findInStrings && isInString(sourceFile, position)) ||
(findInComments && isInNonReferenceComment(sourceFile, position))) {
// In the case where we're looking inside comments/strings, we don't have
// an actual definition. So just use 'undefined' here. Features like
@@ -5515,30 +5515,13 @@ namespace ts {
return result[index];
}
function isInString(position: number) {
let token = getTokenAtPosition(sourceFile, position);
return token && token.kind === SyntaxKind.StringLiteral && position > token.getStart();
}
function isInNonReferenceComment(sourceFile: SourceFile, position: number): boolean {
return isInCommentHelper(sourceFile, position, isNonReferenceComment);
function isInComment(position: number) {
let token = getTokenAtPosition(sourceFile, position);
if (token && position < token.getStart()) {
// First, we have to see if this position actually landed in a comment.
let commentRanges = getLeadingCommentRanges(sourceFile.text, token.pos);
// Then we want to make sure that it wasn't in a "///<" directive comment
// We don't want to unintentionally update a file name.
return forEach(commentRanges, c => {
if (c.pos < position && position < c.end) {
let commentText = sourceFile.text.substring(c.pos, c.end);
if (!tripleSlashDirectivePrefixRegex.test(commentText)) {
return true;
}
}
});
function isNonReferenceComment(c: CommentRange): boolean {
let commentText = sourceFile.text.substring(c.pos, c.end);
return !tripleSlashDirectivePrefixRegex.test(commentText);
}
return false;
}
}
+29
View File
@@ -414,6 +414,35 @@ namespace ts {
}
}
}
export function isInString(sourceFile: SourceFile, position: number) {
let token = getTokenAtPosition(sourceFile, position);
return token && token.kind === SyntaxKind.StringLiteral && position > token.getStart();
}
export function isInComment(sourceFile: SourceFile, position: number) {
return isInCommentHelper(sourceFile, position, c => true);
}
/**
* Returns true if the cursor at position in sourceFile is within a comment that additionally
* satisfies extraCheck, and false otherwise.
*/
export function isInCommentHelper(sourceFile: SourceFile, position: number,
extraCheck: (c: CommentRange) => boolean): boolean {
let token = getTokenAtPosition(sourceFile, position);
if (token && position < token.getStart()) {
// First, we have to see if this position actually landed in a comment.
let commentRanges = getLeadingCommentRanges(sourceFile.text, token.pos);
// Then we want to make sure that it wasn't in a "///<" directive comment
// We don't want to unintentionally update a file name.
return forEach(commentRanges, c => c.pos < position && position < c.end && extraCheck(c));
}
return false;
}
function nodeHasTokens(n: Node): boolean {
// If we have a token or node that has a non-zero width, it must have tokens.