debugModel.ts 22.7 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.
 *--------------------------------------------------------------------------------------------*/

I
isidor 已提交
6
import { TPromise } from 'vs/base/common/winjs.base';
I
isidor 已提交
7
import strings = require('vs/base/common/strings');
E
Erich Gamma 已提交
8 9 10 11 12 13 14 15
import nls = require('vs/nls');
import lifecycle = require('vs/base/common/lifecycle');
import ee = require('vs/base/common/eventEmitter');
import uuid = require('vs/base/common/uuid');
import severity from 'vs/base/common/severity';
import types = require('vs/base/common/types');
import arrays = require('vs/base/common/arrays');
import debug = require('vs/workbench/parts/debug/common/debug');
16
import errors = require('vs/base/common/errors');
I
isidor 已提交
17
import { Source } from 'vs/workbench/parts/debug/common/debugSource';
E
Erich Gamma 已提交
18

19 20
const MAX_REPL_LENGTH = 10000;

E
Erich Gamma 已提交
21
function resolveChildren(debugService: debug.IDebugService, parent: debug.IExpressionContainer): TPromise<Variable[]> {
I
isidor 已提交
22 23
	const session = debugService.getActiveSession();
	// only variables with reference > 0 have children.
E
Erich Gamma 已提交
24 25 26 27
	if (!session || parent.reference <= 0) {
		return TPromise.as([]);
	}

28
	return session.variables({ variablesReference: parent.reference }).then(response => {
29
		return arrays.distinct(response.body.variables.filter(v => !!v), v => v.name).map(
E
Erich Gamma 已提交
30 31
			v => new Variable(parent, v.variablesReference, v.name, v.value)
		);
32
	}, (e: Error) => [new Variable(parent, 0, null, e.message, false)]);
E
Erich Gamma 已提交
33 34 35 36 37 38
}

function massageValue(value: string): string {
	return value ? value.replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/\t/g, '\\t') : value;
}

I
isidor 已提交
39 40 41 42 43
export function evaluateExpression(session: debug.IRawDebugSession, stackFrame: debug.IStackFrame, expression: Expression, context: string): TPromise<Expression> {
	if (!session) {
		expression.value = context === 'repl' ? nls.localize('startDebugFirst', "Please start a debug session to evaluate") : Expression.DEFAULT_VALUE;
		expression.available = false;
		expression.reference = 0;
A
Alex Dima 已提交
44
		return TPromise.as(expression);
I
isidor 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	}

	return session.evaluate({
		expression: expression.name,
		frameId: stackFrame ? stackFrame.frameId : undefined,
		context
	}).then(response => {
		expression.value = response.body.result;
		expression.available = true;
		expression.reference = response.body.variablesReference;

		return expression;
	}, err => {
		expression.value = err.message;
		expression.available = false;
		expression.reference = 0;

		return expression;
	});
}

I
isidor 已提交
66 67
const notPropertySyntax = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
const arrayElementSyntax = /\[.*\]$/;
I
isidor 已提交
68

69 70 71
export function getFullExpressionName(expression: debug.IExpression, sessionType: string): string {
	let names = [expression.name];
	if (expression instanceof Variable) {
I
isidor 已提交
72
		let v = (<Variable> expression).parent;
73 74 75 76 77 78 79 80 81 82 83
		while (v instanceof Variable || v instanceof Expression) {
			names.push((<Variable> v).name);
			v = (<Variable> v).parent;
		}
	}
	names = names.reverse();

	let result = null;
	names.forEach(name => {
		if (!result) {
			result = name;
I
isidor 已提交
84
		} else if (arrayElementSyntax.test(name) || (sessionType === 'node' && !notPropertySyntax.test(name))) {
I
isidor 已提交
85
			// use safe way to access node properties a['property_name']. Also handles array elements.
86 87 88 89 90 91 92 93 94
			result = name && name.indexOf('[') === 0 ? `${ result }${ name }` : `${ result }['${ name }']`;
		} else {
			result = `${ result }.${ name }`;
		}
	});

	return result;
}

