watchExpressionsView.ts 13.1 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, EditWatchExpressionAction, RemoveWatchExpressionAction, 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';
17
import { IAction } 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';
21 22 23
import { IViewletPanelOptions, ViewletPanel } from 'vs/workbench/browser/parts/views/panelViewlet';
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 27
import { IDragAndDropData } from 'vs/base/browser/dnd';
import { onUnexpectedError } from 'vs/base/common/errors';
J
Joao Moreno 已提交
28
import { ElementsDragAndDropData } from 'vs/base/browser/ui/list/listView';
I
isidor 已提交
29 30
import { FuzzyScore } from 'vs/base/common/filters';
import { IHighlight } from 'vs/base/browser/ui/highlightedlabel/highlightedLabel';
I
isidor 已提交
31
import { variableSetEmitter, VariablesRenderer } from 'vs/workbench/contrib/debug/browser/variablesView';
I
isidor 已提交
32

33
const MAX_VALUE_RENDER_LENGTH_IN_VIEWLET = 1024;
I
isidor 已提交
34

35
export class WatchExpressionsView extends ViewletPanel {
I
isidor 已提交
36 37

	private onWatchExpressionsUpdatedScheduler: RunOnceScheduler;
38
	private needsRefresh: boolean;
M
Matt Bierner 已提交
39
	private tree: WorkbenchAsyncDataTree<IDebugService | IExpression, IExpression, FuzzyScore>;
I
isidor 已提交
40 41 42 43

