debugViewer.ts 48.5 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');
I
isidor 已提交
7
import { TPromise } from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
8
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
import strings = require('vs/base/common/strings');
import { isMacintosh } from 'vs/base/common/platform';
import dom = require('vs/base/browser/dom');
16
import {IMouseEvent} from 'vs/base/browser/mouseEvent';
E
Erich Gamma 已提交
17 18 19
import labels = require('vs/base/common/labels');
import actions = require('vs/base/common/actions');
import actionbar = require('vs/base/browser/ui/actionbar/actionbar');
J
Joao Moreno 已提交
20
import tree = require('vs/base/parts/tree/browser/tree');
E
Erich Gamma 已提交
21 22 23 24 25 26
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');
27 28
import debugactions = require('vs/workbench/parts/debug/browser/debugActions');
import { CopyValueAction } from 'vs/workbench/parts/debug/electron-browser/electronDebugActions';
E
Erich Gamma 已提交
29 30 31 32
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';
33
import { Source } from 'vs/workbench/parts/debug/common/debugSource';
A
Cleanup  
Alex Dima 已提交
34
import {IKeyboardEvent} from 'vs/base/browser/keyboardEvent';
E
Erich Gamma 已提交
35

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

41
export function renderExpressionValue(expressionOrValue: debug.IExpression | string, container: HTMLElement, showChanged: boolean, maxValueRenderLength?: number): void {
42
	let value = typeof expressionOrValue === 'string' ? expressionOrValue : expressionOrValue.value;
E
Erich Gamma 已提交
43

I
isidor 已提交
44
	// remove stale classes
E
Erich Gamma 已提交
45
	container.className = 'value';
I
isidor 已提交
46
	// when resolving expressions we represent errors from the server as a variable with name === null.
47
	if (value === null || ((expressionOrValue instanceof model.Expression || expressionOrValue instanceof model.Variable) && !expressionOrValue.available)) {
E
Erich Gamma 已提交
48
		dom.addClass(container, 'unavailable');
49 50 51
		if (value !== model.Expression.DEFAULT_VALUE) {
			dom.addClass(container, 'error');
		}
E
Erich Gamma 已提交
52 53 54 55 56 57 58 59
	} 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');
	}

60 61 62 63
	if (showChanged && (<any>expressionOrValue).valueChanged) {
		// value changed color has priority over other colors.
		container.className = 'value changed';
	}
I
isidor 已提交
64

65 66
	if (maxValueRenderLength && value.length > maxValueRenderLength) {
		value = value.substr(0, maxValueRenderLength) + '...';
I
isidor 已提交
67
	}
E
Erich Gamma 已提交
68 69 70 71
	container.textContent = value;
	container.title = value;
}

72
export function renderVariable(tree: tree.ITree, variable: model.Variable, data: IVariableTemplateData, showChanged: boolean): void {
73
	if (variable.available) {
74
		data.name.textContent = variable.name + ':';
75
		data.name.title = variable.type ? variable.type : '';
76 77
	}

E
Erich Gamma 已提交
78
	if (variable.value) {
79
		renderExpressionValue(variable, data.value, showChanged, MAX_VALUE_RENDER_LENGTH_IN_VIEWLET);
80
		data.value.title = variable.value;
E
Erich Gamma 已提交
81 82 83 84 85 86
	} else {
		data.value.textContent = '';
		data.value.title = '';
	}
}

I
isidor 已提交
87 88 89 90 91 92 93 94
interface IRenameBoxOptions {
	initialValue: string;
	ariaLabel: string;
	placeholder?: string;
	validationOptions?: inputbox.IInputValidationOptions;
}

function renderRenameBox(debugService: debug.IDebugService, contextViewService: IContextViewService, tree: tree.ITree, element: any, container: HTMLElement, options: IRenameBoxOptions): void {
95 96
	let inputBoxContainer = dom.append(container, $('.inputBoxContainer'));
	let inputBox = new inputbox.InputBox(inputBoxContainer, contextViewService, {
I
isidor 已提交
97 98 99
		validationOptions: options.validationOptions,
		placeholder: options.placeholder,
		ariaLabel: options.ariaLabel
100 101
	});

I
isidor 已提交
102
	inputBox.value = options.initialValue ? options.initialValue : '';
103 104
	inputBox.focus();

I
isidor 已提交
105 106
	let disposed = false;
	const toDispose: [lifecycle.IDisposable] = [inputBox];
107

J
Joao Moreno 已提交
108
	const wrapUp = async.once((renamed: boolean) => {
109 110
		if (!disposed) {
			disposed = true;
111 112 113
			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) {
114
				debugService.removeWatchExpressions(element.getId());
115 116 117
			} 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) {
118
				debugService.removeFunctionBreakpoints(element.getId()).done(null, errors.onUnexpectedError);
119 120 121 122 123 124 125
			} else if (element instanceof model.Variable) {
				(<model.Variable>element).errorMessage = null;
				if (renamed) {
					debugService.setVariable(element, inputBox.value)
						// if everything went fine we need to refresh that tree element since his value updated
						.done(() => tree.refresh(element, false), errors.onUnexpectedError);
				}
126
			}
127

128 129
			tree.clearHighlight();
			tree.DOMFocus();
130
			tree.setFocus(element);
131

I
isidor 已提交
132
			// need to remove the input box since this template will be reused.
133
			container.removeChild(inputBoxContainer);
J
Joao Moreno 已提交
134
			lifecycle.dispose(toDispose);
135 136 137
		}
	});

A
Cleanup  
Alex Dima 已提交
138
	toDispose.push(dom.addStandardDisposableListener(inputBox.inputElement, 'keydown', (e: IKeyboardEvent) => {
I
isidor 已提交
139 140
		const isEscape = e.equals(CommonKeybindings.ESCAPE);
		const isEnter = e.equals(CommonKeybindings.ENTER);
141 142 143 144 145 146 147 148 149
		if (isEscape || isEnter) {
			wrapUp(isEnter);
		}
	}));
	toDispose.push(dom.addDisposableListener(inputBox.inputElement, 'blur', () => {
		wrapUp(true);
	}));
}

150 151 152 153 154 155 156 157
function getSourceName(source: Source, contextService: IWorkspaceContextService): string {
	if (source.inMemory) {
		return source.name;
	}

	return labels.getPathLabel(paths.basename(source.uri.fsPath), contextService);
}

