debugEditorModelManager.ts 13.3 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 nls = require('vs/nls');
E
Erich Gamma 已提交
7 8
import lifecycle = require('vs/base/common/lifecycle');
import editorcommon = require('vs/editor/common/editorCommon');
9
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
10
import { IDebugService, ModelEvents, ViewModelEvents, IBreakpoint, IRawBreakpoint, State } from 'vs/workbench/parts/debug/common/debug';
E
Erich Gamma 已提交
11 12 13
import { IModelService } from 'vs/editor/common/services/modelService';

function toMap(arr: string[]): { [key: string]: boolean; } {
14
	const result: { [key: string]: boolean; } = {};
I
isidor 已提交
15
	for (let i = 0, len = arr.length; i < len; i++) {
16
		result[arr[i]] = true;
E
Erich Gamma 已提交
17
	}
18 19 20 21 22 23 24 25 26 27 28

	return result;
}

function createRange(startLineNUmber: number, startColumn: number, endLineNumber: number, endColumn: number): editorcommon.IRange {
	return {
		startLineNumber: startLineNUmber,
		startColumn: startColumn,
		endLineNumber: endLineNumber,
		endColumn: endColumn
	};
E
Erich Gamma 已提交
29 30 31 32 33 34 35 36 37 38 39 40
}

interface IDebugEditorModelData {
	model: editorcommon.IModel;
	toDispose: lifecycle.IDisposable[];
	breakpointDecorationIds: string[];
	breakpointLines: number[];
	breakpointDecorationsAsMap: { [decorationId: string]: boolean; };
	currentStackDecorations: string[];
	topStackFrameRange: editorcommon.IRange;
}

41
export class DebugEditorModelManager implements IWorkbenchContribution {
E
Erich Gamma 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
	static ID = 'breakpointManager';

	private modelData: {
		[modelUrl: string]: IDebugEditorModelData;
	};
	private toDispose: lifecycle.IDisposable[];

	constructor(
		@IModelService private modelService: IModelService,
		@IDebugService private debugService: IDebugService
	) {
		this.modelData = {};
		this.toDispose = [];
		this.registerListeners();
	}

	public getId(): string {
		return DebugEditorModelManager.ID;
	}

	public dispose(): void {
I
isidor 已提交
63
		for (let modelUrlStr in this.modelData) {
E
Erich Gamma 已提交
64
			if (this.modelData.hasOwnProperty(modelUrlStr)) {
I
isidor 已提交
65
				const modelData = this.modelData[modelUrlStr];
E
Erich Gamma 已提交
66 67 68 69 70 71 72 73 74 75 76
				lifecycle.disposeAll(modelData.toDispose);
				modelData.model.deltaDecorations(modelData.breakpointDecorationIds, []);
				modelData.model.deltaDecorations(modelData.currentStackDecorations, []);
			}
		}
		this.toDispose = lifecycle.disposeAll(this.toDispose);

		this.modelData = null;
	}

	private registerListeners(): void {
77
		this.toDispose.push(this.modelService.onModelAdded(this.onModelAdded, this));
78
		this.modelService.getModels().forEach(model => this.onModelAdded(model));
79
		this.toDispose.push(this.modelService.onModelRemoved(this.onModelRemoved, this));
E
Erich Gamma 已提交
80

81 82
		this.toDispose.push(this.debugService.getModel().addListener2(ModelEvents.BREAKPOINTS_UPDATED, () => this.onBreakpointsChanged()));
		this.toDispose.push(this.debugService.getViewModel().addListener2(ViewModelEvents.FOCUSED_STACK_FRAME_UPDATED, () => this.onFocusedStackFrameUpdated()));
E
Erich Gamma 已提交
83 84 85
	}

