Skip to content

Commit

Permalink
Merge pull request #31866 from amcasey/FunctionHintSpan35
Browse files Browse the repository at this point in the history
Correct outline hint spans for functions
  • Loading branch information
uniqueiniquity authored Jun 12, 2019
2 parents cf7b19a + caa66f6 commit a1a2bd6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2238,6 +2238,20 @@ Actual: ${stringify(fullActual)}`);
});
}

public verifyOutliningHintSpans(spans: Range[]) {
const actual = this.languageService.getOutliningSpans(this.activeFile.fileName);

if (actual.length !== spans.length) {
this.raiseError(`verifyOutliningHintSpans failed - expected total spans to be ${spans.length}, but was ${actual.length}`);
}

ts.zipWith(spans, actual, (expectedSpan, actualSpan, i) => {
if (expectedSpan.pos !== actualSpan.hintSpan.start || expectedSpan.end !== ts.textSpanEnd(actualSpan.hintSpan)) {
return this.raiseError(`verifyOutliningSpans failed - span ${(i + 1)} expected: (${expectedSpan.pos},${expectedSpan.end}), actual: (${actualSpan.hintSpan.start},${ts.textSpanEnd(actualSpan.hintSpan)})`);
}
});
}

public verifyTodoComments(descriptors: string[], spans: Range[]) {
const actual = this.languageService.getTodoComments(this.activeFile.fileName,
descriptors.map(d => { return { text: d, priority: 0 }; }));
Expand Down Expand Up @@ -4005,6 +4019,10 @@ namespace FourSlashInterface {
this.state.verifyOutliningSpans(spans, kind);
}

public outliningHintSpansInCurrentFile(spans: FourSlash.Range[]) {
this.state.verifyOutliningHintSpans(spans);
}

public todoCommentsInCurrentFile(descriptors: string[]) {
this.state.verifyTodoComments(descriptors, this.state.getRanges());
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/outliningElementsCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ namespace ts.OutliningElementsCollector {
? findChildOfKind(node, SyntaxKind.OpenParenToken, sourceFile)
: findChildOfKind(body, SyntaxKind.OpenBraceToken, sourceFile);
const closeToken = findChildOfKind(body, SyntaxKind.CloseBraceToken, sourceFile);
return openToken && closeToken && spanBetweenTokens(openToken, closeToken, node.parent, sourceFile, /*autoCollapse*/ node.parent.kind !== SyntaxKind.ArrowFunction);
return openToken && closeToken && spanBetweenTokens(openToken, closeToken, node, sourceFile, /*autoCollapse*/ node.kind !== SyntaxKind.ArrowFunction);
}

function spanBetweenTokens(openToken: Node, closeToken: Node, hintSpanNode: Node, sourceFile: SourceFile, autoCollapse = false, useFullStart = true): OutliningSpan {
Expand Down
1 change: 1 addition & 0 deletions tests/cases/fourslash/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ declare namespace FourSlashInterface {
baselineSmartSelection(): void;
nameOrDottedNameSpanTextIs(text: string): void;
outliningSpansInCurrentFile(spans: Range[]): void;
outliningHintSpansInCurrentFile(spans: Range[]): void;
todoCommentsInCurrentFile(descriptors: string[]): void;
matchingBracePositionInCurrentFile(bracePosition: number, expectedMatchPosition: number): void;
noMatchingBracePositionInCurrentFile(bracePosition: number): void;
Expand Down
16 changes: 16 additions & 0 deletions tests/cases/fourslash/outliningHintSpansForFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference path="fourslash.ts"/>

////[|namespace NS {
//// [|function f(x: number, y: number) {
//// return x + y;
//// }|]
////
//// [|function g(
//// x: number,
//// y: number,
//// ): number {
//// return x + y;
//// }|]
////}|]

verify.outliningHintSpansInCurrentFile(test.ranges());

0 comments on commit a1a2bd6

Please sign in to comment.