watchExpressionsView.ts 13.4 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';
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';
S
Sandeep Somavarapu 已提交
32
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
33
import { dispose } from 'vs/base/common/lifecycle';
I
isidor 已提交
34

35
const MAX_VALUE_RENDER_LENGTH_IN_VIEWLET = 1024;
I
isidor 已提交
36

37
export class WatchExpressionsView extends ViewletPanel {
I
isidor 已提交
38 39

	private onWatchExpressionsUpdatedScheduler: RunOnceScheduler;
I
isidor 已提交
40 41
	private needsRefresh = false;
	private tree!: WorkbenchAsyncDataTree<IDebugService | IExpression, IExpression, FuzzyScore>;
I
isidor 已提交
42 43 44 45

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

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

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

I
isidor 已提交
64
		const expressionsRenderer = this.instantiationService.createInstance(WatchExpressionsRenderer);
J
Joao Moreno 已提交
65
		this.tree = this.instantiationService.createInstance(WorkbenchAsyncDataTree, 'WatchExpressions', treeContainer, new WatchExpressionsDelegate(), [expressionsRenderer, this.instantiationService.createInstance(VariablesRenderer)],
J
Joao Moreno 已提交
66
			new WatchExpressionsDataSource(), {
M
Matt Bierner 已提交
67 68 69 70 71 72
			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() },
			keyboardNavigationLabelProvider: { getKeyboardNavigationLabel: (e: IExpression) => e },
			dnd: new WatchExpressionsDragAndDrop(this.debugService),
		});
I
isidor 已提交
73

I
isidor 已提交
74
		this.tree.setInput(this.debugService).then(undefined, onUnexpectedError);
I
isidor 已提交
75
		CONTEXT_WATCH_EXPRESSIONS_FOCUSED.bindTo(this.tree.contextKeyService);
I
isidor 已提交
76 77

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

M
Matt Bierner 已提交
82 83 84
		this._register(this.tree.onContextMenu(e => this.onContextMenu(e)));
		this._register(this.tree.onMouseDblClick(e => this.onMouseDblClick(e)));
		this._register(this.debugService.getModel().onDidChangeWatchExpressions(we => {
85
			if (!this.isBodyVisible()) {
86
				this.needsRefresh = true;
I
isidor 已提交
87
			} else {
88
				this.tree.updateChildren();
89
			}
90
		}));
M
Matt Bierner 已提交
91
		this._register(this.debugService.getViewModel().onDidFocusStackFrame(() => {
92
			if (!this.isBodyVisible()) {
93 94 95 96
				this.needsRefresh = true;
				return;
			}

I
isidor 已提交
97 98 99 100
			if (!this.onWatchExpressionsUpdatedScheduler.isScheduled()) {
				this.onWatchExpressionsUpdatedScheduler.schedule();
			}
		}));
M
Matt Bierner 已提交
101
		this._register(variableSetEmitter.event(() => this.tree.updateChildren()));
102

M
Matt Bierner 已提交
103
		this._register(this.onDidChangeBodyVisibility(visible => {
104 105 106 107
			if (visible && this.needsRefresh) {
				this.onWatchExpressionsUpdatedScheduler.schedule();
			}
		}));
M
Matt Bierner 已提交
108
		this._register(this.debugService.getViewModel().onDidSelectExpression(e => {
109
			if (e instanceof Expression && e.name) {
I
isidor 已提交
110
				this.tree.rerender(e);
111 112
			}
		}));
I
isidor 已提交
113 114
	}

115 116
	layoutBody(height: number, width: number): void {
		this.tree.layout(height, width);
S
Sandeep Somavarapu 已提交
117 118
	}

I
isidor 已提交
119 120 121 122
	focus(): void {
		this.tree.domFocus();
	}

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

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

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

147 148 149
		if (element instanceof Expression) {
			const expression = <Expression>element;
			actions.push(new AddWatchExpressionAction(AddWatchExpressionAction.ID, AddWatchExpressionAction.LABEL, this.debugService, this.keybindingService));
I
isidor 已提交
150 151 152 153
			actions.push(new Action('debug.editWatchExpression', nls.localize('editWatchExpression', "Edit Expression"), undefined, true, () => {
				this.debugService.getViewModel().setSelectedExpression(expression);
				return Promise.resolve();
			}));
154
			if (!expression.hasChildren) {
I
isidor 已提交
155
				actions.push(this.instantiationService.createInstance(CopyValueAction, CopyValueAction.ID, CopyValueAction.LABEL, expression.value, 'watch', this.debugService));
156 157 158
			}
			actions.push(new Separator());

I
isidor 已提交
159 160 161 162
			actions.push(new Action('debug.removeWatchExpression', nls.localize('removeWatchExpression', "Remove Expression"), undefined, true, () => {
				this.debugService.removeWatchExpressions(expression.getId());
				return Promise.resolve();
			}));
163 164 165 166
			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) {
167
				const variable = element as Variable;
168
				if (!variable.hasChildren) {
I
isidor 已提交
169
					actions.push(this.instantiationService.createInstance(CopyValueAction, CopyValueAction.ID, CopyValueAction.LABEL, variable, 'watch', this.debugService));
170 171 172 173 174 175
				}
				actions.push(new Separator());
			}
			actions.push(new RemoveAllWatchExpressionsAction(RemoveAllWatchExpressionsAction.ID, RemoveAllWatchExpressionsAction.LABEL, this.debugService, this.keybindingService));
		}

