Skip to content

Document highlights on async/await keywords should highlight other oc… #23923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/services/documentHighlights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ namespace ts.DocumentHighlights {
case SyntaxKind.GetKeyword:
case SyntaxKind.SetKeyword:
return getFromAllDeclarations(isAccessor, [SyntaxKind.GetKeyword, SyntaxKind.SetKeyword]);
case SyntaxKind.AwaitKeyword:
return useParent(node.parent, isAwaitExpression, getAsyncAndAwaitOccurrences);
case SyntaxKind.AsyncKeyword:
return highlightSpans(getAsyncAndAwaitOccurrences(node));
default:
return isModifierKind(node.kind) && (isDeclaration(node.parent) || isVariableStatement(node.parent))
? highlightSpans(getModifierOccurrences(node.kind, node.parent))
Expand Down Expand Up @@ -368,6 +372,35 @@ namespace ts.DocumentHighlights {
return keywords;
}

function getAsyncAndAwaitOccurrences(node: Node): Node[] | undefined {
const func = <FunctionLikeDeclaration>getContainingFunction(node);
if (!func) {
return undefined;
}

const keywords: Node[] = [];

if (func.modifiers) {
func.modifiers.forEach(modifier => {
pushKeywordIf(keywords, modifier, SyntaxKind.AsyncKeyword);
});
}

forEachChild(func, aggregate);

return keywords;

function aggregate(node: Node): void {
if (isAwaitExpression(node)) {
pushKeywordIf(keywords, node.getFirstToken(), SyntaxKind.AwaitKeyword);
}
// Do not cross function boundaries.
if (!isFunctionLike(node) && !isClassLike(node) && !isInterfaceDeclaration(node) && !isModuleDeclaration(node) && !isTypeAliasDeclaration(node) && !isTypeNode(node)) {
forEachChild(node, aggregate);
}
}
}

function getIfElseOccurrences(ifStatement: IfStatement, sourceFile: SourceFile): HighlightSpan[] {
const keywords = getIfElseKeywords(ifStatement, sourceFile);
const result: HighlightSpan[] = [];
Expand Down
27 changes: 27 additions & 0 deletions tests/cases/fourslash/getOccurrencesAsyncAwait.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// <reference path='fourslash.ts' />

////[|async|] function f() {
//// [|await|] 100;
//// [|a/**/wait|] [|await|] 200;
////class Foo {
//// async memberFunction() {
//// await 1;
//// }
////}
//// return [|await|] async function () {
//// await 300;
//// }
////}
////async function g() {
//// await 300;
//// async function f() {
//// await 400;
//// }
////}

verify.rangesAreOccurrences(false);

goTo.marker();
for (const range of test.ranges()) {
verify.occurrencesAtPositionContains(range, false);
}
16 changes: 16 additions & 0 deletions tests/cases/fourslash/getOccurrencesAsyncAwait2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference path='fourslash.ts' />

////[|a/**/sync|] function f() {
//// [|await|] 100;
//// [|await|] [|await|] 200;
//// return [|await|] async function () {
//// await 300;
//// }
////}

verify.rangesAreOccurrences(false);

goTo.marker();
for (const range of test.ranges()) {
verify.occurrencesAtPositionContains(range, false);
}
11 changes: 11 additions & 0 deletions tests/cases/fourslash/getOccurrencesAsyncAwait3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />

// Not valid TS ('await' expression is only allowed within an async function.)

////a/**/wait 100;
////async function f() {
//// await 300;
////}

goTo.marker();
verify.occurrencesAtPositionCount(0);