E
Erich Gamma 已提交
95
export class Thread implements debug.IThread {
96 97
	private promisedCallStack: TPromise<debug.IStackFrame[]>;
	private cachedCallStack: debug.IStackFrame[];
I
isidor 已提交
98
	public stoppedDetails: debug.IRawStoppedDetails;
99
	public stopped: boolean;
E
Erich Gamma 已提交
100

101 102
	constructor(public name: string, public threadId) {
		this.promisedCallStack = undefined;
I
isidor 已提交
103
		this.stoppedDetails = undefined;
104 105
		this.cachedCallStack = undefined;
		this.stopped = false;
E
Erich Gamma 已提交
106 107 108 109 110
	}

	public getId(): string {
		return `thread:${ this.name }:${ this.threadId }`;
	}
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

	public clearCallStack(): void {
		this.promisedCallStack = undefined;
		this.cachedCallStack = undefined;
	}

	public getCachedCallStack(): debug.IStackFrame[] {
		return this.cachedCallStack;
	}

	public getCallStack(debugService: debug.IDebugService): TPromise<debug.IStackFrame[]> {
		if (!this.stopped) {
			return TPromise.as([]);
		}
		if (!this.promisedCallStack) {
			this.promisedCallStack = this.getCallStackImpl(debugService);
			this.promisedCallStack.then(result => {
				this.cachedCallStack = result;
			}, errors.onUnexpectedError);
		}

		return this.promisedCallStack;
	}

	private getCallStackImpl(debugService: debug.IDebugService): TPromise<debug.IStackFrame[]> {
		let session = debugService.getActiveSession();
		return session.stackTrace({ threadId: this.threadId, levels: 20 }).then(response => {
			return response.body.stackFrames.map((rsf, level) => {
				if (!rsf) {
I
isidor 已提交
140
					return new StackFrame(this.threadId, 0, new Source({ name: 'unknown' }, false), nls.localize('unknownStack', "Unknown stack location"), undefined, undefined);
141 142
				}

I
isidor 已提交
143
				return new StackFrame(this.threadId, rsf.id, rsf.source ? new Source(rsf.source) : new Source({ name: 'unknown' }, false), rsf.name, rsf.line, rsf.column);
144 145 146
			});
		});
	}
E
Erich Gamma 已提交
147 148 149 150
}

export class OutputElement implements debug.ITreeElement {

I
isidor 已提交
151 152
	constructor(private id = uuid.generateUuid()) {
		// noop
E
Erich Gamma 已提交
153 154 155 156 157 158 159 160 161
	}

	public getId(): string {
		return this.id;
	}
}

export class ValueOutputElement extends OutputElement {

I
isidor 已提交
162 163
	constructor(public value: string, public severity: severity, public category?: string, public counter:number = 1) {
		super();
E
Erich Gamma 已提交
164 165 166 167 168 169 170 171 172 173
	}
}

export class KeyValueOutputElement extends OutputElement {

	private static MAX_CHILDREN = 1000; // upper bound of children per value

	private children: debug.ITreeElement[];
	private _valueName: string;

I
isidor 已提交
174 175
	constructor(public key: string, public valueObj: any, public annotation?: string) {
		super();
E
Erich Gamma 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

		this._valueName = null;
	}

	public get value(): string {
		if (this._valueName === null) {
			if (this.valueObj === null) {
				this._valueName = 'null';
			} else if (Array.isArray(this.valueObj)) {
				this._valueName = `Array[${this.valueObj.length}]`;
			} else if (types.isObject(this.valueObj)) {
				this._valueName = 'Object';
			} else if (types.isString(this.valueObj)) {
				this._valueName = `"${massageValue(this.valueObj)}"`;
			} else {
				this._valueName = String(this.valueObj);
			}

			if (!this._valueName) {
				this._valueName = '';
			}
		}

		return this._valueName;
	}

