Skip to content

Commit e39e6fc

Browse files
authored
Merge pull request microsoft#23894 from Microsoft/importOutlining
Add outlining spans for Import declarations
2 parents a05feed + 64d6b24 commit e39e6fc

File tree

7 files changed

+71
-13
lines changed

7 files changed

+71
-13
lines changed

src/harness/fourslash.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2419,7 +2419,7 @@ Actual: ${stringify(fullActual)}`);
24192419
Harness.IO.log(stringify(spans));
24202420
}
24212421

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

24252425
if (actual.length !== spans.length) {
@@ -4247,7 +4247,7 @@ namespace FourSlashInterface {
42474247
this.state.verifyCurrentNameOrDottedNameSpanText(text);
42484248
}
42494249

4250-
public outliningSpansInCurrentFile(spans: FourSlash.Range[], kind?: "comment" | "region" | "code") {
4250+
public outliningSpansInCurrentFile(spans: FourSlash.Range[], kind?: "comment" | "region" | "code" | "imports") {
42514251
this.state.verifyOutliningSpans(spans, kind);
42524252
}
42534253

src/services/outliningElementsCollector.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,27 @@ namespace ts.OutliningElementsCollector {
99

1010
function addNodeOutliningSpans(sourceFile: SourceFile, cancellationToken: CancellationToken, out: Push<OutliningSpan>): void {
1111
let depthRemaining = 40;
12-
sourceFile.forEachChild(function walk(n) {
12+
let current = 0;
13+
const statements = sourceFile.statements;
14+
const n = statements.length;
15+
while (current < n) {
16+
while (current < n && !isAnyImportSyntax(statements[current])) {
17+
visitNonImportNode(statements[current]);
18+
current++;
19+
}
20+
if (current === n) break;
21+
const firstImport = current;
22+
while (current < n && isAnyImportSyntax(statements[current])) {
23+
addOutliningForLeadingCommentsForNode(statements[current], sourceFile, cancellationToken, out);
24+
current++;
25+
}
26+
const lastImport = current - 1;
27+
if (lastImport !== firstImport) {
28+
out.push(createOutliningSpanFromBounds(findChildOfKind(statements[firstImport], SyntaxKind.ImportKeyword, sourceFile)!.getStart(sourceFile), statements[lastImport].getEnd(), OutliningSpanKind.Imports));
29+
}
30+
}
31+
32+
function visitNonImportNode(n: Node) {
1333
if (depthRemaining === 0) return;
1434
cancellationToken.throwIfCancellationRequested();
1535

@@ -23,17 +43,17 @@ namespace ts.OutliningElementsCollector {
2343
depthRemaining--;
2444
if (isIfStatement(n) && n.elseStatement && isIfStatement(n.elseStatement)) {
2545
// Consider an 'else if' to be on the same depth as the 'if'.
26-
walk(n.expression);
27-
walk(n.thenStatement);
46+
visitNonImportNode(n.expression);
47+
visitNonImportNode(n.thenStatement);
2848
depthRemaining++;
29-
walk(n.elseStatement);
49+
visitNonImportNode(n.elseStatement);
3050
depthRemaining--;
3151
}
3252
else {
33-
n.forEachChild(walk);
53+
n.forEachChild(visitNonImportNode);
3454
}
3555
depthRemaining++;
36-
});
56+
}
3757
}
3858

3959
function addRegionOutliningSpans(sourceFile: SourceFile, out: Push<OutliningSpan>): void {

src/services/types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,9 +824,17 @@ namespace ts {
824824
}
825825

826826
export const enum OutliningSpanKind {
827+
/** Single or multi-line comments */
827828
Comment = "comment",
829+
830+
/** Sections marked by '// #region' and '// #endregion' comments */
828831
Region = "region",
829-
Code = "code"
832+
833+
/** Declarations and expressions */
834+
Code = "code",
835+
836+
/** Contiguous blocks of import declarations */
837+
Imports = "imports"
830838
}
831839

832840
export const enum OutputFileType {

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4890,9 +4890,14 @@ declare namespace ts {
48904890
kind: OutliningSpanKind;
48914891
}
48924892
enum OutliningSpanKind {
4893+
/** Single or multi-line comments */
48934894
Comment = "comment",
4895+
/** Sections marked by '// #region' and '// #endregion' comments */
48944896
Region = "region",
4895-
Code = "code"
4897+
/** Declarations and expressions */
4898+
Code = "code",
4899+
/** Contiguous blocks of import declarations */
4900+
Imports = "imports"
48964901
}
48974902
enum OutputFileType {
48984903
JavaScript = 0,

tests/baselines/reference/api/typescript.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4890,9 +4890,14 @@ declare namespace ts {
48904890
kind: OutliningSpanKind;
48914891
}
48924892
enum OutliningSpanKind {
4893+
/** Single or multi-line comments */
48934894
Comment = "comment",
4895+
/** Sections marked by '// #region' and '// #endregion' comments */
48944896
Region = "region",
4895-
Code = "code"
4897+
/** Declarations and expressions */
4898+
Code = "code",
4899+
/** Contiguous blocks of import declarations */
4900+
Imports = "imports"
48964901
}
48974902
enum OutputFileType {
48984903
JavaScript = 0,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
4+
////[|import * as ns from "mod";
5+
////
6+
////import d from "mod";
7+
////import { a, b, c } from "mod";
8+
////
9+
////import r = require("mod");|]
10+
////
11+
////// statement
12+
////var x = 0;
13+
////
14+
////// another set of imports
15+
////[|import * as ns from "mod";
16+
////import d from "mod";
17+
////import { a, b, c } from "mod";
18+
////import r = require("mod");|]
19+
20+
verify.outliningSpansInCurrentFile(test.ranges(), "imports");

tests/cases/fourslash/incrementalParsingWithJsDoc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/// <reference path="fourslash.ts"/>
22

3-
////import a from 'a/aaaaaaa/aaaaaaa/aaaaaa/aaaaaaa';
3+
////[|import a from 'a/aaaaaaa/aaaaaaa/aaaaaa/aaaaaaa';
44
/////**/import b from 'b';
5-
////import c from 'c';
5+
////import c from 'c';|]
66
////
77
////[|/** @internal */|]
88
////export class LanguageIdentifier[| { }|]

0 commit comments

Comments
 (0)