Add explanatory comments, consolidate main body

This commit is contained in:
uniqueiniquity 2017-08-14 09:27:45 -07:00
parent d6ccee6766
commit 760812f714
2 changed files with 15 additions and 11 deletions

View file

@ -3,11 +3,17 @@ namespace ts.OutliningElementsCollector {
export function collectElements(sourceFile: SourceFile, cancellationToken: CancellationToken): OutliningSpan[] { export function collectElements(sourceFile: SourceFile, cancellationToken: CancellationToken): OutliningSpan[] {
const elements: OutliningSpan[] = []; const elements: OutliningSpan[] = [];
const collapseText = "..."; const collapseText = "...";
let depth = 0;
const maxDepth = 20;
function addOutliningSpan(hintSpanNode: Node, startElement: Node, endElement: Node, autoCollapse: boolean, fullStart: boolean) { walk(sourceFile);
return elements;
// If useFullStart is true, then the collapsing span includes leading whitespace, including linebreaks.
function addOutliningSpan(hintSpanNode: Node, startElement: Node, endElement: Node, autoCollapse: boolean, useFullStart: boolean) {
if (hintSpanNode && startElement && endElement) { if (hintSpanNode && startElement && endElement) {
const span: OutliningSpan = { const span: OutliningSpan = {
textSpan: createTextSpanFromBounds(fullStart ? startElement.pos : startElement.getStart(), endElement.end), textSpan: createTextSpanFromBounds(useFullStart ? startElement.pos : startElement.getStart(), endElement.end),
hintSpan: createTextSpanFromBounds(startElement.getStart(), endElement.end), hintSpan: createTextSpanFromBounds(startElement.getStart(), endElement.end),
bannerText: collapseText, bannerText: collapseText,
autoCollapse, autoCollapse,
@ -82,8 +88,6 @@ namespace ts.OutliningElementsCollector {
return isFunctionBlock(node) && node.parent.kind !== SyntaxKind.ArrowFunction; return isFunctionBlock(node) && node.parent.kind !== SyntaxKind.ArrowFunction;
} }
let depth = 0;
const maxDepth = 20;
function walk(n: Node): void { function walk(n: Node): void {
cancellationToken.throwIfCancellationRequested(); cancellationToken.throwIfCancellationRequested();
if (depth > maxDepth) { if (depth > maxDepth) {
@ -163,6 +167,9 @@ namespace ts.OutliningElementsCollector {
addOutliningSpan(n, openBrace, closeBrace, autoCollapse(n), /* fullStart */ true); addOutliningSpan(n, openBrace, closeBrace, autoCollapse(n), /* fullStart */ true);
break; break;
} }
// If the block has no leading keywords and is a member of an array literal,
// we again want to only collapse the span of the block.
// Otherwise, the collapsed section will include the end of the previous line.
case SyntaxKind.ObjectLiteralExpression: case SyntaxKind.ObjectLiteralExpression:
const openBrace = findChildOfKind(n, SyntaxKind.OpenBraceToken, sourceFile); const openBrace = findChildOfKind(n, SyntaxKind.OpenBraceToken, sourceFile);
const closeBrace = findChildOfKind(n, SyntaxKind.CloseBraceToken, sourceFile); const closeBrace = findChildOfKind(n, SyntaxKind.CloseBraceToken, sourceFile);
@ -178,8 +185,5 @@ namespace ts.OutliningElementsCollector {
forEachChild(n, walk); forEachChild(n, walk);
depth--; depth--;
} }
walk(sourceFile);
return elements;
} }
} }

View file

@ -1,13 +1,13 @@
/// <reference path="fourslash.ts"/> /// <reference path="fourslash.ts"/>
////// objects in x should generate outlining spans that do not render in VS // objects in x should generate outlining spans that do not render in VS
//// const x =[| [ //// const x =[| [
//// [|{ a: 0 }|], //// [|{ a: 0 }|],
//// [|{ b: 1 }|], //// [|{ b: 1 }|],
//// [|{ c: 2 }|] //// [|{ c: 2 }|]
//// ]|]; //// ]|];
//// ////
////// objects in y should generate outlining spans that render as expected // objects in y should generate outlining spans that render as expected
//// const y =[| [ //// const y =[| [
//// [|{ //// [|{
//// a: 0 //// a: 0
@ -20,7 +20,7 @@
//// }|] //// }|]
//// ]|]; //// ]|];
//// ////
////// same behavior for nested arrays // same behavior for nested arrays
//// const w =[| [ //// const w =[| [
//// [|[ 0 ]|], //// [|[ 0 ]|],
//// [|[ 1 ]|], //// [|[ 1 ]|],
@ -39,7 +39,7 @@
//// ]|] //// ]|]
//// ]|]; //// ]|];
//// ////
////// multiple levels of nesting work as expected // multiple levels of nesting work as expected
//// const z =[| [ //// const z =[| [
//// [|[ //// [|[
//// [|{ hello: 0 }|] //// [|{ hello: 0 }|]