	public getChildren(): debug.ITreeElement[] {
		if (!this.children) {
			if (Array.isArray(this.valueObj)) {
I
isidor 已提交
205
				this.children = (<any[]>this.valueObj).slice(0, KeyValueOutputElement.MAX_CHILDREN).map((v, index) => new KeyValueOutputElement(String(index), v, null));
E
Erich Gamma 已提交
206
			} else if (types.isObject(this.valueObj)) {
I
isidor 已提交
207
				this.children = Object.getOwnPropertyNames(this.valueObj).slice(0, KeyValueOutputElement.MAX_CHILDREN).map(key => new KeyValueOutputElement(key, this.valueObj[key], null));
E
Erich Gamma 已提交
208 209 210 211 212 213 214 215 216
			} else {
				this.children = [];
			}
		}

		return this.children;
	}
}

217
export class ExpressionContainer implements debug.IExpressionContainer {
E
Erich Gamma 已提交
218 219

	private children: TPromise<debug.IExpression[]>;
220 221
	public valueChanged: boolean;
	public static allValues: { [id: string]: string } = {};
E
Erich Gamma 已提交
222

223
	constructor(public reference: number, private id: string, private cacheChildren: boolean) {
E
Erich Gamma 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236
		this.children = null;
	}

	public getChildren(debugService: debug.IDebugService): TPromise<debug.IExpression[]> {
		if (!this.cacheChildren) {
			return resolveChildren(debugService, this);
		}
		if (!this.children) {
			this.children = resolveChildren(debugService, this);
		}

		return this.children;
	}
237 238 239 240 241

	public getId(): string {
		return this.id;
	}

E
Erich Gamma 已提交
242 243
}

244 245
export class Expression extends ExpressionContainer implements debug.IExpression {
	static DEFAULT_VALUE = 'not available';
E
Erich Gamma 已提交
246

247 248
	public available: boolean;
	private _value: string;
E
Erich Gamma 已提交
249

250 251 252 253
	constructor(public name: string, cacheChildren: boolean, id = uuid.generateUuid()) {
		super(0, id, cacheChildren);
		this.value = Expression.DEFAULT_VALUE;
		this.available = false;
E
Erich Gamma 已提交
254 255
	}

256 257
	public get value(): string {
		return this._value;
E
Erich Gamma 已提交
258 259
	}

260 261 262 263 264 265 266
	public set value(value: string) {
		this._value = massageValue(value);
		this.valueChanged = ExpressionContainer.allValues[this.getId()] &&
			ExpressionContainer.allValues[this.getId()] !== Expression.DEFAULT_VALUE && ExpressionContainer.allValues[this.getId()] !== value;
		ExpressionContainer.allValues[this.getId()] = value;
	}
}
E
Erich Gamma 已提交
267

268 269 270 271 272 273 274 275 276
export class Variable extends ExpressionContainer implements debug.IExpression {

	public value: string;

	constructor(public parent: debug.IExpressionContainer, reference: number, public name: string, value: string, public available = true) {
		super(reference, `variable:${ parent.getId() }:${ name }`, true);
		this.value = massageValue(value);
		this.valueChanged = ExpressionContainer.allValues[this.getId()] && ExpressionContainer.allValues[this.getId()] !== value;
		ExpressionContainer.allValues[this.getId()] = value;
E
Erich Gamma 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
	}
}

export class Scope implements debug.IScope {

	private children: TPromise<Variable[]>;

	constructor(private threadId: number, public name: string, public reference: number, public expensive: boolean) {
		this.children = null;
	}

	public getId(): string {
		return `scope:${ this.threadId }:${ this.name }:${ this.reference }`;
	}

	public getChildren(debugService: debug.IDebugService): TPromise<Variable[]> {
		if (!this.children) {
			this.children = resolveChildren(debugService, this);
		}

		return this.children;
	}
}

export class StackFrame implements debug.IStackFrame {

