Skip to content

Commit ecfef6e

Browse files
committed
Adding command Show in Memory Inspector to Live Watch context menu
1 parent d202f93 commit ecfef6e

File tree

4 files changed

+103
-9
lines changed

4 files changed

+103
-9
lines changed

__mocks__/vscode.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ module.exports = {
7373
})),
7474
registerTreeDataProvider: jest.fn(() => ({ dispose: jest.fn() })),
7575
showWarningMessage: jest.fn(),
76+
showErrorMessage: jest.fn(),
7677
createStatusBarItem: jest.fn(),
7778
showQuickPick: jest.fn(),
7879
},

package.json

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@
129129
"command": "vscode-cmsis-debugger.liveWatch.addToLiveWatchFromVariablesView",
130130
"title": "Add to Live Watch",
131131
"category": "Live Watch"
132+
},
133+
{
134+
"command": "vscode-cmsis-debugger.liveWatch.showInMemoryInspector",
135+
"title": "Show in Memory Inspector",
136+
"category": "Live Watch"
132137
}
133138
],
134139
"menus": {
@@ -151,6 +156,11 @@
151156
"command": "vscode-cmsis-debugger.liveWatch.addToLiveWatchFromWatchWindow",
152157
"when": "inDebugMode",
153158
"group": "z_commands"
159+
},
160+
{
161+
"command": "vscode-cmsis-debugger.liveWatch.showInMemoryInspector",
162+
"when": "inDebugMode",
163+
"group": "z_commands"
154164
}
155165
],
156166
"commandPalette": [
@@ -189,6 +199,18 @@
189199
{
190200
"command": "vscode-cmsis-debugger.liveWatch.addToLiveWatchFromTextEditor",
191201
"when": "false"
202+
},
203+
{
204+
"command": "vscode-cmsis-debugger.liveWatch.addToLiveWatchFromWatchWindow",
205+
"when": "false"
206+
},
207+
{
208+
"command": "vscode-cmsis-debugger.liveWatch.addToLiveWatchFromVariablesView",
209+
"when": "false"
210+
},
211+
{
212+
"command": "vscode-cmsis-debugger.liveWatch.showInMemoryInspector",
213+
"when": "false"
192214
}
193215
],
194216
"view/title": [
@@ -231,20 +253,25 @@
231253
"when": "view == cmsis-debugger.liveWatch && viewItem == expression",
232254
"group": "inline@2"
233255
},
256+
{
257+
"command": "vscode-cmsis-debugger.liveWatch.add",
258+
"when": "view == cmsis-debugger.liveWatch",
259+
"group": "contextMenuG1@1"
260+
},
234261
{
235262
"command": "vscode-cmsis-debugger.liveWatch.modify",
236263
"when": "view == cmsis-debugger.liveWatch && viewItem == expression",
237264
"group": "contextMenuG1@2"
238265
},
239266
{
240-
"command": "vscode-cmsis-debugger.liveWatch.delete",
267+
"command": "vscode-cmsis-debugger.liveWatch.copy",
241268
"when": "view == cmsis-debugger.liveWatch && viewItem == expression",
242-
"group": "contextMenuG1@4"
269+
"group": "contextMenuG1@3"
243270
},
244271
{
245-
"command": "vscode-cmsis-debugger.liveWatch.add",
246-
"when": "view == cmsis-debugger.liveWatch",
247-
"group": "contextMenuG1@1"
272+
"command": "vscode-cmsis-debugger.liveWatch.delete",
273+
"when": "view == cmsis-debugger.liveWatch && viewItem == expression",
274+
"group": "contextMenuG1@4"
248275
},
249276
{
250277
"command": "vscode-cmsis-debugger.liveWatch.deleteAll",
@@ -257,9 +284,10 @@
257284
"group": "contextMenuG2@2"
258285
},
259286
{
260-
"command": "vscode-cmsis-debugger.liveWatch.copy",
287+
"command": "vscode-cmsis-debugger.liveWatch.showInMemoryInspector",
261288
"when": "view == cmsis-debugger.liveWatch && viewItem == expression",
262-
"group": "contextMenuG1@3"
289+
"group": "contextMenuG3@1",
290+
"extensionDependency": "eclipse-cdt.memory-inspector"
263291
}
264292
]
265293
},

src/views/live-watch/live-watch.test.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ describe('LiveWatchTreeDataProvider', () => {
279279
'vscode-cmsis-debugger.liveWatch.copy',
280280
'vscode-cmsis-debugger.liveWatch.addToLiveWatchFromTextEditor',
281281
'vscode-cmsis-debugger.liveWatch.addToLiveWatchFromWatchWindow',
282-
'vscode-cmsis-debugger.liveWatch.addToLiveWatchFromVariablesView'
282+
'vscode-cmsis-debugger.liveWatch.addToLiveWatchFromVariablesView',
283+
'vscode-cmsis-debugger.liveWatch.showInMemoryInspector'
283284
]));
284285
});
285286

