Skip to content

Commit 13f13b8

Browse files
committed
Merge pull request #752 from Microsoft/getTokenAtPosition
make rename\gotoDef work at the end of token
2 parents dc10492 + a939a71 commit 13f13b8

12 files changed

+279
-123
lines changed

src/services/services.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,7 +2121,7 @@ module ts {
21212121
}
21222122

21232123
// TODO: this is a hack for now, we need a proper walking mechanism to verify that we have the correct node
2124-
var mappedNode = getNodeAtPosition(sourceFile, TypeScript.end(node) - 1);
2124+
var mappedNode = getTouchingToken(sourceFile, TypeScript.end(node) - 1);
21252125
if (isPunctuation(mappedNode.kind)) {
21262126
mappedNode = mappedNode.parent;
21272127
}
@@ -2358,7 +2358,7 @@ module ts {
23582358

23592359
fileName = TypeScript.switchToForwardSlashes(fileName);
23602360
var sourceFile = getSourceFile(fileName);
2361-
var node = getNodeAtPosition(sourceFile, position);
2361+
var node = getTouchingPropertyName(sourceFile, position);
23622362
if (!node) {
23632363
return undefined;
23642364
}
@@ -2466,7 +2466,7 @@ module ts {
24662466

24672467
fileName = TypeScript.switchToForwardSlashes(fileName);
24682468
var sourceFile = getSourceFile(fileName);
2469-
var node = getNodeAtPosition(sourceFile, position);
2469+
var node = getTouchingWord(sourceFile, position);
24702470
if (!node) {
24712471
return undefined;
24722472
}
@@ -2549,7 +2549,7 @@ module ts {
25492549
filename = TypeScript.switchToForwardSlashes(filename);
25502550
var sourceFile = getSourceFile(filename);
25512551

2552-
var node = getNodeAtPosition(sourceFile, position);
2552+
var node = getTouchingPropertyName(sourceFile, position);
25532553
if (!node) {
25542554
return undefined;
25552555
}
@@ -2613,7 +2613,7 @@ module ts {
26132613
filename = TypeScript.switchToForwardSlashes(filename);
26142614
var sourceFile = getSourceFile(filename);
26152615

2616-
var node = getNodeAtPosition(sourceFile, position);
2616+
var node = getTouchingWord(sourceFile, position);
26172617
if (!node) {
26182618
return undefined;
26192619
}
@@ -3058,7 +3058,7 @@ module ts {
30583058
filename = TypeScript.switchToForwardSlashes(filename);
30593059
var sourceFile = getSourceFile(filename);
30603060

3061-
var node = getNodeAtPosition(sourceFile, position);
3061+
var node = getTouchingPropertyName(sourceFile, position);
30623062
if (!node) {
30633063
return undefined;
30643064
}
@@ -3241,7 +3241,7 @@ module ts {
32413241
forEach(possiblePositions, position => {
32423242
cancellationToken.throwIfCancellationRequested();
32433243

3244-
var node = getNodeAtPosition(sourceFile, position);
3244+
var node = getTouchingWord(sourceFile, position);
32453245
if (!node || node.getWidth() !== labelName.length) {
32463246
return;
32473247
}
@@ -3297,7 +3297,7 @@ module ts {
32973297
forEach(possiblePositions, position => {
32983298
cancellationToken.throwIfCancellationRequested();
32993299

3300-
var referenceLocation = getNodeAtPosition(sourceFile, position);
3300+
var referenceLocation = getTouchingPropertyName(sourceFile, position);
33013301
if (!isValidReferencePosition(referenceLocation, searchText)) {
33023302
return;
33033303
}
@@ -3349,7 +3349,7 @@ module ts {
33493349
forEach(possiblePositions, position => {
33503350
cancellationToken.throwIfCancellationRequested();
33513351

3352-
var node = getNodeAtPosition(sourceFile, position);
3352+
var node = getTouchingWord(sourceFile, position);
33533353

33543354
if (!node || node.kind !== SyntaxKind.SuperKeyword) {
33553355
return;
@@ -3415,7 +3415,7 @@ module ts {
34153415
forEach(possiblePositions, position => {
34163416
cancellationToken.throwIfCancellationRequested();
34173417

3418-
var node = getNodeAtPosition(sourceFile, position);
3418+
var node = getTouchingWord(sourceFile, position);
34193419
if (!node || node.kind !== SyntaxKind.ThisKeyword) {
34203420
return;
34213421
}
@@ -4229,7 +4229,7 @@ module ts {
42294229
var sourceFile = getCurrentSourceFile(filename);
42304230
var result: TypeScript.TextSpan[] = [];
42314231

4232-
var token = getTokenAtPosition(sourceFile, position);
4232+
var token = getTouchingToken(sourceFile, position);
42334233

42344234
if (token.getStart(sourceFile) === position) {
42354235
var matchKind = getMatchingTokenKind(token);
@@ -4513,7 +4513,7 @@ module ts {
45134513
fileName = TypeScript.switchToForwardSlashes(fileName);
45144514
var sourceFile = getSourceFile(fileName);
45154515

4516-
var node = getNodeAtPosition(sourceFile, position);
4516+
var node = getTouchingWord(sourceFile, position);
45174517

45184518
// Can only rename an identifier.
45194519
if (node && node.kind === SyntaxKind.Identifier) {

src/services/utilities.ts

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,34 +50,54 @@ module ts {
5050
return -1;
5151
}
5252

53-
/** Get a token that contains the position. This is guaranteed to return a token, the position can be in the
54-
* leading trivia or within the token text.
55-
*/
56-
export function getTokenAtPosition(sourceFile: SourceFile, position: number) {
57-
var current: Node = sourceFile;
58-
outer: while (true) {
59-
// find the child that has this
60-
for (var i = 0, n = current.getChildCount(); i < n; i++) {
61-
var child = current.getChildAt(i);
62-
if (child.getFullStart() <= position && position < child.getEnd()) {
63-
current = child;
64-
continue outer;
65-
}
66-
}
67-
return current;
68-
}
53+
/* Gets the token whose text has range [start, end) and
54+
* position >= start and (position < end or (position === end && token is keyword or identifier))
55+
*/
56+
export function getTouchingWord(sourceFile: SourceFile, position: number): Node {
57+
return getTouchingToken(sourceFile, position, isWord);
58+
}
59+
60+
/* Gets the token whose text has range [start, end) and position >= start
61+
* and (position < end or (position === end && token is keyword or identifier or numeric\string litera))
62+
*/
63+
export function getTouchingPropertyName(sourceFile: SourceFile, position: number): Node {
64+
return getTouchingToken(sourceFile, position, isPropertyName);
65+
}
66+
67+
/** Returns the token if position is in [start, end) or if position === end and includeItemAtEndPosition(token) === true */
68+
export function getTouchingToken(sourceFile: SourceFile, position: number, includeItemAtEndPosition?: (n: Node) => boolean): Node {
69+
return getTokenAtPositionWorker(sourceFile, position, /*allowPositionInLeadingTrivia*/ false, includeItemAtEndPosition);
6970
}
7071

71-
/** Get the token whose text contains the position, or the containing node. */
72-
export function getNodeAtPosition(sourceFile: SourceFile, position: number) {
72+
/** Returns a token if position is in [start-of-leading-trivia, end) */
73+
export function getTokenAtPosition(sourceFile: SourceFile, position: number): Node {
74+
return getTokenAtPositionWorker(sourceFile, position, /*allowPositionInLeadingTrivia*/ true, /*includeItemAtEndPosition*/ undefined);
75+
}
76+
77+
/** Get the token whose text contains the position */
78+
function getTokenAtPositionWorker(sourceFile: SourceFile, position: number, allowPositionInLeadingTrivia: boolean, includeItemAtEndPosition: (n: Node) => boolean): Node {
7379
var current: Node = sourceFile;
7480
outer: while (true) {
75-
// find the child that has this
76-
for (var i = 0, n = current.getChildCount(); i < n; i++) {
81+
if (isToken(current)) {
82+
// exit early
83+
return current;
84+
}
85+
86+
// find the child that contains 'position'
87+
for (var i = 0, n = current.getChildCount(sourceFile); i < n; i++) {
7788
var child = current.getChildAt(i);
78-
if (child.getStart() <= position && position < child.getEnd()) {
79-
current = child;
80-
continue outer;
89+
var start = allowPositionInLeadingTrivia ? child.getFullStart() : child.getStart(sourceFile);
90+
if (start <= position) {
91+
if (position < child.getEnd()) {
92+
current = child;
93+
continue outer;
94+
}
95+
else if (includeItemAtEndPosition && child.getEnd() === position) {
96+
var previousToken = findPrecedingToken(position, sourceFile, child);
97+
if (previousToken && includeItemAtEndPosition(previousToken)) {
98+
return previousToken;
99+
}
100+
}
81101
}
82102
}
83103
return current;
@@ -130,8 +150,8 @@ module ts {
130150
}
131151
}
132152

133-
export function findPrecedingToken(position: number, sourceFile: SourceFile): Node {
134-
return find(sourceFile);
153+
export function findPrecedingToken(position: number, sourceFile: SourceFile, startNode?: Node): Node {
154+
return find(startNode || sourceFile);
135155

136156
function findRightmostToken(n: Node): Node {
137157
if (isToken(n)) {
@@ -167,7 +187,7 @@ module ts {
167187
}
168188
}
169189

170-
Debug.assert(n.kind === SyntaxKind.SourceFile);
190+
Debug.assert(startNode || n.kind === SyntaxKind.SourceFile);
171191

172192
// Here we know that none of child token nodes embrace the position,
173193
// the only known case is when position is at the end of the file.
@@ -205,4 +225,16 @@ module ts {
205225
function isToken(n: Node): boolean {
206226
return n.kind >= SyntaxKind.FirstToken && n.kind <= SyntaxKind.LastToken;
207227
}
228+
229+
function isKeyword(n: Node): boolean {
230+
return n.kind >= SyntaxKind.FirstKeyword && n.kind <= SyntaxKind.LastKeyword;
231+
}
232+
233+
function isWord(n: Node): boolean {
234+
return n.kind === SyntaxKind.Identifier || isKeyword(n);
235+
}
236+
237+
function isPropertyName(n: Node): boolean {
238+
return n.kind === SyntaxKind.StringLiteral || n.kind === SyntaxKind.NumericLiteral || isWord(n);
239+
}
208240
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////if/*1*/ (true) {
4+
//// if/*2*/ (false) {
5+
//// }
6+
//// else/*3*/ {
7+
//// }
8+
//// if/*4*/ (true) {
9+
//// }
10+
//// else/*5*/ {
11+
//// if/*6*/ (false)
12+
//// if/*7*/ (true)
13+
//// var x = undefined;
14+
//// }
15+
////}
16+
////else/*8*/ if (null) {
17+
////}
18+
////else/*9*/ /* whar garbl */ if/*10*/ (undefined) {
19+
////}
20+
////else/*11*/
21+
////if/*12*/ (false) {
22+
////}
23+
////else/*13*/ { }
24+
25+
function verifyOccurencesAtMarker(marker: string, count: number) {
26+
goTo.marker(marker);
27+
verify.occurrencesAtPositionCount(count);
28+
}
29+
30+
verifyOccurencesAtMarker("1", 7);
31+
verifyOccurencesAtMarker("2", 2);
32+
verifyOccurencesAtMarker("3", 2);
33+
verifyOccurencesAtMarker("4", 2);
34+
verifyOccurencesAtMarker("5", 2);
35+
verifyOccurencesAtMarker("6", 1);
36+
verifyOccurencesAtMarker("7", 1);
37+
verifyOccurencesAtMarker("8", 7);
38+
verifyOccurencesAtMarker("9", 7);
39+
verifyOccurencesAtMarker("10", 7);
40+
verifyOccurencesAtMarker("11", 7);
41+
verifyOccurencesAtMarker("12", 7);
42+
verifyOccurencesAtMarker("13", 7);

tests/cases/fourslash/getOccurrencesIfElseNegatives.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////var arr = [1, 2, 3, 4];
4+
////label1: for (var n in arr) {
5+
//// break;
6+
//// continue;
7+
//// break label1;
8+
//// continue label1;
9+
////
10+
//// label2: for (var i = 0; i < arr[n]; i++) {
11+
//// break label1;
12+
//// continue label1;
13+
////
14+
//// break;
15+
//// continue;
16+
//// break label2;
17+
//// continue label2;
18+
////
19+
//// function foo() {
20+
//// label3: while (true) {
21+
//// break;
22+
//// continue;
23+
//// break label3;
24+
//// continue label3;
25+
////
26+
//// // these cross function boundaries
27+
//// br/*1*/eak label1;
28+
//// cont/*2*/inue label1;
29+
//// bre/*3*/ak label2;
30+
//// c/*4*/ontinue label2;
31+
////
32+
//// label4: do {
33+
//// break;
34+
//// continue;
35+
//// break label4;
36+
//// continue label4;
37+
////
38+
//// break label3;
39+
//// continue label3;
40+
////
41+
//// switch (10) {
42+
//// case 1:
43+
//// case 2:
44+
//// break;
45+
//// break label4;
46+
//// default:
47+
//// continue;
48+
//// }
49+
////
50+
//// // these cross function boundaries
51+
//// br/*5*/eak label1;
52+
//// co/*6*/ntinue label1;
53+
//// br/*7*/eak label2;
54+
//// con/*8*/tinue label2;
55+
//// () => { b/*9*/reak; }
56+
//// } while (true)
57+
//// }
58+
//// }
59+
//// }
60+
////}
61+
////
62+
////label5: while (true) break label5;
63+
////
64+
////label7: while (true) co/*10*/ntinue label5;
65+
66+
test.markers().forEach(m => {
67+
goTo.position(m.position);
68+
69+
verify.occurrencesAtPositionCount(0);
70+
});

tests/cases/fourslash/getOccurrencesReturnNegatives.ts renamed to tests/cases/fourslash/getOccurrencesReturn4.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@
1919
//// return/*7*/ true;
2020
////}
2121

22-
test.markers().forEach(m => {
23-
goTo.position(m.position, m.fileName)
24-
verify.occurrencesAtPositionCount(0);
25-
});
22+
function verifyOccurencesAtMarker(marker: string, count: number) {
23+
goTo.marker(marker);
24+
verify.occurrencesAtPositionCount(count);
25+
}
26+
27+
verifyOccurencesAtMarker("1", 4);
28+
verifyOccurencesAtMarker("2", 4);
29+
verifyOccurencesAtMarker("3", 4);
30+
verifyOccurencesAtMarker("4", 4);
31+
verifyOccurencesAtMarker("5", 1);
32+
verifyOccurencesAtMarker("6", 3);
33+
verifyOccurencesAtMarker("7", 3);

0 commit comments

Comments
 (0)