1- //
2- // Copyright (c) Microsoft Corporation. All rights reserved.
3- //
4- // Licensed under the Apache License, Version 2.0 (the "License");
5- // you may not use this file except in compliance with the License.
6- // You may obtain a copy of the License at
7- // http://www.apache.org/licenses/LICENSE-2.0
8- //
9- // Unless required by applicable law or agreed to in writing, software
10- // distributed under the License is distributed on an "AS IS" BASIS,
11- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12- // See the License for the specific language governing permissions and
13- // limitations under the License.
14- //
15-
161module ts {
172 export module OutliningElementsCollector {
183 export function collectElements ( sourceFile : SourceFile ) : OutliningSpan [ ] {
@@ -31,6 +16,66 @@ module ts {
3116 }
3217 }
3318
19+ function addOutliningSpanComments ( commentSpan : CommentRange , autoCollapse : boolean ) {
20+ if ( commentSpan ) {
21+ let span : OutliningSpan = {
22+ textSpan : createTextSpanFromBounds ( commentSpan . pos , commentSpan . end ) ,
23+ hintSpan : createTextSpanFromBounds ( commentSpan . pos , commentSpan . end ) ,
24+ bannerText : collapseText ,
25+ autoCollapse : autoCollapse
26+ } ;
27+ elements . push ( span ) ;
28+ }
29+ }
30+
31+ function addOutliningForLeadingCommentsForNode ( n : Node ) {
32+ let comments = ts . getLeadingCommentRangesOfNode ( n , sourceFile ) ;
33+
34+ if ( comments ) {
35+ let firstSingleLineCommentStart = - 1 ;
36+ let lastSingleLineCommentEnd = - 1 ;
37+ let isFirstSingleLineComment = true ;
38+ let singleLineCommentCount = 0 ;
39+
40+ for ( let currentComment of comments ) {
41+
42+ // For single line comments, combine consecutive ones (2 or more) into
43+ // a single span from the start of the first till the end of the last
44+ if ( currentComment . kind === SyntaxKind . SingleLineCommentTrivia ) {
45+ if ( isFirstSingleLineComment ) {
46+ firstSingleLineCommentStart = currentComment . pos ;
47+ }
48+ isFirstSingleLineComment = false ;
49+ lastSingleLineCommentEnd = currentComment . end ;
50+ singleLineCommentCount ++ ;
51+ }
52+ else if ( currentComment . kind === SyntaxKind . MultiLineCommentTrivia ) {
53+ combineAndAddMultipleSingleLineComments ( singleLineCommentCount , firstSingleLineCommentStart , lastSingleLineCommentEnd ) ;
54+ addOutliningSpanComments ( currentComment , /*autoCollapse*/ false ) ;
55+
56+ singleLineCommentCount = 0 ;
57+ lastSingleLineCommentEnd = - 1 ;
58+ isFirstSingleLineComment = true ;
59+ }
60+ }
61+
62+ combineAndAddMultipleSingleLineComments ( singleLineCommentCount , firstSingleLineCommentStart , lastSingleLineCommentEnd ) ;
63+ }
64+ }
65+
66+ function combineAndAddMultipleSingleLineComments ( count : number , start : number , end : number ) {
67+ // Only outline spans of two or more consecutive single line comments
68+ if ( count > 1 ) {
69+ let multipleSingleLineComments = {
70+ pos : start ,
71+ end : end ,
72+ kind : SyntaxKind . SingleLineCommentTrivia
73+ }
74+
75+ addOutliningSpanComments ( multipleSingleLineComments , /*autoCollapse*/ false ) ;
76+ }
77+ }
78+
3479 function autoCollapse ( node : Node ) {
3580 return isFunctionBlock ( node ) && node . parent . kind !== SyntaxKind . ArrowFunction ;
3681 }
@@ -41,6 +86,11 @@ module ts {
4186 if ( depth > maxDepth ) {
4287 return ;
4388 }
89+
90+ if ( isDeclaration ( n ) ) {
91+ addOutliningForLeadingCommentsForNode ( n ) ;
92+ }
93+
4494 switch ( n . kind ) {
4595 case SyntaxKind . Block :
4696 if ( ! isFunctionBlock ( n ) ) {
@@ -93,7 +143,7 @@ module ts {
93143 } ) ;
94144 break ;
95145 }
96- // Fallthrough.
146+ // Fallthrough.
97147
98148 case SyntaxKind . ModuleBlock : {
99149 let openBrace = findChildOfKind ( n , SyntaxKind . OpenBraceToken , sourceFile ) ;
0 commit comments