Skip to content

Commit c1d717f

Browse files
authored
fix(vscode): support CodeLens for very long route signatures (#1046)
1 parent 5a5017b commit c1d717f

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ abstract class RegularExpressionCodeLensProvider extends ConfigurableCodeLensPro
7777
abstract class OnRequestCodeLensProvider extends RegularExpressionCodeLensProvider {
7878
readonly regex: RegExp =
7979
// eslint-disable-next-line max-len
80-
/(Response|Future<Response>|FutureOr<Response>)\s*onRequest\(RequestContext .*?\)\s*(?:async)?\s*{/g;
80+
/(Response|Future<Response>|FutureOr<Response>)\s*onRequest\(.*?/g;
8181

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

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,23 @@ Response notOnRequest(RequestContext context) {
6565
6666
`;
6767

68+
/**
69+
* A very long dynamic route with multiple arguments.
70+
*/
71+
const longDynamicRouteContent = `
72+
import 'package:dart_frog/dart_frog.dart';
73+
74+
Response onRequest(
75+
RequestContext context,
76+
String id1,
77+
String id2,
78+
String id3,
79+
) {
80+
return Response(body: 'Welcome to Dart Frog!');
81+
}
82+
83+
`;
84+
6885
suite("RunOnRequestCodeLensProvider", () => {
6986
let vscodeStub: any;
7087
let utilsStub: any;
@@ -246,6 +263,33 @@ suite("RunOnRequestCodeLensProvider", () => {
246263
sinon.assert.match(codeLens, new CodeLens(range));
247264
});
248265

266+
test("returns the correct CodeLenses on a long dynamic route", async () => {
267+
const content = longDynamicRouteContent;
268+
const textDocument = await workspace.openTextDocument({
269+
language: "text",
270+
content,
271+
});
272+
document.getText = textDocument.getText.bind(textDocument);
273+
document.positionAt = textDocument.positionAt.bind(textDocument);
274+
document.lineAt = textDocument.lineAt.bind(textDocument);
275+
document.getWordRangeAtPosition =
276+
textDocument.getWordRangeAtPosition.bind(textDocument);
277+
278+
const provider = new RunOnRequestCodeLensProvider();
279+
const result = await provider.provideCodeLenses(document);
280+
281+
assert.strictEqual(result.length, 1);
282+
283+
const codeLens = result[0];
284+
285+
const range = document.getWordRangeAtPosition(
286+
new Position(3, 0),
287+
provider.regex
288+
)!;
289+
290+
sinon.assert.match(codeLens, new CodeLens(range));
291+
});
292+
249293
test("returns the correct CodeLenses on an async route", async () => {
250294
const content = asyncRouteContent;
251295
const textDocument = await workspace.openTextDocument({
@@ -555,6 +599,33 @@ suite("DebugOnRequestCodeLensProvider", () => {
555599
sinon.assert.match(codeLens, new CodeLens(range));
556600
});
557601

602+
test("returns the correct CodeLenses on a long dynamic route", async () => {
603+
const content = longDynamicRouteContent;
604+
const textDocument = await workspace.openTextDocument({
605+
language: "text",
606+
content,
607+
});
608+
document.getText = textDocument.getText.bind(textDocument);
609+
document.positionAt = textDocument.positionAt.bind(textDocument);
610+
document.lineAt = textDocument.lineAt.bind(textDocument);
611+
document.getWordRangeAtPosition =
612+
textDocument.getWordRangeAtPosition.bind(textDocument);
613+
614+
const provider = new DebugOnRequestCodeLensProvider();
615+
const result = await provider.provideCodeLenses(document);
616+
617+
assert.strictEqual(result.length, 1);
618+
619+
const codeLens = result[0];
620+
621+
const range = document.getWordRangeAtPosition(
622+
new Position(3, 0),
623+
provider.regex
624+
)!;
625+
626+
sinon.assert.match(codeLens, new CodeLens(range));
627+
});
628+
558629
test("returns no CodeLenses on a non route file", async () => {
559630
const content = invalidRouteContent;
560631
const textDocument = await workspace.openTextDocument({

0 commit comments

Comments
 (0)