@@ -393,6 +394,42 @@ describe('LiveWatchTreeDataProvider', () => {
393394
await handler({ container: { name: 'local' } });
394395
expect((liveWatchTreeDataProvider as any).roots.length).toBe(0);
395396
});
397+
398+
it('showInMemoryInspector command does nothing when node is undefined', async () => {
399+
liveWatchTreeDataProvider.activate(tracker);
400+
const handler = getRegisteredHandler('vscode-cmsis-debugger.liveWatch.showInMemoryInspector');
401+
expect(handler).toBeDefined();
402+
await handler(undefined);
403+
expect(vscode.commands.executeCommand).not.toHaveBeenCalledWith('memory-inspector.show-variable', expect.anything());
404+
});
405+
406+
it('showInMemoryInspector shows error if extension is missing', async () => {
407+
(vscode.extensions.getExtension as jest.Mock).mockReturnValue(undefined);
408+
(vscode.window.showErrorMessage as jest.Mock).mockClear();
409+
liveWatchTreeDataProvider.activate(tracker);
410+
const handler = getRegisteredHandler('vscode-cmsis-debugger.liveWatch.showInMemoryInspector');
411+
const node = makeNode('node', { result: '0x1234', variablesReference: 77 }, 1);
412+
await handler(node);
413+
expect(vscode.window.showErrorMessage).toHaveBeenCalledWith(expect.stringContaining('Memory Inspector extension is not installed'));
414+
expect(vscode.commands.executeCommand).not.toHaveBeenCalledWith('memory-inspector.show-variable', expect.anything());
415+
});
416+
417+
it('showInMemoryInspector executes command with proper args when extension is present', async () => {
418+
(vscode.extensions.getExtension as jest.Mock).mockReturnValue({ id: 'eclipse-cdt.memory-inspector' });
419+
(vscode.commands.executeCommand as jest.Mock).mockResolvedValue('ok');
420+
liveWatchTreeDataProvider.activate(tracker);
421+
(liveWatchTreeDataProvider as any)._activeSession = { session: { id: 'session-1' } };
422+
const handler = getRegisteredHandler('vscode-cmsis-debugger.liveWatch.showInMemoryInspector');
423+
const node = makeNode('node', { result: '0x1234', variablesReference: 0 }, 1);
424+
await handler(node);
425+
const lastCall = (vscode.commands.executeCommand as jest.Mock).mock.calls.pop();
426+
expect(lastCall[0]).toBe('memory-inspector.show-variable');
427+
const args = lastCall[1];
428+
expect(args.sessionId).toBe('session-1');
429+
expect(args.container.name).toBe('node');
430+
expect(args.variable.name).toBe('node');
431+
expect(args.variable.memoryReference).toBe('&(node)');
432+
});
396433
});
397434

398435
describe('evaluate', () => {

src/views/live-watch/live-watch.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,10 @@ export class LiveWatchTreeDataProvider implements vscode.TreeDataProvider<LiveWa
149149
*/
150150
const addToLiveWatchFromWatchWindowCommand = vscode.commands.registerCommand('vscode-cmsis-debugger.liveWatch.addToLiveWatchFromWatchWindow', async (payload: { container: DebugProtocol.Scope; variable: DebugProtocol.Variable; }) => { await this.registerAddToLiveWatchFromVariablesView(payload); });
151151
const addToLiveWatchFromVariablesViewCommand = vscode.commands.registerCommand('vscode-cmsis-debugger.liveWatch.addToLiveWatchFromVariablesView', async (payload: { container: DebugProtocol.Scope; variable: DebugProtocol.Variable; }) => { await this.registerAddToLiveWatchFromVariablesView(payload); });
152+
const showInMemoryInspectorCommand = vscode.commands.registerCommand('vscode-cmsis-debugger.liveWatch.showInMemoryInspector', async (node: LiveWatchNode) => { await this.showInMemoryInspector(node); });
152153
this._context.subscriptions.push(registerLiveWatchView,
153154
addCommand,
154-
deleteAllCommand, deleteCommand, refreshCommand, modifyCommand, copyCommand, addToLiveWatchCommand, addToLiveWatchFromWatchWindowCommand, addToLiveWatchFromVariablesViewCommand);
155+
deleteAllCommand, deleteCommand, refreshCommand, modifyCommand, copyCommand, addToLiveWatchCommand, addToLiveWatchFromWatchWindowCommand, addToLiveWatchFromVariablesViewCommand, showInMemoryInspectorCommand);
155156
}
156157

157158
private async registerAddCommand() {
@@ -214,6 +215,33 @@ export class LiveWatchTreeDataProvider implements vscode.TreeDataProvider<LiveWa
214215
await this.addToRoots(payload.variable.name);
215216
}
216217

218+
private async showInMemoryInspector(node: LiveWatchNode) {
219+
if (!node) {
220+
return;
221+
}
222+
const extensionId = 'eclipse-cdt.memory-inspector';
223+
const memoryInspectorExtension = vscode.extensions.getExtension(extensionId);
224+
if (!memoryInspectorExtension) {
225+
vscode.window.showErrorMessage(`Memory Inspector extension is not installed. Please install it to use this feature.`);
226+
return;
227+
}
228+
const args = {
229+
sessionId: this._activeSession?.session.id,
230+
container: {
231+
name: node.expression,
232+
variablesReference: node.value.variablesReference
233+
},
234+
variable: {
235+
name: node.expression,
236+
value: node.value.result,
237+
variablesReference: node.value.variablesReference,
238+
memoryReference: `&(${node.expression})`
239+
}
240+
};
241+
const result = await vscode.commands.executeCommand('memory-inspector.show-variable', args);
242+
console.log(result);
243+
}
244+
217245
private async evaluate(expression: string): Promise<LiveWatchValue> {
218246
const response: LiveWatchValue = { result: '', variablesReference: 0 };
219247
if (!this._activeSession) {

0 commit comments

Comments
 (0)