@@ -5,6 +5,7 @@ import { DebugProtocol } from '@vscode/debugprotocol';
5
5
import { DebugServices , BreakpointInfo } from './DebugServices'
6
6
import { ResolvablePromise , createResolvablePromise } from '../utils/PromiseUtils'
7
7
import { IMVM , MVMError , MatlabState } from '../mvm/impl/MVM' ;
8
+ import fs from 'node:fs' ;
8
9
9
10
enum BreakpointChangeType {
10
11
ADD ,
@@ -233,6 +234,8 @@ export default class MatlabDebugAdaptor {
233
234
234
235
private _setupListeners ( ) : void {
235
236
this . _debugServices . on ( DebugServices . Events . BreakpointAdded , async ( breakpoint : BreakpointInfo ) => {
237
+ breakpoint . filePath = this . _mapToMFile ( breakpoint . filePath , false ) ;
238
+
236
239
this . _matlabBreakpoints . push ( breakpoint ) ;
237
240
238
241
this . _breakpointChangeListeners . forEach ( ( listener ) => {
@@ -241,6 +244,8 @@ export default class MatlabDebugAdaptor {
241
244
} ) ;
242
245
243
246
this . _debugServices . on ( DebugServices . Events . BreakpointRemoved , async ( breakpoint : BreakpointInfo ) => {
247
+ breakpoint . filePath = this . _mapToMFile ( breakpoint . filePath , false ) ;
248
+
244
249
this . _matlabBreakpoints = this . _matlabBreakpoints . filter ( ( existingBreakpoint ) => {
245
250
return ! existingBreakpoint . equals ( breakpoint , true ) ;
246
251
} ) ;
@@ -422,6 +427,7 @@ export default class MatlabDebugAdaptor {
422
427
}
423
428
424
429
const canonicalizedPath = await this . _getCanonicalPath ( source . path ) ;
430
+ const pathToSetOrClear = this . _mapToPFile ( canonicalizedPath , true ) ;
425
431
426
432
const newBreakpoints : BreakpointInfo [ ] = ( args . breakpoints != null )
427
433
? args . breakpoints . map ( ( breakpoint ) => {
@@ -458,7 +464,7 @@ export default class MatlabDebugAdaptor {
458
464
// Remove all breakpoints that are now gone.
459
465
const breakpointsRemovalPromises : Array < Promise < void > > = [ ] ;
460
466
breakpointsToRemove . forEach ( ( breakpoint : BreakpointInfo ) => {
461
- breakpointsRemovalPromises . push ( this . _mvm . clearBreakpoint ( breakpoint . filePath , breakpoint . lineNumber ) ) ;
467
+ breakpointsRemovalPromises . push ( this . _mvm . clearBreakpoint ( pathToSetOrClear , breakpoint . lineNumber ) ) ;
462
468
} )
463
469
await Promise . all ( breakpointsRemovalPromises ) ;
464
470
@@ -476,12 +482,12 @@ export default class MatlabDebugAdaptor {
476
482
477
483
let matlabBreakpointInfos : BreakpointInfo [ ] = [ ] ;
478
484
const listener = this . _registerBreakpointChangeListener ( ( changeType , bpInfo ) => {
479
- if ( changeType === BreakpointChangeType . ADD && bpInfo . filePath === canonicalizedPath ) {
485
+ if ( changeType === BreakpointChangeType . ADD && bpInfo . filePath === pathToSetOrClear ) {
480
486
matlabBreakpointInfos . push ( bpInfo ) ;
481
487
}
482
488
} ) ;
483
489
484
- await this . _mvm . setBreakpoint ( canonicalizedPath , newBreakpoint . info . lineNumber , newBreakpoint . info . condition ) ;
490
+ await this . _mvm . setBreakpoint ( pathToSetOrClear , newBreakpoint . info . lineNumber , newBreakpoint . info . condition ) ;
485
491
486
492
listener . remove ( ) ;
487
493
@@ -501,6 +507,36 @@ export default class MatlabDebugAdaptor {
501
507
this . _clearPendingBreakpointsRequest ( ) ;
502
508
}
503
509
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
+
504
540
async continueRequest ( response : DebugProtocol . ContinueResponse , args : DebugProtocol . ContinueArguments , request ?: DebugProtocol . Request ) : Promise < void > {
505
541
try {
506
542
await this . _mvm . eval ( "if system_dependent('IsDebugMode')==1, dbcont; end" ) ;
@@ -558,15 +594,18 @@ export default class MatlabDebugAdaptor {
558
594
if ( stack [ 0 ] ?. mwtype !== undefined ) {
559
595
stack = stack [ 0 ]
560
596
const size = stack . mwsize [ 0 ] ;
561
- const newStack = [ ] ;
597
+ const transformedStack = [ ] ;
562
598
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 ] } ) ;
564
600
}
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 ;
569
602
}
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
+ } ) ;
570
609
} ;
571
610
572
611
const stack = transformStack ( stackResponse . result ) ;
0 commit comments