@@ -15,6 +15,7 @@ import {
1515 type EndpointTreeItem ,
1616 EndpointTreeProvider ,
1717} from "./providers/EndpointTreeProvider"
18+ import { TestCodeLensProvider } from "./providers/TestCodeLensProvider"
1819
1920async function discoverFastAPIApps ( parser : Parser ) : Promise < AppDefinition [ ] > {
2021 const apps : AppDefinition [ ] = [ ]
@@ -102,11 +103,48 @@ export async function activate(context: vscode.ExtensionContext) {
102103 // Discover FastAPI endpoints from workspace
103104 const apps = await discoverFastAPIApps ( parserService )
104105 const endpointProvider = new EndpointTreeProvider ( apps )
106+ const codeLensProvider = new TestCodeLensProvider ( parserService , apps )
107+
108+ let refreshTimeout : NodeJS . Timeout | null = null
109+
110+ const triggerRefresh = ( ) => {
111+ if ( refreshTimeout ) {
112+ clearTimeout ( refreshTimeout )
113+ }
114+ refreshTimeout = setTimeout ( async ( ) => {
115+ if ( ! parserService ) {
116+ return
117+ }
118+ const newApps = await discoverFastAPIApps ( parserService )
119+ endpointProvider . setApps ( newApps )
120+ codeLensProvider . setApps ( newApps )
121+ } , 500 )
122+ }
123+
124+ // Watch for changes in Python files to refresh endpoints
125+ const watcher = vscode . workspace . createFileSystemWatcher ( "**/*.py" )
126+ watcher . onDidChange ( triggerRefresh )
127+ watcher . onDidCreate ( triggerRefresh )
128+ watcher . onDidDelete ( triggerRefresh )
129+ context . subscriptions . push ( watcher )
105130
106131 const treeView = vscode . window . createTreeView ( "endpoint-explorer" , {
107132 treeDataProvider : endpointProvider ,
108133 } )
109134
135+ // Register CodeLens provider for test files
136+ const config = vscode . workspace . getConfiguration ( "fastapi" )
137+ if ( config . get < boolean > ( "showTestCodeLenses" , true ) ) {
138+ context . subscriptions . push (
139+ vscode . languages . registerCodeLensProvider (
140+ // Covers common test file patterns
141+ // e.g., test_*.py, *_test.py, tests/*.py
142+ { language : "python" , pattern : "**/*test*.py" } ,
143+ codeLensProvider ,
144+ ) ,
145+ )
146+ }
147+
110148 context . subscriptions . push (
111149 treeView ,
112150
@@ -119,6 +157,7 @@ export async function activate(context: vscode.ExtensionContext) {
119157 clearImportCache ( )
120158 const newApps = await discoverFastAPIApps ( parserService )
121159 endpointProvider . setApps ( newApps )
160+ codeLensProvider . setApps ( newApps )
122161 } ,
123162 ) ,
124163
@@ -162,6 +201,24 @@ export async function activate(context: vscode.ExtensionContext) {
162201 vscode . commands . registerCommand ( "fastapi-vscode.toggleRouters" , ( ) => {
163202 endpointProvider . toggleRouters ( )
164203 } ) ,
204+
205+ vscode . commands . registerCommand (
206+ "fastapi-vscode.goToDefinition" ,
207+ (
208+ locations : vscode . Location [ ] ,
209+ fromUri : vscode . Uri ,
210+ fromPosition : vscode . Position ,
211+ ) => {
212+ vscode . commands . executeCommand (
213+ "editor.action.goToLocations" ,
214+ fromUri ,
215+ fromPosition ,
216+ locations ,
217+ locations . length === 1 ? "goto" : "peek" ,
218+ "No matching route found" ,
219+ )
220+ } ,
221+ ) ,
165222 )
166223}
167224
0 commit comments