Skip to content

Commit 0628adc

Browse files
Merge pull request #31865 from amcasey/FunctionHintSpan
Correct outline hint spans for functions
2 parents 375487e + 1bf28f2 commit 0628adc

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

src/harness/fourslash.ts

+18
Original file line numberDiff line numberDiff line change
@@ -2238,6 +2238,20 @@ Actual: ${stringify(fullActual)}`);
22382238
});
22392239
}
22402240

2241+
public verifyOutliningHintSpans(spans: Range[]) {
2242+
const actual = this.languageService.getOutliningSpans(this.activeFile.fileName);
2243+
2244+
if (actual.length !== spans.length) {
2245+
this.raiseError(`verifyOutliningHintSpans failed - expected total spans to be ${spans.length}, but was ${actual.length}`);
2246+
}
2247+
2248+
ts.zipWith(spans, actual, (expectedSpan, actualSpan, i) => {
2249+
if (expectedSpan.pos !== actualSpan.hintSpan.start || expectedSpan.end !== ts.textSpanEnd(actualSpan.hintSpan)) {
2250+
return this.raiseError(`verifyOutliningSpans failed - span ${(i + 1)} expected: (${expectedSpan.pos},${expectedSpan.end}), actual: (${actualSpan.hintSpan.start},${ts.textSpanEnd(actualSpan.hintSpan)})`);
2251+
}
2252+
});
2253+
}
2254+
22412255
public verifyTodoComments(descriptors: string[], spans: Range[]) {
22422256
const actual = this.languageService.getTodoComments(this.activeFile.fileName,
22432257
descriptors.map(d => { return { text: d, priority: 0 }; }));
@@ -4005,6 +4019,10 @@ namespace FourSlashInterface {
40054019
this.state.verifyOutliningSpans(spans, kind);
40064020
}
40074021

4022+
public outliningHintSpansInCurrentFile(spans: FourSlash.Range[]) {
4023+
this.state.verifyOutliningHintSpans(spans);
4024+
}
4025+
40084026
public todoCommentsInCurrentFile(descriptors: string[]) {
40094027
this.state.verifyTodoComments(descriptors, this.state.getRanges());
40104028
}

src/services/outliningElementsCollector.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ namespace ts.OutliningElementsCollector {
237237
? findChildOfKind(node, SyntaxKind.OpenParenToken, sourceFile)
238238
: findChildOfKind(body, SyntaxKind.OpenBraceToken, sourceFile);
239239
const closeToken = findChildOfKind(body, SyntaxKind.CloseBraceToken, sourceFile);
240-
return openToken && closeToken && spanBetweenTokens(openToken, closeToken, node.parent, sourceFile, /*autoCollapse*/ node.parent.kind !== SyntaxKind.ArrowFunction);
240+
return openToken && closeToken && spanBetweenTokens(openToken, closeToken, node, sourceFile, /*autoCollapse*/ node.kind !== SyntaxKind.ArrowFunction);
241241
}
242242

243243
function spanBetweenTokens(openToken: Node, closeToken: Node, hintSpanNode: Node, sourceFile: SourceFile, autoCollapse = false, useFullStart = true): OutliningSpan {

tests/cases/fourslash/fourslash.ts

+1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ declare namespace FourSlashInterface {
247247
baselineSmartSelection(): void;
248248
nameOrDottedNameSpanTextIs(text: string): void;
249249
outliningSpansInCurrentFile(spans: Range[]): void;
250+
outliningHintSpansInCurrentFile(spans: Range[]): void;
250251
todoCommentsInCurrentFile(descriptors: string[]): void;
251252
matchingBracePositionInCurrentFile(bracePosition: number, expectedMatchPosition: number): void;
252253
noMatchingBracePositionInCurrentFile(bracePosition: number): void;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////[|namespace NS {
4+
//// [|function f(x: number, y: number) {
5+
//// return x + y;
6+
//// }|]
7+
////
8+
//// [|function g(
9+
//// x: number,
10+
//// y: number,
11+
//// ): number {
12+
//// return x + y;
13+
//// }|]
14+
////}|]
15+
16+
verify.outliningHintSpansInCurrentFile(test.ranges());

0 commit comments

Comments
 (0)