E
Erich Gamma 已提交
158 159
export class BaseDebugController extends treedefaults.DefaultController {

I
isidor 已提交
160 161 162 163 164 165
	constructor(
		protected debugService: debug.IDebugService,
		private contextMenuService: IContextMenuService,
		private actionProvider: renderer.IActionProvider,
		private focusOnContextMenu = true
	) {
E
Erich Gamma 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
		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 已提交
189
			const anchor = { x: event.posx + 1, y: event.posy };
E
Erich Gamma 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
			this.contextMenuService.showContextMenu({
				getAnchor: () => anchor,
				getActions: () => this.actionProvider.getSecondaryActions(tree, element),
				onHide: (wasCancelled?: boolean) => {
					if (wasCancelled) {
						tree.DOMFocus();
					}
				},
				getActionsContext: () => element
			});

			return true;
		}

		return false;
	}

A
Cleanup  
Alex Dima 已提交
207
	protected onDelete(tree: tree.ITree, event: IKeyboardEvent): boolean {
E
Erich Gamma 已提交
208 209 210 211
		return false;
	}
}

I
isidor 已提交
212
// call stack
E
Erich Gamma 已提交
213

214 215
export class CallStackController extends BaseDebugController {

216
	protected onLeftClick(tree: tree.ITree, element: any, event: IMouseEvent): boolean {
217 218 219
		if (typeof element === 'number') {
			return this.showMoreStackFrames(tree, element);
		}
220 221 222
		if (element instanceof model.StackFrame) {
			this.focusStackFrame(element, event, true);
		}
223 224 225 226 227 228 229 230 231

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

	protected onEnter(tree: tree.ITree, event: IKeyboardEvent): boolean {
		const element = tree.getFocus();
		if (typeof element === 'number') {
			return this.showMoreStackFrames(tree, element);
		}
232 233 234
		if (element instanceof model.StackFrame) {
			this.focusStackFrame(element, event, false);
		}
235 236 237 238

		return super.onEnter(tree, event);
	}

I
isidor 已提交
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
	protected onUp(tree: tree.ITree, event: IKeyboardEvent): boolean {
		super.onUp(tree, event);
		this.focusStackFrame(tree.getFocus(), event, true);

		return true;
	}

	protected onPageUp(tree: tree.ITree, event: IKeyboardEvent): boolean {
		super.onPageUp(tree, event);
		this.focusStackFrame(tree.getFocus(), event, true);

		return true;
	}

	protected onDown(tree: tree.ITree, event: IKeyboardEvent): boolean {
		super.onDown(tree, event);
		this.focusStackFrame(tree.getFocus(), event, true);

		return true;
	}

	protected onPageDown(tree: tree.ITree, event: IKeyboardEvent): boolean {
		super.onPageDown(tree, event);
		this.focusStackFrame(tree.getFocus(), event, true);

		return true;
	}

267 268 269 270 271 272 273 274 275 276
	// user clicked / pressed on 'Load More Stack Frames', get those stack frames and refresh the tree.
	private showMoreStackFrames(tree: tree.ITree, threadId: number): boolean {
		const thread = this.debugService.getModel().getThreads()[threadId];
		if (thread) {
			thread.getCallStack(this.debugService, true)
				.done(() => tree.refresh(), errors.onUnexpectedError);
		}

		return true;
	}
277

I
isidor 已提交
278
	private focusStackFrame(stackFrame: debug.IStackFrame, event: IKeyboardEvent | IMouseEvent, preserveFocus: boolean): void {
279 280 281 282 283
		this.debugService.setFocusedStackFrameAndEvaluate(stackFrame).done(null, errors.onUnexpectedError);

		const sideBySide = (event && (event.ctrlKey || event.metaKey));
		this.debugService.openOrRevealSource(stackFrame.source, stackFrame.lineNumber, preserveFocus, sideBySide).done(null, errors.onUnexpectedError);
	}
284 285 286
}


I
isidor 已提交
287 288
export class CallStackActionProvider implements renderer.IActionProvider {

I
isidor 已提交
289
	constructor(@IInstantiationService private instantiationService: IInstantiationService, @debug.IDebugService private debugService: debug.IDebugService) {
I
isidor 已提交
290 291 292 293 294 295 296 297 298 299 300 301
		// noop
	}

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

	public getActions(tree: tree.ITree, element: any): TPromise<actions.IAction[]> {
		return TPromise.as([]);
	}

	public hasSecondaryActions(tree: tree.ITree, element: any): boolean {
A
Andre Weinand 已提交
302
		return element instanceof model.Thread || element instanceof model.StackFrame;
I
isidor 已提交
303 304 305 306
	}

	public getSecondaryActions(tree: tree.ITree, element: any): TPromise<actions.IAction[]> {
		const actions: actions.Action[] = [];
A
Andre Weinand 已提交
307 308 309 310 311 312 313 314 315 316 317
		if (element instanceof model.Thread) {
			const thread = <model.Thread>element;
			if (thread.stopped) {
				actions.push(this.instantiationService.createInstance(debugactions.ContinueAction, debugactions.ContinueAction.ID, debugactions.ContinueAction.LABEL));
				actions.push(this.instantiationService.createInstance(debugactions.StepOverDebugAction, debugactions.StepOverDebugAction.ID, debugactions.StepOverDebugAction.LABEL));
				actions.push(this.instantiationService.createInstance(debugactions.StepIntoDebugAction, debugactions.StepIntoDebugAction.ID, debugactions.StepIntoDebugAction.LABEL));
				actions.push(this.instantiationService.createInstance(debugactions.StepOutDebugAction, debugactions.StepOutDebugAction.ID, debugactions.StepOutDebugAction.LABEL));
			} else {
				actions.push(this.instantiationService.createInstance(debugactions.PauseAction, debugactions.PauseAction.ID, debugactions.PauseAction.LABEL));
			}
		} else if (element instanceof model.StackFrame) {
I
isidor 已提交
318 319
			const capabilities = this.debugService.getActiveSession().configuration.capabilities;
			if (typeof capabilities.supportsRestartFrame === 'boolean' && capabilities.supportsRestartFrame) {
320 321
				actions.push(this.instantiationService.createInstance(debugactions.RestartFrameAction, debugactions.RestartFrameAction.ID, debugactions.RestartFrameAction.LABEL));
			}
I
isidor 已提交
322 323 324 325 326 327 328 329 330 331
		}

		return TPromise.as(actions);
	}

	public getActionItem(tree: tree.ITree, element: any, action: actions.IAction): actionbar.IActionItem {
		return null;
	}
}

E
Erich Gamma 已提交
332 333
export class CallStackDataSource implements tree.IDataSource {

I
isidor 已提交
334
	constructor(@debug.IDebugService private debugService: debug.IDebugService) {
335 336 337
		// noop
	}

E
Erich Gamma 已提交
338
	public getId(tree: tree.ITree, element: any): string {
I
isidor 已提交
339 340 341
		if (typeof element === 'number') {
			return element.toString();
		}
342 343 344
		if (typeof element === 'string') {
			return element;
		}
I
isidor 已提交
345

E
Erich Gamma 已提交
346 347 348 349
		return element.getId();
	}

