Skip to content
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

fix(vscode): support async route handlers #1041

Merged
merged 4 commits into from
Sep 14, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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