	constructor(
		options: IViewletViewOptions,
		@IContextMenuService contextMenuService: IContextMenuService,
44
		@IDebugService private readonly debugService: IDebugService,
I
isidor 已提交
45
		@IKeybindingService keybindingService: IKeybindingService,
46
		@IInstantiationService private readonly instantiationService: IInstantiationService,
I
isidor 已提交
47
		@IConfigurationService configurationService: IConfigurationService,
I
isidor 已提交
48
	) {
I
isidor 已提交
49
		super({ ...(options as IViewletPanelOptions), ariaHeaderLabel: nls.localize('watchExpressionsSection', "Watch Expressions Section") }, keybindingService, contextMenuService, configurationService);
I
isidor 已提交
50 51

		this.onWatchExpressionsUpdatedScheduler = new RunOnceScheduler(() => {
52
			this.needsRefresh = false;
53
			this.tree.updateChildren();
I
isidor 已提交
54 55 56
		}, 50);
	}

I
isidor 已提交
57
	renderBody(container: HTMLElement): void {
I
isidor 已提交
58
		dom.addClass(container, 'debug-watch');
59 60
		const treeContainer = renderViewTree(container);

I
isidor 已提交
61
		const expressionsRenderer = this.instantiationService.createInstance(WatchExpressionsRenderer);
62
		this.tree = this.instantiationService.createInstance(WorkbenchAsyncDataTree, treeContainer, new WatchExpressionsDelegate(), [expressionsRenderer, this.instantiationService.createInstance(VariablesRenderer)],
J
Joao Moreno 已提交
63
			new WatchExpressionsDataSource(), {
I
isidor 已提交
64
				ariaLabel: nls.localize({ comment: ['Debug is a noun in this context, not a verb.'], key: 'watchAriaTreeLabel' }, "Debug Watch Expressions"),
65
				accessibilityProvider: new WatchExpressionsAccessibilityProvider(),
66
				identityProvider: { getId: element => (<IExpression>element).getId() },
I
isidor 已提交
67 68
				keyboardNavigationLabelProvider: { getKeyboardNavigationLabel: e => e },
				dnd: new WatchExpressionsDragAndDrop(this.debugService),
69
			}) as WorkbenchAsyncDataTree<IDebugService | IExpression, IExpression, FuzzyScore>;
I
isidor 已提交
70

I
isidor 已提交
71
		this.tree.setInput(this.debugService).then(undefined, onUnexpectedError);
I
isidor 已提交
72
		CONTEXT_WATCH_EXPRESSIONS_FOCUSED.bindTo(this.tree.contextKeyService);
I
isidor 已提交
73 74

		const addWatchExpressionAction = new AddWatchExpressionAction(AddWatchExpressionAction.ID, AddWatchExpressionAction.LABEL, this.debugService, this.keybindingService);
B
Benjamin Pasero 已提交
75
		const collapseAction = new CollapseAction(this.tree, true, 'explorer-action collapse-explorer');
I
isidor 已提交
76
		const removeAllWatchExpressionsAction = new RemoveAllWatchExpressionsAction(RemoveAllWatchExpressionsAction.ID, RemoveAllWatchExpressionsAction.LABEL, this.debugService, this.keybindingService);
77
		this.toolbar.setActions([addWatchExpressionAction, collapseAction, removeAllWatchExpressionsAction])();
I
isidor 已提交
78

79 80
		this.disposables.push(this.tree.onContextMenu(e => this.onContextMenu(e)));
		this.disposables.push(this.tree.onMouseDblClick(e => this.onMouseDblClick(e)));
I
isidor 已提交
81
		this.disposables.push(this.debugService.getModel().onDidChangeWatchExpressions(we => {
82
			if (!this.isBodyVisible()) {
83
				this.needsRefresh = true;
I
isidor 已提交
84
			} else {
85
				this.tree.updateChildren();
86
			}
87 88
		}));
		this.disposables.push(this.debugService.getViewModel().onDidFocusStackFrame(() => {
89
			if (!this.isBodyVisible()) {
90 91 92 93
				this.needsRefresh = true;
				return;
			}

I
isidor 已提交
94 95 96 97
			if (!this.onWatchExpressionsUpdatedScheduler.isScheduled()) {
				this.onWatchExpressionsUpdatedScheduler.schedule();
			}
		}));
98
		this.disposables.push(variableSetEmitter.event(() => this.tree.updateChildren()));
99 100 101 102 103 104

		this.disposables.push(this.onDidChangeBodyVisibility(visible => {
			if (visible && this.needsRefresh) {
				this.onWatchExpressionsUpdatedScheduler.schedule();
			}
		}));
105 106
		this.disposables.push(this.debugService.getViewModel().onDidSelectExpression(e => {
			if (e instanceof Expression && e.name) {
I
isidor 已提交
107
				this.tree.rerender(e);
108 109
			}
		}));
I
isidor 已提交
110 111
	}

112 113
	layoutBody(height: number, width: number): void {
		this.tree.layout(height, width);
S
Sandeep Somavarapu 已提交
114 115
	}

I
isidor 已提交
116 117 118 119
	focus(): void {
		this.tree.domFocus();
	}

120
	private onMouseDblClick(e: ITreeMouseEvent<IExpression>): void {
121 122 123 124 125
		if ((e.browserEvent.target as HTMLElement).className.indexOf('twistie') >= 0) {
			// Ignore double click events on twistie
			return;
		}

126 127
		const element = e.element;
		// double click on primitive value: open input box to be able to select and copy value.
I
isidor 已提交
128
		if (element instanceof Expression && element !== this.debugService.getViewModel().getSelectedExpression()) {
129
			this.debugService.getViewModel().setSelectedExpression(element);
I
isidor 已提交
130
		} else if (!element) {
131 132 133
			// Double click in watch panel triggers to add a new watch expression
			this.debugService.addWatchExpression();
		}
134 135
	}

136 137
	private onContextMenu(e: ITreeContextMenuEvent<IExpression>): void {
		const element = e.element;
138 139 140 141
		const anchor = e.anchor;
		if (!anchor) {
			return;
		}
142
		const actions: IAction[] = [];
143

144 145 146 147 148
		if (element instanceof Expression) {
			const expression = <Expression>element;
			actions.push(new AddWatchExpressionAction(AddWatchExpressionAction.ID, AddWatchExpressionAction.LABEL, this.debugService, this.keybindingService));
			actions.push(new EditWatchExpressionAction(EditWatchExpressionAction.ID, EditWatchExpressionAction.LABEL, this.debugService, this.keybindingService));
			if (!expression.hasChildren) {
I
isidor 已提交
149
				actions.push(this.instantiationService.createInstance(CopyValueAction, CopyValueAction.ID, CopyValueAction.LABEL, expression.value, 'watch', this.debugService));
150 151 152 153 154 155 156 157
			}
			actions.push(new Separator());

			actions.push(new RemoveWatchExpressionAction(RemoveWatchExpressionAction.ID, RemoveWatchExpressionAction.LABEL, this.debugService, this.keybindingService));
			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) {
158
				const variable = element as Variable;
159
				if (!variable.hasChildren) {
I
isidor 已提交
160
					actions.push(this.instantiationService.createInstance(CopyValueAction, CopyValueAction.ID, CopyValueAction.LABEL, variable, 'watch', this.debugService));
161 162 163 164 165 166
				}
				actions.push(new Separator());
			}
			actions.push(new RemoveAllWatchExpressionsAction(RemoveAllWatchExpressionsAction.ID, RemoveAllWatchExpressionsAction.LABEL, this.debugService, this.keybindingService));
		}

167
		this.contextMenuService.showContextMenu({
168
			getAnchor: () => anchor,
169 170 171
			getActions: () => actions,
			getActionsContext: () => element
		});
172 173 174
	}
}

