@@ -266,37 +266,45 @@ module ts {
266266 /* Gets the token whose text has range [start, end) and position >= start
267267 * and (position < end or (position === end && token is keyword or identifier or numeric\string litera))
268268 */
269- export function getTouchingPropertyName ( sourceFile : SourceFile , position : number ) : Node {
270- return getTouchingToken ( sourceFile , position , n => isPropertyName ( n . kind ) ) ;
269+ export function getTouchingPropertyName ( sourceFile : SourceFile , position : number , beignWithLastTokenPath = false ) : Node {
270+ return getTouchingToken ( sourceFile , position , n => isPropertyName ( n . kind ) , /*beignWithLastTokenPath*/ beignWithLastTokenPath ) ;
271271 }
272272
273273 /** Returns the token if position is in [start, end) or if position === end and includeItemAtEndPosition(token) === true */
274- export function getTouchingToken ( sourceFile : SourceFile , position : number , includeItemAtEndPosition ?: ( n : Node ) => boolean ) : Node {
275- return getTokenAtPositionWorker ( sourceFile , position , /*allowPositionInLeadingTrivia*/ false , includeItemAtEndPosition ) ;
274+ export function getTouchingToken ( sourceFile : SourceFile , position : number , includeItemAtEndPosition ?: ( n : Node ) => boolean , beignWithLastTokenPath = false ) : Node {
275+ return getTokenAtPositionWorker ( sourceFile , position , /*allowPositionInLeadingTrivia*/ false , includeItemAtEndPosition , /*beignWithLastTokenPath*/ beignWithLastTokenPath ) ;
276276 }
277277
278278 /** Returns a token if position is in [start-of-leading-trivia, end) */
279279 export function getTokenAtPosition ( sourceFile : SourceFile , position : number ) : Node {
280280 return getTokenAtPositionWorker ( sourceFile , position , /*allowPositionInLeadingTrivia*/ true , /*includeItemAtEndPosition*/ undefined ) ;
281281 }
282+
283+ export let lastTokenPath : { [ depth : number ] : number } = { } ;
282284
283285 /** Get the token whose text contains the position */
284- function getTokenAtPositionWorker ( sourceFile : SourceFile , position : number , allowPositionInLeadingTrivia : boolean , includeItemAtEndPosition : ( n : Node ) => boolean ) : Node {
286+ function getTokenAtPositionWorker ( sourceFile : SourceFile , position : number , allowPositionInLeadingTrivia : boolean , includeItemAtEndPosition : ( n : Node ) => boolean , beignWithLastTokenPath = false ) : Node {
285287 let current : Node = sourceFile ;
288+ let depthLevel = 0 ;
289+ if ( ! beignWithLastTokenPath ) {
290+ lastTokenPath = { } ;
291+ }
286292 outer: while ( true ) {
287- if ( isToken ( current ) ) {
288- // exit early
293+ if ( isReservedWord ( current ) ) {
289294 return current ;
290295 }
291-
292- // find the child that contains 'position'
296+
297+ // Find the child that contains 'position' and it will begin with the old token path if specified.
298+ let start = beignWithLastTokenPath && lastTokenPath [ depthLevel ] ? lastTokenPath [ depthLevel ] : 0 ;
293299 for ( let i = 0 , n = current . getChildCount ( sourceFile ) ; i < n ; i ++ ) {
294300 let child = current . getChildAt ( i ) ;
295301 let start = allowPositionInLeadingTrivia ? child . getFullStart ( ) : child . getStart ( sourceFile ) ;
296302 if ( start <= position ) {
297303 let end = child . getEnd ( ) ;
298304 if ( position < end || ( position === end && child . kind === SyntaxKind . EndOfFileToken ) ) {
299305 current = child ;
306+ lastTokenPath [ depthLevel ] = i ;
307+ depthLevel ++ ;
300308 continue outer;
301309 }
302310 else if ( includeItemAtEndPosition && end === position ) {
@@ -323,7 +331,7 @@ module ts {
323331 // Ideally, getTokenAtPosition should return a token. However, it is currently
324332 // broken, so we do a check to make sure the result was indeed a token.
325333 let tokenAtPosition = getTokenAtPosition ( file , position ) ;
326- if ( isToken ( tokenAtPosition ) && position > tokenAtPosition . getStart ( file ) && position < tokenAtPosition . getEnd ( ) ) {
334+ if ( isReservedWord ( tokenAtPosition ) && position > tokenAtPosition . getStart ( file ) && position < tokenAtPosition . getEnd ( ) ) {
327335 return tokenAtPosition ;
328336 }
329337
@@ -334,7 +342,7 @@ module ts {
334342 return find ( parent ) ;
335343
336344 function find ( n : Node ) : Node {
337- if ( isToken ( n ) && n . pos === previousToken . end ) {
345+ if ( isReservedWord ( n ) && n . pos === previousToken . end ) {
338346 // this is token that starts at the end of previous token - return it
339347 return n ;
340348 }
@@ -360,7 +368,7 @@ module ts {
360368 return find ( startNode || sourceFile ) ;
361369
362370 function findRightmostToken ( n : Node ) : Node {
363- if ( isToken ( n ) ) {
371+ if ( isReservedWord ( n ) ) {
364372 return n ;
365373 }
366374
@@ -371,7 +379,7 @@ module ts {
371379 }
372380
373381 function find ( n : Node ) : Node {
374- if ( isToken ( n ) ) {
382+ if ( isReservedWord ( n ) ) {
375383 return n ;
376384 }
377385
@@ -447,7 +455,7 @@ module ts {
447455 return undefined ;
448456 }
449457
450- export function isToken ( n : Node ) : boolean {
458+ export function isReservedWord ( n : Node ) : boolean {
451459 return n . kind >= SyntaxKind . FirstToken && n . kind <= SyntaxKind . LastToken ;
452460 }
453461
0 commit comments