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

import lifecycle = require('vs/base/common/lifecycle');
import editorcommon = require('vs/editor/common/editorCommon');
8
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
9
import { IDebugService, ModelEvents, ViewModelEvents, IBreakpoint, IRawBreakpoint, State } from 'vs/workbench/parts/debug/common/debug';
E
Erich Gamma 已提交
10 11 12 13
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
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 63
	static ID = 'breakpointManager';

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

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

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

	public dispose(): void {
I
isidor 已提交
64
		for (let modelUrlStr in this.modelData) {
E
Erich Gamma 已提交
65
			if (this.modelData.hasOwnProperty(modelUrlStr)) {
I
isidor 已提交
66
				const modelData = this.modelData[modelUrlStr];
E
Erich Gamma 已提交
67 68 69 70 71 72 73 74 75 76 77
				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 {
78
		this.toDispose.push(this.modelService.onModelAdded(this.onModelAdded, this));
79
		this.modelService.getModels().forEach(model => this.onModelAdded(model));
80
		this.toDispose.push(this.modelService.onModelRemoved(this.onModelRemoved, this));
E
Erich Gamma 已提交
81

82 83
		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 已提交
84 85 86
	}

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

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

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

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

107 108 109 110 111 112 113 114 115 116
	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 已提交
117
	// call stack management. Represent data coming from the debug service.
118 119 120 121 122

	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 已提交
123 124 125 126
		});
	}

	private createCallStackDecorations(modelUrlStr: string): editorcommon.IModelDeltaDecoration[] {
127 128 129 130
		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 已提交
131 132 133
			return result;
		}

I
isidor 已提交
134
		// only show decorations for the currently focussed thread.
135 136 137
		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 已提交
138

I
isidor 已提交
139
			// compute how to decorate the editor. Different decorations are used if this is a top stack frame, focussed stack frame,
140 141
			// 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 已提交
142 143
				result.push({
					options: DebugEditorModelManager.TOP_STACK_FRAME_MARGIN,
144
					range: createRange(sf.lineNumber, sf.column, sf.lineNumber, sf.column + 1)
E
Erich Gamma 已提交
145 146
				});

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

E
Erich Gamma 已提交
158 159 160 161 162 163 164 165 166 167 168
					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;
					}
				}
169
			} else if (sf === focusedStackFrame) {
E
Erich Gamma 已提交
170 171
				result.push({
					options: DebugEditorModelManager.FOCUSED_STACK_FRAME_MARGIN,
172
					range: createRange(sf.lineNumber, sf.column, sf.lineNumber, sf.column + 1)
E
Erich Gamma 已提交
173 174 175 176 177 178 179
				});

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

		return result;
	}

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

187 188 189
	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 已提交
190
			// nothing to do, my decorations did not change.
E
Erich Gamma 已提交
191 192 193
			return;
		}

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

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

204 205 206
		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 已提交
207
			// check if the line got deleted.
E
Erich Gamma 已提交
208
			if (decorationRange.endColumn - decorationRange.startColumn > 0) {
I
isidor 已提交
209
				// since we know it is collapsed, it cannot grow to multiple lines
I
isidor 已提交
210
				data.push({
211
					uri: modelUrl,
I
isidor 已提交
212 213 214 215
					lineNumber: decorationRange.startLineNumber,
					enabled: enabledAndConditions[modelData.breakpointLines[i]].enabled,
					condition: enabledAndConditions[modelData.breakpointLines[i]].condition
				});
E
Erich Gamma 已提交
216 217 218
			}
		}

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

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

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

245 246 247 248
	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 已提交
249 250
	}

251 252
	private createBreakpointDecorations(breakpoints: IBreakpoint[]): editorcommon.IModelDeltaDecoration[] {
		const activated = this.debugService.getModel().areBreakpointsActivated();
253 254
		const state = this.debugService.getState();
		const debugActive = state === State.Running || state === State.Stopped || state === State.Initializing;
255 256 257
		return breakpoints.map((breakpoint) => {
			return {
				options: (!breakpoint.enabled || !activated) ? DebugEditorModelManager.BREAKPOINT_DISABLED_DECORATION :
258 259
					debugActive && !breakpoint.verified ? DebugEditorModelManager.BREAKPOINT_UNVERIFIED_DECORATION :
					breakpoint.condition ? DebugEditorModelManager.CONDITIONAL_BREAKPOINT_DECORATION : DebugEditorModelManager.BREAKPOINT_DECORATION,
260 261 262 263 264
				range: createRange(breakpoint.lineNumber, 1, breakpoint.lineNumber, 2)
			};
		});
	}

I
isidor 已提交
265
	// editor decorations
266

E
Erich Gamma 已提交
267 268 269
	private static BREAKPOINT_DECORATION: editorcommon.IModelDecorationOptions = {
		glyphMarginClassName: 'debug-breakpoint-glyph',
		stickiness: editorcommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges
270 271 272 273 274
	};

	private static CONDITIONAL_BREAKPOINT_DECORATION: editorcommon.IModelDecorationOptions = {
		glyphMarginClassName: 'debug-breakpoint-conditional-glyph',
		stickiness: editorcommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges
E
Erich Gamma 已提交
275 276 277 278 279 280 281
	};

	private static BREAKPOINT_DISABLED_DECORATION: editorcommon.IModelDecorationOptions = {
		glyphMarginClassName: 'debug-breakpoint-glyph-disabled',
		stickiness: editorcommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges
	};

282 283
	private static BREAKPOINT_UNVERIFIED_DECORATION: editorcommon.IModelDecorationOptions = {
		glyphMarginClassName: 'debug-breakpoint-glyph-unverified',
I
isidor 已提交
284 285 286
		stickiness: editorcommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges
	};

I
isidor 已提交
287
	// 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 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	private static TOP_STACK_FRAME_MARGIN: editorcommon.IModelDecorationOptions = {
		glyphMarginClassName: 'debug-top-stack-frame-glyph',
		stickiness: editorcommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges
	}

	private static FOCUSED_STACK_FRAME_MARGIN: editorcommon.IModelDecorationOptions = {
		glyphMarginClassName: 'debug-focused-stack-frame-glyph',
		stickiness: editorcommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges
	}

	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 已提交
318
		className: 'debug-focused-stack-frame-line',
E
Erich Gamma 已提交
319 320 321
		stickiness: editorcommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges
	};
}