@@ -256,37 +256,6 @@ namespace ts {
256
256
getExternalModuleImportEqualsDeclarationExpression ( node . parent . parent ) === node ;
257
257
}
258
258
259
- /** Returns true if the position is within a comment */
260
- export function isInsideComment ( sourceFile : SourceFile , token : Node , position : number ) : boolean {
261
- // The position has to be: 1. in the leading trivia (before token.getStart()), and 2. within a comment
262
- return position <= token . getStart ( sourceFile ) &&
263
- ( isInsideCommentRange ( getTrailingCommentRanges ( sourceFile . text , token . getFullStart ( ) ) ) ||
264
- isInsideCommentRange ( getLeadingCommentRanges ( sourceFile . text , token . getFullStart ( ) ) ) ) ;
265
-
266
- function isInsideCommentRange ( comments : CommentRange [ ] ) : boolean {
267
- return forEach ( comments , comment => {
268
- // either we are 1. completely inside the comment, or 2. at the end of the comment
269
- if ( comment . pos < position && position < comment . end ) {
270
- return true ;
271
- }
272
- else if ( position === comment . end ) {
273
- const text = sourceFile . text ;
274
- const width = comment . end - comment . pos ;
275
- // is single line comment or just /*
276
- if ( width <= 2 || text . charCodeAt ( comment . pos + 1 ) === CharacterCodes . slash ) {
277
- return true ;
278
- }
279
- else {
280
- // is unterminated multi-line comment
281
- return ! ( text . charCodeAt ( comment . end - 1 ) === CharacterCodes . slash &&
282
- text . charCodeAt ( comment . end - 2 ) === CharacterCodes . asterisk ) ;
283
- }
284
- }
285
- return false ;
286
- } ) ;
287
- }
288
- }
289
-
290
259
export function getContainerNode ( node : Node ) : Declaration {
291
260
while ( true ) {
292
261
node = node . parent ;
@@ -834,10 +803,6 @@ namespace ts {
834
803
return false ;
835
804
}
836
805
837
- export function isInComment ( sourceFile : SourceFile , position : number ) {
838
- return isInCommentHelper ( sourceFile , position , /*predicate*/ undefined ) ;
839
- }
840
-
841
806
/**
842
807
* returns true if the position is in between the open and close elements of an JSX expression.
843
808
*/
@@ -883,15 +848,26 @@ namespace ts {
883
848
}
884
849
885
850
/**
886
- * Returns true if the cursor at position in sourceFile is within a comment that additionally
887
- * satisfies predicate, and false otherwise.
851
+ * Returns true if the cursor at position in sourceFile is within a comment.
852
+ *
853
+ * @param tokenAtPosition Must equal `getTokenAtPosition(sourceFile, position)
854
+ * @param predicate Additional predicate to test on the comment range.
888
855
*/
889
- export function isInCommentHelper ( sourceFile : SourceFile , position : number , predicate ?: ( c : CommentRange ) => boolean ) : boolean {
890
- const token = getTokenAtPosition ( sourceFile , position ) ;
856
+ export function isInComment ( sourceFile : SourceFile , position : number , tokenAtPosition = getTokenAtPosition ( sourceFile , position ) , predicate ?: ( c : CommentRange ) => boolean ) : boolean {
857
+ return position <= tokenAtPosition . getStart ( sourceFile ) &&
858
+ ( isInCommentRange ( getLeadingCommentRanges ( sourceFile . text , tokenAtPosition . pos ) ) ||
859
+ isInCommentRange ( getTrailingCommentRanges ( sourceFile . text , tokenAtPosition . pos ) ) ) ;
891
860
892
- if ( token && position <= token . getStart ( sourceFile ) ) {
893
- const commentRanges = getLeadingCommentRanges ( sourceFile . text , token . pos ) ;
861
+ function isInCommentRange ( commentRanges : CommentRange [ ] ) : boolean {
862
+ return forEach ( commentRanges , c => isPositionInCommentRange ( c , position , sourceFile . text ) && ( ! predicate || predicate ( c ) ) ) ;
863
+ }
864
+ }
894
865
866
+ function isPositionInCommentRange ( { pos, end, kind } : ts . CommentRange , position : number , text : string ) : boolean {
867
+ if ( pos < position && position < end ) {
868
+ return true ;
869
+ }
870
+ else if ( position === end ) {
895
871
// The end marker of a single-line comment does not include the newline character.
896
872
// In the following case, we are inside a comment (^ denotes the cursor position):
897
873
//
@@ -902,15 +878,13 @@ namespace ts {
902
878
// /* asdf */^
903
879
//
904
880
// Internally, we represent the end of the comment at the newline and closing '/', respectively.
905
- return predicate ?
906
- forEach ( commentRanges , c => c . pos < position &&
907
- ( c . kind === SyntaxKind . SingleLineCommentTrivia ? position <= c . end : position < c . end ) &&
908
- predicate ( c ) ) :
909
- forEach ( commentRanges , c => c . pos < position &&
910
- ( c . kind === SyntaxKind . SingleLineCommentTrivia ? position <= c . end : position < c . end ) ) ;
881
+ return kind === SyntaxKind . SingleLineCommentTrivia ||
882
+ // true for unterminated multi-line comment
883
+ ! ( text . charCodeAt ( end - 1 ) === CharacterCodes . slash && text . charCodeAt ( end - 2 ) === CharacterCodes . asterisk ) ;
884
+ }
885
+ else {
886
+ return false ;
911
887
}
912
-
913
- return false ;
914
888
}
915
889
916
890
export function hasDocComment ( sourceFile : SourceFile , position : number ) {
@@ -1093,21 +1067,17 @@ namespace ts {
1093
1067
}
1094
1068
1095
1069
export function isInReferenceComment ( sourceFile : SourceFile , position : number ) : boolean {
1096
- return isInCommentHelper ( sourceFile , position , isReferenceComment ) ;
1097
-
1098
- function isReferenceComment ( c : CommentRange ) : boolean {
1070
+ return isInComment ( sourceFile , position , /*tokenAtPosition*/ undefined , c => {
1099
1071
const commentText = sourceFile . text . substring ( c . pos , c . end ) ;
1100
1072
return tripleSlashDirectivePrefixRegex . test ( commentText ) ;
1101
- }
1073
+ } ) ;
1102
1074
}
1103
1075
1104
1076
export function isInNonReferenceComment ( sourceFile : SourceFile , position : number ) : boolean {
1105
- return isInCommentHelper ( sourceFile , position , isNonReferenceComment ) ;
1106
-
1107
- function isNonReferenceComment ( c : CommentRange ) : boolean {
1077
+ return isInComment ( sourceFile , position , /*tokenAtPosition*/ undefined , c => {
1108
1078
const commentText = sourceFile . text . substring ( c . pos , c . end ) ;
1109
1079
return ! tripleSlashDirectivePrefixRegex . test ( commentText ) ;
1110
- }
1080
+ } ) ;
1111
1081
}
1112
1082
1113
1083
export function createTextSpanFromNode ( node : Node , sourceFile ?: SourceFile ) : TextSpan {
0 commit comments