debugViewer.ts 33.6 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import nls = require('vs/nls');
E
Erich Gamma 已提交
7 8
import { Promise, TPromise } from 'vs/base/common/winjs.base';
import lifecycle = require('vs/base/common/lifecycle');
I
isidor 已提交
9
import { CommonKeybindings } from 'vs/base/common/keyCodes';
E
Erich Gamma 已提交
10 11
import paths = require('vs/base/common/paths');
import async = require('vs/base/common/async');
12
import errors = require('vs/base/common/errors');
E
Erich Gamma 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
import strings = require('vs/base/common/strings');
import { isMacintosh } from 'vs/base/common/platform';
import dom = require('vs/base/browser/dom');
import mouse = require('vs/base/browser/mouseEvent');
import keyboard = require('vs/base/browser/keyboardEvent');
import labels = require('vs/base/common/labels');
import actions = require('vs/base/common/actions');
import actionbar = require('vs/base/browser/ui/actionbar/actionbar');
import tree = require('vs/base/parts/tree/common/tree');
import inputbox = require('vs/base/browser/ui/inputbox/inputBox');
import treedefaults = require('vs/base/parts/tree/browser/treeDefaults');
import renderer = require('vs/base/parts/tree/browser/actionsRenderer');
import debug = require('vs/workbench/parts/debug/common/debug');
import model = require('vs/workbench/parts/debug/common/debugModel');
import viewmodel = require('vs/workbench/parts/debug/common/debugViewModel');
28
import debugactions = require('vs/workbench/parts/debug/electron-browser/debugActions');
E
Erich Gamma 已提交
29 30 31 32 33
import { IContextViewService, IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IMessageService } from 'vs/platform/message/common/message';