	public hasChildren(tree: tree.ITree, element: any): boolean {
I
isidor 已提交
350
		return element instanceof model.Model || (element instanceof model.Thread && (<model.Thread>element).stopped);
E
Erich Gamma 已提交
351 352
	}

I
isidor 已提交
353
	public getChildren(tree: tree.ITree, element: any): TPromise<any> {
E
Erich Gamma 已提交
354
		if (element instanceof model.Thread) {
I
isidor 已提交
355
			return this.getThreadChildren(element);
E
Erich Gamma 已提交
356 357
		}

358 359
		const threads = (<model.Model>element).getThreads();
		return TPromise.as(Object.keys(threads).map(ref => threads[ref]));
E
Erich Gamma 已提交
360 361
	}

I
isidor 已提交
362 363
	private getThreadChildren(thread: debug.IThread): TPromise<any> {
		return thread.getCallStack(this.debugService).then((callStack: any[]) => {
364 365 366
			if (thread.stoppedDetails.framesErrorMessage) {
				return callStack.concat([thread.stoppedDetails.framesErrorMessage]);
			}
I
isidor 已提交
367 368 369 370 371 372 373 374
			if (thread.stoppedDetails && thread.stoppedDetails.totalFrames > callStack.length) {
				return callStack.concat([thread.threadId]);
			}

			return callStack;
		});
	}

I
isidor 已提交
375
	public getParent(tree: tree.ITree, element: any): TPromise<any> {
A
Alex Dima 已提交
376
		return TPromise.as(null);
E
Erich Gamma 已提交
377 378 379 380
	}
}

interface IThreadTemplateData {
I
isidor 已提交
381
	thread: HTMLElement;
E
Erich Gamma 已提交
382
	name: HTMLElement;
I
isidor 已提交
383 384
	state: HTMLElement;
	stateLabel: HTMLSpanElement;
E
Erich Gamma 已提交
385 386
}

387 388 389 390
interface IErrorTemplateData {
	label: HTMLElement;
}

I
isidor 已提交
391 392 393 394
interface ILoadMoreTemplateData {
	label: HTMLElement;
}

E
Erich Gamma 已提交
395 396
interface IStackFrameTemplateData {
	stackFrame: HTMLElement;
397 398 399 400
	label: HTMLElement;
	file: HTMLElement;
	fileName: HTMLElement;
	lineNumber: HTMLElement;
E
Erich Gamma 已提交
401 402 403 404 405 406
}

export class CallStackRenderer implements tree.IRenderer {

	private static THREAD_TEMPLATE_ID = 'thread';
	private static STACK_FRAME_TEMPLATE_ID = 'stackFrame';
407
	private static ERROR_TEMPLATE_ID = 'error';
I
isidor 已提交
408
	private static LOAD_MORE_TEMPLATE_ID = 'loadMore';
E
Erich Gamma 已提交
409

I
isidor 已提交
410
	constructor(@IWorkspaceContextService private contextService: IWorkspaceContextService) {
E
Erich Gamma 已提交
411 412 413
		// noop
	}

414
	public getHeight(tree: tree.ITree, element: any): number {
I
isidor 已提交
415
		return 22;
E
Erich Gamma 已提交
416 417 418 419 420 421 422 423 424
	}

	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;
		}
425 426 427
		if (typeof element === 'string') {
			return CallStackRenderer.ERROR_TEMPLATE_ID;
		}
E
Erich Gamma 已提交
428

I
isidor 已提交
429
		return CallStackRenderer.LOAD_MORE_TEMPLATE_ID;
E
Erich Gamma 已提交
430 431 432
	}

	public renderTemplate(tree: tree.ITree, templateId: string, container: HTMLElement): any {
I
isidor 已提交
433 434 435 436 437 438
		if (templateId === CallStackRenderer.LOAD_MORE_TEMPLATE_ID) {
			let data: ILoadMoreTemplateData = Object.create(null);
			data.label = dom.append(container, $('.load-more'));

			return data;
		}
439 440 441 442 443 444
		if (templateId === CallStackRenderer.ERROR_TEMPLATE_ID) {
			let data: ILoadMoreTemplateData = Object.create(null);
			data.label = dom.append(container, $('.error'));

			return data;
		}
E
Erich Gamma 已提交
445 446
		if (templateId === CallStackRenderer.THREAD_TEMPLATE_ID) {
			let data: IThreadTemplateData = Object.create(null);
I
isidor 已提交
447 448 449 450
			data.thread = dom.append(container, $('.thread'));
			data.name = dom.append(data.thread, $('.name'));
			data.state = dom.append(data.thread, $('.state'));
			data.stateLabel = dom.append(data.state, $('span.label'));
E
Erich Gamma 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467

			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);
I
isidor 已提交
468
		} else if (templateId === CallStackRenderer.STACK_FRAME_TEMPLATE_ID) {
E
Erich Gamma 已提交
469
			this.renderStackFrame(element, templateData);
470 471
		} else if (templateId === CallStackRenderer.ERROR_TEMPLATE_ID) {
			this.renderError(element, templateData);
I
isidor 已提交
472 473
		} else {
			this.renderLoadMore(element, templateData);
E
Erich Gamma 已提交
474 475 476 477
		}
	}

	private renderThread(thread: debug.IThread, data: IThreadTemplateData): void {
I
isidor 已提交
478
		data.thread.title = nls.localize('thread', "Thread");
E
Erich Gamma 已提交
479
		data.name.textContent = thread.name;
I
isidor 已提交
480 481
		data.stateLabel.textContent = thread.stopped ? nls.localize('paused', "paused")
			: nls.localize({ key: 'running', comment: ['indicates state'] }, "running");
E
Erich Gamma 已提交
482 483
	}

484 485
	private renderError(element: string, data: IErrorTemplateData) {
		data.label.textContent = element;
I
isidor 已提交
486
		data.label.title = element;
487 488
	}

I
isidor 已提交
489
	private renderLoadMore(element: any, data: ILoadMoreTemplateData): void {
I
isidor 已提交
490
		data.label.textContent = nls.localize('loadMoreStackFrames', "Load More Stack Frames");
I
isidor 已提交
491 492
	}

E
Erich Gamma 已提交
493 494 495 496
	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;
497
		data.label.title = stackFrame.name;
498
		data.fileName.textContent = getSourceName(stackFrame.source, this.contextService);
I
isidor 已提交
499
		if (stackFrame.lineNumber !== undefined) {
500
			data.lineNumber.textContent = `${stackFrame.lineNumber}`;
I
isidor 已提交
501 502 503 504
			dom.removeClass(data.lineNumber, 'unavailable');
		} else {
			dom.addClass(data.lineNumber, 'unavailable');
		}
