From 64fbf94940794355bf8a64bcca4264baa7e030c0 Mon Sep 17 00:00:00 2001 From: Ahmad Farid Date: Thu, 2 Apr 2015 16:23:22 -0700 Subject: [PATCH] outlinging comments v0.2 --- src/compiler/types.ts | 2 +- src/services/outliningElementsCollector.ts | 45 ++++++++++++++-------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 5a07956908c..bd43006f4e8 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -969,7 +969,7 @@ module ts { kind: SyntaxKind; } - //* Source files are declarations when they are external modules. + // Source files are declarations when they are external modules. export interface SourceFile extends Declaration { statements: NodeArray; endOfFileToken: Node; diff --git a/src/services/outliningElementsCollector.ts b/src/services/outliningElementsCollector.ts index ccba1b5c598..6e0d6434398 100644 --- a/src/services/outliningElementsCollector.ts +++ b/src/services/outliningElementsCollector.ts @@ -32,9 +32,8 @@ module ts { } function addOutliningForLeadingCommentsForNode(n: Node) { - let comments = ts.getLeadingCommentRangesOfNode(n, sourceFile); - - // if we found comments + let comments = ts.getLeadingCommentRangesOfNode(n, sourceFile); + if (comments) { let firstSingleLineCommentStart = -1; let lastSingleLineCommentEnd = -1; @@ -44,25 +43,42 @@ module ts { for (let i = 0; i < comments.length; i++) { let currentComment = comments[i]; - if (currentComment.kind == SyntaxKind.SingleLineCommentTrivia) { + // For single line comments, combine consecutive ones (2 or more) into + // a single span from the start of the first till the end of the last + if (currentComment.kind === SyntaxKind.SingleLineCommentTrivia) { if (isFirstSingleLineComment) { firstSingleLineCommentStart = currentComment.pos; } - + isFirstSingleLineComment = false; lastSingleLineCommentEnd = currentComment.end; singleLineCommentCount++; - } - - if (currentComment.kind == SyntaxKind.MultiLineCommentTrivia) - { - // add the block + } + else { + combineAndAddMultipleSingleLineComments(singleLineCommentCount, firstSingleLineCommentStart, lastSingleLineCommentEnd); addOutliningSpanComments(currentComment, false); - // see if we have multiple single line ones + singleLineCommentCount = 0; + lastSingleLineCommentEnd = -1; + isFirstSingleLineComment = true; } - - } + + combineAndAddMultipleSingleLineComments(singleLineCommentCount, firstSingleLineCommentStart, lastSingleLineCommentEnd); + } + } + + function combineAndAddMultipleSingleLineComments(count: number, start: number, end: number) { + + // Only outline spans of two or more consecutive single line comments + if (count > 1) { + + let multipleSingleLineComments = { + pos: start, + end: end, + kind: SyntaxKind.SingleLineCommentTrivia + } + + addOutliningSpanComments(multipleSingleLineComments, false); } } @@ -153,9 +169,6 @@ module ts { let closeBracket = findChildOfKind(n, SyntaxKind.CloseBracketToken, sourceFile); addOutliningSpan(n, openBracket, closeBracket, autoCollapse(n)); break; - case SyntaxKind.Constructor: - // addOutliningForLeadingCommentsForNode(n); - break; } depth++; forEachChild(n, walk);