	private onModelAdded(model: editorcommon.IModel): void {
86 87
		const modelUrlStr = model.getAssociatedResource().toString();
		const breakpoints = this.debugService.getModel().getBreakpoints().filter(bp => bp.source.uri.toString() === modelUrlStr);
E
Erich Gamma 已提交
88

I
isidor 已提交
89 90
		const currentStackDecorations = model.deltaDecorations([], this.createCallStackDecorations(modelUrlStr));
		const breakPointDecorations = model.deltaDecorations([], this.createBreakpointDecorations(breakpoints));
E
Erich Gamma 已提交
91

92 93
		const toDispose: lifecycle.IDisposable[] = [model.addListener2(editorcommon.EventType.ModelDecorationsChanged, (e: editorcommon.IModelDecorationsChangedEvent) =>
			this.onModelDecorationsChanged(modelUrlStr, e))];
E
Erich Gamma 已提交
94

95
		this.modelData[modelUrlStr] = {
E
Erich Gamma 已提交
96 97 98 99 100 101 102 103 104 105
			model: model,
			toDispose: toDispose,
			breakpointDecorationIds: breakPointDecorations,
			breakpointLines: breakpoints.map(bp => bp.lineNumber),
			breakpointDecorationsAsMap: toMap(breakPointDecorations),
			currentStackDecorations: currentStackDecorations,
			topStackFrameRange: null
		};
	}

106 107 108 109 110 111 112 113 114 115
	private onModelRemoved(model: editorcommon.IModel): void {
		const modelUrlStr = model.getAssociatedResource().toString();
		if (this.modelData.hasOwnProperty(modelUrlStr)) {
			const modelData = this.modelData[modelUrlStr];
			delete this.modelData[modelUrlStr];

			lifecycle.disposeAll(modelData.toDispose);
		}
	}

I
isidor 已提交
116
	// call stack management. Represent data coming from the debug service.
117 118 119 120 121

	private onFocusedStackFrameUpdated(): void {
		Object.keys(this.modelData).forEach(modelUrlStr => {
			const modelData = this.modelData[modelUrlStr];
			modelData.currentStackDecorations = modelData.model.deltaDecorations(modelData.currentStackDecorations, this.createCallStackDecorations(modelUrlStr));
E
Erich Gamma 已提交
122 123 124 125
		});
	}