E
Erich Gamma 已提交
505 506 507 508 509 510 511
	}

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

512 513
export class CallstackAccessibilityProvider implements tree.IAccessibilityProvider {

I
isidor 已提交
514
	constructor(@IWorkspaceContextService private contextService: IWorkspaceContextService) {
515 516 517 518 519 520 521 522
		// noop
	}

	public getAriaLabel(tree: tree.ITree, element: any): string {
		if (element instanceof model.Thread) {
			return nls.localize('threadAriaLabel', "Thread {0}, callstack, debug", (<model.Thread>element).name);
		}
		if (element instanceof model.StackFrame) {
I
isidor 已提交
523
			return nls.localize('stackFrameAriaLabel', "Stack Frame {0} line {1} {2}, callstack, debug", (<model.StackFrame>element).name, (<model.StackFrame>element).lineNumber, getSourceName((<model.StackFrame>element).source, this.contextService));
524 525 526 527 528 529
		}

		return null;
	}
}

I
isidor 已提交
530
// variables
E
Erich Gamma 已提交
531

I
isidor 已提交
532
export class VariablesActionProvider implements renderer.IActionProvider {
E
Erich Gamma 已提交
533

I
isidor 已提交
534 535
	constructor(private instantiationService: IInstantiationService) {
		// noop
E
Erich Gamma 已提交
536 537 538 539 540 541
	}

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

I
isidor 已提交
542
	public getActions(tree: tree.ITree, element: any): TPromise<actions.IAction[]> {
A
Alex Dima 已提交
543
		return TPromise.as([]);
I
isidor 已提交
544 545
	}

E
Erich Gamma 已提交
546 547 548 549
	public hasSecondaryActions(tree: tree.ITree, element: any): boolean {
		return element instanceof model.Variable;
	}

I
isidor 已提交
550
	public getSecondaryActions(tree: tree.ITree, element: any): TPromise<actions.IAction[]> {
E
Erich Gamma 已提交
551
		let actions: actions.Action[] = [];
552
		const variable = <model.Variable>element;
E
Erich Gamma 已提交
553
		if (variable.reference === 0) {
I
isidor 已提交
554
			actions.push(this.instantiationService.createInstance(debugactions.SetValueAction, debugactions.SetValueAction.ID, debugactions.SetValueAction.LABEL, variable));
555
			actions.push(this.instantiationService.createInstance(CopyValueAction, CopyValueAction.ID, CopyValueAction.LABEL, variable));
I
isidor 已提交
556
			actions.push(new actionbar.Separator());
E
Erich Gamma 已提交
557 558
		}

I
isidor 已提交
559
		actions.push(this.instantiationService.createInstance(debugactions.AddToWatchExpressionsAction, debugactions.AddToWatchExpressionsAction.ID, debugactions.AddToWatchExpressionsAction.LABEL, variable));
A
Alex Dima 已提交
560
		return TPromise.as(actions);
E
Erich Gamma 已提交
561
	}
I
isidor 已提交
562 563 564 565

	public getActionItem(tree: tree.ITree, element: any, action: actions.IAction): actionbar.IActionItem {
		return null;
	}
E
Erich Gamma 已提交
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
}

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;
		}

583
		let variable = <model.Variable>element;
E
Erich Gamma 已提交
584 585 586
		return variable.reference !== 0 && !strings.equalsIgnoreCase(variable.value, 'null');
	}

I
isidor 已提交
587
	public getChildren(tree: tree.ITree, element: any): TPromise<any> {
E
Erich Gamma 已提交
588
		if (element instanceof viewmodel.ViewModel) {
589
			let focusedStackFrame = (<viewmodel.ViewModel>element).getFocusedStackFrame();
A
Alex Dima 已提交
590
			return focusedStackFrame ? focusedStackFrame.getScopes(this.debugService) : TPromise.as([]);
E
Erich Gamma 已提交
591 592
		}

593
		let scope = <model.Scope>element;
E
Erich Gamma 已提交
594 595 596
		return scope.getChildren(this.debugService);
	}

I
isidor 已提交
597
	public getParent(tree: tree.ITree, element: any): TPromise<any> {
A
Alex Dima 已提交
598
		return TPromise.as(null);
E
Erich Gamma 已提交
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
	}
}

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';

I
isidor 已提交
617 618 619 620 621 622 623
	constructor(
		@debug.IDebugService private debugService: debug.IDebugService,
		@IContextViewService private contextViewService: IContextViewService
	) {
		// noop
	}

624
	public getHeight(tree: tree.ITree, element: any): number {
I
isidor 已提交
625
		return 22;
E
Erich Gamma 已提交
626 627 628 629 630 631
	}

	public getTemplateId(tree: tree.ITree, element: any): string {
		if (element instanceof model.Scope) {
			return VariablesRenderer.SCOPE_TEMPLATE_ID;
		}
632
		if (element instanceof model.Variable) {
E
Erich Gamma 已提交
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
			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);
648
		data.expression = dom.append(container, $('.expression'));
E
Erich Gamma 已提交
649 650 651 652 653 654 655 656 657 658
		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 {
659 660 661 662 663 664 665 666
			const variable = <model.Variable>element;
			if (variable === this.debugService.getViewModel().getSelectedExpression() || variable.errorMessage) {
				renderRenameBox(this.debugService, this.contextViewService, tree, variable, (<IVariableTemplateData>templateData).expression, {
					initialValue: variable.value,
					ariaLabel: nls.localize('variableValueAriaLabel', "Type new variable value"),
					validationOptions: {
						validation: (value: string) => variable.errorMessage ? ({ content: variable.errorMessage }) : null
					}
I
isidor 已提交
667
				});
I
isidor 已提交
668
			} else {
669
				renderVariable(tree, variable, templateData, true);
I
isidor 已提交
670
			}
E
Erich Gamma 已提交
671 672 673 674 675 676 677 678 679 680 681 682
		}
	}

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

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

683 684 685 686 687 688 689
export class VariablesAccessibilityProvider implements tree.IAccessibilityProvider {

	public getAriaLabel(tree: tree.ITree, element: any): string {
		if (element instanceof model.Scope) {
			return nls.localize('variableScopeAriaLabel', "Scope {0}, variables, debug", (<model.Scope>element).name);
		}
		if (element instanceof model.Variable) {
I
isidor 已提交
690
			return nls.localize('variableAriaLabel', "{0} value {1}, variables, debug", (<model.Variable>element).name, (<model.Variable>element).value);
691 692 693 694 695 696
		}

		return null;
	}
}

