Skip to content

Commit 55c55a7

Browse files
committed
updating expression evaluation periodically
1 parent d396171 commit 55c55a7

File tree

3 files changed

+70
-45
lines changed

3 files changed

+70
-45
lines changed

src/desktop/extension.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export const activate = async (context: vscode.ExtensionContext): Promise<void>
3737
const cpuStatesStatusBarItem = new CpuStatesStatusBarItem();
3838
// Register the Tree View under the id from package.json
3939
const liveWatchTreeDataProvider = new LiveWatchTreeDataProvider(context);
40-
liveWatchTreeDataProvider.activate();
4140

4241
addToolsToPath(context, BUILTIN_TOOLS_PATHS);
4342
// Activate components
@@ -47,6 +46,8 @@ export const activate = async (context: vscode.ExtensionContext): Promise<void>
4746
cpuStates.activate(gdbtargetDebugTracker);
4847
cpuStatesCommands.activate(context, cpuStates);
4948
cpuStatesStatusBarItem.activate(context, cpuStates);
49+
// Live Watch view
50+
liveWatchTreeDataProvider.activate(gdbtargetDebugTracker);
5051

5152
logger.debug('Extension Pack activated');
5253
};

src/features/cpu-states/cpu-states.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ interface SessionCpuStates {
4444
}
4545

4646
export class CpuStates {
47+
// onRefresh event to notify GUI components of the cpu states updates (This is different than that of the periodic refresh timer)
4748
private readonly _onRefresh: vscode.EventEmitter<number> = new vscode.EventEmitter<number>();
4849
public readonly onRefresh: vscode.Event<number> = this._onRefresh.event;
4950

src/live-watch/live-watch.ts

Lines changed: 67 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as vscode from 'vscode';
2+
import { GDBTargetDebugSession, GDBTargetDebugTracker } from '../debug-session';
23

34
interface 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

Comments
 (0)