176
		this.contextMenuService.showContextMenu({
177
			getAnchor: () => anchor,
178
			getActions: () => actions,
179 180
			getActionsContext: () => element,
			onHide: () => dispose(actions)
181
		});
182 183 184
	}
}

185
class WatchExpressionsDelegate implements IListVirtualDelegate<IExpression> {
186

187 188
	getHeight(element: IExpression): number {
		return 22;
189 190
	}

191 192
	getTemplateId(element: IExpression): string {
		if (element instanceof Expression) {
I
isidor 已提交
193
			return WatchExpressionsRenderer.ID;
194 195
		}

196 197
		// Variable
		return VariablesRenderer.ID;
198
	}
199 200
}

J
Joao Moreno 已提交
201 202 203
function isDebugService(element: any): element is IDebugService {
	return typeof element.getConfigurationManager === 'function';
}
204

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

207
	hasChildren(element: IExpression | IDebugService): boolean {
J
Joao Moreno 已提交
208
		return isDebugService(element) || element.hasChildren;
209 210
	}

J
Joao Moreno 已提交
211 212 213 214 215
	getChildren(element: IDebugService | IExpression): Promise<Array<IExpression>> {
		if (isDebugService(element)) {
			const debugService = element as IDebugService;
			const watchExpressions = debugService.getModel().getWatchExpressions();
			const viewModel = debugService.getViewModel();
216
			return Promise.all(watchExpressions.map(we => !!we.name
217
				? we.evaluate(viewModel.focusedSession!, viewModel.focusedStackFrame!, 'watch').then(() => we)
218
				: Promise.resolve(we)));
219 220
		}

221
		return element.getChildren();
222 223 224 225
	}
}


I
isidor 已提交
226
export class WatchExpressionsRenderer extends AbstractExpressionsRenderer {
227

I
isidor 已提交
228
	static readonly ID = 'watchexpression';
229

230
	get templateId() {
I
isidor 已提交
231
		return WatchExpressionsRenderer.ID;
232
	}
233

I
isidor 已提交
234 235 236
	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 已提交
237 238 239 240 241 242 243
		renderExpressionValue(expression, data.value, {
			showChanged: true,
			maxValueLength: MAX_VALUE_RENDER_LENGTH_IN_VIEWLET,
			preserveWhitespace: false,
			showHover: true,
			colorize: true
		});
244 245
	}

I
isidor 已提交
246 247 248 249 250 251 252 253
	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 已提交
254
					variableSetEmitter.fire();
I
isidor 已提交
255 256 257 258 259
				} else if (!expression.name) {
					this.debugService.removeWatchExpressions(expression.getId());
				}
			}
		};
260
	}
261
}
262

263 264 265 266
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);
267
		}
268

269 270
		// Variable
		return nls.localize('watchVariableAriaLabel', "{0} value {1}, watch, debug", (<Variable>element).name, (<Variable>element).value);
271 272
	}
}
273

I
isidor 已提交
274 275 276 277
class WatchExpressionsDragAndDrop implements ITreeDragAndDrop<IExpression> {

	constructor(private debugService: IDebugService) { }

J
Joao Moreno 已提交
278
	onDragOver(data: IDragAndDropData): boolean | ITreeDragOverReaction {
J
Joao Moreno 已提交
279 280 281
		if (!(data instanceof ElementsDragAndDropData)) {
			return false;
		}
I
isidor 已提交
282

J
Joao Moreno 已提交
283 284
		const expressions = (data as ElementsDragAndDropData<IExpression>).elements;
		return expressions.length > 0 && expressions[0] instanceof Expression;
I
isidor 已提交
285 286
	}

J
Joao Moreno 已提交
287
	getDragURI(element: IExpression): string | null {
I
isidor 已提交
288 289 290 291 292 293 294
		if (!(element instanceof Expression) || element === this.debugService.getViewModel().getSelectedExpression()) {
			return null;
		}

		return element.getId();
	}

J
Joao Moreno 已提交
295 296 297
	getDragLabel(elements: IExpression[]): string | undefined {
		if (elements.length === 1) {
			return elements[0].name;
I
isidor 已提交
298 299
		}

J
Joao Moreno 已提交
300
		return undefined;
I
isidor 已提交
301 302
	}

J
Joao Moreno 已提交
303
	drop(data: IDragAndDropData, targetElement: IExpression): void {
J
Joao Moreno 已提交
304 305
		if (!(data instanceof ElementsDragAndDropData)) {
			return;
I
isidor 已提交
306
		}
J
Joao Moreno 已提交
307 308 309 310 311

		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 已提交
312 313
	}
}