Skip to content

Commit 5e0221e

Browse files
committed
switch to using OutliningSpan instead of TextSpan to better support language service
1 parent 785c083 commit 5e0221e

File tree

4 files changed

+45
-14
lines changed

4 files changed

+45
-14
lines changed

src/harness/fourslash.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,8 +1419,8 @@ module FourSlash {
14191419
for (var i = 0; i < spans.length; i++) {
14201420
var expectedSpan = spans[i];
14211421
var actualSpan = actual[i];
1422-
if (expectedSpan.start !== actualSpan.start() || expectedSpan.end !== actualSpan.end()) {
1423-
throw new Error('verifyOutliningSpans failed - span ' + (i + 1) + ' expected: (' + expectedSpan.start + ',' + expectedSpan.end + '), actual: (' + actualSpan.start() + ',' + actualSpan.end() + ')');
1422+
if (expectedSpan.start !== actualSpan.textSpan.start() || expectedSpan.end !== actualSpan.textSpan.end()) {
1423+
throw new Error('verifyOutliningSpans failed - span ' + (i + 1) + ' expected: (' + expectedSpan.start + ',' + expectedSpan.end + '), actual: (' + actualSpan.textSpan.start() + ',' + actualSpan.textSpan.end() + ')');
14241424
}
14251425
}
14261426
}

src/services/outliningElementsCollector.ts

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,44 @@
1616
///<reference path='references.ts' />
1717

1818
module ts {
19+
20+
export interface OutliningSpan {
21+
/**
22+
* @param textSpan The span of the document to actually collapse.
23+
* @param hintSpan The span of the document to display when the user hovers over the
24+
* collapsed span.
25+
* @param bannerText The text to display in the editor for the collapsed region.
26+
* @param autoCollapse Whether or not this region should be automatically collapsed when
27+
* the 'Collapse to Definitions' command is invoked.
28+
*/
29+
textSpan: TypeScript.TextSpan;
30+
hintSpan: TypeScript.TextSpan;
31+
bannerText: string;
32+
autoCollapse: boolean;
33+
}
34+
1935
export module OutliningElementsCollector {
20-
export function collectElements(sourceFile: SourceFile): TypeScript.TextSpan[] {
21-
var elements: TypeScript.TextSpan[] = [];
36+
export function collectElements(sourceFile: SourceFile): OutliningSpan[] {
37+
var elements: OutliningSpan[] = [];
2238

23-
function addOutlineRange(startElement: Node, endElement: Node) {
24-
if (startElement && endElement) {
25-
// Push the new range
26-
elements.push(TypeScript.TextSpan.fromBounds(startElement.pos, endElement.end));
39+
function addOutlineRange(node: Node, startElement: Node, endElement: Node) {
40+
if (node && startElement && endElement) {
41+
var span: OutliningSpan = {
42+
textSpan: TypeScript.TextSpan.fromBounds(startElement.pos, endElement.end),
43+
hintSpan: TypeScript.TextSpan.fromBounds(node.getFullStart(), node.end),
44+
bannerText: "...",
45+
autoCollapse: false
46+
};
47+
elements.push(span);
2748
}
2849
}
2950

30-
function walk(n: Node) {
51+
var depth = 0;
52+
var maxDepth = 10;
53+
function walk(n: Node): void {
54+
if (depth >= maxDepth) {
55+
return;
56+
}
3157
switch (n.kind) {
3258
case SyntaxKind.ClassDeclaration:
3359
case SyntaxKind.InterfaceDeclaration:
@@ -36,7 +62,7 @@ module ts {
3662
case SyntaxKind.ObjectLiteral:
3763
var openBrace = forEach(n.getChildren(), c => c.kind === SyntaxKind.OpenBraceToken && c);
3864
var closeBrace = forEach(n.getChildren(), c => c.kind === SyntaxKind.CloseBraceToken && c);
39-
addOutlineRange(openBrace, closeBrace);
65+
addOutlineRange(n, openBrace, closeBrace);
4066
break;
4167
case SyntaxKind.Constructor:
4268
case SyntaxKind.FunctionDeclaration:
@@ -47,11 +73,13 @@ module ts {
4773
if (body) {
4874
var openBrace = forEach(body.getChildren(), c => c.kind === SyntaxKind.OpenBraceToken && c);
4975
var closeBrace = forEach(body.getChildren(), c => c.kind === SyntaxKind.CloseBraceToken && c);
50-
addOutlineRange(openBrace, closeBrace);
76+
addOutlineRange(n, openBrace, closeBrace);
5177
}
5278
break;
5379
}
80+
depth++;
5481
forEachChild(n, walk);
82+
depth--;
5583
}
5684

5785
walk(sourceFile);

src/services/services.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ module ts {
482482
getNavigateToItems(searchValue: string): NavigateToItem[];
483483
getScriptLexicalStructure(fileName: string): NavigateToItem[];
484484

485-
getOutliningRegions(fileName: string): TypeScript.TextSpan[];
485+
getOutliningRegions(fileName: string): OutliningSpan[];
486486
getBraceMatchingAtPosition(fileName: string, position: number): TypeScript.TextSpan[];
487487
getIndentationAtPosition(fileName: string, position: number, options: EditorOptions): number;
488488

@@ -2148,7 +2148,7 @@ module ts {
21482148
return items;
21492149
}
21502150

2151-
function getOutliningRegions(filename: string) {
2151+
function getOutliningRegions(filename: string): OutliningSpan[] {
21522152
// doesn't use compiler - no need to synchronize with host
21532153
filename = TypeScript.switchToForwardSlashes(filename);
21542154
var sourceFile = getCurrentSourceFile(filename);

src/services/shims.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,10 @@ module ts {
726726
"getOutliningRegions(\"" + fileName + "\")",
727727
() => {
728728
var items = this.languageService.getOutliningRegions(fileName);
729-
return items;
729+
// return just the part of data that language service v2 can understand
730+
// language service v2 will use the entire OutliningSpan
731+
var spans = forEach(items, i => i.textSpan);
732+
return spans;
730733
});
731734
}
732735

0 commit comments

Comments
 (0)