Skip to content

Commit d988012

Browse files
committed
addressed CR feedback
1 parent 1d1e868 commit d988012

File tree

2 files changed

+9
-31
lines changed

2 files changed

+9
-31
lines changed

src/services/services.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,7 +2088,7 @@ module ts {
20882088
}
20892089

20902090
// TODO: this is a hack for now, we need a proper walking mechanism to verify that we have the correct node
2091-
var mappedNode = getTokenAtPosition(sourceFile, TypeScript.end(node) - 1);
2091+
var mappedNode = getTouchingToken(sourceFile, TypeScript.end(node) - 1, /*allowPositionInLeadingTrivia*/ false);
20922092
if (isPunctuation(mappedNode.kind)) {
20932093
mappedNode = mappedNode.parent;
20942094
}
@@ -4022,7 +4022,7 @@ module ts {
40224022
var sourceFile = getCurrentSourceFile(filename);
40234023
var result: TypeScript.TextSpan[] = [];
40244024

4025-
var token = getTokenContainingPosition(sourceFile, position);
4025+
var token = getTouchingToken(sourceFile, position, /*allowPositionInLeadingTrivia*/ true);
40264026

40274027
if (token.getStart(sourceFile) === position) {
40284028
var matchKind = getMatchingTokenKind(token);
@@ -4178,7 +4178,7 @@ module ts {
41784178

41794179
// OK, we have found a match in the file. This is only an acceptable match if
41804180
// it is contained within a comment.
4181-
var token = getTokenContainingPosition(sourceFile, matchPosition);
4181+
var token = getTouchingToken(sourceFile, matchPosition, /*allowPositionInLeadingTrivia*/ true);
41824182

41834183
if (token.getStart() <= matchPosition && matchPosition < token.getEnd()) {
41844184
// match was within the token itself. Not in the comment. Keep searching

src/services/utilities.ts

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,45 +46,22 @@ module ts {
4646
return -1;
4747
}
4848

49-
/** Get a token that contains the position. This is guaranteed to return a token, the position can be in the
50-
* leading trivia or within the token text.
51-
*/
52-
export function getTokenContainingPosition(sourceFile: SourceFile, position: number) {
53-
var current: Node = sourceFile;
54-
outer: while (true) {
55-
// find the child that has this
56-
for (var i = 0, n = current.getChildCount(); i < n; i++) {
57-
var child = current.getChildAt(i);
58-
if (child.getFullStart() <= position && position < child.getEnd()) {
59-
current = child;
60-
continue outer;
61-
}
62-
}
63-
return current;
64-
}
65-
}
66-
67-
/** Gets the token whose text has range [start, end) and position >= start and position < end */
68-
export function getTokenAtPosition(sourceFile: SourceFile, position: number): Node {
69-
return getTouchingToken(sourceFile, position, /*includeItemAtEndPosition*/ undefined);
70-
}
71-
7249
/* Gets the token whose text has range [start, end) and
7350
* position >= start and (position < end or (position === end && token is keyword or identifier))
7451
*/
7552
export function getTouchingWord(sourceFile: SourceFile, position: number): Node {
76-
return getTouchingToken(sourceFile, position, isWord);
53+
return getTouchingToken(sourceFile, position, /*allowPositionInLeadingTrivia*/ false, isWord);
7754
}
7855

7956
/* Gets the token whose text has range [start, end) and position >= start
8057
* and (position < end or (position === end && token is keyword or identifier or numeric\string litera))
8158
*/
8259
export function getTouchingPropertyName(sourceFile: SourceFile, position: number): Node {
83-
return getTouchingToken(sourceFile, position, isPropertyName);
60+
return getTouchingToken(sourceFile, position, /*allowPositionInLeadingTrivia*/ false, isPropertyName);
8461
}
8562

8663
/** Get the token whose text contains the position */
87-
export function getTouchingToken(sourceFile: SourceFile, position: number, includeItemAtEndPosition: (n: Node) => boolean): Node {
64+
export function getTouchingToken(sourceFile: SourceFile, position: number, allowPositionInLeadingTrivia: boolean, includeItemAtEndPosition?: (n: Node) => boolean): Node {
8865
var current: Node = sourceFile;
8966
outer: while (true) {
9067
if (isToken(current)) {
@@ -95,7 +72,8 @@ module ts {
9572
// find the child that contains 'position'
9673
for (var i = 0, n = current.getChildCount(sourceFile); i < n; i++) {
9774
var child = current.getChildAt(i);
98-
if (child.getStart(sourceFile) <= position) {
75+
var start = allowPositionInLeadingTrivia ? child.getFullStart() : child.getStart(sourceFile);
76+
if (start <= position) {
9977
if (position < child.getEnd()) {
10078
current = child;
10179
continue outer;
@@ -123,7 +101,7 @@ module ts {
123101
export function findTokenOnLeftOfPosition(file: SourceFile, position: number): Node {
124102
// Ideally, getTokenAtPosition should return a token. However, it is currently
125103
// broken, so we do a check to make sure the result was indeed a token.
126-
var tokenAtPosition = getTokenContainingPosition(file, position);
104+
var tokenAtPosition = getTouchingToken(file, position, /*allowPositionInLeadingTrivia*/ true);
127105
if (isToken(tokenAtPosition) && position > tokenAtPosition.getStart(file) && position < tokenAtPosition.getEnd()) {
128106
return tokenAtPosition;
129107
}

0 commit comments

Comments
 (0)