@@ -46,45 +46,22 @@ module ts {
46
46
return - 1 ;
47
47
}
48
48
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
-
72
49
/* Gets the token whose text has range [start, end) and
73
50
* position >= start and (position < end or (position === end && token is keyword or identifier))
74
51
*/
75
52
export function getTouchingWord ( sourceFile : SourceFile , position : number ) : Node {
76
- return getTouchingToken ( sourceFile , position , isWord ) ;
53
+ return getTouchingToken ( sourceFile , position , /*allowPositionInLeadingTrivia*/ false , isWord ) ;
77
54
}
78
55
79
56
/* Gets the token whose text has range [start, end) and position >= start
80
57
* and (position < end or (position === end && token is keyword or identifier or numeric\string litera))
81
58
*/
82
59
export function getTouchingPropertyName ( sourceFile : SourceFile , position : number ) : Node {
83
- return getTouchingToken ( sourceFile , position , isPropertyName ) ;
60
+ return getTouchingToken ( sourceFile , position , /*allowPositionInLeadingTrivia*/ false , isPropertyName ) ;
84
61
}
85
62
86
63
/** 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 {
88
65
var current : Node = sourceFile ;
89
66
outer: while ( true ) {
90
67
if ( isToken ( current ) ) {
@@ -95,7 +72,8 @@ module ts {
95
72
// find the child that contains 'position'
96
73
for ( var i = 0 , n = current . getChildCount ( sourceFile ) ; i < n ; i ++ ) {
97
74
var child = current . getChildAt ( i ) ;
98
- if ( child . getStart ( sourceFile ) <= position ) {
75
+ var start = allowPositionInLeadingTrivia ? child . getFullStart ( ) : child . getStart ( sourceFile ) ;
76
+ if ( start <= position ) {
99
77
if ( position < child . getEnd ( ) ) {
100
78
current = child ;
101
79
continue outer;
@@ -123,7 +101,7 @@ module ts {
123
101
export function findTokenOnLeftOfPosition ( file : SourceFile , position : number ) : Node {
124
102
// Ideally, getTokenAtPosition should return a token. However, it is currently
125
103
// 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 ) ;
127
105
if ( isToken ( tokenAtPosition ) && position > tokenAtPosition . getStart ( file ) && position < tokenAtPosition . getEnd ( ) ) {
128
106
return tokenAtPosition ;
129
107
}
0 commit comments