-
Notifications
You must be signed in to change notification settings - Fork 30k
/
watchExpressionsView.ts
353 lines (302 loc) · 14.3 KB
/
watchExpressionsView.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as nls from 'vs/nls';
import { RunOnceScheduler } from 'vs/base/common/async';
import * as dom from 'vs/base/browser/dom';
import { CollapseAction } from 'vs/workbench/browser/viewlet';
import { IViewletViewOptions } from 'vs/workbench/browser/parts/views/viewsViewlet';
import { IDebugService, IExpression, CONTEXT_WATCH_EXPRESSIONS_FOCUSED } from 'vs/workbench/contrib/debug/common/debug';
import { Expression, Variable } from 'vs/workbench/contrib/debug/common/debugModel';
import { AddWatchExpressionAction, RemoveAllWatchExpressionsAction, CopyValueAction } from 'vs/workbench/contrib/debug/browser/debugActions';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IAction, Action } from 'vs/base/common/actions';
import { Separator } from 'vs/base/browser/ui/actionbar/actionbar';
import { renderExpressionValue, renderViewTree, IInputBoxOptions, AbstractExpressionsRenderer, IExpressionTemplateData } from 'vs/workbench/contrib/debug/browser/baseDebugView';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { ViewPane } from 'vs/workbench/browser/parts/views/viewPaneContainer';
import { IListVirtualDelegate } from 'vs/base/browser/ui/list/list';
import { IListAccessibilityProvider } from 'vs/base/browser/ui/list/listWidget';
import { WorkbenchAsyncDataTree } from 'vs/platform/list/browser/listService';
import { IAsyncDataSource, ITreeMouseEvent, ITreeContextMenuEvent, ITreeDragAndDrop, ITreeDragOverReaction } from 'vs/base/browser/ui/tree/tree';
import { IDragAndDropData } from 'vs/base/browser/dnd';
import { ElementsDragAndDropData } from 'vs/base/browser/ui/list/listView';
import { FuzzyScore } from 'vs/base/common/filters';
import { IHighlight } from 'vs/base/browser/ui/highlightedlabel/highlightedLabel';
import { variableSetEmitter, VariablesRenderer } from 'vs/workbench/contrib/debug/browser/variablesView';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { dispose } from 'vs/base/common/lifecycle';
import { IViewDescriptorService } from 'vs/workbench/common/views';
import { IOpenerService } from 'vs/platform/opener/common/opener';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
const MAX_VALUE_RENDER_LENGTH_IN_VIEWLET = 1024;
let ignoreVariableSetEmitter = false;
let useCachedEvaluation = false;
export class WatchExpressionsView extends ViewPane {
private onWatchExpressionsUpdatedScheduler: RunOnceScheduler;
private needsRefresh = false;
private tree!: WorkbenchAsyncDataTree<IDebugService | IExpression, IExpression, FuzzyScore>;
constructor(
options: IViewletViewOptions,
@IContextMenuService contextMenuService: IContextMenuService,
@IDebugService private readonly debugService: IDebugService,
@IKeybindingService keybindingService: IKeybindingService,
@IInstantiationService instantiationService: IInstantiationService,
@IViewDescriptorService viewDescriptorService: IViewDescriptorService,
@IConfigurationService configurationService: IConfigurationService,
@IContextKeyService contextKeyService: IContextKeyService,
@IOpenerService openerService: IOpenerService,
@IThemeService themeService: IThemeService,
@ITelemetryService telemetryService: ITelemetryService,
) {
super(options, keybindingService, contextMenuService, configurationService, contextKeyService, viewDescriptorService, instantiationService, openerService, themeService, telemetryService);
this.onWatchExpressionsUpdatedScheduler = new RunOnceScheduler(() => {
this.needsRefresh = false;
this.tree.updateChildren();
}, 50);
}
renderBody(container: HTMLElement): void {
super.renderBody(container);
dom.addClass(this.element, 'debug-pane');
dom.addClass(container, 'debug-watch');
const treeContainer = renderViewTree(container);
const expressionsRenderer = this.instantiationService.createInstance(WatchExpressionsRenderer);
this.tree = <WorkbenchAsyncDataTree<IDebugService | IExpression, IExpression, FuzzyScore>>this.instantiationService.createInstance(WorkbenchAsyncDataTree, 'WatchExpressions', treeContainer, new WatchExpressionsDelegate(), [expressionsRenderer, this.instantiationService.createInstance(VariablesRenderer)],
new WatchExpressionsDataSource(), {
accessibilityProvider: new WatchExpressionsAccessibilityProvider(),
identityProvider: { getId: (element: IExpression) => element.getId() },
keyboardNavigationLabelProvider: {
getKeyboardNavigationLabel: (e: IExpression) => {
if (e === this.debugService.getViewModel().getSelectedExpression()) {
// Don't filter input box
return undefined;
}
return e;
}
},
dnd: new WatchExpressionsDragAndDrop(this.debugService),
overrideStyles: {
listBackground: this.getBackgroundColor()
}
});
this.tree.setInput(this.debugService);
CONTEXT_WATCH_EXPRESSIONS_FOCUSED.bindTo(this.tree.contextKeyService);
this._register(this.tree.onContextMenu(e => this.onContextMenu(e)));
this._register(this.tree.onMouseDblClick(e => this.onMouseDblClick(e)));
this._register(this.debugService.getModel().onDidChangeWatchExpressions(async we => {
if (!this.isBodyVisible()) {
this.needsRefresh = true;
} else {
if (we && !we.name) {
// We are adding a new input box, no need to re-evaluate watch expressions
useCachedEvaluation = true;
}
await this.tree.updateChildren();
useCachedEvaluation = false;
if (we instanceof Expression) {
this.tree.reveal(we);
}
}
}));
this._register(this.debugService.getViewModel().onDidFocusStackFrame(() => {
if (!this.isBodyVisible()) {
this.needsRefresh = true;
return;
}
if (!this.onWatchExpressionsUpdatedScheduler.isScheduled()) {
this.onWatchExpressionsUpdatedScheduler.schedule();
}
}));
this._register(variableSetEmitter.event(() => {
if (!ignoreVariableSetEmitter) {
this.tree.updateChildren();
}
}));
this._register(this.onDidChangeBodyVisibility(visible => {
if (visible && this.needsRefresh) {
this.onWatchExpressionsUpdatedScheduler.schedule();
}
}));
this._register(this.debugService.getViewModel().onDidSelectExpression(e => {
if (e instanceof Expression && e.name) {
this.tree.rerender(e);
}
}));
}
layoutBody(height: number, width: number): void {
super.layoutBody(height, width);
this.tree.layout(height, width);
}
focus(): void {
this.tree.domFocus();
}
getActions(): IAction[] {
return [
new AddWatchExpressionAction(AddWatchExpressionAction.ID, AddWatchExpressionAction.LABEL, this.debugService, this.keybindingService),
new CollapseAction(() => this.tree, true, 'explorer-action codicon-collapse-all'),
new RemoveAllWatchExpressionsAction(RemoveAllWatchExpressionsAction.ID, RemoveAllWatchExpressionsAction.LABEL, this.debugService, this.keybindingService)
];
}
private onMouseDblClick(e: ITreeMouseEvent<IExpression>): void {
if ((e.browserEvent.target as HTMLElement).className.indexOf('twistie') >= 0) {
// Ignore double click events on twistie
return;
}
const element = e.element;
// double click on primitive value: open input box to be able to select and copy value.
if (element instanceof Expression && element !== this.debugService.getViewModel().getSelectedExpression()) {
this.debugService.getViewModel().setSelectedExpression(element);
} else if (!element) {
// Double click in watch panel triggers to add a new watch expression
this.debugService.addWatchExpression();
}
}
private onContextMenu(e: ITreeContextMenuEvent<IExpression>): void {
const element = e.element;
const anchor = e.anchor;
if (!anchor) {
return;
}
const actions: IAction[] = [];
if (element instanceof Expression) {
const expression = <Expression>element;
actions.push(new AddWatchExpressionAction(AddWatchExpressionAction.ID, AddWatchExpressionAction.LABEL, this.debugService, this.keybindingService));
actions.push(new Action('debug.editWatchExpression', nls.localize('editWatchExpression', "Edit Expression"), undefined, true, () => {
this.debugService.getViewModel().setSelectedExpression(expression);
return Promise.resolve();
}));
actions.push(this.instantiationService.createInstance(CopyValueAction, CopyValueAction.ID, CopyValueAction.LABEL, expression, 'watch'));
actions.push(new Separator());
actions.push(new Action('debug.removeWatchExpression', nls.localize('removeWatchExpression', "Remove Expression"), undefined, true, () => {
this.debugService.removeWatchExpressions(expression.getId());
return Promise.resolve();
}));
actions.push(new RemoveAllWatchExpressionsAction(RemoveAllWatchExpressionsAction.ID, RemoveAllWatchExpressionsAction.LABEL, this.debugService, this.keybindingService));
} else {
actions.push(new AddWatchExpressionAction(AddWatchExpressionAction.ID, AddWatchExpressionAction.LABEL, this.debugService, this.keybindingService));
if (element instanceof Variable) {
const variable = element as Variable;
actions.push(this.instantiationService.createInstance(CopyValueAction, CopyValueAction.ID, CopyValueAction.LABEL, variable, 'watch'));
actions.push(new Separator());
}
actions.push(new RemoveAllWatchExpressionsAction(RemoveAllWatchExpressionsAction.ID, RemoveAllWatchExpressionsAction.LABEL, this.debugService, this.keybindingService));
}
this.contextMenuService.showContextMenu({
getAnchor: () => anchor,
getActions: () => actions,
getActionsContext: () => element,
onHide: () => dispose(actions)
});
}
}
class WatchExpressionsDelegate implements IListVirtualDelegate<IExpression> {
getHeight(_element: IExpression): number {
return 22;
}
getTemplateId(element: IExpression): string {
if (element instanceof Expression) {
return WatchExpressionsRenderer.ID;
}
// Variable
return VariablesRenderer.ID;
}
}
function isDebugService(element: any): element is IDebugService {
return typeof element.getConfigurationManager === 'function';
}
class WatchExpressionsDataSource implements IAsyncDataSource<IDebugService, IExpression> {
hasChildren(element: IExpression | IDebugService): boolean {
return isDebugService(element) || element.hasChildren;
}
getChildren(element: IDebugService | IExpression): Promise<Array<IExpression>> {
if (isDebugService(element)) {
const debugService = element as IDebugService;
const watchExpressions = debugService.getModel().getWatchExpressions();
const viewModel = debugService.getViewModel();
return Promise.all(watchExpressions.map(we => !!we.name && !useCachedEvaluation
? we.evaluate(viewModel.focusedSession!, viewModel.focusedStackFrame!, 'watch').then(() => we)
: Promise.resolve(we)));
}
return element.getChildren();
}
}
export class WatchExpressionsRenderer extends AbstractExpressionsRenderer {
static readonly ID = 'watchexpression';
get templateId() {
return WatchExpressionsRenderer.ID;
}
protected renderExpression(expression: IExpression, data: IExpressionTemplateData, highlights: IHighlight[]): void {
const text = typeof expression.value === 'string' ? `${expression.name}:` : expression.name;
data.label.set(text, highlights, expression.type ? expression.type : expression.value);
renderExpressionValue(expression, data.value, {
showChanged: true,
maxValueLength: MAX_VALUE_RENDER_LENGTH_IN_VIEWLET,
showHover: true,
colorize: true
});
}
protected getInputBoxOptions(expression: IExpression): IInputBoxOptions {
return {
initialValue: expression.name ? expression.name : '',
ariaLabel: nls.localize('watchExpressionInputAriaLabel', "Type watch expression"),
placeholder: nls.localize('watchExpressionPlaceholder', "Expression to watch"),
onFinish: (value: string, success: boolean) => {
if (success && value) {
this.debugService.renameWatchExpression(expression.getId(), value);
ignoreVariableSetEmitter = true;
variableSetEmitter.fire();
ignoreVariableSetEmitter = false;
} else if (!expression.name) {
this.debugService.removeWatchExpressions(expression.getId());
}
}
};
}
}
class WatchExpressionsAccessibilityProvider implements IListAccessibilityProvider<IExpression> {
getWidgetAriaLabel(): string {
return nls.localize({ comment: ['Debug is a noun in this context, not a verb.'], key: 'watchAriaTreeLabel' }, "Debug Watch Expressions");
}
getAriaLabel(element: IExpression): string {
if (element instanceof Expression) {
return nls.localize('watchExpressionAriaLabel', "{0}, value {1}", (<Expression>element).name, (<Expression>element).value);
}
// Variable
return nls.localize('watchVariableAriaLabel', "{0}, value {1}", (<Variable>element).name, (<Variable>element).value);
}
}
class WatchExpressionsDragAndDrop implements ITreeDragAndDrop<IExpression> {
constructor(private debugService: IDebugService) { }
onDragOver(data: IDragAndDropData): boolean | ITreeDragOverReaction {
if (!(data instanceof ElementsDragAndDropData)) {
return false;
}
const expressions = (data as ElementsDragAndDropData<IExpression>).elements;
return expressions.length > 0 && expressions[0] instanceof Expression;
}
getDragURI(element: IExpression): string | null {
if (!(element instanceof Expression) || element === this.debugService.getViewModel().getSelectedExpression()) {
return null;
}
return element.getId();
}
getDragLabel(elements: IExpression[]): string | undefined {
if (elements.length === 1) {
return elements[0].name;
}
return undefined;
}
drop(data: IDragAndDropData, targetElement: IExpression): void {
if (!(data instanceof ElementsDragAndDropData)) {
return;
}
const draggedElement = (data as ElementsDragAndDropData<IExpression>).elements[0];
const watches = this.debugService.getModel().getWatchExpressions();
const position = targetElement instanceof Expression ? watches.indexOf(targetElement) : watches.length - 1;
this.debugService.moveWatchExpression(draggedElement.getId(), position);
}
}