Skip to content

Commit 014c63a

Browse files
✨ Add route search to command palette (#21)
1 parent 26d2325 commit 014c63a

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@
5858
"command": "fastapi-vscode.reportIssue",
5959
"title": "Report Issue...",
6060
"category": "FastAPI"
61+
},
62+
{
63+
"command": "fastapi-vscode.searchEndpoints",
64+
"title": "Search Endpoints...",
65+
"category": "FastAPI",
66+
"icon": "$(search)"
67+
}
68+
],
69+
"keybindings": [
70+
{
71+
"command": "fastapi-vscode.searchEndpoints",
72+
"key": "ctrl+shift+e",
73+
"mac": "cmd+shift+e"
6174
}
6275
],
6376
"menus": {

src/extension.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ import type { SourceLocation } from "./core/types"
1111
import {
1212
type EndpointTreeItem,
1313
EndpointTreeProvider,
14+
METHOD_ICONS,
1415
} from "./providers/endpointTreeProvider"
1516
import { TestCodeLensProvider } from "./providers/testCodeLensProvider"
16-
import { vscodeFileSystem } from "./providers/vscodeFileSystem"
1717
import { disposeLogger, log } from "./utils/logger"
1818

1919
let parserService: Parser | null = null
2020

2121
function navigateToLocation(location: SourceLocation): void {
22-
// filePath is now a URI string, parse it back to vscode.Uri
2322
const uri = vscode.Uri.parse(location.filePath)
2423
const position = new vscode.Position(location.line - 1, location.column)
2524
vscode.window.showTextDocument(uri, {
@@ -138,6 +137,43 @@ function registerCommands(
138137
},
139138
),
140139

140+
vscode.commands.registerCommand(
141+
"fastapi-vscode.searchEndpoints",
142+
async () => {
143+
const workspacePrefix =
144+
vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? ""
145+
const items = endpointProvider
146+
.getAllRoutes()
147+
.map((route) => {
148+
const path = stripLeadingDynamicSegments(route.path)
149+
return {
150+
label: `$(${METHOD_ICONS[route.method]}) ${route.method.toUpperCase()} ${path}`,
151+
description: route.functionName,
152+
detail: vscode.Uri.parse(route.location.filePath)
153+
.fsPath.replace(workspacePrefix, "")
154+
.replace(/^\//, ""),
155+
route,
156+
sortKey: `${path} ${route.method}`,
157+
}
158+
})
159+
.sort((a, b) => a.sortKey.localeCompare(b.sortKey))
160+
161+
if (items.length === 0) {
162+
vscode.window.showInformationMessage(
163+
"No FastAPI endpoints found in the workspace.",
164+
)
165+
return
166+
}
167+
168+
const selected = await vscode.window.showQuickPick(items, {
169+
placeHolder: "Search FastAPI endpoints...",
170+
})
171+
if (selected) {
172+
navigateToLocation(selected.route.location)
173+
}
174+
},
175+
),
176+
141177
vscode.commands.registerCommand(
142178
"fastapi-vscode.copyEndpointPath",
143179
(item: EndpointTreeItem) => {

src/providers/endpointTreeProvider.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const defaultGrouping: GroupingFunction = (apps) =>
2828
apps.map((app) => ({ type: "app" as const, app }))
2929

3030
/** Method icons for route display */
31-
const METHOD_ICONS: Record<RouteMethod, string> = {
31+
export const METHOD_ICONS: Record<RouteMethod, string> = {
3232
GET: "arrow-right",
3333
POST: "plus",
3434
PUT: "edit",
@@ -56,6 +56,23 @@ export class EndpointTreeProvider
5656
this.groupApps = groupApps ?? defaultGrouping
5757
}
5858

59+
getApps(): AppDefinition[] {
60+
return this.apps
61+
}
62+
63+
/** Returns all routes from all apps, including nested router routes. */
64+
getAllRoutes(): RouteDefinition[] {
65+
const collectFromRouters = (
66+
routers: RouterDefinition[],
67+
): RouteDefinition[] =>
68+
routers.flatMap((r) => [...r.routes, ...collectFromRouters(r.children)])
69+
70+
return this.apps.flatMap((app) => [
71+
...app.routes,
72+
...collectFromRouters(app.routers),
73+
])
74+
}
75+
5976
setApps(apps: AppDefinition[], groupApps?: GroupingFunction): void {
6077
this.apps = apps
6178
if (groupApps) {

0 commit comments

Comments
 (0)