Skip to content

Commit

Permalink
fix(vscode): support async route handlers (#1041)
Browse files Browse the repository at this point in the history
  • Loading branch information
alestiago committed Sep 14, 2023
1 parent c27b05c commit 5a5017b
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 5 deletions.
4 changes: 3 additions & 1 deletion extensions/vscode/src/code-lens/on-request-code-lens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ abstract class RegularExpressionCodeLensProvider extends ConfigurableCodeLensPro

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

public provideCodeLenses(document: TextDocument): ProviderResult<CodeLens[]> {
if (document.languageId !== "dart") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ Response onRequest(RequestContext context, String id) {
`;

/**
* The content of a route file with an async route.
*/
const asyncRouteContent = `
import 'package:dart_frog/dart_frog.dart';
Future<Response> onRequest(RequestContext context, String id) async {
return Response(body: 'Welcome to Dart Frog!');
}
`;

/**
* The content of a route file with a possibly async route.
*/
const maybeAsyncRouteContent = `
import 'package:dart_frog/dart_frog.dart';
FutureOr<Response> onRequest(RequestContext context, String id) async {
return Response(body: 'Welcome to Dart Frog!');
}
`;

/**
* The content of something that looks like a route file but isn't.
*/
Expand Down Expand Up @@ -189,7 +213,7 @@ suite("RunOnRequestCodeLensProvider", () => {

const range = document.getWordRangeAtPosition(
new Position(3, 0),
/Response onRequest\(RequestContext context\) {/
provider.regex
)!;

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

const range = document.getWordRangeAtPosition(
new Position(3, 0),
/Response onRequest\(RequestContext context, String id\) {/
provider.regex
)!;

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

test("returns the correct CodeLenses on an async route", async () => {
const content = asyncRouteContent;
const textDocument = await workspace.openTextDocument({
language: "text",
content,
});
document.getText = textDocument.getText.bind(textDocument);
document.positionAt = textDocument.positionAt.bind(textDocument);
document.lineAt = textDocument.lineAt.bind(textDocument);
document.getWordRangeAtPosition =
textDocument.getWordRangeAtPosition.bind(textDocument);

const provider = new RunOnRequestCodeLensProvider();
const result = await provider.provideCodeLenses(document);

assert.strictEqual(result.length, 1);

const codeLens = result[0];

const range = document.getWordRangeAtPosition(
new Position(3, 0),
provider.regex
)!;

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

test("returns the correct CodeLenses on a possibly async route", async () => {
const content = maybeAsyncRouteContent;
const textDocument = await workspace.openTextDocument({
language: "text",
content,
});
document.getText = textDocument.getText.bind(textDocument);
document.positionAt = textDocument.positionAt.bind(textDocument);
document.lineAt = textDocument.lineAt.bind(textDocument);
document.getWordRangeAtPosition =
textDocument.getWordRangeAtPosition.bind(textDocument);

const provider = new RunOnRequestCodeLensProvider();
const result = await provider.provideCodeLenses(document);

assert.strictEqual(result.length, 1);

const codeLens = result[0];

const range = document.getWordRangeAtPosition(
new Position(3, 0),
provider.regex
)!;

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

const range = document.getWordRangeAtPosition(
new Position(3, 0),
/Response onRequest\(RequestContext context\) {/
provider.regex
)!;

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

test("returns the correct CodeLenses on an async route", async () => {
const content = asyncRouteContent;
const textDocument = await workspace.openTextDocument({
language: "text",
content,
});
document.getText = textDocument.getText.bind(textDocument);
document.positionAt = textDocument.positionAt.bind(textDocument);
document.lineAt = textDocument.lineAt.bind(textDocument);
document.getWordRangeAtPosition =
textDocument.getWordRangeAtPosition.bind(textDocument);

const provider = new DebugOnRequestCodeLensProvider();
const result = await provider.provideCodeLenses(document);

assert.strictEqual(result.length, 1);

const codeLens = result[0];

const range = document.getWordRangeAtPosition(
new Position(3, 0),
provider.regex
)!;

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

test("returns the correct CodeLenses on a possibly async route", async () => {
const content = maybeAsyncRouteContent;
const textDocument = await workspace.openTextDocument({
language: "text",
content,
});
document.getText = textDocument.getText.bind(textDocument);
document.positionAt = textDocument.positionAt.bind(textDocument);
document.lineAt = textDocument.lineAt.bind(textDocument);
document.getWordRangeAtPosition =
textDocument.getWordRangeAtPosition.bind(textDocument);

const provider = new DebugOnRequestCodeLensProvider();
const result = await provider.provideCodeLenses(document);

assert.strictEqual(result.length, 1);

const codeLens = result[0];

const range = document.getWordRangeAtPosition(
new Position(3, 0),
provider.regex
)!;

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

const range = document.getWordRangeAtPosition(
new Position(3, 0),
/Response onRequest\(RequestContext context, String id\) {/
provider.regex
)!;

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

0 comments on commit 5a5017b

Please sign in to comment.