Merge pull request #23894 from Microsoft/importOutlining

Add outlining spans for Import declarations
This commit is contained in:
Mohamed Hegazy 2018-05-07 12:08:11 -07:00 committed by GitHub
commit e39e6fc780
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 71 additions and 13 deletions

View File

@ -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);
}

View File

@ -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 {

View File

@ -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 {

View File

@ -4890,9 +4890,14 @@ declare namespace ts {
kind: OutliningSpanKind;
}
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"
}
enum OutputFileType {
JavaScript = 0,

View File

@ -4890,9 +4890,14 @@ declare namespace ts {
kind: OutliningSpanKind;
}
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"
}
enum OutputFileType {
JavaScript = 0,

View File

@ -0,0 +1,20 @@
/// <reference path="fourslash.ts"/>
////[|import * as ns from "mod";
////
////import d from "mod";
////import { a, b, c } from "mod";
////
////import r = require("mod");|]
////
////// statement
////var x = 0;
////
////// another set of imports
////[|import * as ns from "mod";
////import d from "mod";
////import { a, b, c } from "mod";
////import r = require("mod");|]
verify.outliningSpansInCurrentFile(test.ranges(), "imports");

View File

@ -1,8 +1,8 @@
/// <reference path="fourslash.ts"/>
////import a from 'a/aaaaaaa/aaaaaaa/aaaaaa/aaaaaaa';
////[|import a from 'a/aaaaaaa/aaaaaaa/aaaaaa/aaaaaaa';
/////**/import b from 'b';
////import c from 'c';
////import c from 'c';|]
////
////[|/** @internal */|]
////export class LanguageIdentifier[| { }|]