@@ -196,6 +196,45 @@ async function startServer(context: vscode.ExtensionContext): Promise<vscode.Dis
196196 } )
197197}
198198
199+ async function resolveFile ( filePath : string ) : Promise < vscode . Uri > {
200+ if ( path . isAbsolute ( filePath ) ) {
201+ return vscode . Uri . file ( filePath )
202+ }
203+
204+ const workspaceFolders = vscode . workspace . workspaceFolders
205+ if ( ! workspaceFolders || workspaceFolders . length === 0 ) {
206+ throw new Error ( "No workspace folder found" )
207+ }
208+
209+ const workspaceRoot = workspaceFolders [ 0 ] . uri . fsPath
210+ const fullPath = path . join ( workspaceRoot , "contracts" , filePath )
211+
212+ try {
213+ const uri = vscode . Uri . file ( fullPath )
214+ await vscode . workspace . fs . stat ( uri )
215+ return uri
216+ } catch {
217+ const foundFiles = await vscode . workspace . findFiles (
218+ `**/${ path . basename ( filePath ) } ` ,
219+ "**/node_modules/**" , // no need to search in node_modules
220+ 10 ,
221+ )
222+
223+ if ( foundFiles . length === 0 ) {
224+ throw new Error ( `File not found: ${ filePath } ` )
225+ }
226+
227+ if ( foundFiles . length === 1 ) {
228+ return foundFiles [ 0 ]
229+ } else {
230+ const exactMatch = foundFiles . find (
231+ file => file . fsPath . endsWith ( filePath ) || file . fsPath . includes ( filePath ) ,
232+ )
233+ return exactMatch ?? foundFiles [ 0 ]
234+ }
235+ }
236+ }
237+
199238function registerCommands ( disposables : vscode . Disposable [ ] ) : void {
200239 disposables . push (
201240 vscode . commands . registerCommand ( "tolk.showToolchainInfo" , async ( ) => {
@@ -613,6 +652,23 @@ Node.js: ${info.environment.nodeVersion ?? "Unknown"}`
613652 void vscode . env . clipboard . writeText ( str )
614653 void vscode . window . showInformationMessage ( `Copied ${ str } to clipboard` )
615654 } ) ,
655+ vscode . commands . registerCommand ( "ton.openFile" , async ( filePath : string , line ?: number ) => {
656+ try {
657+ const uri = await resolveFile ( filePath )
658+
659+ if ( line !== undefined && line > 0 ) {
660+ const uriWithFragment = uri . with ( {
661+ fragment : `L${ line } ` ,
662+ } )
663+ await vscode . commands . executeCommand ( "vscode.open" , uriWithFragment )
664+ } else {
665+ await vscode . commands . executeCommand ( "vscode.open" , uri )
666+ }
667+ } catch ( error ) {
668+ vscode . window . showErrorMessage ( `Failed to open file: ${ filePath } ` )
669+ console . error ( "Error opening file:" , error )
670+ }
671+ } ) ,
616672 )
617673}
618674
0 commit comments