	private createCallStackDecorations(modelUrlStr: string): editorcommon.IModelDeltaDecoration[] {
126 127 128 129
		const result: editorcommon.IModelDeltaDecoration[] = [];
		const focusedStackFrame = this.debugService.getViewModel().getFocusedStackFrame();
		const allThreads = this.debugService.getModel().getThreads();
		if (!focusedStackFrame || !allThreads[focusedStackFrame.threadId] || !allThreads[focusedStackFrame.threadId].callStack) {
E
Erich Gamma 已提交
130 131 132
			return result;
		}

I
isidor 已提交
133
		// only show decorations for the currently focussed thread.
134 135 136
		const thread = allThreads[focusedStackFrame.threadId];
		thread.callStack.filter(sf => sf.source.uri.toString() === modelUrlStr).forEach(sf => {
			const wholeLineRange = createRange(sf.lineNumber, sf.column, sf.lineNumber, Number.MAX_VALUE);
E
Erich Gamma 已提交
137

I
isidor 已提交
138
			// compute how to decorate the editor. Different decorations are used if this is a top stack frame, focussed stack frame,
139 140
			// an exception or a stack frame that did not change the line number (we only decorate the columns, not the whole line).
			if (sf === thread.callStack[0]) {
E
Erich Gamma 已提交
141 142
				result.push({
					options: DebugEditorModelManager.TOP_STACK_FRAME_MARGIN,
143
					range: createRange(sf.lineNumber, sf.column, sf.lineNumber, sf.column + 1)
E
Erich Gamma 已提交
144 145
				});

I
isidor 已提交
146
				if (thread.stoppedReason === 'exception') {
E
Erich Gamma 已提交
147 148 149 150 151 152 153 154 155
					result.push({
						options: DebugEditorModelManager.TOP_STACK_FRAME_EXCEPTION_DECORATION,
						range: wholeLineRange
					});
				} else {
					result.push({
						options: DebugEditorModelManager.TOP_STACK_FRAME_DECORATION,
						range: wholeLineRange
					});
156

E
Erich Gamma 已提交
157 158 159 160 161 162 163 164 165 166 167
					if (this.modelData[modelUrlStr]) {
						if (this.modelData[modelUrlStr].topStackFrameRange && this.modelData[modelUrlStr].topStackFrameRange.startLineNumber === wholeLineRange.startLineNumber &&
							this.modelData[modelUrlStr].topStackFrameRange.startColumn !== wholeLineRange.startColumn) {
							result.push({
								options: DebugEditorModelManager.TOP_STACK_FRAME_COLUMN_DECORATION,
								range: wholeLineRange
							});
						}
						this.modelData[modelUrlStr].topStackFrameRange = wholeLineRange;
					}
				}
168
			} else if (sf === focusedStackFrame) {
E
Erich Gamma 已提交
169 170
				result.push({
					options: DebugEditorModelManager.FOCUSED_STACK_FRAME_MARGIN,
171
					range: createRange(sf.lineNumber, sf.column, sf.lineNumber, sf.column + 1)
E
Erich Gamma 已提交
172 173 174 175 176 177 178
				});

				result.push({
					options: DebugEditorModelManager.FOCUSED_STACK_FRAME_DECORATION,
					range: wholeLineRange
				});
			}
179
		});
E
Erich Gamma 已提交
180 181 182 183

		return result;
	}

I
isidor 已提交
184
	// breakpoints management. Represent data coming from the debug service and also send data back.
E
Erich Gamma 已提交
185

186 187 188
	private onModelDecorationsChanged(modelUrlStr: string, e: editorcommon.IModelDecorationsChangedEvent): void {
		const modelData = this.modelData[modelUrlStr];
		if (!e.addedOrChangedDecorations.some(d => modelData.breakpointDecorationsAsMap.hasOwnProperty(d.id))) {
I
isidor 已提交
189
			// nothing to do, my decorations did not change.
E
Erich Gamma 已提交
190 191 192
			return;
		}

193
		const data: IRawBreakpoint[] = [];
E
Erich Gamma 已提交
194

I
isidor 已提交
195
		const enabledAndConditions: { [key: number]: { enabled: boolean, condition: string } } = {};
196 197 198 199
		this.debugService.getModel().getBreakpoints().filter(bp => bp.source.uri.toString() === modelUrlStr).forEach(bp => {
			enabledAndConditions[bp.lineNumber] = {
				enabled: bp.enabled,
				condition: bp.condition
I
isidor 已提交
200
			};
201
		});
E
Erich Gamma 已提交
202

203 204 205
		const modelUrl = modelData.model.getAssociatedResource();
		for (let i = 0, len = modelData.breakpointDecorationIds.length; i < len; i++) {
			const decorationRange = modelData.model.getDecorationRange(modelData.breakpointDecorationIds[i]);
I
isidor 已提交
206
			// check if the line got deleted.
E
Erich Gamma 已提交
207
			if (decorationRange.endColumn - decorationRange.startColumn > 0) {
I
isidor 已提交
208
				// since we know it is collapsed, it cannot grow to multiple lines
I
isidor 已提交
209
				data.push({
210
					uri: modelUrl,
I
isidor 已提交
211 212 213 214
					lineNumber: decorationRange.startLineNumber,
					enabled: enabledAndConditions[modelData.breakpointLines[i]].enabled,
					condition: enabledAndConditions[modelData.breakpointLines[i]].condition
				});
E
Erich Gamma 已提交
215 216 217
			}
		}

218
		this.debugService.setBreakpointsForModel(modelUrl, data);
E
Erich Gamma 已提交
219 220
	}

221 222 223 224
	private onBreakpointsChanged(): void {
		const breakpointsMap: { [key: string]: IBreakpoint[] } = {};
		this.debugService.getModel().getBreakpoints().forEach(bp => {
			const uriStr = bp.source.uri.toString();
E
Erich Gamma 已提交
225
			if (breakpointsMap[uriStr]) {
226
				breakpointsMap[uriStr].push(bp);
E
Erich Gamma 已提交
227
			} else {
228
				breakpointsMap[uriStr] = [bp];
E
Erich Gamma 已提交
229
			}
230
		});
E
Erich Gamma 已提交
231

232 233 234
		Object.keys(breakpointsMap).forEach(modelUriStr => {
			if (this.modelData.hasOwnProperty(modelUriStr)) {
				this.updateBreakpoints(this.modelData[modelUriStr], breakpointsMap[modelUriStr]);
E
Erich Gamma 已提交
235
			}
236 237 238
		});
		Object.keys(this.modelData).forEach(modelUriStr => {
			if (!breakpointsMap.hasOwnProperty(modelUriStr)) {
E
Erich Gamma 已提交
239 240
				this.updateBreakpoints(this.modelData[modelUriStr], []);
			}
241
		});
E
Erich Gamma 已提交
242 243
	}

244 245 246 247
	private updateBreakpoints(modelData: IDebugEditorModelData, newBreakpoints: IBreakpoint[]): void {
		modelData.breakpointDecorationIds = modelData.model.deltaDecorations(modelData.breakpointDecorationIds, this.createBreakpointDecorations(newBreakpoints));
		modelData.breakpointDecorationsAsMap = toMap(modelData.breakpointDecorationIds);
		modelData.breakpointLines = newBreakpoints.map(bp => bp.lineNumber);
E
Erich Gamma 已提交
248 249
	}

250 251 252
	private createBreakpointDecorations(breakpoints: IBreakpoint[]): editorcommon.IModelDeltaDecoration[] {
		return breakpoints.map((breakpoint) => {
			return {
I
isidor 已提交
253
				options: this.getBreakpointDecorationOptions(breakpoint),
254 255 256 257 258
				range: createRange(breakpoint.lineNumber, 1, breakpoint.lineNumber, 2)
			};
		});
	}

I
isidor 已提交
259 260 261 262 263 264 265 266 267 268 269 270
	private getBreakpointDecorationOptions(breakpoint: IBreakpoint): editorcommon.IModelDecorationOptions {
		const activated = this.debugService.getModel().areBreakpointsActivated();
		const state = this.debugService.getState();
		const debugActive = state === State.Running || state === State.Stopped || state === State.Initializing;
		const result = (!breakpoint.enabled || !activated) ? DebugEditorModelManager.BREAKPOINT_DISABLED_DECORATION :
			debugActive && !breakpoint.verified ? DebugEditorModelManager.BREAKPOINT_UNVERIFIED_DECORATION :
			!breakpoint.condition ? DebugEditorModelManager.BREAKPOINT_DECORATION : null;

		return result ? result : {
			glyphMarginClassName: 'debug-breakpoint-conditional-glyph',
			hoverMessage: breakpoint.condition,
			stickiness: editorcommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges
I
isidor 已提交
271
		};
I
isidor 已提交
272 273
	}

I
isidor 已提交
274
	// editor decorations
275

E
Erich Gamma 已提交
276 277
	private static BREAKPOINT_DECORATION: editorcommon.IModelDecorationOptions = {
		glyphMarginClassName: 'debug-breakpoint-glyph',
I
isidor 已提交
278
		hoverMessage: nls.localize('breakpointHover', "Breakpoint"),
279
		stickiness: editorcommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges
E
Erich Gamma 已提交
280 281 282 283
	};

