watchExpressionsView.ts 14.7 KB
Newer Older
I
isidor 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  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';
B
Benjamin Pasero 已提交
9
import { CollapseAction } from 'vs/workbench/browser/viewlet';
10
import { IViewletViewOptions } from 'vs/workbench/browser/parts/views/viewsViewlet';
11 12
import { IDebugService, IExpression, CONTEXT_WATCH_EXPRESSIONS_FOCUSED } from 'vs/workbench/contrib/debug/common/debug';
import { Expression, Variable } from 'vs/workbench/contrib/debug/common/debugModel';
I
isidor 已提交
13
import { AddWatchExpressionAction, RemoveAllWatchExpressionsAction, CopyValueAction } from 'vs/workbench/contrib/debug/browser/debugActions';
I
isidor 已提交
14
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
I
isidor 已提交
15 16
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
I
isidor 已提交
17
import { IAction, Action } from 'vs/base/common/actions';
18
import { Separator } from 'vs/base/browser/ui/actionbar/actionbar';
19
import { renderExpressionValue, renderViewTree, IInputBoxOptions, AbstractExpressionsRenderer, IExpressionTemplateData } from 'vs/workbench/contrib/debug/browser/baseDebugView';
20
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
S
SteVen Batten 已提交
21
import { IViewPaneOptions, ViewPane } from 'vs/workbench/browser/parts/views/viewPaneContainer';
22 23
import { IListVirtualDelegate } from 'vs/base/browser/ui/list/list';
import { IAccessibilityProvider } from 'vs/base/browser/ui/list/listWidget';
24
import { WorkbenchAsyncDataTree } from 'vs/platform/list/browser/listService';
B
Benjamin Pasero 已提交
25
import { IAsyncDataSource, ITreeMouseEvent, ITreeContextMenuEvent, ITreeDragAndDrop, ITreeDragOverReaction } from 'vs/base/browser/ui/tree/tree';
I
isidor 已提交
26
import { IDragAndDropData } from 'vs/base/browser/dnd';
J
Joao Moreno 已提交
27
import { ElementsDragAndDropData } from 'vs/base/browser/ui/list/listView';
I
isidor 已提交
28 29
import { FuzzyScore } from 'vs/base/common/filters';
import { IHighlight } from 'vs/base/browser/ui/highlightedlabel/highlightedLabel';
I
isidor 已提交
30
import { variableSetEmitter, VariablesRenderer } from 'vs/workbench/contrib/debug/browser/variablesView';
S
Sandeep Somavarapu 已提交
31
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
32
import { dispose } from 'vs/base/common/lifecycle';
33
import { IViewDescriptorService } from 'vs/workbench/common/views';
34 35
import { IOpenerService } from 'vs/platform/opener/common/opener';
import { IThemeService } from 'vs/platform/theme/common/themeService';
I
isidor 已提交
36

37
const MAX_VALUE_RENDER_LENGTH_IN_VIEWLET = 1024;
I
isidor 已提交
38 39
let ignoreVariableSetEmitter = false;
let useCachedEvaluation = false;
I
isidor 已提交
40

S
SteVen Batten 已提交
41
export class WatchExpressionsView extends ViewPane {
I
isidor 已提交
42 43

	private onWatchExpressionsUpdatedScheduler: RunOnceScheduler;
I
isidor 已提交
44 45
	private needsRefresh = false;
	private tree!: WorkbenchAsyncDataTree<IDebugService | IExpression, IExpression, FuzzyScore>;
I
isidor 已提交
46 47 48 49

