Skip to content

Commit 6f9dc2f

Browse files
authored
Merge pull request microsoft#23923 from Zzzen/master
Document highlights on async/await keywords should highlight other oc…
2 parents 9ea4d93 + 91a15dc commit 6f9dc2f

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

src/services/documentHighlights.ts

+33
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ namespace ts.DocumentHighlights {
7474
case SyntaxKind.GetKeyword:
7575
case SyntaxKind.SetKeyword:
7676
return getFromAllDeclarations(isAccessor, [SyntaxKind.GetKeyword, SyntaxKind.SetKeyword]);
77+
case SyntaxKind.AwaitKeyword:
78+
return useParent(node.parent, isAwaitExpression, getAsyncAndAwaitOccurrences);
79+
case SyntaxKind.AsyncKeyword:
80+
return highlightSpans(getAsyncAndAwaitOccurrences(node));
7781
default:
7882
return isModifierKind(node.kind) && (isDeclaration(node.parent) || isVariableStatement(node.parent))
7983
? highlightSpans(getModifierOccurrences(node.kind, node.parent))
@@ -368,6 +372,35 @@ namespace ts.DocumentHighlights {
368372
return keywords;
369373
}
370374

375+
function getAsyncAndAwaitOccurrences(node: Node): Node[] | undefined {
376+
const func = <FunctionLikeDeclaration>getContainingFunction(node);
377+
if (!func) {
378+
return undefined;
379+
}
380+
381+
const keywords: Node[] = [];
382+
383+
if (func.modifiers) {
384+
func.modifiers.forEach(modifier => {
385+
pushKeywordIf(keywords, modifier, SyntaxKind.AsyncKeyword);
386+
});
387+
}
388+
389+
forEachChild(func, aggregate);
390+
391+
return keywords;
392+
393+
function aggregate(node: Node): void {
394+
if (isAwaitExpression(node)) {
395+
pushKeywordIf(keywords, node.getFirstToken(), SyntaxKind.AwaitKeyword);
396+
}
397+
// Do not cross function boundaries.
398+
if (!isFunctionLike(node) && !isClassLike(node) && !isInterfaceDeclaration(node) && !isModuleDeclaration(node) && !isTypeAliasDeclaration(node) && !isTypeNode(node)) {
399+
forEachChild(node, aggregate);
400+
}
401+
}
402+
}
403+
371404
function getIfElseOccurrences(ifStatement: IfStatement, sourceFile: SourceFile): HighlightSpan[] {
372405
const keywords = getIfElseKeywords(ifStatement, sourceFile);
373406
const result: HighlightSpan[] = [];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////[|async|] function f() {
4+
//// [|await|] 100;
5+
//// [|a/**/wait|] [|await|] 200;
6+
////class Foo {
7+
//// async memberFunction() {
8+
//// await 1;
9+
//// }
10+
////}
11+
//// return [|await|] async function () {
12+
//// await 300;
13+
//// }
14+
////}
15+
////async function g() {
16+
//// await 300;
17+
//// async function f() {
18+
//// await 400;
19+
//// }
20+
////}
21+
22+
verify.rangesAreOccurrences(false);
23+
24+
goTo.marker();
25+
for (const range of test.ranges()) {
26+
verify.occurrencesAtPositionContains(range, false);
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////[|a/**/sync|] function f() {
4+
//// [|await|] 100;
5+
//// [|await|] [|await|] 200;
6+
//// return [|await|] async function () {
7+
//// await 300;
8+
//// }
9+
////}
10+
11+
verify.rangesAreOccurrences(false);
12+
13+
goTo.marker();
14+
for (const range of test.ranges()) {
15+
verify.occurrencesAtPositionContains(range, false);
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// Not valid TS ('await' expression is only allowed within an async function.)
4+
5+
////a/**/wait 100;
6+
////async function f() {
7+
//// await 300;
8+
////}
9+
10+
goTo.marker();
11+
verify.occurrencesAtPositionCount(0);

0 commit comments

Comments
 (0)