	private internalId: string;
	private scopes: TPromise<Scope[]>;

I
isidor 已提交
306
	constructor(public threadId: number, public frameId: number, public source: Source, public name: string, public lineNumber: number, public column: number) {
E
Erich Gamma 已提交
307 308 309 310 311 312 313 314 315 316 317 318
		this.internalId = uuid.generateUuid();
		this.scopes = null;
	}

	public getId(): string {
		return this.internalId;
	}

	public getScopes(debugService: debug.IDebugService): TPromise<debug.IScope[]> {
		if (!this.scopes) {
			this.scopes = debugService.getActiveSession().scopes({ frameId: this.frameId }).then(response => {
				return response.body.scopes.map(rs => new Scope(this.threadId, rs.name, rs.variablesReference, rs.expensive));
319
			}, err => []);
E
Erich Gamma 已提交
320 321 322 323 324 325 326 327 328
		}

		return this.scopes;
	}
}

export class Breakpoint implements debug.IBreakpoint {

	public lineNumber: number;
329
	public verified: boolean;
330
	public idFromAdapter: number;
I
isidor 已提交
331
	public message: string;
E
Erich Gamma 已提交
332 333
	private id: string;

I
isidor 已提交
334
	constructor(public source: Source, public desiredLineNumber: number, public enabled: boolean, public condition: string) {
335 336 337
		if (enabled === undefined) {
			this.enabled = true;
		}
E
Erich Gamma 已提交
338
		this.lineNumber = this.desiredLineNumber;
339
		this.verified = false;
E
Erich Gamma 已提交
340 341 342 343 344 345 346 347
		this.id = uuid.generateUuid();
	}

	public getId(): string {
		return this.id;
	}
}

I
isidor 已提交
348 349 350
export class FunctionBreakpoint implements debug.IFunctionBreakpoint {

	private id: string;
I
isidor 已提交
351
	public verified: boolean;
352
	public idFromAdapter: number;
I
isidor 已提交
353

354
	constructor(public name: string, public enabled: boolean) {
I
isidor 已提交
355
		this.verified = false;
I
isidor 已提交
356 357 358 359 360 361 362 363
		this.id = uuid.generateUuid();
	}

	public getId(): string {
		return this.id;
	}
}

E
Erich Gamma 已提交
364 365 366 367
export class ExceptionBreakpoint implements debug.IExceptionBreakpoint {

	private id: string;

368
	constructor(public filter: string, public label: string, public enabled: boolean) {
E
Erich Gamma 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382
		this.id = uuid.generateUuid();
	}

	public getId(): string {
		return this.id;
	}
}

export class Model extends ee.EventEmitter implements debug.IModel {

	private threads: { [reference: number]: debug.IThread; };
	private toDispose: lifecycle.IDisposable[];
	private replElements: debug.ITreeElement[];

I
isidor 已提交
383
	constructor(private breakpoints: debug.IBreakpoint[], private breakpointsActivated: boolean, private functionBreakpoints: debug.IFunctionBreakpoint[],
E
Erich Gamma 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
		private exceptionBreakpoints: debug.IExceptionBreakpoint[], private watchExpressions: Expression[]) {

		super();
		this.threads = {};
		this.replElements = [];
		this.toDispose = [];
	}

	public getId(): string {
		return 'root';
	}

	public getThreads(): { [reference: number]: debug.IThread; } {
		return this.threads;
	}

