switch to using OutliningSpan instead of TextSpan to better support language service

This commit is contained in:
Vladimir Matveev 2014-08-11 15:08:40 -07:00
parent 785c083888
commit 5e0221eeac
4 changed files with 46 additions and 15 deletions

View File

@ -1419,8 +1419,8 @@ module FourSlash {
for (var i = 0; i < spans.length; i++) {
var expectedSpan = spans[i];
var actualSpan = actual[i];
if (expectedSpan.start !== actualSpan.start() || expectedSpan.end !== actualSpan.end()) {
throw new Error('verifyOutliningSpans failed - span ' + (i + 1) + ' expected: (' + expectedSpan.start + ',' + expectedSpan.end + '), actual: (' + actualSpan.start() + ',' + actualSpan.end() + ')');
if (expectedSpan.start !== actualSpan.textSpan.start() || expectedSpan.end !== actualSpan.textSpan.end()) {
throw new Error('verifyOutliningSpans failed - span ' + (i + 1) + ' expected: (' + expectedSpan.start + ',' + expectedSpan.end + '), actual: (' + actualSpan.textSpan.start() + ',' + actualSpan.textSpan.end() + ')');
}
}
}

View File

@ -16,18 +16,44 @@
///<reference path='references.ts' />
module ts {
export module OutliningElementsCollector {
export function collectElements(sourceFile: SourceFile): TypeScript.TextSpan[] {
var elements: TypeScript.TextSpan[] = [];
function addOutlineRange(startElement: Node, endElement: Node) {
if (startElement && endElement) {
// Push the new range
elements.push(TypeScript.TextSpan.fromBounds(startElement.pos, endElement.end));
export interface OutliningSpan {
/**
* @param textSpan The span of the document to actually collapse.
* @param hintSpan The span of the document to display when the user hovers over the
* collapsed span.
* @param bannerText The text to display in the editor for the collapsed region.
* @param autoCollapse Whether or not this region should be automatically collapsed when
* the 'Collapse to Definitions' command is invoked.
*/
textSpan: TypeScript.TextSpan;
hintSpan: TypeScript.TextSpan;
bannerText: string;
autoCollapse: boolean;
}
export module OutliningElementsCollector {
export function collectElements(sourceFile: SourceFile): OutliningSpan[] {
var elements: OutliningSpan[] = [];
function addOutlineRange(node: Node, startElement: Node, endElement: Node) {
if (node && startElement && endElement) {
var span: OutliningSpan = {
textSpan: TypeScript.TextSpan.fromBounds(startElement.pos, endElement.end),
hintSpan: TypeScript.TextSpan.fromBounds(node.getFullStart(), node.end),
bannerText: "...",
autoCollapse: false
};
elements.push(span);
}
}
function walk(n: Node) {
var depth = 0;
var maxDepth = 10;
function walk(n: Node): void {
if (depth >= maxDepth) {
return;
}
switch (n.kind) {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.InterfaceDeclaration:
@ -36,7 +62,7 @@ module ts {
case SyntaxKind.ObjectLiteral:
var openBrace = forEach(n.getChildren(), c => c.kind === SyntaxKind.OpenBraceToken && c);
var closeBrace = forEach(n.getChildren(), c => c.kind === SyntaxKind.CloseBraceToken && c);
addOutlineRange(openBrace, closeBrace);
addOutlineRange(n, openBrace, closeBrace);
break;
case SyntaxKind.Constructor:
case SyntaxKind.FunctionDeclaration:
@ -47,11 +73,13 @@ module ts {
if (body) {
var openBrace = forEach(body.getChildren(), c => c.kind === SyntaxKind.OpenBraceToken && c);
var closeBrace = forEach(body.getChildren(), c => c.kind === SyntaxKind.CloseBraceToken && c);
addOutlineRange(openBrace, closeBrace);
addOutlineRange(n, openBrace, closeBrace);
}
break;
}
depth++;
forEachChild(n, walk);
depth--;
}
walk(sourceFile);

View File

@ -482,7 +482,7 @@ module ts {
getNavigateToItems(searchValue: string): NavigateToItem[];
getScriptLexicalStructure(fileName: string): NavigateToItem[];
getOutliningRegions(fileName: string): TypeScript.TextSpan[];
getOutliningRegions(fileName: string): OutliningSpan[];
getBraceMatchingAtPosition(fileName: string, position: number): TypeScript.TextSpan[];
getIndentationAtPosition(fileName: string, position: number, options: EditorOptions): number;
@ -2148,7 +2148,7 @@ module ts {
return items;
}
function getOutliningRegions(filename: string) {
function getOutliningRegions(filename: string): OutliningSpan[] {
// doesn't use compiler - no need to synchronize with host
filename = TypeScript.switchToForwardSlashes(filename);
var sourceFile = getCurrentSourceFile(filename);

View File

@ -726,7 +726,10 @@ module ts {
"getOutliningRegions(\"" + fileName + "\")",
() => {
var items = this.languageService.getOutliningRegions(fileName);
return items;
// return just the part of data that language service v2 can understand
// language service v2 will use the entire OutliningSpan
var spans = forEach(items, i => i.textSpan);
return spans;
});
}