11import * as vscode from 'vscode' ;
2+ import { GDBTargetDebugSession , GDBTargetDebugTracker } from '../debug-session' ;
23
34interface LiveWatchNode {
45 id : number ;
@@ -13,10 +14,12 @@ export class LiveWatchTreeDataProvider implements vscode.TreeDataProvider<LiveWa
1314
1415 private readonly _onDidChangeTreeData = new vscode . EventEmitter < LiveWatchNode | void > ( ) ;
1516 readonly onDidChangeTreeData : vscode . Event < LiveWatchNode | void > = this . _onDidChangeTreeData . event ;
16-
17+
1718 private roots : LiveWatchNode [ ] = [ ] ;
1819 private _context : vscode . ExtensionContext ;
19-
20+ private continueEvaluate : boolean = true ;
21+ public activeSession : GDBTargetDebugSession | undefined ;
22+
2023 constructor ( private readonly context : vscode . ExtensionContext ) {
2124 this . roots = this . context . globalState . get < LiveWatchNode [ ] > ( this . STORAGE_KEY ) ?? [ ] ;
2225 this . _context = context ;
@@ -39,55 +42,63 @@ export class LiveWatchTreeDataProvider implements vscode.TreeDataProvider<LiveWa
3942 getNumberOfNodes ( ) : number {
4043 return this . roots . length ;
4144 }
42-
43- activate ( ) {
45+
46+ activate ( tracker : GDBTargetDebugTracker ) : void {
47+ this . addVSCodeCommands ( ) ;
48+ const onDidChangeActiveDebugSession = tracker . onDidChangeActiveDebugSession ( ( session ) => {
49+ this . activeSession = session ;
50+ this . continueEvaluate = false ;
51+ } ) ;
52+ const onWillStartSession = tracker . onWillStartSession ( session => this . handleOnWillStartSession ( session ) ) ;
53+ this . _context . subscriptions . push ( onDidChangeActiveDebugSession ) ;
54+ this . _context . subscriptions . push ( onWillStartSession ) ;
55+ }
56+
57+ handleOnWillStartSession ( session : GDBTargetDebugSession ) : void {
58+ session . refreshTimer . onRefresh ( async ( refreshSession ) => {
59+ this . activeSession = refreshSession ;
60+ this . refresh ( )
61+ } ) ;
62+ }
63+ addVSCodeCommands ( ) {
4464 this . _context . subscriptions . push (
4565 vscode . window . registerTreeDataProvider ( 'cmsis-debugger-view' , this )
4666 ) ;
4767 this . _context . subscriptions . push (
4868 vscode . commands . registerCommand ( 'cmsis-debugger-view.add' , async ( ) => {
49- const expression = await vscode . window . showInputBox ( { prompt : 'Expression' } ) ;
50- if ( ! expression ) return ;
51- await this . add ( expression ) ;
52- } ) ,
53-
54- vscode . commands . registerCommand ( 'cmsis-debugger-view.clear' , async ( ) => {
55- const confirm = await vscode . window . showWarningMessage ( 'Clear all expressions?' , { modal : true } , 'Yes' ) ;
56- if ( confirm === 'Yes' ) await this . clear ( ) ;
57- } ) ,
58-
59- vscode . commands . registerCommand ( 'cmsis-debugger-view.delete' , async ( node ) => {
60- if ( ! node ) return ;
61- await this . delete ( node ) ;
62- } ) ,
63-
64- vscode . commands . registerCommand ( 'cmsis-debugger-view.refresh' , ( ) => this . refresh ( ) ) ,
65-
66- vscode . commands . registerCommand ( 'cmsis-debugger-view.rename' , async ( node ) => {
67- if ( ! node ) return ;
68- const expression = await vscode . window . showInputBox ( { prompt : 'Expression' , value : node . expression } ) ;
69- if ( ! expression ) return ;
70- await this . rename ( node , expression ) ;
71- } )
72- ) ;
73- }
69+ const expression = await vscode . window . showInputBox ( { prompt : 'Expression' } ) ;
70+ if ( ! expression ) return ;
71+ await this . add ( expression ) ;
72+ } ) ,
7473
74+ vscode . commands . registerCommand ( 'cmsis-debugger-view.clear' , async ( ) => {
75+ const confirm = await vscode . window . showWarningMessage ( 'Clear all expressions?' , { modal : true } , 'Yes' ) ;
76+ if ( confirm === 'Yes' ) await this . clear ( ) ;
77+ } ) ,
78+
79+ vscode . commands . registerCommand ( 'cmsis-debugger-view.delete' , async ( node ) => {
80+ if ( ! node ) return ;
81+ await this . delete ( node ) ;
82+ } ) ,
83+
84+ vscode . commands . registerCommand ( 'cmsis-debugger-view.refresh' , ( ) => this . refresh ( ) ) ,
85+
86+ vscode . commands . registerCommand ( 'cmsis-debugger-view.rename' , async ( node ) => {
87+ if ( ! node ) return ;
88+ const expression = await vscode . window . showInputBox ( { prompt : 'Expression' , value : node . expression } ) ;
89+ if ( ! expression ) return ;
90+ await this . rename ( node , expression ) ;
91+ } )
92+ ) ;
93+ }
7594 async evaluate ( expression : string ) : Promise < string > {
76- // get the active debug session
77- const session = vscode . debug . activeDebugSession ;
78- if ( ! session ) {
79- return '' ;
80- }
81- try {
82- // using the 'evaluate' request to get the value of the expression
83- const result = await session . customRequest ( 'evaluate' , { expression, context : 'watch' } ) ;
84- return result . result ;
85- } catch ( error ) {
86- // Handle errors gracefully by viewing the error message as the value
87- return error instanceof Error ? error . message : String ( error ) ;
95+ if ( ! this . activeSession ) {
96+ return 'No active session' ;
8897 }
98+ const result = await this . activeSession . evaluateGlobalExpression ( expression ) ;
99+ return result ;
89100 }
90-
101+
91102 async add ( expression : string , parent ?: LiveWatchNode ) {
92103 // Create a new node with a unique ID and evaluate its value
93104 const newNode : LiveWatchNode = {
@@ -126,8 +137,20 @@ export class LiveWatchTreeDataProvider implements vscode.TreeDataProvider<LiveWa
126137 this . refresh ( node ) ;
127138 }
128139
129- refresh ( node ?: LiveWatchNode ) {
130- this . _onDidChangeTreeData . fire ( node ) ;
140+ async refresh ( node ?: LiveWatchNode ) {
141+ if ( node ) {
142+ node . value = await this . evaluate ( node . expression ) ;
143+ this . _onDidChangeTreeData . fire ( node ) ;
144+ return ;
145+ }
146+ this . _onDidChangeTreeData . fire ( ) ;
147+ for ( const n of this . roots ) {
148+ if ( ! this . continueEvaluate ) {
149+ break ;
150+ }
151+ n . value = await this . evaluate ( n . expression ) ;
152+ }
153+ this . continueEvaluate = true ;
131154 }
132155
133156 private async save ( ) {
0 commit comments