From 27f349ca7dfac7c0c02ee2c83aa8baee4a173637 Mon Sep 17 00:00:00 2001 From: Ahmad Farid Date: Thu, 2 Apr 2015 14:38:22 -0700 Subject: [PATCH] outlining Comments v0.1 4/2 --- src/compiler/parser.ts | 2 +- src/compiler/scanner.ts | 3 +- src/compiler/types.ts | 3 +- src/services/outliningElementsCollector.ts | 70 +++++++++++++++++----- 4 files changed, 60 insertions(+), 18 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 14311ccbc52..5e359ef462a 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -5195,7 +5195,7 @@ module ts { break; } - let range = { pos: triviaScanner.getTokenPos(), end: triviaScanner.getTextPos() }; + let range = { pos: triviaScanner.getTokenPos(), end: triviaScanner.getTextPos(), kind: triviaScanner.getToken() }; let comment = sourceText.substring(range.pos, range.end); let referencePathMatchResult = getFileReferenceFromReferencePath(comment, range); diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index 3a208698c25..1e7ce744bc2 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -523,6 +523,7 @@ module ts { let nextChar = text.charCodeAt(pos + 1); let hasTrailingNewLine = false; if (nextChar === CharacterCodes.slash || nextChar === CharacterCodes.asterisk) { + let kind = nextChar === CharacterCodes.slash ? SyntaxKind.SingleLineCommentTrivia : SyntaxKind.MultiLineCommentTrivia; let startPos = pos; pos += 2; if (nextChar === CharacterCodes.slash) { @@ -548,7 +549,7 @@ module ts { result = []; } - result.push({ pos: startPos, end: pos, hasTrailingNewLine: hasTrailingNewLine }); + result.push({ pos: startPos, end: pos, hasTrailingNewLine: hasTrailingNewLine, kind }); } continue; } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 8992a6fbd94..5a07956908c 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -966,9 +966,10 @@ module ts { export interface CommentRange extends TextRange { hasTrailingNewLine?: boolean; + 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 4c9dcedc7a4..ccba1b5c598 100644 --- a/src/services/outliningElementsCollector.ts +++ b/src/services/outliningElementsCollector.ts @@ -1,17 +1,5 @@ -// -// Copyright (c) Microsoft Corporation. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// + +/// module ts { export module OutliningElementsCollector { @@ -31,6 +19,53 @@ module ts { } } + function addOutliningSpanComments(commentSpan: CommentRange, autoCollapse: boolean) { + if (commentSpan) { + let span: OutliningSpan = { + textSpan: createTextSpanFromBounds(commentSpan.pos, commentSpan.end), + hintSpan: createTextSpanFromBounds(commentSpan.pos, commentSpan.end), + bannerText: collapseText, + autoCollapse: autoCollapse + }; + elements.push(span); + } + } + + function addOutliningForLeadingCommentsForNode(n: Node) { + let comments = ts.getLeadingCommentRangesOfNode(n, sourceFile); + + // if we found comments + if (comments) { + let firstSingleLineCommentStart = -1; + let lastSingleLineCommentEnd = -1; + let isFirstSingleLineComment = true; + let singleLineCommentCount = 0; + + for (let i = 0; i < comments.length; i++) { + let currentComment = comments[i]; + + if (currentComment.kind == SyntaxKind.SingleLineCommentTrivia) { + if (isFirstSingleLineComment) { + firstSingleLineCommentStart = currentComment.pos; + } + + lastSingleLineCommentEnd = currentComment.end; + singleLineCommentCount++; + } + + if (currentComment.kind == SyntaxKind.MultiLineCommentTrivia) + { + // add the block + addOutliningSpanComments(currentComment, false); + + // see if we have multiple single line ones + } + + + } + } + } + function autoCollapse(node: Node) { return isFunctionBlock(node) && node.parent.kind !== SyntaxKind.ArrowFunction; } @@ -41,6 +76,8 @@ module ts { if (depth > maxDepth) { return; } + + addOutliningForLeadingCommentsForNode(n); switch (n.kind) { case SyntaxKind.Block: if (!isFunctionBlock(n)) { @@ -93,7 +130,7 @@ module ts { }); break; } - // Fallthrough. + // Fallthrough. case SyntaxKind.ModuleBlock: { let openBrace = findChildOfKind(n, SyntaxKind.OpenBraceToken, sourceFile); @@ -116,6 +153,9 @@ 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);