@@ -18,8 +18,17 @@ export class VisualizationPanel {
1818 private _trace : FrontendTrace ;
1919 private _traceIndex : number ;
2020 private _tracePortSelfClose : boolean ;
21+ private _outChannel : vscode . OutputChannel ;
2122
22- private constructor ( context : vscode . ExtensionContext , filePath : string , fileHash : string , trace : BackendTrace , tracePort : MessagePort | null ) {
23+ private constructor (
24+ context : vscode . ExtensionContext ,
25+ outChannel : vscode . OutputChannel ,
26+ filePath : string ,
27+ fileHash : string ,
28+ trace : BackendTrace ,
29+ tracePort : MessagePort | null
30+ ) {
31+ this . _outChannel = outChannel ;
2332 this . _fileHash = fileHash ;
2433 this . _tracePort = tracePort ;
2534 this . _backendTrace = { trace : trace , complete : trace . length > 0 } ;
@@ -74,8 +83,8 @@ export class VisualizationPanel {
7483 if ( this . _panel ?. active ) {
7584 this . updateLineHighlight ( ) ;
7685 }
77- } , undefined , context . subscriptions ) ;
78-
86+ } , undefined , context . subscriptions ) ;
87+
7988
8089 // Message Receivers
8190 this . _panel . webview . onDidReceiveMessage (
@@ -100,7 +109,7 @@ export class VisualizationPanel {
100109 this . _trace . push ( ( new HTMLGenerator ( ) ) . generateHTML ( backendTraceElem ) ) ;
101110 await this . postMessagesToWebview ( 'updateButtons' , 'updateContent' ) ;
102111 if ( firstElement ) {
103- this . updateLineHighlight ( ) ;
112+ await this . updateLineHighlight ( ) ;
104113 }
105114 } ) ;
106115 this . _tracePort ?. on ( 'close' , async ( ) => {
@@ -118,12 +127,13 @@ export class VisualizationPanel {
118127
119128 public static async getVisualizationPanel (
120129 context : vscode . ExtensionContext ,
130+ outChannel : vscode . OutputChannel ,
121131 filePath : string ,
122132 fileHash : string ,
123133 trace : BackendTrace ,
124134 tracePort : MessagePort | null
125135 ) : Promise < VisualizationPanel | undefined > {
126- return new VisualizationPanel ( context , filePath , fileHash , trace , tracePort ) ;
136+ return new VisualizationPanel ( context , outChannel , filePath , fileHash , trace , tracePort ) ;
127137 }
128138
129139 // TODO: Look if Typescript is possible OR do better documentation in all files
@@ -185,35 +195,59 @@ export class VisualizationPanel {
185195 }
186196
187197 private async updateLineHighlight ( remove : boolean = false ) {
188- if ( this . _trace . length === 0 ) {
189- return ;
190- }
191- let editor : vscode . TextEditor | undefined = vscode . window . visibleTextEditors . filter (
192- editor => path . basename ( editor . document . uri . path ) === path . basename ( this . _trace [ this . _traceIndex ] [ 3 ] ! )
193- ) [ 0 ] ;
198+ try {
199+ if ( this . _trace . length === 0 ) {
200+ this . _outChannel . appendLine ( "updateLineHighlight: no trace available, aborting" ) ;
201+ return ;
202+ }
203+ const traceFile = this . _trace [ this . _traceIndex ] . filename ;
204+ this . _outChannel . appendLine (
205+ `updateLineHighlight: traceFile=${ traceFile } , traceIndex=${ this . _traceIndex } , remove=${ remove } ` ) ;
194206
195- const openPath = vscode . Uri . parse ( this . _trace [ this . _traceIndex ] [ 3 ] ! ) ;
196- if ( ! editor || editor . document . uri . path !== openPath . path ) {
197- await vscode . commands . executeCommand ( 'workbench.action.focusFirstEditorGroup' ) ;
198- const document = await vscode . workspace . openTextDocument ( openPath ) ;
199- editor = await vscode . window . showTextDocument ( document ) ;
200- }
207+ // Use vscode.Uri.file() for proper file path to URI conversion
208+ const openPath = vscode . Uri . file ( traceFile ) ;
201209
202- if ( remove || editor . document . lineCount < this . _trace [ this . _traceIndex ] [ 0 ] ) {
203- editor . setDecorations ( nextLineExecuteHighlightType , [ ] ) ;
204- } else {
205- this . setNextLineHighlighting ( editor ) ;
206- }
207- }
210+ // Find editor by full normalized path, not just basename
211+ let editor : vscode . TextEditor | undefined = vscode . window . visibleTextEditors . find (
212+ editor => editor . document . uri . fsPath === openPath . fsPath
213+ ) ;
208214
209- private setNextLineHighlighting ( editor : vscode . TextEditor ) {
210- if ( this . _trace . length === 0 ) {
211- return ;
212- }
213- const nextLine = this . _trace [ this . _traceIndex ] [ 0 ] - 1 ;
215+ if ( ! editor && remove ) {
216+ return ;
217+ } else if ( ! editor ) {
218+ this . _outChannel . appendLine ( `updateLineHighlight: editor not found, opening document: ${ openPath . fsPath } ` ) ;
219+ await vscode . commands . executeCommand ( 'workbench.action.focusFirstEditorGroup' ) ;
220+ const document = await vscode . workspace . openTextDocument ( openPath ) ;
221+ editor = await vscode . window . showTextDocument ( document , { preserveFocus : false } ) ;
222+ // Give the editor time to fully initialize
223+ await new Promise ( resolve => setTimeout ( resolve , 100 ) ) ;
224+ if ( ! editor ) {
225+ this . _outChannel . appendLine ( `updateLineHighlight: failed to get editor after opening document` ) ;
226+ return ;
227+ }
228+ }
214229
215- if ( nextLine > - 1 ) {
216- this . setEditorDecorations ( editor , nextLineExecuteHighlightType , nextLine ) ;
230+ const traceLine = this . _trace [ this . _traceIndex ] . lineNumber ;
231+ const lineNo = traceLine - 1 ; // zero-based indexing in vscode
232+ if ( remove ) {
233+ this . _outChannel . appendLine (
234+ "updateLineHighlight: removing highlighting in " + editor . document . fileName ) ;
235+ editor . setDecorations ( nextLineExecuteHighlightType , [ ] ) ;
236+ } else if ( lineNo < 0 || lineNo >= editor . document . lineCount ) {
237+ this . _outChannel . appendLine (
238+ "updateLineHighlight: traceLine " + traceLine + " out of range (doc has " +
239+ editor . document . lineCount + " lines) in " + editor . document . fileName ) ;
240+ editor . setDecorations ( nextLineExecuteHighlightType , [ ] ) ;
241+ } else {
242+ this . _outChannel . appendLine (
243+ "updateLineHighlight: highlighting line " + traceLine + " in " + editor . document . fileName ) ;
244+ this . setEditorDecorations ( editor , nextLineExecuteHighlightType , lineNo ) ;
245+ }
246+ } catch ( error ) {
247+ this . _outChannel . appendLine ( `updateLineHighlight: ERROR - ${ error } ` ) ;
248+ if ( error instanceof Error ) {
249+ this . _outChannel . appendLine ( `Stack: ${ error . stack } ` ) ;
250+ }
217251 }
218252 }
219253
0 commit comments