debugViewer.ts 48.4 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');
A
Alexandru Dima 已提交
9
import {KeyCode, KeyMod} 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
import strings = require('vs/base/common/strings');
I
isidor 已提交
14
import {isMacintosh} from 'vs/base/common/platform';
E
Erich Gamma 已提交
15
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');
21
import {InputBox, IInputValidationOptions} from 'vs/base/browser/ui/inputbox/inputBox';
E
Erich Gamma 已提交
22 23 24 25 26
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
import debugactions = require('vs/workbench/parts/debug/browser/debugActions');
I
isidor 已提交
28 29 30 31 32
import {CopyValueAction} from 'vs/workbench/parts/debug/electron-browser/electronDebugActions';
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 {Source} from 'vs/workbench/parts/debug/common/debugSource';
A
Cleanup  
Alex Dima 已提交
33
import {IKeyboardEvent} from 'vs/base/browser/keyboardEvent';
E
Erich Gamma 已提交
34

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

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

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

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

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

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

E
Erich Gamma 已提交
77
	if (variable.value) {
78
		data.name.textContent += ':';
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
interface IRenameBoxOptions {
	initialValue: string;
	ariaLabel: string;
	placeholder?: string;
91
	validationOptions?: IInputValidationOptions;
I
isidor 已提交
92 93 94
}

function renderRenameBox(debugService: debug.IDebugService, contextViewService: IContextViewService, tree: tree.ITree, element: any, container: HTMLElement, options: IRenameBoxOptions): void {
95
	let inputBoxContainer = dom.append(container, $('.inputBoxContainer'));
96
	let inputBox = new 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
	inputBox.focus();
104
	inputBox.select();
105

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

J
Joao Moreno 已提交
109
	const wrapUp = async.once((renamed: boolean) => {
110 111
		if (!disposed) {
			disposed = true;
112 113 114
			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) {
115
				debugService.removeWatchExpressions(element.getId());
116 117 118
			} 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) {
119
				debugService.removeFunctionBreakpoints(element.getId()).done(null, errors.onUnexpectedError);
120 121
			} else if (element instanceof model.Variable) {
				(<model.Variable>element).errorMessage = null;
122
				if (renamed && element.value !== inputBox.value) {
123 124 125 126
					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);
				}
127
			}
128

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

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

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

151 152 153 154 155 156 157 158
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 已提交
159 160
export class BaseDebugController extends treedefaults.DefaultController {

I
isidor 已提交
161 162 163 164 165 166
	constructor(
		protected debugService: debug.IDebugService,
		private contextMenuService: IContextMenuService,
		private actionProvider: renderer.IActionProvider,
		private focusOnContextMenu = true
	) {
E
Erich Gamma 已提交
167 168 169
		super();

		if (isMacintosh) {
A
Alexandru Dima 已提交
170
			this.downKeyBindingDispatcher.set(KeyMod.CtrlCmd | KeyCode.Backspace, this.onDelete.bind(this));
E
Erich Gamma 已提交
171
		} else {
A
Alexandru Dima 已提交
172 173
			this.downKeyBindingDispatcher.set(KeyCode.Delete, this.onDelete.bind(this));
			this.downKeyBindingDispatcher.set(KeyMod.Shift | KeyCode.Delete, this.onDelete.bind(this));
E
Erich Gamma 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
		}
	}

	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 已提交
190
			const anchor = { x: event.posx + 1, y: event.posy };
E
Erich Gamma 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
			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 已提交
208
	protected onDelete(tree: tree.ITree, event: IKeyboardEvent): boolean {
E
Erich Gamma 已提交
209 210 211 212
		return false;
	}
}

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

215 216
export class CallStackController extends BaseDebugController {

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

		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);
		}
233 234 235
		if (element instanceof model.StackFrame) {
			this.focusStackFrame(element, event, false);
		}
236 237 238 239

		return super.onEnter(tree, event);
	}

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

268 269 270 271 272 273 274 275 276 277
	// 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;
	}
278

I
isidor 已提交
279
	private focusStackFrame(stackFrame: debug.IStackFrame, event: IKeyboardEvent | IMouseEvent, preserveFocus: boolean): void {
280 281 282 283 284
		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);
	}
285 286 287
}


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

I
isidor 已提交
290
	constructor(@IInstantiationService private instantiationService: IInstantiationService, @debug.IDebugService private debugService: debug.IDebugService) {
I
isidor 已提交
291 292 293 294 295 296 297 298 299 300 301 302
		// 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 已提交
303
		return element instanceof model.Thread || element instanceof model.StackFrame;
I
isidor 已提交
304 305 306 307
	}

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

		return TPromise.as(actions);
	}

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

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

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

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

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

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

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

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

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

			return callStack;
		});
	}

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

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

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

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

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

export class CallStackRenderer implements tree.IRenderer {

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

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

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

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

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

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

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

			return data;
		}
