Skip to content

Commit 4a638d7

Browse files
extension.ts cleanup
1 parent 3a4bb8b commit 4a638d7

3 files changed

Lines changed: 27 additions & 21 deletions

File tree

package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,22 @@
4646
"command": "fastapi-vscode.copyEndpointPath",
4747
"title": "Copy Endpoint Path",
4848
"category": "FastAPI"
49+
},
50+
{
51+
"command": "fastapi-vscode.goToRouter",
52+
"title": "Go to Router Definition",
53+
"category": "FastAPI"
4954
}
5055
],
5156
"menus": {
5257
"commandPalette": [
5358
{
5459
"command": "fastapi-vscode.copyEndpointPath",
5560
"when": "false"
61+
},
62+
{
63+
"command": "fastapi-vscode.goToRouter",
64+
"when": "false"
5665
}
5766
],
5867
"view/title": [
@@ -72,6 +81,11 @@
7281
"command": "fastapi-vscode.copyEndpointPath",
7382
"when": "view == endpoint-explorer && viewItem == route",
7483
"group": "navigation"
84+
},
85+
{
86+
"command": "fastapi-vscode.goToRouter",
87+
"when": "view == endpoint-explorer && viewItem == router",
88+
"group": "navigation"
7589
}
7690
]
7791
},

src/extension.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* VSCode extension entry point for FastAPI endpoint discovery.
33
*/
44

5+
import { existsSync } from "node:fs"
56
import { sep } from "node:path"
67
import * as vscode from "vscode"
78
import { clearImportCache } from "./core/importResolver"
@@ -34,18 +35,22 @@ async function discoverFastAPIApps(parser: Parser): Promise<AppDefinition[]> {
3435
const entryPath = customEntryPoint.startsWith("/")
3536
? customEntryPoint
3637
: vscode.Uri.joinPath(folder.uri, customEntryPoint).fsPath
38+
39+
if (!existsSync(entryPath)) {
40+
vscode.window.showWarningMessage(
41+
`FastAPI entry point not found: ${customEntryPoint}`,
42+
)
43+
continue
44+
}
45+
3746
candidates = [entryPath]
3847
} else {
3948
// Scan for main.py and __init__.py files (likely FastAPI entry points)
4049
const mainFiles = await vscode.workspace.findFiles(
4150
new vscode.RelativePattern(folder, "**/main.py"),
42-
undefined,
43-
20,
4451
)
4552
const initFiles = await vscode.workspace.findFiles(
4653
new vscode.RelativePattern(folder, "**/__init__.py"),
47-
undefined,
48-
20,
4954
)
5055
// Prefer main.py, then __init__.py, sorted by path depth (shallower first)
5156
candidates = [...mainFiles, ...initFiles]
@@ -68,10 +73,6 @@ async function discoverFastAPIApps(parser: Parser): Promise<AppDefinition[]> {
6873
}
6974

7075
function navigateToLocation(location: SourceLocation): void {
71-
if (!location.filePath) {
72-
vscode.window.showErrorMessage("File path is missing for the endpoint.")
73-
return
74-
}
7576
const uri = vscode.Uri.file(location.filePath)
7677
const position = new vscode.Position(location.line - 1, location.column)
7778
vscode.window.showTextDocument(uri, {

src/providers/EndpointTreeProvider.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ const METHOD_ICONS: Record<RouteMethod, string> = {
3939
WEBSOCKET: "broadcast",
4040
}
4141

42-
/** Parent directories that don't provide useful context for labels */
43-
const GENERIC_PARENT_DIRS = new Set(["routes", "api", "routers", "endpoints"])
44-
4542
export class EndpointTreeProvider
4643
implements TreeDataProvider<EndpointTreeItem>
4744
{
@@ -346,17 +343,11 @@ export class EndpointTreeProvider
346343
// Add / prefix to tag-based labels for consistency
347344
routerLabel = `/${element.router.tags[0]}`
348345
} else {
349-
// Use parent directory + filename for context (e.g., "integrations/router")
346+
// Use filename as label (e.g., "api_routes" from "routes/api_routes.py")
350347
const filePath = element.router.location.filePath
351-
const parts = filePath.split("/")
352-
const fileName = parts.pop()?.replace(/\.py$/, "") ?? ""
353-
const parentDir = parts.pop() ?? ""
354-
// Skip generic parent dirs like "routes" or "api"
355-
if (parentDir && !GENERIC_PARENT_DIRS.has(parentDir)) {
356-
routerLabel = `${parentDir}/${fileName}`
357-
} else {
358-
routerLabel = fileName
359-
}
348+
const fileName =
349+
filePath.split("/").pop()?.replace(/\.py$/, "") ?? ""
350+
routerLabel = fileName
360351
}
361352
}
362353
const routerItem = new TreeItem(

0 commit comments

Comments
 (0)