	public clearThreads(removeThreads: boolean, reference: number = undefined): void {
		if (reference) {
			if (removeThreads) {
				delete this.threads[reference];
			} else {
405
				this.threads[reference].clearCallStack();
I
isidor 已提交
406
				this.threads[reference].stoppedDetails = undefined;
E
Erich Gamma 已提交
407 408 409 410
			}
		} else {
			if (removeThreads) {
				this.threads = {};
411
				ExpressionContainer.allValues = {};
E
Erich Gamma 已提交
412
			} else {
I
isidor 已提交
413
				for (let ref in this.threads) {
E
Erich Gamma 已提交
414
					if (this.threads.hasOwnProperty(ref)) {
415
						this.threads[ref].clearCallStack();
I
isidor 已提交
416
						this.threads[ref].stoppedDetails = undefined;
E
Erich Gamma 已提交
417 418 419 420 421 422 423 424
					}
				}
			}
		}

		this.emit(debug.ModelEvents.CALLSTACK_UPDATED);
	}

425 426 427 428 429 430 431 432 433 434
	public continueThreads(): void {
		for (let ref in this.threads) {
			if (this.threads.hasOwnProperty(ref)) {
				this.threads[ref].stopped = false;
			}
		}

		this.clearThreads(false);
	}

E
Erich Gamma 已提交
435 436 437 438
	public getBreakpoints(): debug.IBreakpoint[] {
		return this.breakpoints;
	}

I
isidor 已提交
439 440 441 442
	public getFunctionBreakpoints(): debug.IFunctionBreakpoint[] {
		return this.functionBreakpoints;
	}

E
Erich Gamma 已提交
443 444 445 446
	public getExceptionBreakpoints(): debug.IExceptionBreakpoint[] {
		return this.exceptionBreakpoints;
	}

447
	public setExceptionBreakpoints(data: [{ filter: string, label: string, default?: boolean }]): void {
448
		if (data) {
449 450 451 452
			this.exceptionBreakpoints = data.map(d => {
				const ebp = this.exceptionBreakpoints.filter(ebp => ebp.filter === d.filter).pop();
				return new ExceptionBreakpoint(d.filter, d.label, ebp ? ebp.enabled : d.default);
			});
453 454 455
		}
	}

E
Erich Gamma 已提交
456 457 458 459 460 461 462 463 464
	public areBreakpointsActivated(): boolean {
		return this.breakpointsActivated;
	}

	public toggleBreakpointsActivated(): void {
		this.breakpointsActivated = !this.breakpointsActivated;
		this.emit(debug.ModelEvents.BREAKPOINTS_UPDATED);
	}

465
	public addBreakpoints(rawData: debug.IRawBreakpoint[]): void {
466 467
		this.breakpoints = this.breakpoints.concat(rawData.map(rawBp =>
			new Breakpoint(new Source(Source.toRawSource(rawBp.uri, this)), rawBp.lineNumber, rawBp.enabled, rawBp.condition)));
468 469 470
		this.breakpointsActivated = true;
		this.emit(debug.ModelEvents.BREAKPOINTS_UPDATED);
	}
E
Erich Gamma 已提交
471

472 473
	public removeBreakpoints(toRemove: debug.IBreakpoint[]): void {
		this.breakpoints = this.breakpoints.filter(bp => !toRemove.some(toRemove => toRemove.getId() === bp.getId()));
E
Erich Gamma 已提交
474 475 476
		this.emit(debug.ModelEvents.BREAKPOINTS_UPDATED);
	}

I
isidor 已提交
477
	public updateBreakpoints(data: { [id: string]: DebugProtocol.Breakpoint }): void {
478 479 480
		this.breakpoints.forEach(bp => {
			const bpData = data[bp.getId()];
			if (bpData) {
481
				bp.lineNumber = bpData.line ? bpData.line : bp.lineNumber;
482
				bp.verified = bpData.verified;
483
				bp.idFromAdapter = bpData.id;
I
isidor 已提交
484
				bp.message = bpData.message;
485 486 487
			}
		});
		this.emit(debug.ModelEvents.BREAKPOINTS_UPDATED);
488 489
	}

E
Erich Gamma 已提交
490 491 492 493 494
	public toggleEnablement(element: debug.IEnablement): void {
		element.enabled = !element.enabled;
		if (element instanceof Breakpoint && !element.enabled) {
			var breakpoint = <Breakpoint> element;
			breakpoint.lineNumber = breakpoint.desiredLineNumber;
495
			breakpoint.verified = false;
E
Erich Gamma 已提交
496 497 498 499 500 501 502 503 504 505
		}

		this.emit(debug.ModelEvents.BREAKPOINTS_UPDATED);
	}