	constructor(
		options: IViewletViewOptions,
		@IContextMenuService contextMenuService: IContextMenuService,
50
		@IDebugService private readonly debugService: IDebugService,
I
isidor 已提交
51
		@IKeybindingService keybindingService: IKeybindingService,
52
		@IInstantiationService instantiationService: IInstantiationService,
53
		@IViewDescriptorService viewDescriptorService: IViewDescriptorService,
I
isidor 已提交
54
		@IConfigurationService configurationService: IConfigurationService,
S
Sandeep Somavarapu 已提交
55
		@IContextKeyService contextKeyService: IContextKeyService,
56 57
		@IOpenerService openerService: IOpenerService,
		@IThemeService themeService: IThemeService,
I
isidor 已提交
58
	) {
59
		super({ ...(options as IViewPaneOptions), ariaHeaderLabel: nls.localize('watchExpressionsSection', "Watch Expressions Section") }, keybindingService, contextMenuService, configurationService, contextKeyService, viewDescriptorService, instantiationService, openerService, themeService);
I
isidor 已提交
60 61

		this.onWatchExpressionsUpdatedScheduler = new RunOnceScheduler(() => {
62
			this.needsRefresh = false;
63
			this.tree.updateChildren();
I
isidor 已提交
64 65 66
		}, 50);
	}

I
isidor 已提交
67
	renderBody(container: HTMLElement): void {
J
Joao Moreno 已提交
68 69
		super.renderBody(container);

I
isidor 已提交
70
		dom.addClass(container, 'debug-watch');
71 72
		const treeContainer = renderViewTree(container);

I
isidor 已提交
73
		const expressionsRenderer = this.instantiationService.createInstance(WatchExpressionsRenderer);
74
		this.tree = <WorkbenchAsyncDataTree<IDebugService | IExpression, IExpression, FuzzyScore>>this.instantiationService.createInstance(WorkbenchAsyncDataTree, 'WatchExpressions', treeContainer, new WatchExpressionsDelegate(), [expressionsRenderer, this.instantiationService.createInstance(VariablesRenderer)],
J
Joao Moreno 已提交
75
			new WatchExpressionsDataSource(), {
M
Matt Bierner 已提交
76 77 78
			ariaLabel: nls.localize({ comment: ['Debug is a noun in this context, not a verb.'], key: 'watchAriaTreeLabel' }, "Debug Watch Expressions"),
			accessibilityProvider: new WatchExpressionsAccessibilityProvider(),
			identityProvider: { getId: (element: IExpression) => element.getId() },
J
jeanp413 已提交
79 80 81 82 83 84 85 86 87 88
			keyboardNavigationLabelProvider: {
				getKeyboardNavigationLabel: (e: IExpression) => {
					if (e === this.debugService.getViewModel().getSelectedExpression()) {
						// Don't filter input box
						return undefined;
					}

					return e;
				}
			},
M
Matt Bierner 已提交
89
			dnd: new WatchExpressionsDragAndDrop(this.debugService),
90
			overrideStyles: {
S
SteVen Batten 已提交
91
				listBackground: this.getBackgroundColor()
92
			}
M
Matt Bierner 已提交
93
		});
I
isidor 已提交
94

I
isidor 已提交
95
		this.tree.setInput(this.debugService);
I
isidor 已提交
96
		CONTEXT_WATCH_EXPRESSIONS_FOCUSED.bindTo(this.tree.contextKeyService);
I
isidor 已提交
97

98 99
		if (this.toolbar) {
			const addWatchExpressionAction = new AddWatchExpressionAction(AddWatchExpressionAction.ID, AddWatchExpressionAction.LABEL, this.debugService, this.keybindingService);
M
Miguel Solorio 已提交
100
			const collapseAction = new CollapseAction(this.tree, true, 'explorer-action codicon-collapse-all');
101 102 103
			const removeAllWatchExpressionsAction = new RemoveAllWatchExpressionsAction(RemoveAllWatchExpressionsAction.ID, RemoveAllWatchExpressionsAction.LABEL, this.debugService, this.keybindingService);
			this.toolbar.setActions([addWatchExpressionAction, collapseAction, removeAllWatchExpressionsAction])();
		}
I
isidor 已提交
104

M
Matt Bierner 已提交
105 106
		this._register(this.tree.onContextMenu(e => this.onContextMenu(e)));
		this._register(this.tree.onMouseDblClick(e => this.onMouseDblClick(e)));
I
isidor 已提交
107
		this._register(this.debugService.getModel().onDidChangeWatchExpressions(async we => {
108
			if (!this.isBodyVisible()) {
109
				this.needsRefresh = true;
I
isidor 已提交
110
			} else {
I
isidor 已提交
111 112 113 114
				if (we && !we.name) {
					// We are adding a new input box, no need to re-evaluate watch expressions
					useCachedEvaluation = true;
				}
I
isidor 已提交
115
				await this.tree.updateChildren();
I
isidor 已提交
116
				useCachedEvaluation = false;
I
isidor 已提交
117 118 119
				if (we instanceof Expression) {
					this.tree.reveal(we);
				}
120
			}
121
		}));
M
Matt Bierner 已提交
122
		this._register(this.debugService.getViewModel().onDidFocusStackFrame(() => {
123
			if (!this.isBodyVisible()) {
124 125 126 127
				this.needsRefresh = true;
				return;
			}

I
isidor 已提交
128 129 130 131
			if (!this.onWatchExpressionsUpdatedScheduler.isScheduled()) {
				this.onWatchExpressionsUpdatedScheduler.schedule();
			}
		}));
I
isidor 已提交
132 133 134 135 136
		this._register(variableSetEmitter.event(() => {
			if (!ignoreVariableSetEmitter) {
				this.tree.updateChildren();
			}
		}));
137

M
Matt Bierner 已提交
138
		this._register(this.onDidChangeBodyVisibility(visible => {
139 140 141 142
			if (visible && this.needsRefresh) {
				this.onWatchExpressionsUpdatedScheduler.schedule();
			}
		}));
M
Matt Bierner 已提交
143
		this._register(this.debugService.getViewModel().onDidSelectExpression(e => {
144
			if (e instanceof Expression && e.name) {
I
isidor 已提交
145
				this.tree.rerender(e);
146 147
			}
		}));
S
SteVen Batten 已提交
148 149 150 151 152
		this._register(this.viewDescriptorService.onDidChangeLocation(({ views, from, to }) => {
			if (views.some(v => v.id === this.id)) {
				this.tree.updateOptions({ overrideStyles: { listBackground: this.getBackgroundColor() } });
			}
		}));
I
isidor 已提交
153 154
	}

155 156
	layoutBody(height: number, width: number): void {
		this.tree.layout(height, width);
S
Sandeep Somavarapu 已提交
157 158
	}

I
isidor 已提交
159 160 161 162
	focus(): void {
		this.tree.domFocus();
	}

163
	private onMouseDblClick(e: ITreeMouseEvent<IExpression>): void {
164 165 166 167 168
		if ((e.browserEvent.target as HTMLElement).className.indexOf('twistie') >= 0) {
			// Ignore double click events on twistie
			return;
		}

169 170
		const element = e.element;
		// double click on primitive value: open input box to be able to select and copy value.
I
isidor 已提交
171
		if (element instanceof Expression && element !== this.debugService.getViewModel().getSelectedExpression()) {
172
			this.debugService.getViewModel().setSelectedExpression(element);
I
isidor 已提交
173
		} else if (!element) {
174 175 176
			// Double click in watch panel triggers to add a new watch expression
			this.debugService.addWatchExpression();
		}
177 178
	}

179 180
	private onContextMenu(e: ITreeContextMenuEvent<IExpression>): void {
		const element = e.element;
181 182 183 184
		const anchor = e.anchor;
		if (!anchor) {
			return;
		}
185
		const actions: IAction[] = [];
186

187 188 189
		if (element instanceof Expression) {
			const expression = <Expression>element;
			actions.push(new AddWatchExpressionAction(AddWatchExpressionAction.ID, AddWatchExpressionAction.LABEL, this.debugService, this.keybindingService));
I
isidor 已提交
190 191 192 193
			actions.push(new Action('debug.editWatchExpression', nls.localize('editWatchExpression', "Edit Expression"), undefined, true, () => {
				this.debugService.getViewModel().setSelectedExpression(expression);
				return Promise.resolve();
			}));
194
			if (!expression.hasChildren) {
195
				actions.push(this.instantiationService.createInstance(CopyValueAction, CopyValueAction.ID, CopyValueAction.LABEL, expression.value, 'watch'));
196 197 198
			}
			actions.push(new Separator());

I
isidor 已提交
199 200 201 202
			actions.push(new Action('debug.removeWatchExpression', nls.localize('removeWatchExpression', "Remove Expression"), undefined, true, () => {
				this.debugService.removeWatchExpressions(expression.getId());
				return Promise.resolve();
			}));
203 204 205 206
			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) {
207
				const variable = element as Variable;
208
				if (!variable.hasChildren) {
209
					actions.push(this.instantiationService.createInstance(CopyValueAction, CopyValueAction.ID, CopyValueAction.LABEL, variable, 'watch'));
210 211 212 213 214 215
				}
				actions.push(new Separator());
			}
			actions.push(new RemoveAllWatchExpressionsAction(RemoveAllWatchExpressionsAction.ID, RemoveAllWatchExpressionsAction.LABEL, this.debugService, this.keybindingService));
		}

216
		this.contextMenuService.showContextMenu({
217
			getAnchor: () => anchor,
218
			getActions: () => actions,
219 220
			getActionsContext: () => element,
			onHide: () => dispose(actions)
221
		});
222 223 224
	}
}