E
Erich Gamma 已提交
446 447
		if (templateId === CallStackRenderer.THREAD_TEMPLATE_ID) {
			let data: IThreadTemplateData = Object.create(null);
I
isidor 已提交
448 449 450 451
			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 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468

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

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

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

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

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

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

513 514
export class CallstackAccessibilityProvider implements tree.IAccessibilityProvider {

I
isidor 已提交
515
	constructor(@IWorkspaceContextService private contextService: IWorkspaceContextService) {
516 517 518 519 520 521 522 523
		// 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 已提交
524
			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));
525 526 527 528 529 530
		}

		return null;
	}
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 已提交
618 619 620 621 622 623 624
	constructor(
		@debug.IDebugService private debugService: debug.IDebugService,
		@IContextViewService private contextViewService: IContextViewService
	) {
		// noop
	}

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

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

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

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

684 685 686 687 688 689 690
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 已提交
691
			return nls.localize('variableAriaLabel', "{0} value {1}, variables, debug", (<model.Variable>element).name, (<model.Variable>element).value);
692 693 694 695 696 697
		}

		return null;
	}
}

698 699
export class VariablesController extends BaseDebugController {

700 701
	constructor(debugService: debug.IDebugService, contextMenuService: IContextMenuService, actionProvider: renderer.IActionProvider) {
		super(debugService, contextMenuService, actionProvider);
A
Alexandru Dima 已提交
702
		this.downKeyBindingDispatcher.set(KeyCode.Enter, this.setSelectedExpression.bind(this));
703 704
	}

705 706 707 708 709 710 711 712 713 714 715 716
	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 已提交
717

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

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

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

I
isidor 已提交
731
export class WatchExpressionsActionProvider implements renderer.IActionProvider {
E
Erich Gamma 已提交
732 733 734 735 736 737 738 739

	private instantiationService: IInstantiationService;

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

	public hasActions(tree: tree.ITree, element: any): boolean {
D
Dirk Baeumer 已提交
740
		return element instanceof model.Expression && !!element.name;
E
Erich Gamma 已提交
741 742 743 744 745 746
	}

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

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

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

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

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

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

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

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

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

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

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

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

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 已提交
832 833 834
	constructor(
		actionProvider: renderer.IActionProvider,
		private actionRunner: actions.IActionRunner,
E
Erich Gamma 已提交
835 836 837 838
		@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
		data.name.textContent = watchExpression.name;
888
		if (watchExpression.value) {
889
			data.name.textContent += ':';
890
			renderExpressionValue(watchExpression, data.value, true, MAX_VALUE_RENDER_LENGTH_IN_VIEWLET);
891
			data.name.title = watchExpression.type ? watchExpression.type : watchExpression.value;
E
Erich Gamma 已提交
892 893 894 895
		}
	}

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

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

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

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

		return null;
	}
}

E
Erich Gamma 已提交
920 921 922 923 924 925
export class WatchExpressionsController extends BaseDebugController {

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

		if (isMacintosh) {
A
Alexandru Dima 已提交
926
			this.downKeyBindingDispatcher.set(KeyCode.Enter, this.onRename.bind(this));
E
Erich Gamma 已提交
927
		} else {
A
Alexandru Dima 已提交
928
			this.downKeyBindingDispatcher.set(KeyCode.F2, this.onRename.bind(this));
E
Erich Gamma 已提交
929 930 931
		}
	}

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

		return false;
	}

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

			return true;
		}

		return false;
	}
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 已提交
1042
	public getChildren(tree: tree.ITree, element: any): TPromise<any> {
1043 1044
		const model = <model.Model>element;
		const exBreakpoints = <debug.IEnablement[]>model.getExceptionBreakpoints();
E
Erich Gamma 已提交
1045

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

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

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

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

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

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

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

	constructor(
		private actionProvider: BreakpointsActionProvider,
		private actionRunner: actions.IActionRunner,
		@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
		if (debugActive && !breakpoint.verified) {
			tree.addTraits('disabled', [breakpoint]);
			if (breakpoint.message) {
				data.breakpoint.title = breakpoint.message;
			}
1193 1194
		} else if (breakpoint.condition || breakpoint.hitCondition) {
			data.breakpoint.title = breakpoint.condition ? breakpoint.condition : breakpoint.hitCondition;
1195
		}
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
	constructor(debugService: debug.IDebugService, contextMenuService: IContextMenuService, actionProvider: renderer.IActionProvider) {
		super(debugService, contextMenuService, actionProvider);
		if (isMacintosh) {
A
Alexandru Dima 已提交
1231
			this.downKeyBindingDispatcher.set(KeyCode.Enter, this.onRename.bind(this));
I
isidor 已提交
1232
		} else {
A
Alexandru Dima 已提交
1233
			this.downKeyBindingDispatcher.set(KeyCode.F2, this.onRename.bind(this));
I
isidor 已提交
1234 1235 1236
		}
	}

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
}