@@ -109,7 +109,7 @@ export class VisualizationPanel {
109109 this . _trace . push ( ( new HTMLGenerator ( ) ) . generateHTML ( backendTraceElem ) ) ;
110110 await this . postMessagesToWebview ( 'updateButtons' , 'updateContent' ) ;
111111 if ( firstElement ) {
112- this . updateLineHighlight ( ) ;
112+ await this . updateLineHighlight ( ) ;
113113 }
114114 } ) ;
115115 this . _tracePort ?. on ( 'close' , async ( ) => {
@@ -195,45 +195,58 @@ export class VisualizationPanel {
195195 }
196196
197197 private async updateLineHighlight ( remove : boolean = false ) {
198- if ( this . _trace . length === 0 ) {
199- this . _outChannel . appendLine ( "updateLineHighlight: no trace available, aborting" ) ;
200- return ;
201- }
202- const traceFile = this . _trace [ this . _traceIndex ] [ 3 ] ! ;
203- let editor : vscode . TextEditor | undefined = vscode . window . visibleTextEditors . filter (
204- editor => path . basename ( editor . document . uri . path ) === path . basename ( traceFile )
205- ) [ 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 } ` ) ;
206206
207- const openPath = vscode . Uri . parse ( traceFile ) ;
208- if ( ! editor || editor . document . uri . path !== openPath . path ) {
209- // How can it be that editor is not null/undefined, but the path does not match?
210- await vscode . commands . executeCommand ( 'workbench.action.focusFirstEditorGroup' ) ;
211- const document = await vscode . workspace . openTextDocument ( openPath ) ;
212- editor = await vscode . window . showTextDocument ( document ) ;
213- }
207+ // Use vscode.Uri.file() for proper file path to URI conversion
208+ const openPath = vscode . Uri . file ( traceFile ) ;
214209
215- const traceLine = this . _trace [ this . _traceIndex ] [ 0 ] ;
216- if ( remove ) {
217- this . _outChannel . appendLine (
218- "updateLineHighlight: removing highlighting in " + editor . document . fileName ) ;
219- editor . setDecorations ( nextLineExecuteHighlightType , [ ] ) ;
220- } else if ( editor . document . lineCount < traceLine ) {
221- // How can it be that traceLine is out of range?
222- this . _outChannel . appendLine (
223- "updateLineHighlight: removing highlighting in " + editor . document . fileName +
224- "(out of range)" ) ;
225- editor . setDecorations ( nextLineExecuteHighlightType , [ ] ) ;
226- } else {
227- const hlLine = traceLine - 1 ; // why - 1
228- if ( hlLine > - 1 ) {
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+ ) ;
214+
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+ }
229+
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 ) {
229237 this . _outChannel . appendLine (
230- "updateLineHighlight: highlighting line " + hlLine + " in " + editor . document . fileName ) ;
231- this . setEditorDecorations ( editor , nextLineExecuteHighlightType , hlLine ) ;
238+ "updateLineHighlight: traceLine " + traceLine + " out of range (doc has " +
239+ editor . document . lineCount + " lines) in " + editor . document . fileName ) ;
240+ editor . setDecorations ( nextLineExecuteHighlightType , [ ] ) ;
232241 } else {
233- // how can this happen?
234242 this . _outChannel . appendLine (
235- "updateLineHighlight: cannot highlight line " + hlLine + " in " +
236- editor . document . fileName + "(out of range)" ) ;
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 } ` ) ;
237250 }
238251 }
239252 }
0 commit comments