breakpointWidget.ts 12.5 KB
Newer Older
I
isidor 已提交
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 'vs/css!../browser/media/breakpointWidget';
7
import * as nls from 'vs/nls';
I
isidor 已提交
8
import * as errors from 'vs/base/common/errors';
I
isidor 已提交
9
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
J
Johannes Rieken 已提交
10
import { SelectBox } from 'vs/base/browser/ui/selectBox/selectBox';
I
isidor 已提交
11 12
import * as lifecycle from 'vs/base/common/lifecycle';
import * as dom from 'vs/base/browser/dom';
13
import { Position } from 'vs/editor/common/core/position';
I
isidor 已提交
14
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
15
import { ZoneWidget } from 'vs/editor/contrib/zoneWidget/zoneWidget';
J
Johannes Rieken 已提交
16
import { IContextViewService } from 'vs/platform/contextview/browser/contextView';
I
isidor 已提交
17
import { IDebugService, IBreakpoint, BreakpointWidgetContext as Context, CONTEXT_BREAKPOINT_WIDGET_VISIBLE, DEBUG_SCHEME, IDebugEditorContribution, EDITOR_CONTRIBUTION_ID, CONTEXT_IN_BREAKPOINT_WIDGET } from 'vs/workbench/parts/debug/common/debug';
I
isidor 已提交
18
import { attachSelectBoxStyler } from 'vs/platform/theme/common/styler';
B
Benjamin Pasero 已提交
19
import { IThemeService } from 'vs/platform/theme/common/themeService';
I
isidor 已提交
20 21 22 23 24 25 26
import { SimpleDebugEditor } from 'vs/workbench/parts/debug/electron-browser/simpleDebugEditor';
import { createDecorator, IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { ServicesAccessor, EditorCommand, registerEditorCommand } from 'vs/editor/browser/editorExtensions';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { IModelService } from 'vs/editor/common/services/modelService';
import uri from 'vs/base/common/uri';
I
isidor 已提交
27
import { SuggestRegistry, ISuggestResult, SuggestContext } from 'vs/editor/common/modes';
28 29 30 31 32
import { CancellationToken } from 'vs/base/common/cancellation';
import { ITextModel } from 'vs/editor/common/model';
import { wireCancellationToken } from 'vs/base/common/async';
import { provideSuggestionItems } from 'vs/editor/contrib/suggest/suggest';
import { TPromise } from 'vs/base/common/winjs.base';
33 34
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
import { transparent, editorForeground } from 'vs/platform/theme/common/colorRegistry';
35 36
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
import { IDecorationOptions } from 'vs/editor/common/editorCommon';
I
isidor 已提交
37

J
Joao Moreno 已提交
38
const $ = dom.$;
I
isidor 已提交
39 40 41 42 43
const IPrivateBreakopintWidgetService = createDecorator<IPrivateBreakopintWidgetService>('privateBreakopintWidgetService');
export interface IPrivateBreakopintWidgetService {
	_serviceBrand: any;
	close(success: boolean): void;
}
44
const DECORATION_KEY = 'breakpointwidgetdecoration';
I
isidor 已提交
45

I
isidor 已提交
46 47
export class BreakpointWidget extends ZoneWidget implements IPrivateBreakopintWidgetService {
	public _serviceBrand: any;
48

I
isidor 已提交
49 50
	private selectContainer: HTMLElement;
	private input: SimpleDebugEditor;
51
	private toDispose: lifecycle.IDisposable[];
I
isidor 已提交
52 53 54
	private conditionInput = '';
	private hitCountInput = '';
	private logMessageInput = '';
55
	private breakpoint: IBreakpoint;
I
isidor 已提交
56

57
	constructor(editor: ICodeEditor, private lineNumber: number, private column: number, private context: Context,
I
isidor 已提交
58
		@IContextViewService private contextViewService: IContextViewService,
B
Benjamin Pasero 已提交
59
		@IDebugService private debugService: IDebugService,
I
isidor 已提交
60 61 62
		@IThemeService private themeService: IThemeService,
		@IContextKeyService private contextKeyService: IContextKeyService,
		@IInstantiationService private instantiationService: IInstantiationService,
63 64
		@IModelService private modelService: IModelService,
		@ICodeEditorService private codeEditorService: ICodeEditorService,
I
isidor 已提交
65
	) {
66
		super(editor, { showFrame: true, showArrow: false, frameWidth: 1 });
67

68
		this.toDispose = [];
69
		const uri = this.editor.getModel().uri;
70
		this.breakpoint = this.debugService.getModel().getBreakpoints().filter(bp => bp.lineNumber === this.lineNumber && bp.uri.toString() === uri.toString()).pop();
71

72 73 74 75 76 77 78 79 80 81
		if (this.context === undefined) {
			if (this.breakpoint && !this.breakpoint.condition && !this.breakpoint.hitCondition && this.breakpoint.logMessage) {
				this.context = Context.LOG_MESSAGE;
			} else if (this.breakpoint && !this.breakpoint.condition && this.breakpoint.hitCondition) {
				this.context = Context.HIT_COUNT;
			} else {
				this.context = Context.CONDITION;
			}
		}

82 83 84 85 86
		this.toDispose.push(this.debugService.getModel().onDidChangeBreakpoints(e => {
			if (this.breakpoint && e.removed && e.removed.indexOf(this.breakpoint) >= 0) {
				this.dispose();
			}
		}));
87 88
		this.codeEditorService.registerDecorationType(DECORATION_KEY, {});

I
isidor 已提交
89 90 91
		this.create();
	}

92 93 94 95 96 97 98 99 100 101
	private get placeholder(): string {
		switch (this.context) {
			case Context.LOG_MESSAGE:
				return nls.localize('breakpointWidgetLogMessagePlaceholder', "Message to log when breakpoint is hit. Expressions within {} are interpolated. 'Enter' to accept, 'esc' to cancel.");
			case Context.HIT_COUNT:
				return nls.localize('breakpointWidgetHitCountPlaceholder', "Break when hit count condition is met. 'Enter' to accept, 'esc' to cancel.");
			default:
				return nls.localize('breakpointWidgetExpressionPlaceholder', "Break when expression evaluates to true. 'Enter' to accept, 'esc' to cancel.");
		}
	}
102

103
	private getInputValue(breakpoint: IBreakpoint): string {
I
isidor 已提交
104 105 106 107 108 109 110
		switch (this.context) {
			case Context.LOG_MESSAGE:
				return breakpoint && breakpoint.logMessage ? breakpoint.logMessage : this.logMessageInput;
			case Context.HIT_COUNT:
				return breakpoint && breakpoint.hitCondition ? breakpoint.hitCondition : this.hitCountInput;
			default:
				return breakpoint && breakpoint.condition ? breakpoint.condition : this.conditionInput;
111
		}
I
isidor 已提交
112
	}
113

I
isidor 已提交
114
	private rememberInput(): void {
I
isidor 已提交
115
		const value = this.input.getModel().getValue();
I
isidor 已提交
116 117
		switch (this.context) {
			case Context.LOG_MESSAGE:
I
isidor 已提交
118
				this.logMessageInput = value;
I
isidor 已提交
119 120
				break;
			case Context.HIT_COUNT:
I
isidor 已提交
121
				this.hitCountInput = value;
I
isidor 已提交
122 123
				break;
			default:
I
isidor 已提交
124
				this.conditionInput = value;
I
isidor 已提交
125
		}
126 127
	}

J
Johannes Rieken 已提交
128
	protected _fillContainer(container: HTMLElement): void {
129
		this.setCssClass('breakpoint-widget');
130
		const selectBox = new SelectBox([nls.localize('expression', "Expression"), nls.localize('hitCount', "Hit Count"), nls.localize('logMessage', "Log Message")], this.context, this.contextViewService);
131
		this.toDispose.push(attachSelectBoxStyler(selectBox, this.themeService));
I
isidor 已提交
132 133
		this.selectContainer = $('.breakpoint-select-container');
		selectBox.render(dom.append(container, this.selectContainer));
134
		selectBox.onDidSelect(e => {
I
isidor 已提交
135 136
			this.rememberInput();
			this.context = e.index;
137

138 139
			const value = this.getInputValue(this.breakpoint);
			this.input.getModel().setValue(value);
140 141
		});

I
isidor 已提交
142
		this.createBreakpointInput(dom.append(container, $('.inputContainer')));
143

I
isidor 已提交
144
		this.input.getModel().setValue(this.getInputValue(this.breakpoint));
145
		this.input.setPosition({ lineNumber: 1, column: this.input.getModel().getLineMaxColumn(1) });
I
isidor 已提交
146
		// Due to an electron bug we have to do the timeout, otherwise we do not get focus
I
isidor 已提交
147
		setTimeout(() => this.input.focus(), 100);
I
isidor 已提交
148
	}
I
isidor 已提交
149

I
isidor 已提交
150 151 152
	public close(success: boolean): void {
		if (success) {
			// if there is already a breakpoint on this location - remove it.
I
isidor 已提交
153

I
isidor 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166
			let condition = this.breakpoint && this.breakpoint.condition;
			let hitCondition = this.breakpoint && this.breakpoint.hitCondition;
			let logMessage = this.breakpoint && this.breakpoint.logMessage;
			this.rememberInput();

			if (this.conditionInput) {
				condition = this.conditionInput;
			}
			if (this.hitCountInput) {
				hitCondition = this.hitCountInput;
			}
			if (this.logMessageInput) {
				logMessage = this.logMessageInput;
I
isidor 已提交
167 168
			}

I
isidor 已提交
169 170 171 172 173 174
			if (this.breakpoint) {
				this.debugService.updateBreakpoints(this.breakpoint.uri, {
					[this.breakpoint.getId()]: {
						condition,
						hitCondition,
						verified: this.breakpoint.verified,
I
isidor 已提交
175
						column: this.breakpoint.column,
I
isidor 已提交
176 177 178 179 180 181 182 183 184 185 186
						logMessage
					}
				}, false);
			} else {
				this.debugService.addBreakpoints(this.editor.getModel().uri, [{
					lineNumber: this.lineNumber,
					enabled: true,
					condition,
					hitCondition,
					logMessage
				}]).done(null, errors.onUnexpectedError);
I
isidor 已提交
187
			}
I
isidor 已提交
188 189 190 191 192 193
		}

		this.dispose();
	}

	protected _doLayout(heightInPixel: number, widthInPixel: number): void {
194
		this.input.layout({ height: 18, width: widthInPixel - 113 });
I
isidor 已提交
195 196 197 198 199 200 201 202 203 204 205
	}

	private createBreakpointInput(container: HTMLElement): void {
		const scopedContextKeyService = this.contextKeyService.createScoped(container);
		this.toDispose.push(scopedContextKeyService);

		const scopedInstatiationService = this.instantiationService.createChild(new ServiceCollection(
			[IContextKeyService, scopedContextKeyService], [IPrivateBreakopintWidgetService, this]));

		const options = SimpleDebugEditor.getEditorOptions();
		this.input = scopedInstatiationService.createInstance(SimpleDebugEditor, container, options);
I
isidor 已提交
206
		CONTEXT_IN_BREAKPOINT_WIDGET.bindTo(scopedContextKeyService).set(true);
I
isidor 已提交
207 208 209
		const model = this.modelService.createModel('', null, uri.parse(`${DEBUG_SCHEME}:breakpointinput`), true);
		this.input.setModel(model);
		this.toDispose.push(model);
210 211 212 213 214 215 216
		const setDecorations = () => {
			const value = this.input.getModel().getValue();
			const decorations = !!value ? [] : this.createDecorations();
			this.input.setDecorations(DECORATION_KEY, decorations);
		};
		this.input.getModel().onDidChangeContent(() => setDecorations());
		this.themeService.onThemeChange(() => setDecorations());
217

218
		this.toDispose.push(SuggestRegistry.register({ scheme: DEBUG_SCHEME, hasAccessToAllModels: true }, {
219
			provideCompletionItems: (model: ITextModel, position: Position, _context: SuggestContext, token: CancellationToken): Thenable<ISuggestResult> => {
I
isidor 已提交
220
				let suggestionsPromise: TPromise<ISuggestResult>;
221
				if (this.context === Context.CONDITION || this.context === Context.LOG_MESSAGE && this.isCurlyBracketOpen()) {
I
isidor 已提交
222
					suggestionsPromise = provideSuggestionItems(this.editor.getModel(), new Position(this.lineNumber, this.column), 'none', undefined, _context).then(suggestions => {
I
isidor 已提交
223 224 225 226 227 228 229 230 231 232
						return {
							suggestions: suggestions.map(s => {
								if (this.context === Context.CONDITION) {
									s.suggestion.overwriteBefore = position.column - 1;
									s.suggestion.overwriteAfter = 0;
								}

								return s.suggestion;
							})
						};
233 234
					});
				} else {
I
isidor 已提交
235
					suggestionsPromise = TPromise.as({ suggestions: [] });
236 237
				}

I
isidor 已提交
238
				return wireCancellationToken(token, suggestionsPromise);
239
			}
240
		}));
241 242
	}

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
	private createDecorations(): IDecorationOptions[] {
		return [{
			range: {
				startLineNumber: 0,
				endLineNumber: 0,
				startColumn: 0,
				endColumn: 1
			},
			renderOptions: {
				after: {
					contentText: this.placeholder,
					color: transparent(editorForeground, 0.4)(this.themeService.getTheme()).toString()
				}
			}
		}];
	}

260 261 262 263 264 265 266 267 268 269 270 271 272
	private isCurlyBracketOpen(): boolean {
		const value = this.input.getModel().getValue();
		for (let i = this.input.getPosition().column - 2; i >= 0; i--) {
			if (value[i] === '{') {
				return true;
			}

			if (value[i] === '}') {
				return false;
			}
		}

		return false;
I
isidor 已提交
273
	}
274 275 276

	public dispose(): void {
		super.dispose();
I
isidor 已提交
277
		this.input.dispose();
J
Joao Moreno 已提交
278
		lifecycle.dispose(this.toDispose);
279
		setTimeout(() => this.editor.focus(), 0);
280
	}
I
isidor 已提交
281
}
I
isidor 已提交
282 283 284 285 286 287

class AcceptBreakpointWidgetInputAction extends EditorCommand {

	constructor() {
		super({
			id: 'breakpointWidget.action.acceptInput',
I
isidor 已提交
288
			precondition: CONTEXT_BREAKPOINT_WIDGET_VISIBLE,
I
isidor 已提交
289
			kbOpts: {
I
isidor 已提交
290
				kbExpr: CONTEXT_IN_BREAKPOINT_WIDGET,
I
isidor 已提交
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 318 319 320 321 322 323 324 325 326 327
				primary: KeyCode.Enter
			}
		});
	}

	public runEditorCommand(accessor: ServicesAccessor, editor: ICodeEditor): void {
		accessor.get(IPrivateBreakopintWidgetService).close(true);
	}
}

class CloseBreakpointWidgetCommand extends EditorCommand {

	constructor() {
		super({
			id: 'closeBreakpointWidget',
			precondition: CONTEXT_BREAKPOINT_WIDGET_VISIBLE,
			kbOpts: {
				kbExpr: EditorContextKeys.textInputFocus,
				primary: KeyCode.Escape,
				secondary: [KeyMod.Shift | KeyCode.Escape]
			}
		});
	}

	public runEditorCommand(accessor: ServicesAccessor, editor: ICodeEditor, args: any): void {
		const debugContribution = editor.getContribution<IDebugEditorContribution>(EDITOR_CONTRIBUTION_ID);
		if (debugContribution) {
			// if focus is in outer editor we need to use the debug contribution to close
			return debugContribution.closeBreakpointWidget();
		}

		accessor.get(IPrivateBreakopintWidgetService).close(false);
	}
}

registerEditorCommand(new AcceptBreakpointWidgetInputAction());
registerEditorCommand(new CloseBreakpointWidgetCommand());