	public enableOrDisableAllBreakpoints(enabled: boolean): void {
		this.breakpoints.forEach(bp => {
			bp.enabled = enabled;
			if (!enabled) {
				bp.lineNumber = bp.desiredLineNumber;
506
				bp.verified = false;
E
Erich Gamma 已提交
507 508
			}
		});
I
isidor 已提交
509 510
		this.exceptionBreakpoints.forEach(ebp => ebp.enabled = enabled);
		this.functionBreakpoints.forEach(fbp => fbp.enabled = enabled);
E
Erich Gamma 已提交
511 512 513 514

		this.emit(debug.ModelEvents.BREAKPOINTS_UPDATED);
	}

I
isidor 已提交
515 516 517 518 519
	public addFunctionBreakpoint(functionName: string): void {
		this.functionBreakpoints.push(new FunctionBreakpoint(functionName, true));
		this.emit(debug.ModelEvents.BREAKPOINTS_UPDATED);
	}

520
	public updateFunctionBreakpoints(data: { [id: string]: { name?: string, verified?: boolean; id?: number } }): void {
I
isidor 已提交
521 522 523 524 525
		this.functionBreakpoints.forEach(fbp => {
			const fbpData = data[fbp.getId()];
			if (fbpData) {
				fbp.name = fbpData.name || fbp.name;
				fbp.verified = fbpData.verified;
526
				fbp.idFromAdapter = fbpData.id;
I
isidor 已提交
527 528 529 530
			}
		});

		this.emit(debug.ModelEvents.BREAKPOINTS_UPDATED);
I
isidor 已提交
531 532
	}

533
	public removeFunctionBreakpoints(id?: string): void {
I
isidor 已提交
534
		this.functionBreakpoints = id ? this.functionBreakpoints.filter(fbp => fbp.getId() !== id) : [];
535 536 537
		this.emit(debug.ModelEvents.BREAKPOINTS_UPDATED);
	}

E
Erich Gamma 已提交
538 539 540 541
	public getReplElements(): debug.ITreeElement[] {
		return this.replElements;
	}

I
isidor 已提交
542
	public addReplExpression(session: debug.IRawDebugSession, stackFrame: debug.IStackFrame, name: string): TPromise<void> {
I
isidor 已提交
543
		const expression = new Expression(name, true);
544
		this.addReplElements([expression]);
I
isidor 已提交
545
		return evaluateExpression(session, stackFrame, expression, 'repl').then(() =>
E
Erich Gamma 已提交
546 547 548 549 550 551 552 553 554 555
			this.emit(debug.ModelEvents.REPL_ELEMENTS_UPDATED, expression)
		);
	}

	public logToRepl(value: string, severity?: severity): void;
	public logToRepl(value: { [key: string]: any }, severity?: severity): void;
	public logToRepl(value: any, severity?: severity): void {
		let elements:OutputElement[] = [];
		let previousOutput = this.replElements.length && (<ValueOutputElement>this.replElements[this.replElements.length - 1]);

I
isidor 已提交
556
		// string message
E
Erich Gamma 已提交
557
		if (typeof value === 'string') {
I
isidor 已提交
558
			value = strings.removeAnsiEscapeCodes(value);
E
Erich Gamma 已提交
559 560 561
			if (value && value.trim() && previousOutput && previousOutput.value === value && previousOutput.severity === severity) {
				previousOutput.counter++; // we got the same output (but not an empty string when trimmed) so we just increment the counter
			} else {
I
isidor 已提交
562
				let lines = value.trim().split('\n');
E
Erich Gamma 已提交
563
				lines.forEach((line, index) => {
I
isidor 已提交
564
					elements.push(new ValueOutputElement(line, severity));
E
Erich Gamma 已提交
565 566 567 568
				});
			}
		}

I
isidor 已提交
569
		// key-value output
E
Erich Gamma 已提交
570
		else {
I
isidor 已提交
571
			elements.push(new KeyValueOutputElement(value.prototype, value, nls.localize('snapshotObj', "Only primitive values are shown for this object.")));
E
Erich Gamma 已提交
572 573 574
		}

		if (elements.length) {
575
			this.addReplElements(elements);
E
Erich Gamma 已提交
576 577 578 579 580
			this.emit(debug.ModelEvents.REPL_ELEMENTS_UPDATED, elements);
		}
	}