I
isidor 已提交
34 35 36
const $ = dom.emmet;
const booleanRegex = /^true|false$/i;
const stringRegex = /^(['"]).*\1$/;
E
Erich Gamma 已提交
37

38
export function renderExpressionValue(arg2: debug.IExpression|string, debugInactive: boolean, container: HTMLElement): void {
E
Erich Gamma 已提交
39 40
	let value = typeof arg2 === 'string' ? arg2 : arg2.value;

I
isidor 已提交
41
	// remove stale classes
E
Erich Gamma 已提交
42
	container.className = 'value';
I
isidor 已提交
43
	// when resolving expressions we represent errors from the server as a variable with name === null.
44
	if (value === null || ((arg2 instanceof model.Expression || arg2 instanceof model.Variable) && !arg2.available)) {
E
Erich Gamma 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58
		dom.addClass(container, 'unavailable');
		debugInactive ? dom.removeClass(container, 'error') : dom.addClass(container, 'error');
	} else if (!isNaN(+value)) {
		dom.addClass(container, 'number');
	} else if (booleanRegex.test(value)) {
		dom.addClass(container, 'boolean');
	} else if (stringRegex.test(value)) {
		dom.addClass(container, 'string');
	}

	container.textContent = value;
	container.title = value;
}

59
export function renderVariable(tree: tree.ITree, variable: model.Variable, data: IVariableTemplateData, debugInactive: boolean, showChanged: boolean): void {
60 61 62 63
	if (variable.available) {
		data.name.textContent = `${variable.name}:`;
	}

E
Erich Gamma 已提交
64
	if (variable.value) {
65
		renderExpressionValue(variable, debugInactive, data.value);
66
		if (variable.valueChanged && showChanged) {
I
isidor 已提交
67
			// value changed color has priority over other colors.
68 69
			data.value.className = 'value changed';
		}
E
Erich Gamma 已提交
70 71 72 73 74 75
	} else {
		data.value.textContent = '';
		data.value.title = '';
	}
}

76
function renderRenameBox(debugService: debug.IDebugService, contextViewService: IContextViewService, tree: tree.ITree, element: any, container: HTMLElement, placeholder: string): void {
77 78 79 80 81
	let inputBoxContainer = dom.append(container, $('.inputBoxContainer'));
	let inputBox = new inputbox.InputBox(inputBoxContainer, contextViewService, {
		validationOptions: {
			validation: null,
			showMessage: false
82 83
		},
		placeholder: placeholder
84 85
	});

86
	inputBox.value = element.name ? element.name : '';
87 88
	inputBox.focus();

I
isidor 已提交
89 90
	let disposed = false;
	const toDispose: [lifecycle.IDisposable] = [inputBox];
91

I
isidor 已提交
92
	const wrapUp = async.once<any, void>((renamed: boolean) => {
93 94
		if (!disposed) {
			disposed = true;
95 96 97 98 99 100 101
			if (element instanceof model.Expression && renamed && inputBox.value) {
				debugService.renameWatchExpression(element.getId(), inputBox.value).done(null, errors.onUnexpectedError);
			} else if (element instanceof model.Expression && !element.name) {
				debugService.clearWatchExpressions(element.getId());
			} else if (element instanceof model.FunctionBreakpoint && renamed && inputBox.value) {
				debugService.renameFunctionBreakpoint(element.getId(), inputBox.value).done(null, errors.onUnexpectedError);
			} else if (element instanceof model.FunctionBreakpoint && !element.name) {
102
				debugService.removeFunctionBreakpoints(element.getId()).done(null, errors.onUnexpectedError);
103
			}
104

105 106
			tree.clearHighlight();
			tree.DOMFocus();
I
isidor 已提交
107
			// need to remove the input box since this template will be reused.
108 109 110 111 112 113
			container.removeChild(inputBoxContainer);
			lifecycle.disposeAll(toDispose);
		}
	});

	toDispose.push(dom.addStandardDisposableListener(inputBox.inputElement, 'keydown', (e: dom.IKeyboardEvent) => {
I
isidor 已提交
114 115
		const isEscape = e.equals(CommonKeybindings.ESCAPE);
		const isEnter = e.equals(CommonKeybindings.ENTER);
116 117 118 119 120 121 122 123 124
		if (isEscape || isEnter) {
			wrapUp(isEnter);
		}
	}));
	toDispose.push(dom.addDisposableListener(inputBox.inputElement, 'blur', () => {
		wrapUp(true);
	}));
}

E
Erich Gamma 已提交
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
export class BaseDebugController extends treedefaults.DefaultController {

	constructor(protected debugService: debug.IDebugService, private contextMenuService: IContextMenuService, private actionProvider: renderer.IActionProvider, private focusOnContextMenu = true) {
		super();

		if (isMacintosh) {
			this.downKeyBindingDispatcher.set(CommonKeybindings.CTRLCMD_BACKSPACE, this.onDelete.bind(this));
		} else {
			this.downKeyBindingDispatcher.set(CommonKeybindings.DELETE, this.onDelete.bind(this));
			this.downKeyBindingDispatcher.set(CommonKeybindings.SHIFT_DELETE, this.onDelete.bind(this));
		}
	}

	public onContextMenu(tree: tree.ITree, element: debug.IEnablement, event: tree.ContextMenuEvent): boolean {
		if (event.target && event.target.tagName && event.target.tagName.toLowerCase() === 'input') {
			return false;
		}

		event.preventDefault();
		event.stopPropagation();

		if (this.focusOnContextMenu) {
			tree.setFocus(element);
		}

		if (this.actionProvider.hasSecondaryActions(tree, element)) {
I
isidor 已提交
151
			const anchor = { x: event.posx + 1, y: event.posy };
E
Erich Gamma 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
			this.contextMenuService.showContextMenu({
				getAnchor: () => anchor,
				getActions: () => this.actionProvider.getSecondaryActions(tree, element),
				onHide: (wasCancelled?: boolean) => {
					if (wasCancelled) {
						tree.DOMFocus();
					}
				},
				getActionsContext: () => element
			});

			return true;
		}

		return false;
	}

	protected onDelete(tree: tree.ITree, event: keyboard.StandardKeyboardEvent): boolean {
		return false;
	}
}

I
isidor 已提交
174
// call stack
E
Erich Gamma 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

export class CallStackDataSource implements tree.IDataSource {

	public getId(tree: tree.ITree, element: any): string {
		return element.getId();
	}

	public hasChildren(tree: tree.ITree, element: any): boolean {
		return element instanceof model.Model || element instanceof model.Thread;
	}

	public getChildren(tree: tree.ITree, element: any): Promise {
		if (element instanceof model.Thread) {
			return Promise.as((<model.Thread> element).callStack);
		}

I
isidor 已提交
191 192
		const threads = (<model.Model> element).getThreads();
		const threadsArray: debug.IThread[] = [];
I
isidor 已提交
193 194 195
		Object.keys(threads).forEach(threadId => {
			threadsArray.push(threads[threadId]);
		});
E
Erich Gamma 已提交
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

		if (threadsArray.length === 1) {
			return Promise.as(threadsArray[0].callStack);
		} else {
			return Promise.as(threadsArray);
		}
	}

	public getParent(tree: tree.ITree, element: any): Promise {
		return Promise.as(null);
	}
}

interface IThreadTemplateData {
	name: HTMLElement;
}

interface IStackFrameTemplateData {
	stackFrame: HTMLElement;
	label : HTMLElement;
	file : HTMLElement;
	fileName : HTMLElement;
	lineNumber : HTMLElement;
}

export class CallStackRenderer implements tree.IRenderer {

	private static THREAD_TEMPLATE_ID = 'thread';
	private static STACK_FRAME_TEMPLATE_ID = 'stackFrame';

	constructor(@IWorkspaceContextService private contextService: IWorkspaceContextService) {
		// noop
	}

	public getHeight(tree:tree.ITree, element:any): number {
I
isidor 已提交
231
		return 22;
E
Erich Gamma 已提交
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
	}

	public getTemplateId(tree: tree.ITree, element: any): string {
		if (element instanceof model.Thread) {
			return CallStackRenderer.THREAD_TEMPLATE_ID;
		}
		if (element instanceof model.StackFrame) {
			return CallStackRenderer.STACK_FRAME_TEMPLATE_ID;
		}

		return null;
	}

	public renderTemplate(tree: tree.ITree, templateId: string, container: HTMLElement): any {
		if (templateId === CallStackRenderer.THREAD_TEMPLATE_ID) {
			let data: IThreadTemplateData = Object.create(null);
			data.name = dom.append(container, $('.thread'));

			return data;
		}

		let data: IStackFrameTemplateData = Object.create(null);
		data.stackFrame = dom.append(container, $('.stack-frame'));
		data.label = dom.append(data.stackFrame, $('span.label'));
		data.file = dom.append(data.stackFrame, $('.file'));
		data.fileName = dom.append(data.file, $('span.file-name'));
		data.lineNumber = dom.append(data.file, $('span.line-number'));

		return data;
	}

	public renderElement(tree: tree.ITree, element: any, templateId: string, templateData: any): void {
		if (templateId === CallStackRenderer.THREAD_TEMPLATE_ID) {
			this.renderThread(element, templateData);
		} else {
			this.renderStackFrame(element, templateData);
		}
	}

	private renderThread(thread: debug.IThread, data: IThreadTemplateData): void {
		data.name.textContent = thread.name;
	}

	private renderStackFrame(stackFrame: debug.IStackFrame, data: IStackFrameTemplateData): void {
		stackFrame.source.available ? dom.removeClass(data.stackFrame, 'disabled') : dom.addClass(data.stackFrame, 'disabled');
		data.file.title = stackFrame.source.uri.fsPath;
		data.label.textContent = stackFrame.name;
		if (stackFrame.source.inMemory) {
			data.fileName.textContent = stackFrame.source.name;
		} else {
			data.fileName.textContent = labels.getPathLabel(paths.basename(stackFrame.source.uri.fsPath), this.contextService);
		}
284
		data.lineNumber.textContent = stackFrame.lineNumber !== undefined ? `${ stackFrame.lineNumber }` : '';
E
Erich Gamma 已提交
285 286 287 288 289 290 291
	}

	public disposeTemplate(tree: tree.ITree, templateId: string, templateData: any): void {
		// noop
	}
}

I
isidor 已提交
292
// variables
E
Erich Gamma 已提交
293

I
isidor 已提交
294
export class VariablesActionProvider implements renderer.IActionProvider {
E
Erich Gamma 已提交
295 296 297 298 299 300 301 302 303 304 305

	private instantiationService: IInstantiationService;

	constructor(instantiationService: IInstantiationService) {
		this.instantiationService = instantiationService;
	}

	public hasActions(tree: tree.ITree, element: any): boolean {
		return false;
	}

I
isidor 已提交
306 307 308 309
	public getActions(tree: tree.ITree, element: any): TPromise<actions.IAction[]> {
		return Promise.as([]);
	}

E
Erich Gamma 已提交
310 311 312 313 314 315 316
	public hasSecondaryActions(tree: tree.ITree, element: any): boolean {
		return element instanceof model.Variable;
	}

	public getSecondaryActions(tree: tree.ITree, element: any): Promise {
		let actions: actions.Action[] = [];
		const variable = <model.Variable> element;
I
isidor 已提交
317
		actions.push(this.instantiationService.createInstance(debugactions.AddToWatchExpressionsAction, debugactions.AddToWatchExpressionsAction.ID, debugactions.AddToWatchExpressionsAction.LABEL, variable));
E
Erich Gamma 已提交
318
		if (variable.reference === 0) {
I
isidor 已提交
319
			actions.push(this.instantiationService.createInstance(debugactions.CopyValueAction, debugactions.CopyValueAction.ID, debugactions.CopyValueAction.LABEL, variable));
E
Erich Gamma 已提交
320 321 322 323
		}

		return Promise.as(actions);
	}
I
isidor 已提交
324 325 326 327

	public getActionItem(tree: tree.ITree, element: any, action: actions.IAction): actionbar.IActionItem {
		return null;
	}
E
Erich Gamma 已提交
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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
}

export class VariablesDataSource implements tree.IDataSource {

	constructor(private debugService: debug.IDebugService) {
		// noop
	}

	public getId(tree: tree.ITree, element: any): string {
		return element.getId();
	}

	public hasChildren(tree: tree.ITree, element: any): boolean {
		if (element instanceof viewmodel.ViewModel || element instanceof model.Scope) {
			return true;
		}

		let variable = <model.Variable> element;
		return variable.reference !== 0 && !strings.equalsIgnoreCase(variable.value, 'null');
	}

	public getChildren(tree: tree.ITree, element: any): Promise {
		if (element instanceof viewmodel.ViewModel) {
			let focusedStackFrame = (<viewmodel.ViewModel> element).getFocusedStackFrame();
			return focusedStackFrame ? focusedStackFrame.getScopes(this.debugService) : Promise.as([]);
		}

		let scope = <model.Scope> element;
		return scope.getChildren(this.debugService);
	}

	public getParent(tree: tree.ITree, element: any): Promise {
		return Promise.as(null);
	}
}

interface IScopeTemplateData {
	name: HTMLElement;
}

export interface IVariableTemplateData {
	expression: HTMLElement;
	name: HTMLElement;
	value: HTMLElement;
}

export class VariablesRenderer implements tree.IRenderer {

	private static SCOPE_TEMPLATE_ID = 'scope';
	private static VARIABLE_TEMPLATE_ID = 'variable';

	constructor(@debug.IDebugService private debugService: debug.IDebugService) {
		// noop
	}

383
	public getHeight(tree: tree.ITree, element: any): number {
I
isidor 已提交
384
		return 22;
E
Erich Gamma 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
	}

	public getTemplateId(tree: tree.ITree, element: any): string {
		if (element instanceof model.Scope) {
			return VariablesRenderer.SCOPE_TEMPLATE_ID;
		}
		if (element instanceof model.Expression) {
			return VariablesRenderer.VARIABLE_TEMPLATE_ID;
		}

		return null;
	}

	public renderTemplate(tree: tree.ITree, templateId: string, container: HTMLElement): any {
		if (templateId === VariablesRenderer.SCOPE_TEMPLATE_ID) {
			let data: IScopeTemplateData = Object.create(null);
			data.name = dom.append(container, $('.scope'));

			return data;
		}

		let data: IVariableTemplateData = Object.create(null);
		data.expression = dom.append(container, $(isMacintosh ? '.expression.mac' : '.expression.win-linux'));
		data.name = dom.append(data.expression, $('span.name'));
		data.value = dom.append(data.expression, $('span.value'));

		return data;
	}

	public renderElement(tree: tree.ITree, element: any, templateId: string, templateData: any): void {
		if (templateId === VariablesRenderer.SCOPE_TEMPLATE_ID) {
			this.renderScope(element, templateData);
		} else {
418
			renderVariable(tree, element, templateData, this.debugService.getState() === debug.State.Inactive, true);
E
Erich Gamma 已提交
419 420 421 422 423 424 425 426 427 428 429 430
		}
	}

	private renderScope(scope: model.Scope, data: IScopeTemplateData): void {
		data.name.textContent = scope.name;
	}

	public disposeTemplate(tree: tree.ITree, templateId: string, templateData: any): void {
		// noop
	}
}

I
isidor 已提交
431
// watch expressions
E
Erich Gamma 已提交
432

I
isidor 已提交
433
export class WatchExpressionsActionProvider implements renderer.IActionProvider {
E
Erich Gamma 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

	private instantiationService: IInstantiationService;

	constructor(instantiationService: IInstantiationService) {
		this.instantiationService = instantiationService;
	}

	public hasActions(tree: tree.ITree, element: any): boolean {
		return element instanceof model.Expression && element.name;
	}

	public hasSecondaryActions(tree: tree.ITree, element: any): boolean {
		return true;
	}

	public getActions(tree: tree.ITree, element: any): Promise {
		return Promise.as(this.getExpressionActions());
	}

	public getExpressionActions(): actions.IAction[] {
I
isidor 已提交
454
		return [this.instantiationService.createInstance(debugactions.RemoveWatchExpressionAction, debugactions.RemoveWatchExpressionAction.ID, debugactions.RemoveWatchExpressionAction.LABEL)];
E
Erich Gamma 已提交
455 456 457
	}

	public getSecondaryActions(tree: tree.ITree, element: any): Promise {
I
isidor 已提交
458
		const actions: actions.Action[] = [];
E
Erich Gamma 已提交
459 460
		if (element instanceof model.Expression) {
			const expression = <model.Expression> element;
I
isidor 已提交
461 462
			actions.push(this.instantiationService.createInstance(debugactions.AddWatchExpressionAction, debugactions.AddWatchExpressionAction.ID, debugactions.AddWatchExpressionAction.LABEL));
			actions.push(this.instantiationService.createInstance(debugactions.RenameWatchExpressionAction, debugactions.RenameWatchExpressionAction.ID, debugactions.RenameWatchExpressionAction.LABEL, expression));
E
Erich Gamma 已提交
463
			if (expression.reference === 0) {
I
isidor 已提交
464
				actions.push(this.instantiationService.createInstance(debugactions.CopyValueAction, debugactions.CopyValueAction.ID, debugactions.CopyValueAction.LABEL, expression.value));
E
Erich Gamma 已提交
465 466 467
			}
			actions.push(new actionbar.Separator());

I
isidor 已提交
468 469
			actions.push(this.instantiationService.createInstance(debugactions.RemoveWatchExpressionAction, debugactions.RemoveWatchExpressionAction.ID, debugactions.RemoveWatchExpressionAction.LABEL));
			actions.push(this.instantiationService.createInstance(debugactions.RemoveAllWatchExpressionsAction, debugactions.RemoveAllWatchExpressionsAction.ID, debugactions.RemoveAllWatchExpressionsAction.LABEL));
E
Erich Gamma 已提交
470
		} else {
I
isidor 已提交
471
			actions.push(this.instantiationService.createInstance(debugactions.AddWatchExpressionAction, debugactions.AddWatchExpressionAction.ID, debugactions.AddWatchExpressionAction.LABEL));
E
Erich Gamma 已提交
472 473 474
			if (element instanceof model.Variable) {
				const variable = <model.Variable> element;
				if (variable.reference === 0) {
I
isidor 已提交
475
					actions.push(this.instantiationService.createInstance(debugactions.CopyValueAction, debugactions.CopyValueAction.ID, debugactions.CopyValueAction.LABEL, variable.value));
E
Erich Gamma 已提交
476 477 478
				}
				actions.push(new actionbar.Separator());
			}
I
isidor 已提交
479
			actions.push(this.instantiationService.createInstance(debugactions.RemoveAllWatchExpressionsAction, debugactions.RemoveAllWatchExpressionsAction.ID, debugactions.RemoveAllWatchExpressionsAction.LABEL));
E
Erich Gamma 已提交
480 481 482 483
		}

		return Promise.as(actions);
	}
I
isidor 已提交
484 485 486 487

	public getActionItem(tree: tree.ITree, element: any, action: actions.IAction): actionbar.IActionItem {
		return null;
	}
E
Erich Gamma 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
}

export class WatchExpressionsDataSource implements tree.IDataSource {

	constructor(private debugService: debug.IDebugService) {
		// noop
	}

	public getId(tree: tree.ITree, element: any): string {
		return element.getId();
	}

	public hasChildren(tree: tree.ITree, element: any): boolean {
		if (element instanceof model.Model) {
			return true;
		}

I
isidor 已提交
505
		const watchExpression = <model.Expression> element;
E
Erich Gamma 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
		return watchExpression.reference !== 0 && !strings.equalsIgnoreCase(watchExpression.value, 'null');
	}

	public getChildren(tree: tree.ITree, element: any): Promise {
		if (element instanceof model.Model) {
			return Promise.as((<model.Model> element).getWatchExpressions());
		}

		let expression = <model.Expression> element;
		return expression.getChildren(this.debugService);
	}

	public getParent(tree: tree.ITree, element: any): Promise {
		return Promise.as(null);
	}
}

interface IWatchExpressionTemplateData extends IVariableTemplateData {
	actionBar: actionbar.ActionBar;
}

export class WatchExpressionsRenderer implements tree.IRenderer {

	private static WATCH_EXPRESSION_TEMPLATE_ID = 'watchExpression';
	private static VARIABLE_TEMPLATE_ID = 'variables';
	private toDispose: lifecycle.IDisposable[];
	private actionProvider: WatchExpressionsActionProvider;

	constructor(actionProvider: renderer.IActionProvider, private actionRunner: actions.IActionRunner,
		@IMessageService private messageService: IMessageService,
		@debug.IDebugService private debugService: debug.IDebugService,
		@IContextViewService private contextViewService: IContextViewService
	) {
		this.toDispose = [];
		this.actionProvider = <WatchExpressionsActionProvider> actionProvider;
	}

	public getHeight(tree:tree.ITree, element:any): number {
I
isidor 已提交
544
		return 22;
E
Erich Gamma 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
	}

	public getTemplateId(tree: tree.ITree, element: any): string {
		if (element instanceof model.Expression) {
			return WatchExpressionsRenderer.WATCH_EXPRESSION_TEMPLATE_ID;
		}

		return WatchExpressionsRenderer.VARIABLE_TEMPLATE_ID;
	}

	public renderTemplate(tree: tree.ITree, templateId: string, container: HTMLElement): any {
		let data: IWatchExpressionTemplateData = Object.create(null);
		if (templateId === WatchExpressionsRenderer.WATCH_EXPRESSION_TEMPLATE_ID) {
			data.actionBar = new actionbar.ActionBar(container, { actionRunner: this.actionRunner });
			data.actionBar.push(this.actionProvider.getExpressionActions() , { icon: true, label: false });
		}

		data.expression = dom.append(container, $(isMacintosh ? '.expression.mac' : '.expression.win-linux'));
		data.name = dom.append(data.expression, $('span.name'));
		data.value = dom.append(data.expression, $('span.value'));

		return data;
	}

	public renderElement(tree: tree.ITree, element: any, templateId: string, templateData: any): void {
		if (templateId === WatchExpressionsRenderer.WATCH_EXPRESSION_TEMPLATE_ID) {
			this.renderWatchExpression(tree, element, templateData);
		} else {
			this.renderExpression(tree, element, templateData);
		}
	}

	private renderWatchExpression(tree: tree.ITree, watchExpression: debug.IExpression, data: IWatchExpressionTemplateData): void {
		let selectedExpression = this.debugService.getViewModel().getSelectedExpression();
		if ((selectedExpression instanceof model.Expression && selectedExpression.getId() === watchExpression.getId()) || (watchExpression instanceof model.Expression && !watchExpression.name)) {
580
			renderRenameBox(this.debugService, this.contextViewService, tree, watchExpression, data.expression, nls.localize('watchExpressionPlaceholder', "Expression to watch"));
E
Erich Gamma 已提交
581 582 583 584 585 586 587 588 589
		}
		data.actionBar.context = watchExpression;

		this.renderExpression(tree, watchExpression, data);
	}

	private renderExpression(tree: tree.ITree, expression: debug.IExpression, data: IVariableTemplateData): void {
		data.name.textContent = `${expression.name}:`;
		if (expression.value) {
590
			renderExpressionValue(expression, this.debugService.getState() === debug.State.Inactive, data.value);
E
Erich Gamma 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
		}
	}

	public disposeTemplate(tree: tree.ITree, templateId: string, templateData: any): void {
		// noop
	}

	public dispose(): void {
		this.toDispose = lifecycle.disposeAll(this.toDispose);
	}
}

export class WatchExpressionsController extends BaseDebugController {

	constructor(debugService: debug.IDebugService, contextMenuService: IContextMenuService, actionProvider: renderer.IActionProvider) {
		super(debugService, contextMenuService, actionProvider);

		if (isMacintosh) {
			this.downKeyBindingDispatcher.set(CommonKeybindings.ENTER, this.onRename.bind(this));
		} else {
			this.downKeyBindingDispatcher.set(CommonKeybindings.F2, this.onRename.bind(this));
		}
	}

	/* protected */ public onLeftClick(tree: tree.ITree, element: any, event: mouse.StandardMouseEvent): boolean {
I
isidor 已提交
616
		// double click on primitive value: open input box to be able to select and copy value.
E
Erich Gamma 已提交
617
		if (element instanceof model.Expression && event.detail === 2) {
I
isidor 已提交
618
			const expression = <debug.IExpression> element;
E
Erich Gamma 已提交
619 620 621 622 623 624 625 626 627 628
			if (expression.reference === 0) {
				this.debugService.getViewModel().setSelectedExpression(expression);
			}
			return true;
		}

		return super.onLeftClick(tree, element, event);
	}

	protected onRename(tree: tree.ITree, event: KeyboardEvent): boolean {
I
isidor 已提交
629
		const element = tree.getFocus();
E
Erich Gamma 已提交
630
		if (element instanceof model.Expression) {
I
isidor 已提交
631
			const watchExpression = <model.Expression> element;
E
Erich Gamma 已提交
632 633 634 635 636 637 638 639 640 641
			if (watchExpression.reference === 0) {
				this.debugService.getViewModel().setSelectedExpression(watchExpression);
			}
			return true;
		}

		return false;
	}

	protected onDelete(tree: tree.ITree, event: keyboard.StandardKeyboardEvent): boolean {
I
isidor 已提交
642
		const element = tree.getFocus();
E
Erich Gamma 已提交
643
		if (element instanceof model.Expression) {
I
isidor 已提交
644
			const we = <model.Expression> element;
E
Erich Gamma 已提交
645 646 647 648 649 650 651 652 653
			this.debugService.clearWatchExpressions(we.getId());

			return true;
		}

		return false;
	}
}

I
isidor 已提交
654
// breakpoints
E
Erich Gamma 已提交
655

I
isidor 已提交
656
export class BreakpointsActionProvider implements renderer.IActionProvider {
E
Erich Gamma 已提交
657 658

	constructor(private instantiationService: IInstantiationService) {
I
isidor 已提交
659
		// noop
E
Erich Gamma 已提交
660 661 662 663 664 665 666
	}

	public hasActions(tree: tree.ITree, element: any): boolean {
		return element instanceof model.Breakpoint;
	}

	public hasSecondaryActions(tree: tree.ITree, element: any): boolean {
667
		return element instanceof model.Breakpoint || element instanceof model.ExceptionBreakpoint || element instanceof model.FunctionBreakpoint;
E
Erich Gamma 已提交
668 669 670 671 672 673 674 675 676 677 678
	}

	public getActions(tree: tree.ITree, element: any): TPromise<actions.IAction[]> {
		if (element instanceof model.Breakpoint) {
			return Promise.as(this.getBreakpointActions());
		}

		return Promise.as([]);
	}

	public getBreakpointActions(): actions.IAction[] {
I
isidor 已提交
679
		return [this.instantiationService.createInstance(debugactions.RemoveBreakpointAction, debugactions.RemoveBreakpointAction.ID, debugactions.RemoveBreakpointAction.LABEL)];
E
Erich Gamma 已提交
680 681 682
	}

	public getSecondaryActions(tree: tree.ITree, element: any): TPromise<actions.IAction[]> {
I
isidor 已提交
683
		const actions: actions.Action[] = [this.instantiationService.createInstance(debugactions.ToggleEnablementAction, debugactions.ToggleEnablementAction.ID, debugactions.ToggleEnablementAction.LABEL)];
E
Erich Gamma 已提交
684 685
		actions.push(new actionbar.Separator());

I
isidor 已提交
686 687
		actions.push(this.instantiationService.createInstance(debugactions.RemoveBreakpointAction, debugactions.RemoveBreakpointAction.ID, debugactions.RemoveBreakpointAction.LABEL));
		actions.push(this.instantiationService.createInstance(debugactions.RemoveAllBreakpointsAction, debugactions.RemoveAllBreakpointsAction.ID, debugactions.RemoveAllBreakpointsAction.LABEL));
E
Erich Gamma 已提交
688 689
		actions.push(new actionbar.Separator());

I
isidor 已提交
690
		actions.push(this.instantiationService.createInstance(debugactions.ToggleBreakpointsActivatedAction, debugactions.ToggleBreakpointsActivatedAction.ID, debugactions.ToggleBreakpointsActivatedAction.LABEL));
E
Erich Gamma 已提交
691 692
		actions.push(new actionbar.Separator());

I
isidor 已提交
693 694
		actions.push(this.instantiationService.createInstance(debugactions.EnableAllBreakpointsAction, debugactions.EnableAllBreakpointsAction.ID, debugactions.EnableAllBreakpointsAction.LABEL));
		actions.push(this.instantiationService.createInstance(debugactions.DisableAllBreakpointsAction, debugactions.DisableAllBreakpointsAction.ID, debugactions.DisableAllBreakpointsAction.LABEL));
E
Erich Gamma 已提交
695 696
		actions.push(new actionbar.Separator());

697 698
		actions.push(this.instantiationService.createInstance(debugactions.AddFunctionBreakpointAction, debugactions.AddFunctionBreakpointAction.ID, debugactions.AddFunctionBreakpointAction.LABEL));
		actions.push(new actionbar.Separator());
I
isidor 已提交
699

I
isidor 已提交
700
		actions.push(this.instantiationService.createInstance(debugactions.ReapplyBreakpointsAction, debugactions.ReapplyBreakpointsAction.ID, debugactions.ReapplyBreakpointsAction.LABEL));
E
Erich Gamma 已提交
701 702 703

		return Promise.as(actions);
	}
I
isidor 已提交
704 705 706 707

	public getActionItem(tree: tree.ITree, element: any, action: actions.IAction): actionbar.IActionItem {
		return null;
	}
E
Erich Gamma 已提交
708 709 710 711 712 713 714 715 716 717 718 719 720
}

export class BreakpointsDataSource implements tree.IDataSource {

	public getId(tree: tree.ITree, element: any): string {
		return element.getId();
	}

	public hasChildren(tree: tree.ITree, element: any): boolean {
		return element instanceof model.Model;
	}

	public getChildren(tree: tree.ITree, element: any): Promise {
I
isidor 已提交
721 722
		const model = <model.Model> element;
		const exBreakpoints = <debug.IEnablement[]> model.getExceptionBreakpoints();
E
Erich Gamma 已提交
723

724
		return Promise.as(exBreakpoints.concat(model.getFunctionBreakpoints()).concat(model.getBreakpoints()));
E
Erich Gamma 已提交
725 726 727 728 729 730 731 732
	}

	public getParent(tree: tree.ITree, element: any): Promise {
		return Promise.as(null);
	}
}

interface IExceptionBreakpointTemplateData {
733
	breakpoint: HTMLElement;
E
Erich Gamma 已提交
734 735 736 737 738 739 740 741 742 743 744
	name: HTMLElement;
	checkbox: HTMLInputElement;
	toDisposeBeforeRender: lifecycle.IDisposable[];
}

interface IBreakpointTemplateData extends IExceptionBreakpointTemplateData {
	actionBar: actionbar.ActionBar;
	lineNumber: HTMLElement;
	filePath: HTMLElement;
}

745 746 747 748
interface IFunctionBreakpointTemplateData extends IExceptionBreakpointTemplateData {
	actionBar: actionbar.ActionBar;
}

E
Erich Gamma 已提交
749 750 751
export class BreakpointsRenderer implements tree.IRenderer {

	private static EXCEPTION_BREAKPOINT_TEMPLATE_ID = 'exceptionBreakpoint';
752
	private static FUNCTION_BREAKPOINT_TEMPLATE_ID = 'functionBreakpoint';
E
Erich Gamma 已提交
753 754 755 756 757 758 759
	private static BREAKPOINT_TEMPLATE_ID = 'breakpoint';

	constructor(
		private actionProvider: BreakpointsActionProvider,
		private actionRunner: actions.IActionRunner,
		@IMessageService private messageService: IMessageService,
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
760 761
		@debug.IDebugService private debugService: debug.IDebugService,
		@IContextViewService private contextViewService: IContextViewService
E
Erich Gamma 已提交
762 763 764 765 766
	) {
		// noop
	}

	public getHeight(tree:tree.ITree, element:any): number {
I
isidor 已提交
767
		return 22;
E
Erich Gamma 已提交
768 769 770 771 772 773
	}

	public getTemplateId(tree: tree.ITree, element: any): string {
		if (element instanceof model.Breakpoint) {
			return BreakpointsRenderer.BREAKPOINT_TEMPLATE_ID;
		}
774 775 776
		if (element instanceof model.FunctionBreakpoint) {
			return BreakpointsRenderer.FUNCTION_BREAKPOINT_TEMPLATE_ID;
		}
E
Erich Gamma 已提交
777 778 779 780 781 782 783 784
		if (element instanceof model.ExceptionBreakpoint) {
			return BreakpointsRenderer.EXCEPTION_BREAKPOINT_TEMPLATE_ID;
		}

		return null;
	}

	public renderTemplate(tree: tree.ITree, templateId: string, container: HTMLElement): any {
I
isidor 已提交
785
		const data: IBreakpointTemplateData = Object.create(null);
786
		if (templateId === BreakpointsRenderer.BREAKPOINT_TEMPLATE_ID || templateId === BreakpointsRenderer.FUNCTION_BREAKPOINT_TEMPLATE_ID) {
E
Erich Gamma 已提交
787 788 789 790
			data.actionBar = new actionbar.ActionBar(container, { actionRunner: this.actionRunner });
			data.actionBar.push(this.actionProvider.getBreakpointActions(), { icon: true, label: false });
		}

791
		data.breakpoint = dom.append(container, $('.breakpoint'));
E
Erich Gamma 已提交
792 793 794 795 796
		data.toDisposeBeforeRender = [];

		data.checkbox = <HTMLInputElement> $('input');
		data.checkbox.type = 'checkbox';
		data.checkbox.className = 'checkbox';
797
		dom.append(data.breakpoint, data.checkbox);
E
Erich Gamma 已提交
798

799
		data.name = dom.append(data.breakpoint, $('span.name'));
E
Erich Gamma 已提交
800 801

		if (templateId === BreakpointsRenderer.BREAKPOINT_TEMPLATE_ID) {
802 803
			data.lineNumber = dom.append(data.breakpoint, $('span.line-number'));
			data.filePath = dom.append(data.breakpoint, $('span.file-path'));
E
Erich Gamma 已提交
804 805 806 807 808 809
		}

		return data;
	}

	public renderElement(tree: tree.ITree, element: any, templateId: string, templateData: any): void {
810 811 812 813 814
		templateData.toDisposeBeforeRender = lifecycle.disposeAll(templateData.toDisposeBeforeRender);
		templateData.toDisposeBeforeRender.push(dom.addStandardDisposableListener(templateData.checkbox, 'change', (e) => {
			this.debugService.toggleEnablement(element);
		}));

E
Erich Gamma 已提交
815 816
		if (templateId === BreakpointsRenderer.EXCEPTION_BREAKPOINT_TEMPLATE_ID) {
			this.renderExceptionBreakpoint(element, templateData);
817 818
		} else if (templateId === BreakpointsRenderer.FUNCTION_BREAKPOINT_TEMPLATE_ID) {
			this.renderFunctionBreakpoint(tree, element, templateData);
E
Erich Gamma 已提交
819 820 821 822 823 824
		} else {
			this.renderBreakpoint(tree, element, templateData);
		}
	}

	private renderExceptionBreakpoint(exceptionBreakpoint: debug.IExceptionBreakpoint, data: IExceptionBreakpointTemplateData): void {
I
isidor 已提交
825
		const namePascalCase = exceptionBreakpoint.name.charAt(0).toUpperCase() + exceptionBreakpoint.name.slice(1);
E
Erich Gamma 已提交
826 827
		data.name.textContent = `${ namePascalCase} exceptions`;
		data.checkbox.checked = exceptionBreakpoint.enabled;
828
	}
E
Erich Gamma 已提交
829

830
	private renderFunctionBreakpoint(tree: tree.ITree, functionBreakpoint: debug.IFunctionBreakpoint, data: IFunctionBreakpointTemplateData): void {
831 832 833 834 835 836 837
		if (!functionBreakpoint.name) {
			renderRenameBox(this.debugService, this.contextViewService, tree, functionBreakpoint, data.breakpoint, nls.localize('functionBreakpointPlaceholder', "Function to break on"));
		} else {
			this.debugService.getModel().areBreakpointsActivated() ? tree.removeTraits('disabled', [functionBreakpoint]) : tree.addTraits('disabled', [functionBreakpoint]);
			data.name.textContent = functionBreakpoint.name;
			data.checkbox.checked = functionBreakpoint.enabled;
		}
838
		data.actionBar.context = functionBreakpoint;
E
Erich Gamma 已提交
839 840 841
	}

	private renderBreakpoint(tree: tree.ITree, breakpoint: debug.IBreakpoint, data: IBreakpointTemplateData): void {
842
		this.debugService.getModel().areBreakpointsActivated() ? tree.removeTraits('disabled', [breakpoint]) : tree.addTraits('disabled', [breakpoint]);
E
Erich Gamma 已提交
843 844 845 846 847 848 849 850 851

		data.name.textContent = labels.getPathLabel(paths.basename(breakpoint.source.uri.fsPath), this.contextService);
		data.lineNumber.textContent = breakpoint.desiredLineNumber !== breakpoint.lineNumber ? breakpoint.desiredLineNumber + ' \u2192 ' + breakpoint.lineNumber : '' + breakpoint.lineNumber;
		data.filePath.textContent = labels.getPathLabel(paths.dirname(breakpoint.source.uri.fsPath), this.contextService);
		data.checkbox.checked = breakpoint.enabled;
		data.actionBar.context = breakpoint;
	}

	public disposeTemplate(tree: tree.ITree, templateId: string, templateData: any): void {
852
		if (templateId === BreakpointsRenderer.BREAKPOINT_TEMPLATE_ID || templateId === BreakpointsRenderer.FUNCTION_BREAKPOINT_TEMPLATE_ID) {
E
Erich Gamma 已提交
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
			templateData.actionBar.dispose();
		}
	}
}

export class BreakpointsController extends BaseDebugController {

	/* protected */ public onLeftClick(tree:tree.ITree, element: any, eventish:treedefaults.ICancelableEvent, origin: string = 'mouse'):boolean {
		if (element instanceof model.ExceptionBreakpoint) {
			return false;
		}

		return super.onLeftClick(tree, element, eventish, origin);
	}

	/* protected */ public onUp(tree:tree.ITree, event:keyboard.StandardKeyboardEvent): boolean {
		return this.doNotFocusExceptionBreakpoint(tree, super.onUp(tree, event));
	}

	/* protected */ public onPageUp(tree:tree.ITree, event:keyboard.StandardKeyboardEvent): boolean {
		return this.doNotFocusExceptionBreakpoint(tree, super.onPageUp(tree, event));
	}

F
Francois Valdy 已提交
876 877
	private doNotFocusExceptionBreakpoint(tree: tree.ITree, upSucceeded: boolean) : boolean {
		if (upSucceeded) {
I
isidor 已提交
878
			const focus = tree.getFocus();
E
Erich Gamma 已提交
879 880 881 882 883
			if (focus instanceof model.ExceptionBreakpoint) {
				tree.focusNth(2);
			}
		}

F
Francois Valdy 已提交
884
		return upSucceeded;
E
Erich Gamma 已提交
885 886 887
	}

	protected onDelete(tree: tree.ITree, event: keyboard.StandardKeyboardEvent): boolean {
888
		const element = tree.getFocus();
E
Erich Gamma 已提交
889
		if (element instanceof model.Breakpoint) {
890
			const bp = <model.Breakpoint> element;
891
			this.debugService.toggleBreakpoint({ uri: bp.source.uri, lineNumber: bp.lineNumber }).done(null, errors.onUnexpectedError);
892 893 894 895

			return true;
		} else if (element instanceof model.FunctionBreakpoint) {
			const fbp = <model.FunctionBreakpoint> element;
896
			this.debugService.removeFunctionBreakpoints(fbp.getId()).done(null, errors.onUnexpectedError);
E
Erich Gamma 已提交
897 898 899 900 901 902 903

			return true;
		}

		return false;
	}
}