@@ -73,32 +73,30 @@ namespace ts.OutliningElementsCollector {
73
73
const lineStarts = sourceFile . getLineStarts ( ) ;
74
74
for ( let i = 0 ; i < lineStarts . length ; i ++ ) {
75
75
const currentLineStart = lineStarts [ i ] ;
76
- const lineEnd = i + 1 === lineStarts . length ? sourceFile . getEnd ( ) : lineStarts [ i + 1 ] - 1 ;
76
+ const newlinePosition = i + 1 === lineStarts . length ? sourceFile . getEnd ( ) : lineStarts [ i + 1 ] - 1 ;
77
+ // If the character before \n is \r, decrement line end by 1.
78
+ const lineEnd = sourceFile . text . charCodeAt ( newlinePosition - 1 ) === CharacterCodes . carriageReturn ? newlinePosition - 1 : newlinePosition ;
77
79
const lineText = sourceFile . text . substring ( currentLineStart , lineEnd ) ;
78
- const result = isRegionDelimiter ( lineText ) ;
79
- if ( ! result || isInComment ( sourceFile , currentLineStart ) ) {
80
- continue ;
81
- }
82
-
83
- if ( ! result [ 1 ] ) {
84
- const span = createTextSpanFromBounds ( sourceFile . text . indexOf ( "//" , currentLineStart ) , lineEnd ) ;
85
- regions . push ( createOutliningSpan ( span , OutliningSpanKind . Region , span , /*autoCollapse*/ false , result [ 2 ] || "#region" ) ) ;
86
- }
87
- else {
88
- const region = regions . pop ( ) ;
89
- if ( region ) {
90
- region . textSpan . length = lineEnd - region . textSpan . start ;
91
- region . hintSpan . length = lineEnd - region . textSpan . start ;
92
- out . push ( region ) ;
80
+ const regionDelimiterRegExp = / ^ \s * \/ \/ \s * # ( e n d ) ? r e g i o n (?: \s + ( .* ) ) ? (?: \r ) ? $ / ;
81
+ const result = regionDelimiterRegExp . exec ( lineText ) ;
82
+ if ( result && ! isInComment ( sourceFile , currentLineStart ) ) {
83
+ if ( result [ 1 ] ) {
84
+ const region = regions . pop ( ) ;
85
+ if ( region ) {
86
+ region . textSpan . length = lineEnd - region . textSpan . start ;
87
+ region . hintSpan . length = lineEnd - region . textSpan . start ;
88
+ out . push ( region ) ;
89
+ }
90
+ }
91
+ else {
92
+ const span = createTextSpanFromBounds ( sourceFile . text . indexOf ( "//" , currentLineStart ) , lineEnd ) ;
93
+ regions . push ( createOutliningSpan ( span , OutliningSpanKind . Region , span , /*autoCollapse*/ false , result [ 2 ] || "#region" ) ) ;
93
94
}
94
95
}
95
96
}
96
97
}
97
98
98
- const regionDelimiterRegExp = / ^ \s * \/ \/ \s * # ( e n d ) ? r e g i o n (?: \s + ( .* ) ) ? (?: \r ) ? $ / ;
99
- function isRegionDelimiter ( lineText : string ) {
100
- return regionDelimiterRegExp . exec ( lineText ) ;
101
- }
99
+
102
100
103
101
function addOutliningForLeadingCommentsForNode ( n : Node , sourceFile : SourceFile , cancellationToken : CancellationToken , out : Push < OutliningSpan > ) : void {
104
102
const comments = getLeadingCommentRangesOfNode ( n , sourceFile ) ;
@@ -113,7 +111,9 @@ namespace ts.OutliningElementsCollector {
113
111
case SyntaxKind . SingleLineCommentTrivia :
114
112
// never fold region delimiters into single-line comment regions
115
113
const commentText = sourceText . slice ( pos , end ) ;
116
- if ( isRegionDelimiter ( commentText ) ) {
114
+ const regionDelimiterRegExp = / ^ \s * \/ \/ \s * # ( e n d ) ? r e g i o n (?: \s + ( .* ) ) ? (?: \r ) ? $ / ;
115
+ const result = regionDelimiterRegExp . exec ( commentText ) ;
116
+ if ( result ) {
117
117
combineAndAddMultipleSingleLineComments ( ) ;
118
118
singleLineCommentCount = 0 ;
119
119
break ;
@@ -178,7 +178,7 @@ namespace ts.OutliningElementsCollector {
178
178
else if ( tryStatement . finallyBlock === n ) {
179
179
return spanForNode ( findChildOfKind ( tryStatement , SyntaxKind . FinallyKeyword , sourceFile ) ! ) ;
180
180
}
181
- // falls through
181
+ // falls through
182
182
default :
183
183
// Block was a standalone block. In this case we want to only collapse
184
184
// the span of the block, independent of any parent span.
0 commit comments