697 698
export class VariablesController extends BaseDebugController {

699 700 701 702 703
	constructor(debugService: debug.IDebugService, contextMenuService: IContextMenuService, actionProvider: renderer.IActionProvider) {
		super(debugService, contextMenuService, actionProvider);
		this.downKeyBindingDispatcher.set(CommonKeybindings.ENTER, this.setSelectedExpression.bind(this));
	}

704 705 706 707 708 709 710 711 712 713 714 715
	protected onLeftClick(tree: tree.ITree, element: any, event: IMouseEvent): boolean {
		// double click on primitive value: open input box to be able to set the value
		if (element instanceof model.Variable && event.detail === 2) {
			const expression = <debug.IExpression>element;
			if (expression.reference === 0) {
				this.debugService.getViewModel().setSelectedExpression(expression);
			}
			return true;
		}

		return super.onLeftClick(tree, element, event);
	}
I
isidor 已提交
716

717
	protected setSelectedExpression(tree: tree.ITree, event: KeyboardEvent): boolean {
I
isidor 已提交
718
		const element = tree.getFocus();
719
		if (element instanceof model.Variable && element.reference === 0) {
I
isidor 已提交
720
			this.debugService.getViewModel().setSelectedExpression(element);
721
			return true;
I
isidor 已提交
722 723
		}

724
		return false;
I
isidor 已提交
725
	}
726 727
}

I
isidor 已提交
728
// watch expressions
E
Erich Gamma 已提交
729

I
isidor 已提交
730
export class WatchExpressionsActionProvider implements renderer.IActionProvider {
E
Erich Gamma 已提交
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745

	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;
	}

I
isidor 已提交
746
	public getActions(tree: tree.ITree, element: any): TPromise<actions.IAction[]> {
A
Alex Dima 已提交
747
		return TPromise.as(this.getExpressionActions());
E
Erich Gamma 已提交
748 749 750
	}

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

I
isidor 已提交
754
	public getSecondaryActions(tree: tree.ITree, element: any): TPromise<actions.IAction[]> {
I
isidor 已提交
755
		const actions: actions.Action[] = [];
E
Erich Gamma 已提交
756
		if (element instanceof model.Expression) {
757
			const expression = <model.Expression>element;
I
isidor 已提交
758 759
			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 已提交
760
			if (expression.reference === 0) {
761
				actions.push(this.instantiationService.createInstance(CopyValueAction, CopyValueAction.ID, CopyValueAction.LABEL, expression.value));
E
Erich Gamma 已提交
762 763 764
			}
			actions.push(new actionbar.Separator());

I
isidor 已提交
765 766
			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 已提交
767
		} else {
I
isidor 已提交
768
			actions.push(this.instantiationService.createInstance(debugactions.AddWatchExpressionAction, debugactions.AddWatchExpressionAction.ID, debugactions.AddWatchExpressionAction.LABEL));
E
Erich Gamma 已提交
769
			if (element instanceof model.Variable) {
770
				const variable = <model.Variable>element;
E
Erich Gamma 已提交
771
				if (variable.reference === 0) {
772
					actions.push(this.instantiationService.createInstance(CopyValueAction, CopyValueAction.ID, CopyValueAction.LABEL, variable.value));
E
Erich Gamma 已提交
773 774 775
				}
				actions.push(new actionbar.Separator());
			}
I
isidor 已提交
776
			actions.push(this.instantiationService.createInstance(debugactions.RemoveAllWatchExpressionsAction, debugactions.RemoveAllWatchExpressionsAction.ID, debugactions.RemoveAllWatchExpressionsAction.LABEL));
E
Erich Gamma 已提交
777 778
		}

A
Alex Dima 已提交
779
		return TPromise.as(actions);
E
Erich Gamma 已提交
780
	}
I
isidor 已提交
781 782 783 784

	public getActionItem(tree: tree.ITree, element: any, action: actions.IAction): actionbar.IActionItem {
		return null;
	}
E
Erich Gamma 已提交
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
}

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;
		}

802
		const watchExpression = <model.Expression>element;
E
Erich Gamma 已提交
803 804 805
		return watchExpression.reference !== 0 && !strings.equalsIgnoreCase(watchExpression.value, 'null');
	}

I
isidor 已提交
806
	public getChildren(tree: tree.ITree, element: any): TPromise<any> {
E
Erich Gamma 已提交
807
		if (element instanceof model.Model) {
808
			return TPromise.as((<model.Model>element).getWatchExpressions());
E
Erich Gamma 已提交
809 810
		}

811
		let expression = <model.Expression>element;
E
Erich Gamma 已提交
812 813 814
		return expression.getChildren(this.debugService);
	}

I
isidor 已提交
815
	public getParent(tree: tree.ITree, element: any): TPromise<any> {
A
Alex Dima 已提交
816
		return TPromise.as(null);
E
Erich Gamma 已提交
817 818 819 820 821 822 823 824 825 826 827 828 829 830
	}
}

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;

I
isidor 已提交
831 832 833
	constructor(
		actionProvider: renderer.IActionProvider,
		private actionRunner: actions.IActionRunner,
E
Erich Gamma 已提交
834 835 836 837 838
		@IMessageService private messageService: IMessageService,
		@debug.IDebugService private debugService: debug.IDebugService,
		@IContextViewService private contextViewService: IContextViewService
	) {
		this.toDispose = [];
839
		this.actionProvider = <WatchExpressionsActionProvider>actionProvider;
E
Erich Gamma 已提交
840 841
	}

842
	public getHeight(tree: tree.ITree, element: any): number {
I
isidor 已提交
843
		return 22;
E
Erich Gamma 已提交
844 845 846 847 848 849 850 851 852 853 854 855 856 857
	}

	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 });
858
			data.actionBar.push(this.actionProvider.getExpressionActions(), { icon: true, label: false });
E
Erich Gamma 已提交
859 860
		}

861
		data.expression = dom.append(container, $('.expression'));
E
Erich Gamma 已提交
862 863 864 865 866 867 868 869 870 871
		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 {
872
			renderVariable(tree, element, templateData, true);
E
Erich Gamma 已提交
873 874 875 876 877 878
		}
	}

	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)) {
I
isidor 已提交
879 880 881 882 883
			renderRenameBox(this.debugService, this.contextViewService, tree, watchExpression, data.expression, {
				initialValue: watchExpression.name,
				placeholder: nls.localize('watchExpressionPlaceholder', "Expression to watch"),
				ariaLabel: nls.localize('watchExpressionInputAriaLabel', "Type watch expression")
			});
E
Erich Gamma 已提交
884 885 886
		}
		data.actionBar.context = watchExpression;

