Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry picks 36880 into 3.8 #37082

Merged
merged 2 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/harness/fourslashImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2500,18 +2500,35 @@ namespace FourSlash {

public printOutliningSpans() {
const spans = this.languageService.getOutliningSpans(this.activeFile.fileName);
Harness.IO.log(`Outlining spans (${spans.length} items)`);
Harness.IO.log(`Outlining spans (${spans.length} items)\nResults:`);
Harness.IO.log(stringify(spans));
this.printOutliningSpansInline(spans);
}

private printOutliningSpansInline(spans: ts.OutliningSpan[]) {
const allSpanInsets = [] as { text: string, pos: number }[];
let annotated = this.activeFile.content;
ts.forEach(spans, span => {
allSpanInsets.push({ text: "[|", pos: span.textSpan.start });
allSpanInsets.push({ text: "|]", pos: span.textSpan.start + span.textSpan.length });
});

const reverseSpans = allSpanInsets.sort((l, r) => r.pos - l.pos);
ts.forEach(reverseSpans, span => {
annotated = annotated.slice(0, span.pos) + span.text + annotated.slice(span.pos);
});
Harness.IO.log(`\nMockup:\n${annotated}`);
}

public verifyOutliningSpans(spans: Range[], kind?: "comment" | "region" | "code" | "imports") {
const actual = this.languageService.getOutliningSpans(this.activeFile.fileName);

if (actual.length !== spans.length) {
this.raiseError(`verifyOutliningSpans failed - expected total spans to be ${spans.length}, but was ${actual.length}`);
const filterActual = ts.filter(actual, f => kind === undefined ? true : f.kind === kind);
if (filterActual.length !== spans.length) {
this.raiseError(`verifyOutliningSpans failed - expected total spans to be ${spans.length}, but was ${actual.length}\n\nFound Spans:\n\n${this.printOutliningSpansInline(actual)}`);
}

ts.zipWith(spans, actual, (expectedSpan, actualSpan, i) => {
ts.zipWith(spans, filterActual, (expectedSpan, actualSpan, i) => {
if (expectedSpan.pos !== actualSpan.textSpan.start || expectedSpan.end !== ts.textSpanEnd(actualSpan.textSpan)) {
return this.raiseError(`verifyOutliningSpans failed - span ${(i + 1)} expected: (${expectedSpan.pos},${expectedSpan.end}), actual: (${actualSpan.textSpan.start},${ts.textSpanEnd(actualSpan.textSpan)})`);
}
Expand Down
5 changes: 3 additions & 2 deletions src/services/outliningElementsCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ namespace ts.OutliningElementsCollector {
function addNodeOutliningSpans(sourceFile: SourceFile, cancellationToken: CancellationToken, out: Push<OutliningSpan>): void {
let depthRemaining = 40;
let current = 0;
const statements = sourceFile.statements;
// Includes the EOF Token so that comments which aren't attached to statements are included
const statements = [...sourceFile.statements, sourceFile.endOfFileToken];
const n = statements.length;
while (current < n) {
while (current < n && !isAnyImportSyntax(statements[current])) {
Expand All @@ -33,7 +34,7 @@ namespace ts.OutliningElementsCollector {
if (depthRemaining === 0) return;
cancellationToken.throwIfCancellationRequested();

if (isDeclaration(n)) {
if (isDeclaration(n) || n.kind === SyntaxKind.EndOfFileToken) {
addOutliningForLeadingCommentsForNode(n, sourceFile, cancellationToken, out);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ declare namespace FourSlashInterface {
baselineQuickInfo(): void;
baselineSmartSelection(): void;
nameOrDottedNameSpanTextIs(text: string): void;
outliningSpansInCurrentFile(spans: Range[]): void;
outliningSpansInCurrentFile(spans: Range[], kind?: "comment" | "region" | "code" | "imports"): void;
outliningHintSpansInCurrentFile(spans: Range[]): void;
todoCommentsInCurrentFile(descriptors: string[]): void;
matchingBracePositionInCurrentFile(bracePosition: number, expectedMatchPosition: number): void;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference path="fourslash.ts"/>

// #22732

////[|/*
///// * Some text
//// */|]

verify.outliningHintSpansInCurrentFile(test.ranges());
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference path="fourslash.ts"/>

// #22732

////console.log(0);
////[|/*
///// * Some text
//// */|]

verify.outliningHintSpansInCurrentFile(test.ranges());
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@
////// #endregion
////*/

verify.outliningSpansInCurrentFile(test.ranges(), "region");
verify.outliningSpansInCurrentFile(test.ranges(), "region");