225
class WatchExpressionsDelegate implements IListVirtualDelegate<IExpression> {
226

I
isidor 已提交
227
	getHeight(_element: IExpression): number {
228
		return 22;
229 230
	}

231 232
	getTemplateId(element: IExpression): string {
		if (element instanceof Expression) {
I
isidor 已提交
233
			return WatchExpressionsRenderer.ID;
234 235
		}

236 237
		// Variable
		return VariablesRenderer.ID;
238
	}
239 240
}

J
Joao Moreno 已提交
241 242 243
function isDebugService(element: any): element is IDebugService {
	return typeof element.getConfigurationManager === 'function';
}
244

J
Joao Moreno 已提交
245
class WatchExpressionsDataSource implements IAsyncDataSource<IDebugService, IExpression> {
246

247
	hasChildren(element: IExpression | IDebugService): boolean {
J
Joao Moreno 已提交
248
		return isDebugService(element) || element.hasChildren;
249 250
	}

J
Joao Moreno 已提交
251 252 253 254 255
	getChildren(element: IDebugService | IExpression): Promise<Array<IExpression>> {
		if (isDebugService(element)) {
			const debugService = element as IDebugService;
			const watchExpressions = debugService.getModel().getWatchExpressions();
			const viewModel = debugService.getViewModel();
I
isidor 已提交
256
			return Promise.all(watchExpressions.map(we => !!we.name && !useCachedEvaluation
257
				? we.evaluate(viewModel.focusedSession!, viewModel.focusedStackFrame!, 'watch').then(() => we)
258
				: Promise.resolve(we)));
259 260
		}

261
		return element.getChildren();
262 263 264 265
	}
}


