Skip to content

Commit 760812f

Browse files
Add explanatory comments, consolidate main body
1 parent d6ccee6 commit 760812f

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/services/outliningElementsCollector.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@ namespace ts.OutliningElementsCollector {
33
export function collectElements(sourceFile: SourceFile, cancellationToken: CancellationToken): OutliningSpan[] {
44
const elements: OutliningSpan[] = [];
55
const collapseText = "...";
6+
let depth = 0;
7+
const maxDepth = 20;
8+
9+
walk(sourceFile);
10+
return elements;
611

7-
function addOutliningSpan(hintSpanNode: Node, startElement: Node, endElement: Node, autoCollapse: boolean, fullStart: boolean) {
12+
// If useFullStart is true, then the collapsing span includes leading whitespace, including linebreaks.
13+
function addOutliningSpan(hintSpanNode: Node, startElement: Node, endElement: Node, autoCollapse: boolean, useFullStart: boolean) {
814
if (hintSpanNode && startElement && endElement) {
915
const span: OutliningSpan = {
10-
textSpan: createTextSpanFromBounds(fullStart ? startElement.pos : startElement.getStart(), endElement.end),
16+
textSpan: createTextSpanFromBounds(useFullStart ? startElement.pos : startElement.getStart(), endElement.end),
1117
hintSpan: createTextSpanFromBounds(startElement.getStart(), endElement.end),
1218
bannerText: collapseText,
1319
autoCollapse,
@@ -82,8 +88,6 @@ namespace ts.OutliningElementsCollector {
8288
return isFunctionBlock(node) && node.parent.kind !== SyntaxKind.ArrowFunction;
8389
}
8490

85-
let depth = 0;
86-
const maxDepth = 20;
8791
function walk(n: Node): void {
8892
cancellationToken.throwIfCancellationRequested();
8993
if (depth > maxDepth) {
@@ -163,6 +167,9 @@ namespace ts.OutliningElementsCollector {
163167
addOutliningSpan(n, openBrace, closeBrace, autoCollapse(n), /* fullStart */ true);
164168
break;
165169
}
170+
// If the block has no leading keywords and is a member of an array literal,
171+
// we again want to only collapse the span of the block.
172+
// Otherwise, the collapsed section will include the end of the previous line.
166173
case SyntaxKind.ObjectLiteralExpression:
167174
const openBrace = findChildOfKind(n, SyntaxKind.OpenBraceToken, sourceFile);
168175
const closeBrace = findChildOfKind(n, SyntaxKind.CloseBraceToken, sourceFile);
@@ -178,8 +185,5 @@ namespace ts.OutliningElementsCollector {
178185
forEachChild(n, walk);
179186
depth--;
180187
}
181-
182-
walk(sourceFile);
183-
return elements;
184188
}
185189
}

tests/cases/fourslash/getOutliningForObjectsInArray.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/// <reference path="fourslash.ts"/>
22

3-
////// objects in x should generate outlining spans that do not render in VS
3+
// objects in x should generate outlining spans that do not render in VS
44
//// const x =[| [
55
//// [|{ a: 0 }|],
66
//// [|{ b: 1 }|],
77
//// [|{ c: 2 }|]
88
//// ]|];
99
////
10-
////// objects in y should generate outlining spans that render as expected
10+
// objects in y should generate outlining spans that render as expected
1111
//// const y =[| [
1212
//// [|{
1313
//// a: 0
@@ -20,7 +20,7 @@
2020
//// }|]
2121
//// ]|];
2222
////
23-
////// same behavior for nested arrays
23+
// same behavior for nested arrays
2424
//// const w =[| [
2525
//// [|[ 0 ]|],
2626
//// [|[ 1 ]|],
@@ -39,7 +39,7 @@
3939
//// ]|]
4040
//// ]|];
4141
////
42-
////// multiple levels of nesting work as expected
42+
// multiple levels of nesting work as expected
4343
//// const z =[| [
4444
//// [|[
4545
//// [|{ hello: 0 }|]

0 commit comments

Comments
 (0)