mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-25 16:07:52 -05:00
Merge remote-tracking branch 'origin/master' into checkJSFiles
This commit is contained in:
@@ -2,6 +2,36 @@
|
||||
|
||||
/* @internal */
|
||||
namespace ts.NavigationBar {
|
||||
/**
|
||||
* Matches all whitespace characters in a string. Eg:
|
||||
*
|
||||
* "app.
|
||||
*
|
||||
* onactivated"
|
||||
*
|
||||
* matches because of the newline, whereas
|
||||
*
|
||||
* "app.onactivated"
|
||||
*
|
||||
* does not match.
|
||||
*/
|
||||
const whiteSpaceRegex = /\s+/g;
|
||||
|
||||
// Keep sourceFile handy so we don't have to search for it every time we need to call `getText`.
|
||||
let curCancellationToken: CancellationToken;
|
||||
let curSourceFile: SourceFile;
|
||||
|
||||
/**
|
||||
* For performance, we keep navigation bar parents on a stack rather than passing them through each recursion.
|
||||
* `parent` is the current parent and is *not* stored in parentsStack.
|
||||
* `startNode` sets a new parent and `endNode` returns to the previous parent.
|
||||
*/
|
||||
let parentsStack: NavigationBarNode[] = [];
|
||||
let parent: NavigationBarNode;
|
||||
|
||||
// NavigationBarItem requires an array, but will not mutate it, so just give it this for performance.
|
||||
let emptyChildItemArray: NavigationBarItem[] = [];
|
||||
|
||||
/**
|
||||
* Represents a navigation bar item and its children.
|
||||
* The returned NavigationBarItem is more complicated and doesn't include 'parent', so we use these to do work before converting.
|
||||
@@ -14,22 +44,36 @@ namespace ts.NavigationBar {
|
||||
indent: number; // # of parents
|
||||
}
|
||||
|
||||
export function getNavigationBarItems(sourceFile: SourceFile): NavigationBarItem[] {
|
||||
export function getNavigationBarItems(sourceFile: SourceFile, cancellationToken: CancellationToken): NavigationBarItem[] {
|
||||
curCancellationToken = cancellationToken;
|
||||
curSourceFile = sourceFile;
|
||||
const result = map(topLevelItems(rootNavigationBarNode(sourceFile)), convertToTopLevelItem);
|
||||
curSourceFile = undefined;
|
||||
return result;
|
||||
try {
|
||||
return map(topLevelItems(rootNavigationBarNode(sourceFile)), convertToTopLevelItem);
|
||||
}
|
||||
finally {
|
||||
reset();
|
||||
}
|
||||
}
|
||||
|
||||
export function getNavigationTree(sourceFile: SourceFile): NavigationTree {
|
||||
export function getNavigationTree(sourceFile: SourceFile, cancellationToken: CancellationToken): NavigationTree {
|
||||
curCancellationToken = cancellationToken;
|
||||
curSourceFile = sourceFile;
|
||||
const result = convertToTree(rootNavigationBarNode(sourceFile));
|
||||
curSourceFile = undefined;
|
||||
return result;
|
||||
try {
|
||||
return convertToTree(rootNavigationBarNode(sourceFile));
|
||||
}
|
||||
finally {
|
||||
reset();
|
||||
}
|
||||
}
|
||||
|
||||
function reset() {
|
||||
curSourceFile = undefined;
|
||||
curCancellationToken = undefined;
|
||||
parentsStack = [];
|
||||
parent = undefined;
|
||||
emptyChildItemArray = [];
|
||||
}
|
||||
|
||||
// Keep sourceFile handy so we don't have to search for it every time we need to call `getText`.
|
||||
let curSourceFile: SourceFile;
|
||||
function nodeText(node: Node): string {
|
||||
return node.getText(curSourceFile);
|
||||
}
|
||||
@@ -47,14 +91,6 @@ namespace ts.NavigationBar {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
For performance, we keep navigation bar parents on a stack rather than passing them through each recursion.
|
||||
`parent` is the current parent and is *not* stored in parentsStack.
|
||||
`startNode` sets a new parent and `endNode` returns to the previous parent.
|
||||
*/
|
||||
const parentsStack: NavigationBarNode[] = [];
|
||||
let parent: NavigationBarNode;
|
||||
|
||||
function rootNavigationBarNode(sourceFile: SourceFile): NavigationBarNode {
|
||||
Debug.assert(!parentsStack.length);
|
||||
const root: NavigationBarNode = { node: sourceFile, additionalNodes: undefined, parent: undefined, children: undefined, indent: 0 };
|
||||
@@ -111,6 +147,8 @@ namespace ts.NavigationBar {
|
||||
|
||||
/** Look for navigation bar items in node's subtree, adding them to the current `parent`. */
|
||||
function addChildrenRecursively(node: Node): void {
|
||||
curCancellationToken.throwIfCancellationRequested();
|
||||
|
||||
if (!node || isToken(node)) {
|
||||
return;
|
||||
}
|
||||
@@ -487,9 +525,6 @@ namespace ts.NavigationBar {
|
||||
}
|
||||
}
|
||||
|
||||
// NavigationBarItem requires an array, but will not mutate it, so just give it this for performance.
|
||||
const emptyChildItemArray: NavigationBarItem[] = [];
|
||||
|
||||
function convertToTree(n: NavigationBarNode): NavigationTree {
|
||||
return {
|
||||
text: getItemName(n.node),
|
||||
@@ -610,19 +645,4 @@ namespace ts.NavigationBar {
|
||||
function isFunctionOrClassExpression(node: Node): boolean {
|
||||
return node.kind === SyntaxKind.FunctionExpression || node.kind === SyntaxKind.ArrowFunction || node.kind === SyntaxKind.ClassExpression;
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches all whitespace characters in a string. Eg:
|
||||
*
|
||||
* "app.
|
||||
*
|
||||
* onactivated"
|
||||
*
|
||||
* matches because of the newline, whereas
|
||||
*
|
||||
* "app.onactivated"
|
||||
*
|
||||
* does not match.
|
||||
*/
|
||||
const whiteSpaceRegex = /\s+/g;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* @internal */
|
||||
namespace ts.OutliningElementsCollector {
|
||||
export function collectElements(sourceFile: SourceFile): OutliningSpan[] {
|
||||
export function collectElements(sourceFile: SourceFile, cancellationToken: CancellationToken): OutliningSpan[] {
|
||||
const elements: OutliningSpan[] = [];
|
||||
const collapseText = "...";
|
||||
|
||||
@@ -38,6 +38,7 @@ namespace ts.OutliningElementsCollector {
|
||||
let singleLineCommentCount = 0;
|
||||
|
||||
for (const currentComment of comments) {
|
||||
cancellationToken.throwIfCancellationRequested();
|
||||
|
||||
// For single line comments, combine consecutive ones (2 or more) into
|
||||
// a single span from the start of the first till the end of the last
|
||||
@@ -84,6 +85,7 @@ namespace ts.OutliningElementsCollector {
|
||||
let depth = 0;
|
||||
const maxDepth = 20;
|
||||
function walk(n: Node): void {
|
||||
cancellationToken.throwIfCancellationRequested();
|
||||
if (depth > maxDepth) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -982,6 +982,36 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
/* @internal */
|
||||
/** A cancellation that throttles calls to the host */
|
||||
export class ThrottledCancellationToken implements CancellationToken {
|
||||
// Store when we last tried to cancel. Checking cancellation can be expensive (as we have
|
||||
// to marshall over to the host layer). So we only bother actually checking once enough
|
||||
// time has passed.
|
||||
private lastCancellationCheckTime = 0;
|
||||
|
||||
constructor(private hostCancellationToken: HostCancellationToken, private readonly throttleWaitMilliseconds = 20) {
|
||||
}
|
||||
|
||||
public isCancellationRequested(): boolean {
|
||||
const time = timestamp();
|
||||
const duration = Math.abs(time - this.lastCancellationCheckTime);
|
||||
if (duration >= this.throttleWaitMilliseconds) {
|
||||
// Check no more than once every throttle wait milliseconds
|
||||
this.lastCancellationCheckTime = time;
|
||||
return this.hostCancellationToken.isCancellationRequested();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public throwIfCancellationRequested(): void {
|
||||
if (this.isCancellationRequested()) {
|
||||
throw new OperationCanceledException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function createLanguageService(host: LanguageServiceHost,
|
||||
documentRegistry: DocumentRegistry = createDocumentRegistry(host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames(), host.getCurrentDirectory())): LanguageService {
|
||||
|
||||
@@ -1553,11 +1583,11 @@ namespace ts {
|
||||
}
|
||||
|
||||
function getNavigationBarItems(fileName: string): NavigationBarItem[] {
|
||||
return NavigationBar.getNavigationBarItems(syntaxTreeCache.getCurrentSourceFile(fileName));
|
||||
return NavigationBar.getNavigationBarItems(syntaxTreeCache.getCurrentSourceFile(fileName), cancellationToken);
|
||||
}
|
||||
|
||||
function getNavigationTree(fileName: string): NavigationTree {
|
||||
return NavigationBar.getNavigationTree(syntaxTreeCache.getCurrentSourceFile(fileName));
|
||||
return NavigationBar.getNavigationTree(syntaxTreeCache.getCurrentSourceFile(fileName), cancellationToken);
|
||||
}
|
||||
|
||||
function isTsOrTsxFile(fileName: string): boolean {
|
||||
@@ -1596,7 +1626,7 @@ namespace ts {
|
||||
function getOutliningSpans(fileName: string): OutliningSpan[] {
|
||||
// doesn't use compiler - no need to synchronize with host
|
||||
const sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName);
|
||||
return OutliningElementsCollector.collectElements(sourceFile);
|
||||
return OutliningElementsCollector.collectElements(sourceFile, cancellationToken);
|
||||
}
|
||||
|
||||
function getBraceMatchingAtPosition(fileName: string, position: number) {
|
||||
|
||||
@@ -469,29 +469,6 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
/** A cancellation that throttles calls to the host */
|
||||
class ThrottledCancellationToken implements HostCancellationToken {
|
||||
// Store when we last tried to cancel. Checking cancellation can be expensive (as we have
|
||||
// to marshall over to the host layer). So we only bother actually checking once enough
|
||||
// time has passed.
|
||||
private lastCancellationCheckTime = 0;
|
||||
|
||||
constructor(private hostCancellationToken: HostCancellationToken) {
|
||||
}
|
||||
|
||||
public isCancellationRequested(): boolean {
|
||||
const time = timestamp();
|
||||
const duration = Math.abs(time - this.lastCancellationCheckTime);
|
||||
if (duration > 10) {
|
||||
// Check no more than once every 10 ms.
|
||||
this.lastCancellationCheckTime = time;
|
||||
return this.hostCancellationToken.isCancellationRequested();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export class CoreServicesShimHostAdapter implements ParseConfigHost, ModuleResolutionHost {
|
||||
|
||||
public directoryExists: (directoryName: string) => boolean;
|
||||
|
||||
Reference in New Issue
Block a user