I
isidor 已提交
266
export class WatchExpressionsRenderer extends AbstractExpressionsRenderer {
267

I
isidor 已提交
268
	static readonly ID = 'watchexpression';
269

270
	get templateId() {
I
isidor 已提交
271
		return WatchExpressionsRenderer.ID;
272
	}
273

I
isidor 已提交
274 275 276
	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);
I
isidor 已提交
277 278 279 280 281 282
		renderExpressionValue(expression, data.value, {
			showChanged: true,
			maxValueLength: MAX_VALUE_RENDER_LENGTH_IN_VIEWLET,
			showHover: true,
			colorize: true
		});
283 284
	}

I
isidor 已提交
285 286 287 288 289 290 291 292
	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);
I
isidor 已提交
293
					ignoreVariableSetEmitter = true;
I
isidor 已提交
294
					variableSetEmitter.fire();
I
isidor 已提交
295
					ignoreVariableSetEmitter = false;
I
isidor 已提交
296 297 298 299 300
				} else if (!expression.name) {
					this.debugService.removeWatchExpressions(expression.getId());
				}
			}
		};
301
	}
302
}
303

304 305 306 307
class WatchExpressionsAccessibilityProvider implements IAccessibilityProvider<IExpression> {
	getAriaLabel(element: IExpression): string {
		if (element instanceof Expression) {
			return nls.localize('watchExpressionAriaLabel', "{0} value {1}, watch, debug", (<Expression>element).name, (<Expression>element).value);
308
		}
309

310 311
		// Variable
		return nls.localize('watchVariableAriaLabel', "{0} value {1}, watch, debug", (<Variable>element).name, (<Variable>element).value);
312 313
	}
}
314

I
isidor 已提交
315 316 317 318
class WatchExpressionsDragAndDrop implements ITreeDragAndDrop<IExpression> {

	constructor(private debugService: IDebugService) { }

J
Joao Moreno 已提交
319
	onDragOver(data: IDragAndDropData): boolean | ITreeDragOverReaction {
J
Joao Moreno 已提交
320 321 322
		if (!(data instanceof ElementsDragAndDropData)) {
			return false;
		}
I
isidor 已提交
323

J
Joao Moreno 已提交
324 325
		const expressions = (data as ElementsDragAndDropData<IExpression>).elements;
		return expressions.length > 0 && expressions[0] instanceof Expression;
I
isidor 已提交
326 327
	}

J
Joao Moreno 已提交
328
	getDragURI(element: IExpression): string | null {
I
isidor 已提交
329 330 331 332 333 334 335
		if (!(element instanceof Expression) || element === this.debugService.getViewModel().getSelectedExpression()) {
			return null;
		}

		return element.getId();
	}

J
Joao Moreno 已提交
336 337 338
	getDragLabel(elements: IExpression[]): string | undefined {
		if (elements.length === 1) {
			return elements[0].name;
I
isidor 已提交
339 340
		}

J
Joao Moreno 已提交
341
		return undefined;
I
isidor 已提交
342 343
	}

J
Joao Moreno 已提交
344
	drop(data: IDragAndDropData, targetElement: IExpression): void {
J
Joao Moreno 已提交
345 346
		if (!(data instanceof ElementsDragAndDropData)) {
			return;
I
isidor 已提交
347
		}
J
Joao Moreno 已提交
348 349 350 351 352

		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);
I
isidor 已提交
353 354
	}
}