887 888
		data.name.textContent = `${watchExpression.name}:`;
		if (watchExpression.value) {
889
			renderExpressionValue(watchExpression, data.value, true, MAX_VALUE_RENDER_LENGTH_IN_VIEWLET);
890
			data.expression.title = watchExpression.value;
E
Erich Gamma 已提交
891 892 893 894
		}
	}

	public disposeTemplate(tree: tree.ITree, templateId: string, templateData: any): void {
895 896 897
		if (templateId === WatchExpressionsRenderer.WATCH_EXPRESSION_TEMPLATE_ID) {
			(<IWatchExpressionTemplateData>templateData).actionBar.dispose();
		}
E
Erich Gamma 已提交
898 899 900
	}

	public dispose(): void {
J
Joao Moreno 已提交
901
		this.toDispose = lifecycle.dispose(this.toDispose);
E
Erich Gamma 已提交
902 903 904
	}
}

905 906 907 908
export class WatchExpressionsAccessibilityProvider implements tree.IAccessibilityProvider {

	public getAriaLabel(tree: tree.ITree, element: any): string {
		if (element instanceof model.Expression) {
I
isidor 已提交
909
			return nls.localize('watchExpressionAriaLabel', "{0} value {1}, watch, debug", (<model.Expression>element).name, (<model.Expression>element).value);
910 911
		}
		if (element instanceof model.Variable) {
I
isidor 已提交
912
			return nls.localize('watchVariableAriaLabel', "{0} value {1}, watch, debug", (<model.Variable>element).name, (<model.Variable>element).value);
913 914 915 916 917 918
		}

		return null;
	}
}

E
Erich Gamma 已提交
919 920 921 922 923 924 925 926 927 928 929 930
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));
		}
	}

931
	protected onLeftClick(tree: tree.ITree, element: any, event: IMouseEvent): boolean {
I
isidor 已提交
932
		// double click on primitive value: open input box to be able to select and copy value.
E
Erich Gamma 已提交
933
		if (element instanceof model.Expression && event.detail === 2) {
934
			const expression = <debug.IExpression>element;
E
Erich Gamma 已提交
935 936 937 938 939 940 941 942 943 944
			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 已提交
945
		const element = tree.getFocus();
E
Erich Gamma 已提交
946
		if (element instanceof model.Expression) {
947
			const watchExpression = <model.Expression>element;
E
Erich Gamma 已提交
948 949 950 951 952 953 954 955 956
			if (watchExpression.reference === 0) {
				this.debugService.getViewModel().setSelectedExpression(watchExpression);
			}
			return true;
		}

		return false;
	}

A
Cleanup  
Alex Dima 已提交
957
	protected onDelete(tree: tree.ITree, event: IKeyboardEvent): boolean {
I
isidor 已提交
958
		const element = tree.getFocus();
E
Erich Gamma 已提交
959
		if (element instanceof model.Expression) {
960
			const we = <model.Expression>element;
961
			this.debugService.removeWatchExpressions(we.getId());
E
Erich Gamma 已提交
962 963 964 965 966 967 968 969

			return true;
		}

		return false;
	}
}

I
isidor 已提交
970
// breakpoints
E
Erich Gamma 已提交
971

I
isidor 已提交
972
export class BreakpointsActionProvider implements renderer.IActionProvider {
E
Erich Gamma 已提交
973 974

	constructor(private instantiationService: IInstantiationService) {
I
isidor 已提交
975
		// noop
E
Erich Gamma 已提交
976 977 978 979 980 981 982
	}

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

	public hasSecondaryActions(tree: tree.ITree, element: any): boolean {
983
		return element instanceof model.Breakpoint || element instanceof model.ExceptionBreakpoint || element instanceof model.FunctionBreakpoint;
E
Erich Gamma 已提交
984 985 986 987
	}

	public getActions(tree: tree.ITree, element: any): TPromise<actions.IAction[]> {
		if (element instanceof model.Breakpoint) {
A
Alex Dima 已提交
988
			return TPromise.as(this.getBreakpointActions());
E
Erich Gamma 已提交
989 990
		}

A
Alex Dima 已提交
991
		return TPromise.as([]);
E
Erich Gamma 已提交
992 993 994
	}

	public getBreakpointActions(): actions.IAction[] {
I
isidor 已提交
995
		return [this.instantiationService.createInstance(debugactions.RemoveBreakpointAction, debugactions.RemoveBreakpointAction.ID, debugactions.RemoveBreakpointAction.LABEL)];
E
Erich Gamma 已提交
996 997 998
	}

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

1002 1003 1004
		if (element instanceof model.Breakpoint || element instanceof model.FunctionBreakpoint) {
			actions.push(this.instantiationService.createInstance(debugactions.RemoveBreakpointAction, debugactions.RemoveBreakpointAction.ID, debugactions.RemoveBreakpointAction.LABEL));
		}
I
isidor 已提交
1005
		actions.push(this.instantiationService.createInstance(debugactions.RemoveAllBreakpointsAction, debugactions.RemoveAllBreakpointsAction.ID, debugactions.RemoveAllBreakpointsAction.LABEL));
E
Erich Gamma 已提交
1006 1007
		actions.push(new actionbar.Separator());

1008
		actions.push(this.instantiationService.createInstance(debugactions.ToggleBreakpointsActivatedAction, debugactions.ToggleBreakpointsActivatedAction.ID, debugactions.ToggleBreakpointsActivatedAction.ACTIVATE_LABEL));
E
Erich Gamma 已提交
1009 1010
		actions.push(new actionbar.Separator());

I
isidor 已提交
1011 1012
		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 已提交
1013 1014
		actions.push(new actionbar.Separator());

I
isidor 已提交
1015
		actions.push(this.instantiationService.createInstance(debugactions.AddFunctionBreakpointAction, debugactions.AddFunctionBreakpointAction.ID, debugactions.AddFunctionBreakpointAction.LABEL));
1016 1017 1018
		if (element instanceof model.FunctionBreakpoint) {
			actions.push(this.instantiationService.createInstance(debugactions.RenameFunctionBreakpointAction, debugactions.RenameFunctionBreakpointAction.ID, debugactions.RenameFunctionBreakpointAction.LABEL));
		}
I
isidor 已提交
1019
		actions.push(new actionbar.Separator());
I
isidor 已提交
1020

I
isidor 已提交
1021
		actions.push(this.instantiationService.createInstance(debugactions.ReapplyBreakpointsAction, debugactions.ReapplyBreakpointsAction.ID, debugactions.ReapplyBreakpointsAction.LABEL));
E
Erich Gamma 已提交
1022

A
Alex Dima 已提交
1023
		return TPromise.as(actions);
E
Erich Gamma 已提交
1024
	}
I
isidor 已提交
1025 1026 1027 1028

	public getActionItem(tree: tree.ITree, element: any, action: actions.IAction): actionbar.IActionItem {
		return null;
	}
E
Erich Gamma 已提交
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
}

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;
	}

