@@ -354,6 +354,80 @@ module ts {
354354 }
355355 }
356356
357+ // Extract comments from the given source text starting at the given position. If trailing is false, whitespace is skipped until
358+ // the first line break and comments between that location and the next token are returned. If trailing is true, comments occurring
359+ // between the given position and the next line break are returned. The return value is an array containing a TextRange for each
360+ // comment. Single-line comment ranges include the the beginning '//' characters but not the ending line break. Multi-line comment
361+ // ranges include the beginning '/* and ending '*/' characters. The return value is undefined if no comments were found.
362+ function getCommentRanges ( text : string , pos : number , trailing : boolean ) : TextRange [ ] {
363+ var result : TextRange [ ] ;
364+ var collecting = trailing ;
365+ while ( true ) {
366+ var ch = text . charCodeAt ( pos ) ;
367+ switch ( ch ) {
368+ case CharacterCodes . carriageReturn :
369+ if ( text . charCodeAt ( pos + 1 ) === CharacterCodes . lineFeed ) pos ++ ;
370+ case CharacterCodes . lineFeed :
371+ pos ++ ;
372+ if ( trailing ) {
373+ return result ;
374+ }
375+ collecting = true ;
376+ continue ;
377+ case CharacterCodes . tab :
378+ case CharacterCodes . verticalTab :
379+ case CharacterCodes . formFeed :
380+ case CharacterCodes . space :
381+ pos ++ ;
382+ continue ;
383+ case CharacterCodes . slash :
384+ var nextChar = text . charCodeAt ( pos + 1 ) ;
385+ if ( nextChar === CharacterCodes . slash || nextChar === CharacterCodes . asterisk ) {
386+ var startPos = pos ;
387+ pos += 2 ;
388+ if ( nextChar === CharacterCodes . slash ) {
389+ while ( pos < text . length ) {
390+ if ( isLineBreak ( text . charCodeAt ( pos ) ) ) {
391+ break ;
392+ }
393+ pos ++ ;
394+ }
395+ }
396+ else {
397+ while ( pos < text . length ) {
398+ if ( text . charCodeAt ( pos ) === CharacterCodes . asterisk && text . charCodeAt ( pos + 1 ) === CharacterCodes . slash ) {
399+ pos += 2 ;
400+ break ;
401+ }
402+ pos ++ ;
403+ }
404+ }
405+ if ( collecting ) {
406+ if ( ! result ) result = [ ] ;
407+ result . push ( { pos : startPos , end : pos } ) ;
408+ }
409+ continue ;
410+ }
411+ break ;
412+ default :
413+ if ( ch > CharacterCodes . maxAsciiCharacter && ( isWhiteSpace ( ch ) || isLineBreak ( ch ) ) ) {
414+ pos ++ ;
415+ continue ;
416+ }
417+ break ;
418+ }
419+ return result ;
420+ }
421+ }
422+
423+ export function getLeadingComments ( text : string , pos : number ) : TextRange [ ] {
424+ return getCommentRanges ( text , pos , /*trailing*/ false ) ;
425+ }
426+
427+ export function getTrailingComments ( text : string , pos : number ) : TextRange [ ] {
428+ return getCommentRanges ( text , pos , /*trailing*/ true ) ;
429+ }
430+
357431 export function createScanner ( languageVersion : ScriptTarget , text ?: string , onError ?: ErrorCallback , onComment ?: CommentCallback ) : Scanner {
358432 var pos : number ; // Current position (end position of text of current token)
359433 var len : number ; // Length of text
0 commit comments