Skip to content

Commit 7ca3aa7

Browse files
committed
Adds an expression from source file to Live Watch
1 parent 8f9919c commit 7ca3aa7

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,21 @@
114114
"command": "vscode-cmsis-debugger.liveWatch.copy",
115115
"title": "Copy Expression",
116116
"category": "Live Watch"
117+
},
118+
{
119+
"command": "vscode-cmsis-debugger.liveWatch.addToLiveWatch",
120+
"title": "Add To Live Watch",
121+
"category": "Live Watch"
117122
}
118123
],
119124
"menus": {
125+
"editor/context": [
126+
{
127+
"command": "vscode-cmsis-debugger.liveWatch.addToLiveWatch",
128+
"when": "editorTextFocus && editorHasSelection && resourceScheme == file",
129+
"group": "5_cutcopypaste@1"
130+
}
131+
],
120132
"commandPalette": [
121133
{
122134
"command": "vscode-cmsis-debugger.showCpuTimeHistory",
@@ -149,6 +161,10 @@
149161
{
150162
"command": "vscode-cmsis-debugger.liveWatch.copy",
151163
"when": "false"
164+
},
165+
{
166+
"command": "vscode-cmsis-debugger.liveWatch.addToLiveWatch",
167+
"when": "false"
152168
}
153169
],
154170
"view/title": [

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,20 @@ describe('LiveWatchTreeDataProvider', () => {
199199
await (liveWatchTreeDataProvider as any).registerCopyCommand(node);
200200
expect(vscode.env.clipboard.writeText).toHaveBeenCalledWith('node-to-copy');
201201
});
202+
203+
it('AddFromSelection adds selected text as new live watch expression to roots', async () => {
204+
(vscode.window as any).activeTextEditor = {
205+
document: {
206+
getText: jest.fn().mockReturnValue('selected-expression')
207+
},
208+
selection: {} // dummy selection
209+
};
210+
jest.spyOn(liveWatchTreeDataProvider as any, 'evaluate').mockResolvedValue({ result: '5678', variablesReference: 0 });
211+
await (liveWatchTreeDataProvider as any).registerAddFromSelectionCommand();
212+
expect((liveWatchTreeDataProvider as any).roots.length).toBe(1);
213+
expect((liveWatchTreeDataProvider as any).roots[0].expression).toBe('selected-expression');
214+
expect((liveWatchTreeDataProvider as any).roots[0].value.result).toBe('5678');
215+
});
202216
});
203217

204218
describe('refresh', () => {
@@ -255,7 +269,8 @@ describe('LiveWatchTreeDataProvider', () => {
255269
'vscode-cmsis-debugger.liveWatch.delete',
256270
'vscode-cmsis-debugger.liveWatch.refresh',
257271
'vscode-cmsis-debugger.liveWatch.modify',
258-
'vscode-cmsis-debugger.liveWatch.copy'
272+
'vscode-cmsis-debugger.liveWatch.copy',
273+
'vscode-cmsis-debugger.liveWatch.addToLiveWatch'
259274
]));
260275
});
261276

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,10 @@ export class LiveWatchTreeDataProvider implements vscode.TreeDataProvider<LiveWa
139139
const refreshCommand = vscode.commands.registerCommand('vscode-cmsis-debugger.liveWatch.refresh', async () => await this.refresh());
140140
const modifyCommand = vscode.commands.registerCommand('vscode-cmsis-debugger.liveWatch.modify', async (node) => await this.registerRenameCommand(node));
141141
const copyCommand = vscode.commands.registerCommand('vscode-cmsis-debugger.liveWatch.copy', async (node) => { await this.registerCopyCommand(node); });
142+
const addToLiveWatchCommand = vscode.commands.registerCommand('vscode-cmsis-debugger.liveWatch.addToLiveWatch', async () => { await this.registerAddFromSelectionCommand(); });
142143
this._context.subscriptions.push(registerLiveWatchView,
143144
addCommand,
144-
deleteAllCommand, deleteCommand, refreshCommand, modifyCommand, copyCommand);
145+
deleteAllCommand, deleteCommand, refreshCommand, modifyCommand, copyCommand, addToLiveWatchCommand);
145146
}
146147

147148
private async registerAddCommand() {
@@ -181,6 +182,19 @@ export class LiveWatchTreeDataProvider implements vscode.TreeDataProvider<LiveWa
181182
await vscode.env.clipboard.writeText(node.expression);
182183
}
183184

185+
private async registerAddFromSelectionCommand() {
186+
const editor = vscode.window.activeTextEditor;
187+
if (!editor) {
188+
return;
189+
}
190+
const selection = editor.selection;
191+
const selectedText = editor.document.getText(selection).trim();
192+
if (!selectedText) {
193+
return;
194+
}
195+
await this.addToRoots(selectedText);
196+
}
197+
184198
private async evaluate(expression: string): Promise<LiveWatchValue> {
185199
const response: LiveWatchValue = { result: '', variablesReference: 0 };
186200
if (!this._activeSession) {

0 commit comments

Comments
 (0)