mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-11 10:46:28 -05:00
Merge pull request #23894 from Microsoft/importOutlining
Add outlining spans for Import declarations
This commit is contained in:
@@ -2419,7 +2419,7 @@ Actual: ${stringify(fullActual)}`);
|
||||
Harness.IO.log(stringify(spans));
|
||||
}
|
||||
|
||||
public verifyOutliningSpans(spans: Range[], kind?: "comment" | "region" | "code") {
|
||||
public verifyOutliningSpans(spans: Range[], kind?: "comment" | "region" | "code" | "imports") {
|
||||
const actual = this.languageService.getOutliningSpans(this.activeFile.fileName);
|
||||
|
||||
if (actual.length !== spans.length) {
|
||||
@@ -4247,7 +4247,7 @@ namespace FourSlashInterface {
|
||||
this.state.verifyCurrentNameOrDottedNameSpanText(text);
|
||||
}
|
||||
|
||||
public outliningSpansInCurrentFile(spans: FourSlash.Range[], kind?: "comment" | "region" | "code") {
|
||||
public outliningSpansInCurrentFile(spans: FourSlash.Range[], kind?: "comment" | "region" | "code" | "imports") {
|
||||
this.state.verifyOutliningSpans(spans, kind);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,27 @@ namespace ts.OutliningElementsCollector {
|
||||
|
||||
function addNodeOutliningSpans(sourceFile: SourceFile, cancellationToken: CancellationToken, out: Push<OutliningSpan>): void {
|
||||
let depthRemaining = 40;
|
||||
sourceFile.forEachChild(function walk(n) {
|
||||
let current = 0;
|
||||
const statements = sourceFile.statements;
|
||||
const n = statements.length;
|
||||
while (current < n) {
|
||||
while (current < n && !isAnyImportSyntax(statements[current])) {
|
||||
visitNonImportNode(statements[current]);
|
||||
current++;
|
||||
}
|
||||
if (current === n) break;
|
||||
const firstImport = current;
|
||||
while (current < n && isAnyImportSyntax(statements[current])) {
|
||||
addOutliningForLeadingCommentsForNode(statements[current], sourceFile, cancellationToken, out);
|
||||
current++;
|
||||
}
|
||||
const lastImport = current - 1;
|
||||
if (lastImport !== firstImport) {
|
||||
out.push(createOutliningSpanFromBounds(findChildOfKind(statements[firstImport], SyntaxKind.ImportKeyword, sourceFile)!.getStart(sourceFile), statements[lastImport].getEnd(), OutliningSpanKind.Imports));
|
||||
}
|
||||
}
|
||||
|
||||
function visitNonImportNode(n: Node) {
|
||||
if (depthRemaining === 0) return;
|
||||
cancellationToken.throwIfCancellationRequested();
|
||||
|
||||
@@ -23,17 +43,17 @@ namespace ts.OutliningElementsCollector {
|
||||
depthRemaining--;
|
||||
if (isIfStatement(n) && n.elseStatement && isIfStatement(n.elseStatement)) {
|
||||
// Consider an 'else if' to be on the same depth as the 'if'.
|
||||
walk(n.expression);
|
||||
walk(n.thenStatement);
|
||||
visitNonImportNode(n.expression);
|
||||
visitNonImportNode(n.thenStatement);
|
||||
depthRemaining++;
|
||||
walk(n.elseStatement);
|
||||
visitNonImportNode(n.elseStatement);
|
||||
depthRemaining--;
|
||||
}
|
||||
else {
|
||||
n.forEachChild(walk);
|
||||
n.forEachChild(visitNonImportNode);
|
||||
}
|
||||
depthRemaining++;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function addRegionOutliningSpans(sourceFile: SourceFile, out: Push<OutliningSpan>): void {
|
||||
|
||||
@@ -824,9 +824,17 @@ namespace ts {
|
||||
}
|
||||
|
||||
export const enum OutliningSpanKind {
|
||||
/** Single or multi-line comments */
|
||||
Comment = "comment",
|
||||
|
||||
/** Sections marked by '// #region' and '// #endregion' comments */
|
||||
Region = "region",
|
||||
Code = "code"
|
||||
|
||||
/** Declarations and expressions */
|
||||
Code = "code",
|
||||
|
||||
/** Contiguous blocks of import declarations */
|
||||
Imports = "imports"
|
||||
}
|
||||
|
||||
export const enum OutputFileType {
|
||||
|
||||
Reference in New Issue
Block a user