Skip to content

Commit ccaf904

Browse files
committed
support outlining for arbitrary blocks
1 parent 911cb2b commit ccaf904

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

src/services/outliningElementsCollector.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ module ts {
3636
export function collectElements(sourceFile: SourceFile): OutliningSpan[] {
3737
var elements: OutliningSpan[] = [];
3838

39-
function addOutlineRange(node: Node, startElement: Node, endElement: Node) {
40-
if (node && startElement && endElement) {
39+
function addOutlineRange(hintSpanNode: Node, startElement: Node, endElement: Node) {
40+
if (hintSpanNode && startElement && endElement) {
4141
var span: OutliningSpan = {
4242
textSpan: TypeScript.TextSpan.fromBounds(startElement.pos, endElement.end),
43-
hintSpan: TypeScript.TextSpan.fromBounds(node.getStart(), node.end),
43+
hintSpan: TypeScript.TextSpan.fromBounds(hintSpanNode.getStart(), hintSpanNode.end),
4444
bannerText: "...",
4545
autoCollapse: false
4646
};
@@ -49,33 +49,31 @@ module ts {
4949
}
5050

5151
var depth = 0;
52-
var maxDepth = 10;
52+
var maxDepth = 20;
5353
function walk(n: Node): void {
54-
if (depth >= maxDepth) {
54+
if (depth > maxDepth) {
5555
return;
5656
}
5757
switch (n.kind) {
58+
case SyntaxKind.Block:
59+
case SyntaxKind.FunctionBlock:
60+
case SyntaxKind.ModuleBlock:
61+
case SyntaxKind.TryBlock:
62+
case SyntaxKind.TryBlock:
63+
case SyntaxKind.CatchBlock:
64+
case SyntaxKind.FinallyBlock:
65+
var openBrace = forEach(n.getChildren(), c => c.kind === SyntaxKind.OpenBraceToken && c);
66+
var closeBrace = forEach(n.getChildren(), c => c.kind === SyntaxKind.CloseBraceToken && c);
67+
addOutlineRange(n.parent, openBrace, closeBrace);
68+
break;
5869
case SyntaxKind.ClassDeclaration:
5970
case SyntaxKind.InterfaceDeclaration:
60-
case SyntaxKind.ModuleDeclaration:
6171
case SyntaxKind.EnumDeclaration:
6272
case SyntaxKind.ObjectLiteral:
6373
var openBrace = forEach(n.getChildren(), c => c.kind === SyntaxKind.OpenBraceToken && c);
6474
var closeBrace = forEach(n.getChildren(), c => c.kind === SyntaxKind.CloseBraceToken && c);
6575
addOutlineRange(n, openBrace, closeBrace);
6676
break;
67-
case SyntaxKind.Constructor:
68-
case SyntaxKind.FunctionDeclaration:
69-
case SyntaxKind.Method:
70-
case SyntaxKind.GetAccessor:
71-
case SyntaxKind.SetAccessor:
72-
var body = (<FunctionDeclaration>n).body;
73-
if (body) {
74-
var openBrace = forEach(body.getChildren(), c => c.kind === SyntaxKind.OpenBraceToken && c);
75-
var closeBrace = forEach(body.getChildren(), c => c.kind === SyntaxKind.CloseBraceToken && c);
76-
addOutlineRange(n, openBrace, closeBrace);
77-
}
78-
break;
7977
}
8078
depth++;
8179
forEachChild(n, walk);

src/services/shims.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ module ts {
728728
var items = this.languageService.getOutliningRegions(fileName);
729729
// return just the part of data that language service v2 can understand
730730
// language service v2 will use the entire OutliningSpan
731-
var spans = forEach(items, i => i.textSpan);
731+
var spans = map(items, i => i.textSpan);
732732
return spans;
733733
});
734734
}

tests/cases/fourslash/getOutliningSpans.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@
6363
////}|] /* even more */
6464
////
6565
////// object literals
66-
////var x = [|{
66+
////var x =[|{
6767
//// a:1,
6868
//// b:2,
69-
//// get foo() [|{
69+
//// get foo()[| {
7070
//// return 1;
7171
//// }|]
7272
////}|]

0 commit comments

Comments
 (0)