	private static BREAKPOINT_DISABLED_DECORATION: editorcommon.IModelDecorationOptions = {
		glyphMarginClassName: 'debug-breakpoint-glyph-disabled',
I
isidor 已提交
284
		hoverMessage: nls.localize('breakpointDisabledHover', "Disabled Breakpoint"),
E
Erich Gamma 已提交
285 286 287
		stickiness: editorcommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges
	};

288 289
	private static BREAKPOINT_UNVERIFIED_DECORATION: editorcommon.IModelDecorationOptions = {
		glyphMarginClassName: 'debug-breakpoint-glyph-unverified',
I
isidor 已提交
290
		hoverMessage: nls.localize('breakpointDisabledHover', "Unverified Breakpoint"),
I
isidor 已提交
291 292 293
		stickiness: editorcommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges
	};

I
isidor 已提交
294
	// we need a separate decoration for glyph margin, since we do not want it on each line of a multi line statement.
E
Erich Gamma 已提交
295 296 297
	private static TOP_STACK_FRAME_MARGIN: editorcommon.IModelDecorationOptions = {
		glyphMarginClassName: 'debug-top-stack-frame-glyph',
		stickiness: editorcommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges
I
isidor 已提交
298
	};
E
Erich Gamma 已提交
299 300 301 302

	private static FOCUSED_STACK_FRAME_MARGIN: editorcommon.IModelDecorationOptions = {
		glyphMarginClassName: 'debug-focused-stack-frame-glyph',
		stickiness: editorcommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges
I
isidor 已提交
303
	};
E
Erich Gamma 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324

	private static TOP_STACK_FRAME_DECORATION: editorcommon.IModelDecorationOptions = {
		isWholeLine: true,
		className: 'debug-top-stack-frame-line',
		stickiness: editorcommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges
	};

	private static TOP_STACK_FRAME_EXCEPTION_DECORATION: editorcommon.IModelDecorationOptions = {
		isWholeLine: true,
		className: 'debug-top-stack-frame-exception-line',
		stickiness: editorcommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges
	};

	private static TOP_STACK_FRAME_COLUMN_DECORATION: editorcommon.IModelDecorationOptions = {
		isWholeLine: false,
		className: 'debug-top-stack-frame-column',
		stickiness: editorcommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges
	};

	private static FOCUSED_STACK_FRAME_DECORATION: editorcommon.IModelDecorationOptions = {
		isWholeLine: true,
F
Francois Valdy 已提交
325
		className: 'debug-focused-stack-frame-line',
E
Erich Gamma 已提交
326 327 328
		stickiness: editorcommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges
	};
}