I
isidor 已提交
1041
	public getChildren(tree: tree.ITree, element: any): TPromise<any> {
1042 1043
		const model = <model.Model>element;
		const exBreakpoints = <debug.IEnablement[]>model.getExceptionBreakpoints();
E
Erich Gamma 已提交
1044

A
Alex Dima 已提交
1045
		return TPromise.as(exBreakpoints.concat(model.getFunctionBreakpoints()).concat(model.getBreakpoints()));
E
Erich Gamma 已提交
1046 1047
	}

I
isidor 已提交
1048
	public getParent(tree: tree.ITree, element: any): TPromise<any> {
A
Alex Dima 已提交
1049
		return TPromise.as(null);
E
Erich Gamma 已提交
1050 1051 1052 1053
	}
}

interface IExceptionBreakpointTemplateData {
1054
	breakpoint: HTMLElement;
E
Erich Gamma 已提交
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
	name: HTMLElement;
	checkbox: HTMLInputElement;
	toDisposeBeforeRender: lifecycle.IDisposable[];
}

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

1066 1067 1068 1069
interface IFunctionBreakpointTemplateData extends IExceptionBreakpointTemplateData {
	actionBar: actionbar.ActionBar;
}

E
Erich Gamma 已提交
1070 1071 1072
export class BreakpointsRenderer implements tree.IRenderer {

	private static EXCEPTION_BREAKPOINT_TEMPLATE_ID = 'exceptionBreakpoint';
1073
	private static FUNCTION_BREAKPOINT_TEMPLATE_ID = 'functionBreakpoint';
E
Erich Gamma 已提交
1074 1075 1076 1077 1078 1079 1080
	private static BREAKPOINT_TEMPLATE_ID = 'breakpoint';

	constructor(
		private actionProvider: BreakpointsActionProvider,
		private actionRunner: actions.IActionRunner,
		@IMessageService private messageService: IMessageService,
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
1081 1082
		@debug.IDebugService private debugService: debug.IDebugService,
		@IContextViewService private contextViewService: IContextViewService
E
Erich Gamma 已提交
1083 1084 1085 1086
	) {
		// noop
	}

1087
	public getHeight(tree: tree.ITree, element: any): number {
I
isidor 已提交
1088
		return 22;
E
Erich Gamma 已提交
1089 1090 1091 1092 1093 1094
	}

	public getTemplateId(tree: tree.ITree, element: any): string {
		if (element instanceof model.Breakpoint) {
			return BreakpointsRenderer.BREAKPOINT_TEMPLATE_ID;
		}
1095 1096 1097
		if (element instanceof model.FunctionBreakpoint) {
			return BreakpointsRenderer.FUNCTION_BREAKPOINT_TEMPLATE_ID;
		}
E
Erich Gamma 已提交
1098 1099 1100 1101 1102 1103 1104 1105
		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 已提交
1106
		const data: IBreakpointTemplateData = Object.create(null);
1107
		if (templateId === BreakpointsRenderer.BREAKPOINT_TEMPLATE_ID || templateId === BreakpointsRenderer.FUNCTION_BREAKPOINT_TEMPLATE_ID) {
E
Erich Gamma 已提交
1108 1109 1110 1111
			data.actionBar = new actionbar.ActionBar(container, { actionRunner: this.actionRunner });
			data.actionBar.push(this.actionProvider.getBreakpointActions(), { icon: true, label: false });
		}

1112
		data.breakpoint = dom.append(container, $('.breakpoint'));
E
Erich Gamma 已提交
1113 1114
		data.toDisposeBeforeRender = [];

1115
		data.checkbox = <HTMLInputElement>$('input');
E
Erich Gamma 已提交
1116
		data.checkbox.type = 'checkbox';
1117

1118
		dom.append(data.breakpoint, data.checkbox);
E
Erich Gamma 已提交
1119

1120
		data.name = dom.append(data.breakpoint, $('span.name'));
E
Erich Gamma 已提交
1121 1122

		if (templateId === BreakpointsRenderer.BREAKPOINT_TEMPLATE_ID) {
1123 1124
			data.lineNumber = dom.append(data.breakpoint, $('span.line-number'));
			data.filePath = dom.append(data.breakpoint, $('span.file-path'));
E
Erich Gamma 已提交
1125 1126 1127 1128 1129 1130
		}

		return data;
	}

	public renderElement(tree: tree.ITree, element: any, templateId: string, templateData: any): void {
J
Joao Moreno 已提交
1131
		templateData.toDisposeBeforeRender = lifecycle.dispose(templateData.toDisposeBeforeRender);
1132
		templateData.toDisposeBeforeRender.push(dom.addStandardDisposableListener(templateData.checkbox, 'change', (e) => {
1133
			this.debugService.enableOrDisableBreakpoints(!element.enabled, element);
1134 1135
		}));

E
Erich Gamma 已提交
1136 1137
		if (templateId === BreakpointsRenderer.EXCEPTION_BREAKPOINT_TEMPLATE_ID) {
			this.renderExceptionBreakpoint(element, templateData);
1138 1139
		} else if (templateId === BreakpointsRenderer.FUNCTION_BREAKPOINT_TEMPLATE_ID) {
			this.renderFunctionBreakpoint(tree, element, templateData);
E
Erich Gamma 已提交
1140 1141 1142 1143 1144 1145
		} else {
			this.renderBreakpoint(tree, element, templateData);
		}
	}

	private renderExceptionBreakpoint(exceptionBreakpoint: debug.IExceptionBreakpoint, data: IExceptionBreakpointTemplateData): void {
1146
		data.name.textContent = exceptionBreakpoint.label || `${exceptionBreakpoint.filter} exceptions`;;
1147
		data.breakpoint.title = data.name.textContent;
E
Erich Gamma 已提交
1148
		data.checkbox.checked = exceptionBreakpoint.enabled;
1149
	}
E
Erich Gamma 已提交
1150

1151
	private renderFunctionBreakpoint(tree: tree.ITree, functionBreakpoint: debug.IFunctionBreakpoint, data: IFunctionBreakpointTemplateData): void {
I
isidor 已提交
1152 1153
		const selected = this.debugService.getViewModel().getSelectedFunctionBreakpoint();
		if (!functionBreakpoint.name || (selected && selected.getId() === functionBreakpoint.getId())) {
I
isidor 已提交
1154 1155 1156 1157 1158
			renderRenameBox(this.debugService, this.contextViewService, tree, functionBreakpoint, data.breakpoint, {
				initialValue: functionBreakpoint.name,
				placeholder: nls.localize('functionBreakpointPlaceholder', "Function to break on"),
				ariaLabel: nls.localize('functionBreakPointInputAriaLabel', "Type function breakpoint")
			});
1159 1160 1161
		} else {
			data.name.textContent = functionBreakpoint.name;
			data.checkbox.checked = functionBreakpoint.enabled;
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
			data.breakpoint.title = functionBreakpoint.name;

			// Mark function breakpoints as disabled if deactivated or if debug type does not support them #9099
			const session = this.debugService.getActiveSession();
			if ((session && !session.configuration.capabilities.supportsFunctionBreakpoints) || !this.debugService.getModel().areBreakpointsActivated()) {
				tree.addTraits('disabled', [functionBreakpoint]);
				if (session && !session.configuration.capabilities.supportsFunctionBreakpoints) {
					data.breakpoint.title = nls.localize('functionBreakpointsNotSupported', "Function breakpoints are not supported by this debug type");
				}
			} else {
				tree.removeTraits('disabled', [functionBreakpoint]);
			}
1174
		}
1175
		data.actionBar.context = functionBreakpoint;
E
Erich Gamma 已提交
1176 1177 1178
	}

