Skip to content

Commit 497d8d3

Browse files
committed
Updates from CR comments
1 parent a37053f commit 497d8d3

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

src/compiler/program.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -887,18 +887,28 @@ namespace ts {
887887
return sourceFile.parseDiagnostics;
888888
}
889889

890-
function onCancel() {
891-
// Because our type checker might be a bad state, we need to throw it away.
892-
// Note: we are overly aggressive here. We do not actually *have* to throw away
893-
// the "noDiagnosticsTypeChecker". However, for simplicity, i'd like to keep
894-
// the lifetimes of these two TypeCheckers the same. Also, we generally only
895-
// cancel when the user has made a change anyways. And, in that case, we (the
896-
// program instance) will get thrown away anyways. So trying to keep one of
897-
// these type checkers alive doesn't serve much purpose.
898-
noDiagnosticsTypeChecker = undefined;
899-
diagnosticsProducingTypeChecker = undefined;
900-
}
890+
function runWithCancellationToken<T>(func: () => T): T {
891+
try {
892+
return func();
893+
}
894+
catch (e) {
895+
if (e instanceof OperationCanceledException) {
896+
// We were canceled while performing the operation. Because our type checker
897+
// might be a bad state, we need to throw it away.
898+
//
899+
// Note: we are overly aggressive here. We do not actually *have* to throw away
900+
// the "noDiagnosticsTypeChecker". However, for simplicity, i'd like to keep
901+
// the lifetimes of these two TypeCheckers the same. Also, we generally only
902+
// cancel when the user has made a change anyways. And, in that case, we (the
903+
// program instance) will get thrown away anyways. So trying to keep one of
904+
// these type checkers alive doesn't serve much purpose.
905+
noDiagnosticsTypeChecker = undefined;
906+
diagnosticsProducingTypeChecker = undefined;
907+
}
901908

909+
throw e;
910+
}
911+
}
902912

903913
function getSemanticDiagnosticsForFile(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
904914
return runWithCancellationToken(() => {
@@ -914,7 +924,7 @@ namespace ts {
914924
const programDiagnosticsInFile = programDiagnostics.getDiagnostics(sourceFile.fileName);
915925

916926
return bindDiagnostics.concat(checkDiagnostics, fileProcessingDiagnosticsInFile, programDiagnosticsInFile);
917-
}, onCancel);
927+
});
918928
}
919929

920930
function getJavaScriptSyntacticDiagnosticsForFile(sourceFile: SourceFile): Diagnostic[] {
@@ -1093,15 +1103,15 @@ namespace ts {
10931103
function createDiagnosticForNode(node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): Diagnostic {
10941104
return createDiagnosticForNodeInSourceFile(sourceFile, node, message, arg0, arg1, arg2);
10951105
}
1096-
}, onCancel);
1106+
});
10971107
}
10981108

10991109
function getDeclarationDiagnosticsWorker(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
11001110
return runWithCancellationToken(() => {
11011111
const resolver = getDiagnosticsProducingTypeChecker().getEmitResolver(sourceFile, cancellationToken);
11021112
// Don't actually write any files since we're just getting diagnostics.
11031113
return ts.getDeclarationDiagnostics(getEmitHost(noop), resolver, sourceFile);
1104-
}, onCancel);
1114+
});
11051115
}
11061116

11071117
function getDeclarationDiagnosticsForFile(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {

src/services/navigationBar.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,27 @@ namespace ts.NavigationBar {
1717
export function getNavigationBarItems(sourceFile: SourceFile, cancellationToken: ThrottledCancellationToken): NavigationBarItem[] {
1818
curCancellationToken = cancellationToken;
1919
curSourceFile = sourceFile;
20-
const result = runWithCancellationToken(() => map(topLevelItems(rootNavigationBarNode(sourceFile)), convertToTopLevelItem), () => curSourceFile = undefined);
21-
curSourceFile = undefined;
22-
return result;
20+
try {
21+
return map(topLevelItems(rootNavigationBarNode(sourceFile)), convertToTopLevelItem);
22+
}
23+
finally {
24+
curSourceFile = undefined;
25+
}
2326
}
2427

2528
export function getNavigationTree(sourceFile: SourceFile, cancellationToken: ThrottledCancellationToken): NavigationTree {
2629
curCancellationToken = cancellationToken;
2730
curSourceFile = sourceFile;
28-
const result = runWithCancellationToken(() => convertToTree(rootNavigationBarNode(sourceFile)), () => curSourceFile = undefined);
29-
curSourceFile = undefined;
30-
return result;
31+
try {
32+
return convertToTree(rootNavigationBarNode(sourceFile));
33+
}
34+
finally {
35+
curSourceFile = undefined;
36+
}
3137
}
3238

3339
// Keep sourceFile handy so we don't have to search for it every time we need to call `getText`.
34-
let curCancellationToken: ThrottledCancellationToken;
40+
let curCancellationToken: CancellationToken;
3541
let curSourceFile: SourceFile;
3642
function nodeText(node: Node): string {
3743
return node.getText(curSourceFile);
@@ -63,7 +69,7 @@ namespace ts.NavigationBar {
6369
const root: NavigationBarNode = { node: sourceFile, additionalNodes: undefined, parent: undefined, children: undefined, indent: 0 };
6470
parent = root;
6571
for (const statement of sourceFile.statements) {
66-
addChildrenRecursively(statement, curCancellationToken);
72+
addChildrenRecursively(statement);
6773
}
6874
endNode();
6975
Debug.assert(!parent && !parentsStack.length);
@@ -113,9 +119,9 @@ namespace ts.NavigationBar {
113119
}
114120

115121
/** Look for navigation bar items in node's subtree, adding them to the current `parent`. */
116-
function addChildrenRecursively(node: Node, cancellationToken: ThrottledCancellationToken): void {
122+
function addChildrenRecursively(node: Node): void {
117123
function addChildrenRecursively(node: Node): void {
118-
cancellationToken.throwIfCancellationRequested();
124+
curCancellationToken.throwIfCancellationRequested();
119125

120126
if (!node || isToken(node)) {
121127
return;

0 commit comments

Comments
 (0)