175
class WatchExpressionsDelegate implements IListVirtualDelegate<IExpression> {
176

177 178
	getHeight(element: IExpression): number {
		return 22;
179 180
	}

181 182
	getTemplateId(element: IExpression): string {
		if (element instanceof Expression) {
I
isidor 已提交
183
			return WatchExpressionsRenderer.ID;
184 185
		}

186 187
		// Variable
		return VariablesRenderer.ID;
188
	}
189 190
}

J
Joao Moreno 已提交
191 192 193
function isDebugService(element: any): element is IDebugService {
	return typeof element.getConfigurationManager === 'function';
}
194

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

197
	hasChildren(element: IExpression | IDebugService): boolean {
J
Joao Moreno 已提交
198
		return isDebugService(element) || element.hasChildren;
199 200
	}

J
Joao Moreno 已提交
201 202 203 204 205
	getChildren(element: IDebugService | IExpression): Promise<Array<IExpression>> {
		if (isDebugService(element)) {
			const debugService = element as IDebugService;
			const watchExpressions = debugService.getModel().getWatchExpressions();
			const viewModel = debugService.getViewModel();
206
			return Promise.all(watchExpressions.map(we => !!we.name
207
				? we.evaluate(viewModel.focusedSession!, viewModel.focusedStackFrame!, 'watch').then(() => we)
208
				: Promise.resolve(we)));
209 210
		}

211
		return element.getChildren();
212 213 214 215
	}
}


I
isidor 已提交
216
export class WatchExpressionsRenderer extends AbstractExpressionsRenderer {
217

I
isidor 已提交
218
	static readonly ID = 'watchexpression';
219

220
	get templateId() {
I
isidor 已提交
221
		return WatchExpressionsRenderer.ID;
222
	}
223

I
isidor 已提交
224 225 226
	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 已提交
227 228 229 230 231 232 233
		renderExpressionValue(expression, data.value, {
			showChanged: true,
			maxValueLength: MAX_VALUE_RENDER_LENGTH_IN_VIEWLET,
			preserveWhitespace: false,
			showHover: true,
			colorize: true
		});
234 235
	}

I
isidor 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248
	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);
				} else if (!expression.name) {
					this.debugService.removeWatchExpressions(expression.getId());
				}
			}
		};
249
	}
250
}
251

252 253 254 255
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);
256
		}
257

258 259
		// Variable
		return nls.localize('watchVariableAriaLabel', "{0} value {1}, watch, debug", (<Variable>element).name, (<Variable>element).value);
260 261
	}
}
262

I
isidor 已提交
263 264 265 266
class WatchExpressionsDragAndDrop implements ITreeDragAndDrop<IExpression> {

	constructor(private debugService: IDebugService) { }

J
Joao Moreno 已提交
267
	onDragOver(data: IDragAndDropData): boolean | ITreeDragOverReaction {
J
Joao Moreno 已提交
268 269 270
		if (!(data instanceof ElementsDragAndDropData)) {
			return false;
		}
I
isidor 已提交
271

J
Joao Moreno 已提交
272 273
		const expressions = (data as ElementsDragAndDropData<IExpression>).elements;
		return expressions.length > 0 && expressions[0] instanceof Expression;
I
isidor 已提交
274 275
	}

J
Joao Moreno 已提交
276
	getDragURI(element: IExpression): string | null {
I
isidor 已提交
277 278 279 280 281 282 283
		if (!(element instanceof Expression) || element === this.debugService.getViewModel().getSelectedExpression()) {
			return null;
		}

		return element.getId();
	}

J
Joao Moreno 已提交
284 285 286
	getDragLabel(elements: IExpression[]): string | undefined {
		if (elements.length === 1) {
			return elements[0].name;
I
isidor 已提交
287 288
		}

J
Joao Moreno 已提交
289
		return undefined;
I
isidor 已提交
290 291
	}

J
Joao Moreno 已提交
292
	drop(data: IDragAndDropData, targetElement: IExpression): void {
J
Joao Moreno 已提交
293 294
		if (!(data instanceof ElementsDragAndDropData)) {
			return;
I
isidor 已提交
295
		}
J
Joao Moreno 已提交
296 297 298 299 300

		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 已提交
301 302
	}
}