Skip to content

Commit 5a5017b

Browse files
authored
fix(vscode): support async route handlers (#1041)
1 parent c27b05c commit 5a5017b

File tree

2 files changed

+139
-5
lines changed

2 files changed

+139
-5
lines changed

extensions/vscode/src/code-lens/on-request-code-lens.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ abstract class RegularExpressionCodeLensProvider extends ConfigurableCodeLensPro
7575

7676
// eslint-disable-next-line max-len
7777
abstract class OnRequestCodeLensProvider extends RegularExpressionCodeLensProvider {
78-
readonly regex: RegExp = /Response\s*onRequest\(RequestContext .*?\)\s*{/g;
78+
readonly regex: RegExp =
79+
// eslint-disable-next-line max-len
80+
/(Response|Future<Response>|FutureOr<Response>)\s*onRequest\(RequestContext .*?\)\s*(?:async)?\s*{/g;
7981

8082
public provideCodeLenses(document: TextDocument): ProviderResult<CodeLens[]> {
8183
if (document.languageId !== "dart") {

extensions/vscode/src/test/suite/code-lens/on-request-code-lens.test.ts

Lines changed: 136 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,30 @@ Response onRequest(RequestContext context, String id) {
2929
3030
`;
3131

32+
/**
33+
* The content of a route file with an async route.
34+
*/
35+
const asyncRouteContent = `
36+
import 'package:dart_frog/dart_frog.dart';
37+
38+
Future<Response> onRequest(RequestContext context, String id) async {
39+
return Response(body: 'Welcome to Dart Frog!');
40+
}
41+
42+
`;
43+
44+
/**
45+
* The content of a route file with a possibly async route.
46+
*/
47+
const maybeAsyncRouteContent = `
48+
import 'package:dart_frog/dart_frog.dart';
49+
50+
FutureOr<Response> onRequest(RequestContext context, String id) async {
51+
return Response(body: 'Welcome to Dart Frog!');
52+
}
53+
54+
`;
55+
3256
/**
3357
* The content of something that looks like a route file but isn't.
3458
*/
@@ -189,7 +213,7 @@ suite("RunOnRequestCodeLensProvider", () => {
189213

190214
const range = document.getWordRangeAtPosition(
191215
new Position(3, 0),
192-
/Response onRequest\(RequestContext context\) {/
216+
provider.regex
193217
)!;
194218

195219
sinon.assert.match(codeLens, new CodeLens(range));
@@ -216,7 +240,61 @@ suite("RunOnRequestCodeLensProvider", () => {
216240

217241
const range = document.getWordRangeAtPosition(
218242
new Position(3, 0),
219-
/Response onRequest\(RequestContext context, String id\) {/
243+
provider.regex
244+
)!;
245+
246+
sinon.assert.match(codeLens, new CodeLens(range));
247+
});
248+
249+
test("returns the correct CodeLenses on an async route", async () => {
250+
const content = asyncRouteContent;
251+
const textDocument = await workspace.openTextDocument({
252+
language: "text",
253+
content,
254+
});
255+
document.getText = textDocument.getText.bind(textDocument);
256+
document.positionAt = textDocument.positionAt.bind(textDocument);
257+
document.lineAt = textDocument.lineAt.bind(textDocument);
258+
document.getWordRangeAtPosition =
259+
textDocument.getWordRangeAtPosition.bind(textDocument);
260+
261+
const provider = new RunOnRequestCodeLensProvider();
262+
const result = await provider.provideCodeLenses(document);
263+
264+
assert.strictEqual(result.length, 1);
265+
266+
const codeLens = result[0];
267+
268+
const range = document.getWordRangeAtPosition(
269+
new Position(3, 0),
270+
provider.regex
271+
)!;
272+
273+
sinon.assert.match(codeLens, new CodeLens(range));
274+
});
275+
276+
test("returns the correct CodeLenses on a possibly async route", async () => {
277+
const content = maybeAsyncRouteContent;
278+
const textDocument = await workspace.openTextDocument({
279+
language: "text",
280+
content,
281+
});
282+
document.getText = textDocument.getText.bind(textDocument);
283+
document.positionAt = textDocument.positionAt.bind(textDocument);
284+
document.lineAt = textDocument.lineAt.bind(textDocument);
285+
document.getWordRangeAtPosition =
286+
textDocument.getWordRangeAtPosition.bind(textDocument);
287+
288+
const provider = new RunOnRequestCodeLensProvider();
289+
const result = await provider.provideCodeLenses(document);
290+
291+
assert.strictEqual(result.length, 1);
292+
293+
const codeLens = result[0];
294+
295+
const range = document.getWordRangeAtPosition(
296+
new Position(3, 0),
297+
provider.regex
220298
)!;
221299

222300
sinon.assert.match(codeLens, new CodeLens(range));
@@ -390,7 +468,61 @@ suite("DebugOnRequestCodeLensProvider", () => {
390468

391469
const range = document.getWordRangeAtPosition(
392470
new Position(3, 0),
393-
/Response onRequest\(RequestContext context\) {/
471+
provider.regex
472+
)!;
473+
474+
sinon.assert.match(codeLens, new CodeLens(range));
475+
});
476+
477+
test("returns the correct CodeLenses on an async route", async () => {
478+
const content = asyncRouteContent;
479+
const textDocument = await workspace.openTextDocument({
480+
language: "text",
481+
content,
482+
});
483+
document.getText = textDocument.getText.bind(textDocument);
484+
document.positionAt = textDocument.positionAt.bind(textDocument);
485+
document.lineAt = textDocument.lineAt.bind(textDocument);
486+
document.getWordRangeAtPosition =
487+
textDocument.getWordRangeAtPosition.bind(textDocument);
488+
489+
const provider = new DebugOnRequestCodeLensProvider();
490+
const result = await provider.provideCodeLenses(document);
491+
492+
assert.strictEqual(result.length, 1);
493+
494+
const codeLens = result[0];
495+
496+
const range = document.getWordRangeAtPosition(
497+
new Position(3, 0),
498+
provider.regex
499+
)!;
500+
501+
sinon.assert.match(codeLens, new CodeLens(range));
502+
});
503+
504+
test("returns the correct CodeLenses on a possibly async route", async () => {
505+
const content = maybeAsyncRouteContent;
506+
const textDocument = await workspace.openTextDocument({
507+
language: "text",
508+
content,
509+
});
510+
document.getText = textDocument.getText.bind(textDocument);
511+
document.positionAt = textDocument.positionAt.bind(textDocument);
512+
document.lineAt = textDocument.lineAt.bind(textDocument);
513+
document.getWordRangeAtPosition =
514+
textDocument.getWordRangeAtPosition.bind(textDocument);
515+
516+
const provider = new DebugOnRequestCodeLensProvider();
517+
const result = await provider.provideCodeLenses(document);
518+
519+
assert.strictEqual(result.length, 1);
520+
521+
const codeLens = result[0];
522+
523+
const range = document.getWordRangeAtPosition(
524+
new Position(3, 0),
525+
provider.regex
394526
)!;
395527

396528
sinon.assert.match(codeLens, new CodeLens(range));
@@ -417,7 +549,7 @@ suite("DebugOnRequestCodeLensProvider", () => {
417549

418550
const range = document.getWordRangeAtPosition(
419551
new Position(3, 0),
420-
/Response onRequest\(RequestContext context, String id\) {/
552+
provider.regex
421553
)!;
422554

423555
sinon.assert.match(codeLens, new CodeLens(range));

0 commit comments

Comments
 (0)