From 01533905355a41edb585aaaa0e788a8ffffef00a Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Tue, 7 Oct 2014 15:07:49 -0700 Subject: [PATCH] Outlining spans for a standalone block shouldn't have the span of their parent. --- Jakefile | 3 +- src/services/outliningElementsCollector.ts | 34 ++++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/Jakefile b/Jakefile index 4f2808dcbd1..74f950be9e0 100644 --- a/Jakefile +++ b/Jakefile @@ -58,7 +58,8 @@ var servicesSources = [ "shims.ts", "signatureHelp.ts", "utilities.ts", - "navigationBar.ts" + "navigationBar.ts", + "outliningElementsCollector.ts" ].map(function (f) { return path.join(servicesDirectory, f); })); diff --git a/src/services/outliningElementsCollector.ts b/src/services/outliningElementsCollector.ts index c9a513ee2aa..aaaedb735ea 100644 --- a/src/services/outliningElementsCollector.ts +++ b/src/services/outliningElementsCollector.ts @@ -33,13 +33,14 @@ module ts { export module OutliningElementsCollector { export function collectElements(sourceFile: SourceFile): OutliningSpan[] { var elements: OutliningSpan[] = []; + var collapseText = "..."; function addOutliningSpan(hintSpanNode: Node, startElement: Node, endElement: Node, autoCollapse: boolean) { if (hintSpanNode && startElement && endElement) { var span: OutliningSpan = { textSpan: TypeScript.TextSpan.fromBounds(startElement.pos, endElement.end), hintSpan: TypeScript.TextSpan.fromBounds(hintSpanNode.getStart(), hintSpanNode.end), - bannerText: "...", + bannerText: collapseText, autoCollapse: autoCollapse }; elements.push(span); @@ -66,10 +67,39 @@ module ts { } switch (n.kind) { case SyntaxKind.Block: + var parent = n.parent; + var openBrace = findChildOfKind(n, SyntaxKind.OpenBraceToken, sourceFile); + var closeBrace = findChildOfKind(n, SyntaxKind.CloseBraceToken, sourceFile); + + // Check if the block is standalone, or 'attached' to some parent statement. + // If the latter, we want to collaps the block, but consider its hint span + // to be the entire span of the parent. + if (parent.kind === SyntaxKind.DoStatement || + parent.kind === SyntaxKind.ForInStatement || + parent.kind === SyntaxKind.ForStatement || + parent.kind === SyntaxKind.IfStatement || + parent.kind === SyntaxKind.WhileStatement || + parent.kind === SyntaxKind.WithStatement) { + + addOutliningSpan(parent, openBrace, closeBrace, autoCollapse(n)); + } + else { + // Block was a standalone block. In this case we want to only collapse + // the span of the block, independent of any parent span. + var span = TypeScript.TextSpan.fromBounds(n.getStart(), n.end); + elements.push({ + textSpan: span, + hintSpan: span, + bannerText: collapseText, + autoCollapse: autoCollapse(n) + }); + } + break; + + case SyntaxKind.FunctionBlock: case SyntaxKind.ModuleBlock: case SyntaxKind.TryBlock: - case SyntaxKind.TryBlock: case SyntaxKind.CatchBlock: case SyntaxKind.FinallyBlock: var openBrace = findChildOfKind(n, SyntaxKind.OpenBraceToken, sourceFile);