	public appendReplOutput(value: string, severity?: severity): void {
I
isidor 已提交
581
		value = strings.removeAnsiEscapeCodes(value);
I
isidor 已提交
582
		const elements: OutputElement[] = [];
E
Erich Gamma 已提交
583
		let previousOutput = this.replElements.length && (<ValueOutputElement>this.replElements[this.replElements.length - 1]);
584 585
		let lines = value.split('\n');
		let groupTogether = !!previousOutput && (previousOutput.category === 'output' && severity === previousOutput.severity);
E
Erich Gamma 已提交
586 587

		if (groupTogether) {
588 589 590 591 592
			// append to previous line if same group
			previousOutput.value += lines.shift();
		} else if (previousOutput && previousOutput.value === '') {
			// remove potential empty lines between different output types
			this.replElements.pop();
E
Erich Gamma 已提交
593 594 595 596
		}

		// fill in lines as output value elements
		lines.forEach((line, index) => {
I
isidor 已提交
597
			elements.push(new ValueOutputElement(line, severity, 'output'));
E
Erich Gamma 已提交
598 599
		});

600
		this.addReplElements(elements);
E
Erich Gamma 已提交
601 602 603
		this.emit(debug.ModelEvents.REPL_ELEMENTS_UPDATED, elements);
	}

604 605 606 607 608 609 610
	private addReplElements(newElements: debug.ITreeElement[]): void {
		this.replElements.push(...newElements);
		if (this.replElements.length > MAX_REPL_LENGTH) {
			this.replElements.splice(0, this.replElements.length - MAX_REPL_LENGTH);
		}
	}

E
Erich Gamma 已提交
611
	public clearReplExpressions(): void {
612 613 614 615
		if (this.replElements.length > 0) {
			this.replElements = [];
			this.emit(debug.ModelEvents.REPL_ELEMENTS_UPDATED);
		}
E
Erich Gamma 已提交
616 617 618 619 620 621
	}

	public getWatchExpressions(): Expression[] {
		return this.watchExpressions;
	}

I
isidor 已提交
622
	public addWatchExpression(session: debug.IRawDebugSession, stackFrame: debug.IStackFrame, name: string): TPromise<void> {
I
isidor 已提交
623
		const we = new Expression(name, false);
E
Erich Gamma 已提交
624 625 626
		this.watchExpressions.push(we);
		if (!name) {
			this.emit(debug.ModelEvents.WATCH_EXPRESSIONS_UPDATED, we);
A
Alex Dima 已提交
627
			return TPromise.as(null);
E
Erich Gamma 已提交
628 629 630 631 632
		}

		return this.evaluateWatchExpressions(session, stackFrame, we.getId());
	}

I
isidor 已提交
633
	public renameWatchExpression(session: debug.IRawDebugSession, stackFrame: debug.IStackFrame, id: string, newName: string): TPromise<void> {
I
isidor 已提交
634
		const filtered = this.watchExpressions.filter(we => we.getId() === id);
E
Erich Gamma 已提交
635 636
		if (filtered.length === 1) {
			filtered[0].name = newName;
I
isidor 已提交
637
			return evaluateExpression(session, stackFrame, filtered[0], 'watch').then(() => {
E
Erich Gamma 已提交
638 639 640 641
				this.emit(debug.ModelEvents.WATCH_EXPRESSIONS_UPDATED, filtered[0]);
			});
		}

A
Alex Dima 已提交
642
		return TPromise.as(null);
E
Erich Gamma 已提交
643 644
	}

I
isidor 已提交
645
	public evaluateWatchExpressions(session: debug.IRawDebugSession, stackFrame: debug.IStackFrame, id: string = null): TPromise<void> {
E
Erich Gamma 已提交
646
		if (id) {
I
isidor 已提交
647
			const filtered = this.watchExpressions.filter(we => we.getId() === id);
E
Erich Gamma 已提交
648
			if (filtered.length !== 1) {
A
Alex Dima 已提交
649
				return TPromise.as(null);
E
Erich Gamma 已提交
650 651
			}

I
isidor 已提交
652
			return evaluateExpression(session, stackFrame, filtered[0], 'watch').then(() => {
E
Erich Gamma 已提交
653 654 655 656
				this.emit(debug.ModelEvents.WATCH_EXPRESSIONS_UPDATED, filtered[0]);
			});
		}

I
isidor 已提交
657
		return TPromise.join(this.watchExpressions.map(we => evaluateExpression(session, stackFrame, we, 'watch'))).then(() => {
E
Erich Gamma 已提交
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
			this.emit(debug.ModelEvents.WATCH_EXPRESSIONS_UPDATED);
		});
	}