	private renderBreakpoint(tree: tree.ITree, breakpoint: debug.IBreakpoint, data: IBreakpointTemplateData): void {
1179
		this.debugService.getModel().areBreakpointsActivated() ? tree.removeTraits('disabled', [breakpoint]) : tree.addTraits('disabled', [breakpoint]);
E
Erich Gamma 已提交
1180 1181 1182 1183 1184 1185

		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;
I
isidor 已提交
1186

I
isidor 已提交
1187
		const debugActive = this.debugService.state === debug.State.Running || this.debugService.state === debug.State.Stopped || this.debugService.state === debug.State.Initializing;
I
isidor 已提交
1188 1189 1190 1191 1192 1193
		if (debugActive && !breakpoint.verified) {
			tree.addTraits('disabled', [breakpoint]);
			if (breakpoint.message) {
				data.breakpoint.title = breakpoint.message;
			}
		} else if (breakpoint.condition) {
1194 1195
			data.breakpoint.title = breakpoint.condition;
		}
E
Erich Gamma 已提交
1196 1197 1198
	}

	public disposeTemplate(tree: tree.ITree, templateId: string, templateData: any): void {
1199
		if (templateId === BreakpointsRenderer.BREAKPOINT_TEMPLATE_ID || templateId === BreakpointsRenderer.FUNCTION_BREAKPOINT_TEMPLATE_ID) {
E
Erich Gamma 已提交
1200 1201 1202 1203 1204
			templateData.actionBar.dispose();
		}
	}
}

1205 1206
export class BreakpointsAccessibilityProvider implements tree.IAccessibilityProvider {

I
isidor 已提交
1207
	constructor(@IWorkspaceContextService private contextService: IWorkspaceContextService) {
1208 1209 1210 1211 1212
		// noop
	}

	public getAriaLabel(tree: tree.ITree, element: any): string {
		if (element instanceof model.Breakpoint) {
I
isidor 已提交
1213
			return nls.localize('breakpointAriaLabel', "Breakpoint line {0} {1}, breakpoints, debug", (<model.Breakpoint>element).lineNumber, getSourceName((<model.Breakpoint>element).source, this.contextService));
1214 1215
		}
		if (element instanceof model.FunctionBreakpoint) {
I
isidor 已提交
1216
			return nls.localize('functionBreakpointAriaLabel', "Function breakpoint {0}, breakpoints, debug", (<model.FunctionBreakpoint>element).name);
1217 1218
		}
		if (element instanceof model.ExceptionBreakpoint) {
1219
			return nls.localize('exceptionBreakpointAriaLabel', "Exception breakpoint {0}, breakpoints, debug", (<model.ExceptionBreakpoint>element).filter);
1220 1221 1222 1223 1224 1225
		}

		return null;
	}
}

E
Erich Gamma 已提交
1226 1227
export class BreakpointsController extends BaseDebugController {

I
isidor 已提交
1228 1229 1230 1231 1232 1233 1234 1235 1236
	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));
		}
	}

1237
	protected onLeftClick(tree: tree.ITree, element: any, event: IMouseEvent): boolean {
I
isidor 已提交
1238 1239 1240 1241
		if (element instanceof model.FunctionBreakpoint && event.detail === 2) {
			this.debugService.getViewModel().setSelectedFunctionBreakpoint(element);
			return true;
		}
1242 1243 1244
		if (element instanceof model.Breakpoint) {
			this.openBreakpointSource(element, event, true);
		}
I
isidor 已提交
1245 1246

		return super.onLeftClick(tree, element, event);
E
Erich Gamma 已提交
1247 1248
	}

I
isidor 已提交
1249
	protected onRename(tree: tree.ITree, event: IKeyboardEvent): boolean {
1250
		const element = tree.getFocus();
I
isidor 已提交
1251
		if (element instanceof model.FunctionBreakpoint && element.name) {
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
			this.debugService.getViewModel().setSelectedFunctionBreakpoint(element);
			return true;
		}
		if (element instanceof model.Breakpoint) {
			this.openBreakpointSource(element, event, false);
		}

		return super.onEnter(tree, event);
	}

1262 1263 1264
	protected onSpace(tree: tree.ITree, event: IKeyboardEvent): boolean {
		super.onSpace(tree, event);
		const element = <debug.IEnablement>tree.getFocus();
1265
		this.debugService.enableOrDisableBreakpoints(!element.enabled, element).done(null, errors.onUnexpectedError);
1266 1267 1268 1269

		return true;
	}

A
Cleanup  
Alex Dima 已提交
1270
	protected onDelete(tree: tree.ITree, event: IKeyboardEvent): boolean {
1271
		const element = tree.getFocus();
E
Erich Gamma 已提交
1272
		if (element instanceof model.Breakpoint) {
1273
			this.debugService.removeBreakpoints((<model.Breakpoint>element).getId()).done(null, errors.onUnexpectedError);
1274 1275
			return true;
		} else if (element instanceof model.FunctionBreakpoint) {
1276
			const fbp = <model.FunctionBreakpoint>element;
1277
			this.debugService.removeFunctionBreakpoints(fbp.getId()).done(null, errors.onUnexpectedError);
E
Erich Gamma 已提交
1278 1279 1280 1281 1282 1283

			return true;
		}

		return false;
	}
1284

I
isidor 已提交
1285
	private openBreakpointSource(breakpoint: debug.IBreakpoint, event: IKeyboardEvent | IMouseEvent, preserveFocus: boolean): void {
1286 1287 1288 1289 1290
		if (!breakpoint.source.inMemory) {
			const sideBySide = (event && (event.ctrlKey || event.metaKey));
			this.debugService.openOrRevealSource(breakpoint.source, breakpoint.lineNumber, preserveFocus, sideBySide).done(null, errors.onUnexpectedError);
		}
	}
E
Erich Gamma 已提交
1291
}