@@ -5,6 +5,7 @@ import { DebugProtocol } from '@vscode/debugprotocol';
55import { DebugServices , BreakpointInfo } from './DebugServices'
66import { ResolvablePromise , createResolvablePromise } from '../utils/PromiseUtils'
77import { IMVM , MVMError , MatlabState } from '../mvm/impl/MVM' ;
8+ import fs from 'node:fs' ;
89
910enum BreakpointChangeType {
1011 ADD ,
@@ -233,6 +234,8 @@ export default class MatlabDebugAdaptor {
233234
234235 private _setupListeners ( ) : void {
235236 this . _debugServices . on ( DebugServices . Events . BreakpointAdded , async ( breakpoint : BreakpointInfo ) => {
237+ breakpoint . filePath = this . _mapToMFile ( breakpoint . filePath , false ) ;
238+
236239 this . _matlabBreakpoints . push ( breakpoint ) ;
237240
238241 this . _breakpointChangeListeners . forEach ( ( listener ) => {
@@ -241,6 +244,8 @@ export default class MatlabDebugAdaptor {
241244 } ) ;
242245
243246 this . _debugServices . on ( DebugServices . Events . BreakpointRemoved , async ( breakpoint : BreakpointInfo ) => {
247+ breakpoint . filePath = this . _mapToMFile ( breakpoint . filePath , false ) ;
248+
244249 this . _matlabBreakpoints = this . _matlabBreakpoints . filter ( ( existingBreakpoint ) => {
245250 return ! existingBreakpoint . equals ( breakpoint , true ) ;
246251 } ) ;
@@ -422,6 +427,7 @@ export default class MatlabDebugAdaptor {
422427 }
423428
424429 const canonicalizedPath = await this . _getCanonicalPath ( source . path ) ;
430+ const pathToSetOrClear = this . _mapToPFile ( canonicalizedPath , true ) ;
425431
426432 const newBreakpoints : BreakpointInfo [ ] = ( args . breakpoints != null )
427433 ? args . breakpoints . map ( ( breakpoint ) => {
@@ -458,7 +464,7 @@ export default class MatlabDebugAdaptor {
458464 // Remove all breakpoints that are now gone.
459465 const breakpointsRemovalPromises : Array < Promise < void > > = [ ] ;
460466 breakpointsToRemove . forEach ( ( breakpoint : BreakpointInfo ) => {
461- breakpointsRemovalPromises . push ( this . _mvm . clearBreakpoint ( breakpoint . filePath , breakpoint . lineNumber ) ) ;
467+ breakpointsRemovalPromises . push ( this . _mvm . clearBreakpoint ( pathToSetOrClear , breakpoint . lineNumber ) ) ;
462468 } )
463469 await Promise . all ( breakpointsRemovalPromises ) ;
464470
@@ -476,12 +482,12 @@ export default class MatlabDebugAdaptor {
476482
477483 let matlabBreakpointInfos : BreakpointInfo [ ] = [ ] ;
478484 const listener = this . _registerBreakpointChangeListener ( ( changeType , bpInfo ) => {
479- if ( changeType === BreakpointChangeType . ADD && bpInfo . filePath === canonicalizedPath ) {
485+ if ( changeType === BreakpointChangeType . ADD && bpInfo . filePath === pathToSetOrClear ) {
480486 matlabBreakpointInfos . push ( bpInfo ) ;
481487 }
482488 } ) ;
483489
484- await this . _mvm . setBreakpoint ( canonicalizedPath , newBreakpoint . info . lineNumber , newBreakpoint . info . condition ) ;
490+ await this . _mvm . setBreakpoint ( pathToSetOrClear , newBreakpoint . info . lineNumber , newBreakpoint . info . condition ) ;
485491
486492 listener . remove ( ) ;
487493
@@ -501,6 +507,36 @@ export default class MatlabDebugAdaptor {
501507 this . _clearPendingBreakpointsRequest ( ) ;
502508 }
503509
510+ _mapToPFile ( filePath : string , checkIfExists : boolean ) : string {
511+ // If this is an m-file then convert to p-file and check existence
512+ if ( filePath . endsWith ( '.m' ) ) {
513+ const pFile = filePath . substring ( 0 , filePath . length - 1 ) + 'p' ;
514+ if ( ! checkIfExists || fs . existsSync ( pFile ) ) {
515+ return pFile ;
516+ } else {
517+ return filePath ;
518+ }
519+ }
520+
521+ // Not an m file so p-code not supported
522+ return filePath ;
523+ }
524+
525+ _mapToMFile ( filePath : string , checkIfExists : boolean ) : string {
526+ // If this is an p-file then convert to m-file and check existence
527+ if ( filePath . endsWith ( '.p' ) ) {
528+ const mFile = filePath . substring ( 0 , filePath . length - 1 ) + 'm' ;
529+ if ( ! checkIfExists || fs . existsSync ( mFile ) ) {
530+ return mFile ;
531+ } else {
532+ return filePath ;
533+ }
534+ }
535+
536+ // Not an m file so p-code not supported
537+ return filePath ;
538+ }
539+
504540 async continueRequest ( response : DebugProtocol . ContinueResponse , args : DebugProtocol . ContinueArguments , request ?: DebugProtocol . Request ) : Promise < void > {
505541 try {
506542 await this . _mvm . eval ( "if system_dependent('IsDebugMode')==1, dbcont; end" ) ;
@@ -558,15 +594,18 @@ export default class MatlabDebugAdaptor {
558594 if ( stack [ 0 ] ?. mwtype !== undefined ) {
559595 stack = stack [ 0 ]
560596 const size = stack . mwsize [ 0 ] ;
561- const newStack = [ ] ;
597+ const transformedStack = [ ] ;
562598 for ( let i = 0 ; i < size ; i ++ ) {
563- newStack . push ( new debug . StackFrame ( size - i + 1 , stack . mwdata . name [ i ] , new debug . Source ( stack . mwdata . name [ i ] , stack . mwdata . file [ i ] ) , Math . abs ( stack . mwdata . line [ i ] ) , 1 ) )
599+ transformedStack . push ( { name : stack . mwdata . name [ i ] , file : stack . mwdata . file [ i ] , line : stack . mwdata . line [ i ] } ) ;
564600 }
565- return newStack ;
566- } else {
567- const numberOfStackFrames : number = stack . length ;
568- return stack . map ( ( stackFrame : MatlabData , i : number ) => new debug . StackFrame ( numberOfStackFrames - i + 1 , stackFrame . name , new debug . Source ( stackFrame . name as string , stackFrame . file as string ) , Math . abs ( stackFrame . line ) , 1 ) ) ;
601+ stack = transformedStack ;
569602 }
603+
604+ const numberOfStackFrames : number = stack . length ;
605+ return stack . map ( ( stackFrame : MatlabData , i : number ) => {
606+ const fileName : string = this . _mapToMFile ( stackFrame . file , true ) ;
607+ return new debug . StackFrame ( numberOfStackFrames - i + 1 , stackFrame . name , new debug . Source ( stackFrame . name as string , fileName ) , Math . abs ( stackFrame . line ) , 1 )
608+ } ) ;
570609 } ;
571610
572611 const stack = transformStack ( stackResponse . result ) ;
0 commit comments