	public clearWatchExpressionValues(): void {
		this.watchExpressions.forEach(we => {
			we.value = Expression.DEFAULT_VALUE;
			we.available = false;
			we.reference = 0;
		});

		this.emit(debug.ModelEvents.WATCH_EXPRESSIONS_UPDATED);
	}

	public clearWatchExpressions(id: string = null): void {
673
		this.watchExpressions = id ? this.watchExpressions.filter(we => we.getId() !== id) : [];
E
Erich Gamma 已提交
674 675 676
		this.emit(debug.ModelEvents.WATCH_EXPRESSIONS_UPDATED);
	}

I
isidor 已提交
677
	public sourceIsUnavailable(source: Source): void {
678
		Object.keys(this.threads).forEach(key => {
679 680 681 682 683 684 685
			if (this.threads[key].getCachedCallStack()) {
				this.threads[key].getCachedCallStack().forEach(stackFrame => {
					if (stackFrame.source.uri.toString() === source.uri.toString()) {
						stackFrame.source.available = false;
					}
				});
			}
686 687
		});

E
Erich Gamma 已提交
688 689 690 691 692
		this.emit(debug.ModelEvents.CALLSTACK_UPDATED);
	}

	public rawUpdate(data: debug.IRawModelUpdate): void {
		if (data.thread) {
693
			this.threads[data.threadId] = new Thread(data.thread.name, data.thread.id);
E
Erich Gamma 已提交
694 695
		}

696 697 698 699 700 701 702 703 704 705
		if (data.stoppedDetails) {
			// Set the availability of the threads' callstacks depending on
			// whether the thread is stopped or not
			for (let ref in this.threads) {
				if (this.threads.hasOwnProperty(ref)) {
					if (data.allThreadsStopped) {
						// Only update the details if all the threads are stopped
						// because we don't want to overwrite the details of other
						// threads that have stopped for a different reason
						this.threads[ref].stoppedDetails = data.stoppedDetails;
E
Erich Gamma 已提交
706
					}
707

708 709 710 711
					this.threads[ref].stopped = data.allThreadsStopped;
					this.threads[ref].clearCallStack();
				}
			}
E
Erich Gamma 已提交
712

I
isidor 已提交
713
			this.threads[data.threadId].stoppedDetails = data.stoppedDetails;
714
			this.threads[data.threadId].stopped = true;
E
Erich Gamma 已提交
715 716 717 718 719 720 721 722 723 724
		}

		this.emit(debug.ModelEvents.CALLSTACK_UPDATED);
	}

	public dispose(): void {
		super.dispose();
		this.threads = null;
		this.breakpoints = null;
		this.exceptionBreakpoints = null;
I
isidor 已提交
725
		this.functionBreakpoints = null;
E
Erich Gamma 已提交
726 727 728 729 730
		this.watchExpressions = null;
		this.replElements = null;
		this.toDispose = lifecycle.disposeAll(this.toDispose);
	}
}