Skip to content

Commit 1d1e868

Browse files
committed
rename getTokenAtPosition to getTouchingToken, add a few helpers for common tasks
1 parent c116faa commit 1d1e868

File tree

2 files changed

+43
-51
lines changed

2 files changed

+43
-51
lines changed

src/services/services.ts

Lines changed: 11 additions & 50 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, /*includeItemAtEndPosition*/ undefined);
2091+
var mappedNode = getTokenAtPosition(sourceFile, TypeScript.end(node) - 1);
20922092
if (isPunctuation(mappedNode.kind)) {
20932093
mappedNode = mappedNode.parent;
20942094
}
@@ -2325,7 +2325,7 @@ module ts {
23252325

23262326
fileName = TypeScript.switchToForwardSlashes(fileName);
23272327
var sourceFile = getSourceFile(fileName);
2328-
var node = getTokenAtPosition(sourceFile, position, /*includeItemAtEndPosition*/ undefined);
2328+
var node = getTouchingPropertyName(sourceFile, position);
23292329
if (!node) {
23302330
return undefined;
23312331
}
@@ -2434,7 +2434,7 @@ module ts {
24342434

24352435
fileName = TypeScript.switchToForwardSlashes(fileName);
24362436
var sourceFile = getSourceFile(fileName);
2437-
var node = getTokenAtPosition(sourceFile, position, /*includeItemAtEndPosition*/ undefined);
2437+
var node = getTouchingWord(sourceFile, position);
24382438
if (!node) {
24392439
return undefined;
24402440
}
@@ -2512,25 +2512,12 @@ module ts {
25122512
return false;
25132513
}
25142514

2515-
function isValidGotoDefinitionTarget(n: Node): boolean {
2516-
switch (n.kind) {
2517-
case SyntaxKind.Identifier:
2518-
case SyntaxKind.SuperKeyword:
2519-
case SyntaxKind.ThisKeyword:
2520-
case SyntaxKind.NumericLiteral:
2521-
case SyntaxKind.StringLiteral:
2522-
return true;
2523-
default:
2524-
return false;
2525-
}
2526-
}
2527-
25282515
synchronizeHostData();
25292516

25302517
filename = TypeScript.switchToForwardSlashes(filename);
25312518
var sourceFile = getSourceFile(filename);
25322519

2533-
var node = getTokenAtPosition(sourceFile, position, isValidGotoDefinitionTarget);
2520+
var node = getTouchingPropertyName(sourceFile, position);
25342521
if (!node) {
25352522
return undefined;
25362523
}
@@ -2594,7 +2581,7 @@ module ts {
25942581
filename = TypeScript.switchToForwardSlashes(filename);
25952582
var sourceFile = getSourceFile(filename);
25962583

2597-
var node = getTokenAtPosition(sourceFile, position, isValidFindOccurencesTarget);
2584+
var node = getTouchingWord(sourceFile, position);
25982585
if (!node) {
25992586
return undefined;
26002587
}
@@ -2660,32 +2647,6 @@ module ts {
26602647

26612648
return undefined;
26622649

2663-
function isValidFindOccurencesTarget(n: Node): boolean {
2664-
switch (n.kind) {
2665-
case SyntaxKind.Identifier:
2666-
case SyntaxKind.SuperKeyword:
2667-
case SyntaxKind.ThisKeyword:
2668-
case SyntaxKind.IfKeyword:
2669-
case SyntaxKind.ElseKeyword:
2670-
case SyntaxKind.ReturnKeyword:
2671-
case SyntaxKind.TryKeyword:
2672-
case SyntaxKind.CatchKeyword:
2673-
case SyntaxKind.FinallyKeyword:
2674-
case SyntaxKind.SwitchKeyword:
2675-
case SyntaxKind.CaseKeyword:
2676-
case SyntaxKind.DefaultKeyword:
2677-
case SyntaxKind.BreakKeyword:
2678-
case SyntaxKind.ContinueKeyword:
2679-
case SyntaxKind.ForKeyword:
2680-
case SyntaxKind.WhileKeyword:
2681-
case SyntaxKind.DoKeyword:
2682-
case SyntaxKind.Constructor:
2683-
return true;
2684-
default:
2685-
return false;
2686-
}
2687-
}
2688-
26892650
function getIfElseOccurrences(ifStatement: IfStatement): ReferenceEntry[] {
26902651
var keywords: Node[] = [];
26912652

@@ -2942,7 +2903,7 @@ module ts {
29422903
filename = TypeScript.switchToForwardSlashes(filename);
29432904
var sourceFile = getSourceFile(filename);
29442905

2945-
var node = getTokenAtPosition(sourceFile, position, /*includeItemAtEndPosition*/ undefined);
2906+
var node = getTouchingPropertyName(sourceFile, position);
29462907
if (!node) {
29472908
return undefined;
29482909
}
@@ -3125,7 +3086,7 @@ module ts {
31253086
forEach(possiblePositions, position => {
31263087
cancellationToken.throwIfCancellationRequested();
31273088

3128-
var node = getTokenAtPosition(sourceFile, position, /*includeItemAtEndPosition*/ undefined);
3089+
var node = getTouchingWord(sourceFile, position);
31293090
if (!node || node.getWidth() !== labelName.length) {
31303091
return;
31313092
}
@@ -3181,7 +3142,7 @@ module ts {
31813142
forEach(possiblePositions, position => {
31823143
cancellationToken.throwIfCancellationRequested();
31833144

3184-
var referenceLocation = getTokenAtPosition(sourceFile, position, /*includeItemAtEndPosition*/ undefined);
3145+
var referenceLocation = getTouchingPropertyName(sourceFile, position);
31853146
if (!isValidReferencePosition(referenceLocation, searchText)) {
31863147
return;
31873148
}
@@ -3233,7 +3194,7 @@ module ts {
32333194
forEach(possiblePositions, position => {
32343195
cancellationToken.throwIfCancellationRequested();
32353196

3236-
var node = getTokenAtPosition(sourceFile, position, n => n.kind === SyntaxKind.SuperKeyword);
3197+
var node = getTouchingWord(sourceFile, position);
32373198

32383199
if (!node || node.kind !== SyntaxKind.SuperKeyword) {
32393200
return;
@@ -3299,7 +3260,7 @@ module ts {
32993260
forEach(possiblePositions, position => {
33003261
cancellationToken.throwIfCancellationRequested();
33013262

3302-
var node = getTokenAtPosition(sourceFile, position, n => n.kind === SyntaxKind.ThisKeyword);
3263+
var node = getTouchingWord(sourceFile, position);
33033264
if (!node || node.kind !== SyntaxKind.ThisKeyword) {
33043265
return;
33053266
}
@@ -4345,7 +4306,7 @@ module ts {
43454306
fileName = TypeScript.switchToForwardSlashes(fileName);
43464307
var sourceFile = getSourceFile(fileName);
43474308

4348-
var node = getTokenAtPosition(sourceFile, position, n => n.kind === SyntaxKind.Identifier);
4309+
var node = getTouchingWord(sourceFile, position);
43494310

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

src/services/utilities.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,27 @@ module ts {
6464
}
6565
}
6666

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+
72+
/* Gets the token whose text has range [start, end) and
73+
* position >= start and (position < end or (position === end && token is keyword or identifier))
74+
*/
75+
export function getTouchingWord(sourceFile: SourceFile, position: number): Node {
76+
return getTouchingToken(sourceFile, position, isWord);
77+
}
78+
79+
/* Gets the token whose text has range [start, end) and position >= start
80+
* and (position < end or (position === end && token is keyword or identifier or numeric\string litera))
81+
*/
82+
export function getTouchingPropertyName(sourceFile: SourceFile, position: number): Node {
83+
return getTouchingToken(sourceFile, position, isPropertyName);
84+
}
85+
6786
/** Get the token whose text contains the position */
68-
export function getTokenAtPosition(sourceFile: SourceFile, position: number, includeItemAtEndPosition: (n: Node) => boolean) {
87+
export function getTouchingToken(sourceFile: SourceFile, position: number, includeItemAtEndPosition: (n: Node) => boolean): Node {
6988
var current: Node = sourceFile;
7089
outer: while (true) {
7190
if (isToken(current)) {
@@ -214,4 +233,16 @@ module ts {
214233
function isToken(n: Node): boolean {
215234
return n.kind >= SyntaxKind.FirstToken && n.kind <= SyntaxKind.LastToken;
216235
}
236+
237+
function isKeyword(n: Node): boolean {
238+
return n.kind >= SyntaxKind.FirstKeyword && n.kind <= SyntaxKind.LastKeyword;
239+
}
240+
241+
function isWord(n: Node): boolean {
242+
return n.kind === SyntaxKind.Identifier || isKeyword(n);
243+
}
244+
245+
function isPropertyName(n: Node): boolean {
246+
return n.kind === SyntaxKind.StringLiteral || n.kind === SyntaxKind.NumericLiteral || isWord(n);
247+
}
217248
}

0 commit comments

Comments
 (0)