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

import 'vs/css!./media/editor';
import 'vs/css!./media/tokens';
J
Johannes Rieken 已提交
9
import { onUnexpectedError } from 'vs/base/common/errors';
10
import { TPromise } from 'vs/base/common/winjs.base';
A
Alex Dima 已提交
11
import * as dom from 'vs/base/browser/dom';
J
Johannes Rieken 已提交
12 13 14 15 16
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { CommonCodeEditor } from 'vs/editor/common/commonCodeEditor';
import { CommonEditorConfiguration } from 'vs/editor/common/config/commonEditorConfig';
A
Alex Dima 已提交
17
import * as editorCommon from 'vs/editor/common/editorCommon';
18
import { EditorAction, EditorExtensionsRegistry, IEditorContributionCtor } from 'vs/editor/browser/editorExtensions';
19
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
J
Johannes Rieken 已提交
20
import { Configuration } from 'vs/editor/browser/config/configuration';
A
Alex Dima 已提交
21
import * as editorBrowser from 'vs/editor/browser/editorBrowser';
A
Alex Dima 已提交
22
import { View, IOverlayWidgetData, IContentWidgetData } from 'vs/editor/browser/view/viewImpl';
23
import { Disposable, IDisposable } from 'vs/base/common/lifecycle';
M
Matt Bierner 已提交
24
import { Event, Emitter } from 'vs/base/common/event';
J
Johannes Rieken 已提交
25 26
import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { InternalEditorAction } from 'vs/editor/common/editorAction';
27 28
import { IEditorOptions } from 'vs/editor/common/config/editorOptions';
import { IPosition } from 'vs/editor/common/core/position';
29
import { CoreEditorCommand } from 'vs/editor/browser/controller/coreCommands';
30
import { IThemeService, registerThemingParticipant } from 'vs/platform/theme/common/themeService';
31
import { editorErrorForeground, editorErrorBorder, editorWarningForeground, editorWarningBorder, editorInfoBorder, editorInfoForeground, editorHintForeground, editorHintBorder } from 'vs/editor/common/view/editorColorRegistry';
32
import { Color } from 'vs/base/common/color';
B
Benjamin Pasero 已提交
33
import { IMouseEvent } from 'vs/base/browser/mouseEvent';
34
import { ClassName } from 'vs/editor/common/model/intervalTree';
A
Alex Dima 已提交
35
import { ITextModel, IModelDecorationOptions } from 'vs/editor/common/model';
I
isidor 已提交
36
import { ICommandDelegate } from 'vs/editor/browser/view/viewController';
E
Erich Gamma 已提交
37

38
export abstract class CodeEditorWidget extends CommonCodeEditor implements editorBrowser.ICodeEditor {
E
Erich Gamma 已提交
39

A
Alex Dima 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	private readonly _onMouseUp: Emitter<editorBrowser.IEditorMouseEvent> = this._register(new Emitter<editorBrowser.IEditorMouseEvent>());
	public readonly onMouseUp: Event<editorBrowser.IEditorMouseEvent> = this._onMouseUp.event;

	private readonly _onMouseDown: Emitter<editorBrowser.IEditorMouseEvent> = this._register(new Emitter<editorBrowser.IEditorMouseEvent>());
	public readonly onMouseDown: Event<editorBrowser.IEditorMouseEvent> = this._onMouseDown.event;

	private readonly _onMouseDrag: Emitter<editorBrowser.IEditorMouseEvent> = this._register(new Emitter<editorBrowser.IEditorMouseEvent>());
	public readonly onMouseDrag: Event<editorBrowser.IEditorMouseEvent> = this._onMouseDrag.event;

	private readonly _onMouseDrop: Emitter<editorBrowser.IEditorMouseEvent> = this._register(new Emitter<editorBrowser.IEditorMouseEvent>());
	public readonly onMouseDrop: Event<editorBrowser.IEditorMouseEvent> = this._onMouseDrop.event;

	private readonly _onContextMenu: Emitter<editorBrowser.IEditorMouseEvent> = this._register(new Emitter<editorBrowser.IEditorMouseEvent>());
	public readonly onContextMenu: Event<editorBrowser.IEditorMouseEvent> = this._onContextMenu.event;

	private readonly _onMouseMove: Emitter<editorBrowser.IEditorMouseEvent> = this._register(new Emitter<editorBrowser.IEditorMouseEvent>());
	public readonly onMouseMove: Event<editorBrowser.IEditorMouseEvent> = this._onMouseMove.event;

	private readonly _onMouseLeave: Emitter<editorBrowser.IEditorMouseEvent> = this._register(new Emitter<editorBrowser.IEditorMouseEvent>());
	public readonly onMouseLeave: Event<editorBrowser.IEditorMouseEvent> = this._onMouseLeave.event;

	private readonly _onKeyUp: Emitter<IKeyboardEvent> = this._register(new Emitter<IKeyboardEvent>());
	public readonly onKeyUp: Event<IKeyboardEvent> = this._onKeyUp.event;

	private readonly _onKeyDown: Emitter<IKeyboardEvent> = this._register(new Emitter<IKeyboardEvent>());
	public readonly onKeyDown: Event<IKeyboardEvent> = this._onKeyDown.event;

	private readonly _onDidScrollChange: Emitter<editorCommon.IScrollEvent> = this._register(new Emitter<editorCommon.IScrollEvent>());
	public readonly onDidScrollChange: Event<editorCommon.IScrollEvent> = this._onDidScrollChange.event;

	private readonly _onDidChangeViewZones: Emitter<void> = this._register(new Emitter<void>());
	public readonly onDidChangeViewZones: Event<void> = this._onDidChangeViewZones.event;
A
Alex Dima 已提交
72

73
	private _codeEditorService: ICodeEditorService;
74
	private _commandService: ICommandService;
75
	private _themeService: IThemeService;
76

J
Johannes Rieken 已提交
77
	protected domElement: HTMLElement;
78
	private _focusTracker: CodeEditorWidgetFocusTracker;
E
Erich Gamma 已提交
79

J
Johannes Rieken 已提交
80
	_configuration: Configuration;
81

A
Alex Dima 已提交
82 83
	private contentWidgets: { [key: string]: IContentWidgetData; };
	private overlayWidgets: { [key: string]: IOverlayWidgetData; };
E
Erich Gamma 已提交
84

A
Alex Dima 已提交
85
	_view: View;
E
Erich Gamma 已提交
86 87

	constructor(
J
Johannes Rieken 已提交
88
		domElement: HTMLElement,
89
		options: IEditorOptions,
90
		isSimpleWidget: boolean,
E
Erich Gamma 已提交
91 92
		@IInstantiationService instantiationService: IInstantiationService,
		@ICodeEditorService codeEditorService: ICodeEditorService,
93
		@ICommandService commandService: ICommandService,
94 95
		@IContextKeyService contextKeyService: IContextKeyService,
		@IThemeService themeService: IThemeService
E
Erich Gamma 已提交
96
	) {
97
		super(domElement, options, isSimpleWidget, instantiationService, contextKeyService);
98
		this._codeEditorService = codeEditorService;
99
		this._commandService = commandService;
100
		this._themeService = themeService;
E
Erich Gamma 已提交
101

102
		this._focusTracker = new CodeEditorWidgetFocusTracker(domElement);
A
Alex Dima 已提交
103
		this._focusTracker.onChange(() => {
104
			let hasFocus = this._focusTracker.hasFocus();
105

106
			if (hasFocus) {
A
Alex Dima 已提交
107
				this._onDidFocusEditor.fire();
108
			} else {
A
Alex Dima 已提交
109
				this._onDidBlurEditor.fire();
E
Erich Gamma 已提交
110 111 112 113 114 115
			}
		});

		this.contentWidgets = {};
		this.overlayWidgets = {};

116 117 118
		let contributions = this._getContributions();
		for (let i = 0, len = contributions.length; i < len; i++) {
			let ctor = contributions[i];
E
Erich Gamma 已提交
119
			try {
120
				let contribution = this._instantiationService.createInstance(ctor, this);
A
Alex Dima 已提交
121
				this._contributions[contribution.getId()] = contribution;
E
Erich Gamma 已提交
122
			} catch (err) {
123
				onUnexpectedError(err);
E
Erich Gamma 已提交
124 125
			}
		}
126

127
		this._getActions().forEach((action) => {
128 129 130 131 132 133 134 135 136 137 138 139
			const internalAction = new InternalEditorAction(
				action.id,
				action.label,
				action.alias,
				action.precondition,
				(): void | TPromise<void> => {
					return this._instantiationService.invokeFunction((accessor) => {
						return action.runEditorCommand(accessor, this, null);
					});
				},
				this._contextKeyService
			);
A
Alex Dima 已提交
140
			this._actions[internalAction.id] = internalAction;
141
		});
142 143

		this._codeEditorService.addCodeEditor(this);
E
Erich Gamma 已提交
144 145
	}

146
	protected abstract _getContributions(): IEditorContributionCtor[];
147 148
	protected abstract _getActions(): EditorAction[];

A
Alex Dima 已提交
149
	protected _createConfiguration(options: IEditorOptions): CommonEditorConfiguration {
150
		return new Configuration(options, this.domElement);
E
Erich Gamma 已提交
151 152 153
	}

	public dispose(): void {
154 155
		this._codeEditorService.removeCodeEditor(this);

E
Erich Gamma 已提交
156 157 158
		this.contentWidgets = {};
		this.overlayWidgets = {};

159
		this._focusTracker.dispose();
E
Erich Gamma 已提交
160 161 162
		super.dispose();
	}

163 164
	public createOverviewRuler(cssClassName: string): editorBrowser.IOverviewRuler {
		return this._view.createOverviewRuler(cssClassName);
E
Erich Gamma 已提交
165 166 167 168 169 170
	}

	public getDomNode(): HTMLElement {
		if (!this.hasView) {
			return null;
		}
A
Alex Dima 已提交
171
		return this._view.domNode.domNode;
E
Erich Gamma 已提交
172 173
	}

174
	public delegateVerticalScrollbarMouseDown(browserEvent: IMouseEvent): void {
E
Erich Gamma 已提交
175
		if (!this.hasView) {
176
			return;
E
Erich Gamma 已提交
177
		}
A
Alex Dima 已提交
178
		this._view.delegateVerticalScrollbarMouseDown(browserEvent);
E
Erich Gamma 已提交
179 180
	}

J
Johannes Rieken 已提交
181
	public layout(dimension?: editorCommon.IDimension): void {
E
Erich Gamma 已提交
182
		this._configuration.observeReferenceElement(dimension);
183
		this.render();
E
Erich Gamma 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196
	}

	public focus(): void {
		if (!this.hasView) {
			return;
		}
		this._view.focus();
	}

	public isFocused(): boolean {
		return this.hasView && this._view.isFocused();
	}

197
	public hasWidgetFocus(): boolean {
198
		return this._focusTracker && this._focusTracker.hasFocus();
199 200
	}

A
Alex Dima 已提交
201
	public addContentWidget(widget: editorBrowser.IContentWidget): void {
A
Alex Dima 已提交
202
		let widgetData: IContentWidgetData = {
E
Erich Gamma 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
			widget: widget,
			position: widget.getPosition()
		};

		if (this.contentWidgets.hasOwnProperty(widget.getId())) {
			console.warn('Overwriting a content widget with the same id.');
		}

		this.contentWidgets[widget.getId()] = widgetData;

		if (this.hasView) {
			this._view.addContentWidget(widgetData);
		}
	}

A
Alex Dima 已提交
218
	public layoutContentWidget(widget: editorBrowser.IContentWidget): void {
A
Alex Dima 已提交
219
		let widgetId = widget.getId();
E
Erich Gamma 已提交
220
		if (this.contentWidgets.hasOwnProperty(widgetId)) {
A
Alex Dima 已提交
221
			let widgetData = this.contentWidgets[widgetId];
E
Erich Gamma 已提交
222 223 224 225 226 227 228
			widgetData.position = widget.getPosition();
			if (this.hasView) {
				this._view.layoutContentWidget(widgetData);
			}
		}
	}

A
Alex Dima 已提交
229
	public removeContentWidget(widget: editorBrowser.IContentWidget): void {
A
Alex Dima 已提交
230
		let widgetId = widget.getId();
E
Erich Gamma 已提交
231
		if (this.contentWidgets.hasOwnProperty(widgetId)) {
A
Alex Dima 已提交
232
			let widgetData = this.contentWidgets[widgetId];
E
Erich Gamma 已提交
233 234 235 236 237 238 239
			delete this.contentWidgets[widgetId];
			if (this.hasView) {
				this._view.removeContentWidget(widgetData);
			}
		}
	}

A
Alex Dima 已提交
240
	public addOverlayWidget(widget: editorBrowser.IOverlayWidget): void {
A
Alex Dima 已提交
241
		let widgetData: IOverlayWidgetData = {
E
Erich Gamma 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
			widget: widget,
			position: widget.getPosition()
		};

		if (this.overlayWidgets.hasOwnProperty(widget.getId())) {
			console.warn('Overwriting an overlay widget with the same id.');
		}

		this.overlayWidgets[widget.getId()] = widgetData;

		if (this.hasView) {
			this._view.addOverlayWidget(widgetData);
		}
	}

A
Alex Dima 已提交
257
	public layoutOverlayWidget(widget: editorBrowser.IOverlayWidget): void {
A
Alex Dima 已提交
258
		let widgetId = widget.getId();
E
Erich Gamma 已提交
259
		if (this.overlayWidgets.hasOwnProperty(widgetId)) {
A
Alex Dima 已提交
260
			let widgetData = this.overlayWidgets[widgetId];
E
Erich Gamma 已提交
261 262 263 264 265 266 267
			widgetData.position = widget.getPosition();
			if (this.hasView) {
				this._view.layoutOverlayWidget(widgetData);
			}
		}
	}

A
Alex Dima 已提交
268
	public removeOverlayWidget(widget: editorBrowser.IOverlayWidget): void {
A
Alex Dima 已提交
269
		let widgetId = widget.getId();
E
Erich Gamma 已提交
270
		if (this.overlayWidgets.hasOwnProperty(widgetId)) {
A
Alex Dima 已提交
271
			let widgetData = this.overlayWidgets[widgetId];
E
Erich Gamma 已提交
272 273 274 275 276 277 278
			delete this.overlayWidgets[widgetId];
			if (this.hasView) {
				this._view.removeOverlayWidget(widgetData);
			}
		}
	}

J
Johannes Rieken 已提交
279
	public changeViewZones(callback: (accessor: editorBrowser.IViewZoneChangeAccessor) => void): void {
E
Erich Gamma 已提交
280 281 282
		if (!this.hasView) {
			return;
		}
A
Alex Dima 已提交
283
		let hasChanges = this._view.change(callback);
E
Erich Gamma 已提交
284
		if (hasChanges) {
A
Alex Dima 已提交
285
			this._onDidChangeViewZones.fire();
E
Erich Gamma 已提交
286 287 288
		}
	}

289 290 291 292
	public getTargetAtClientPoint(clientX: number, clientY: number): editorBrowser.IMouseTarget {
		if (!this.hasView) {
			return null;
		}
A
Alex Dima 已提交
293
		return this._view.getTargetAtClientPoint(clientX, clientY);
294 295
	}

A
Alex Dima 已提交
296
	public getScrolledVisiblePosition(rawPosition: IPosition): { top: number; left: number; height: number; } {
E
Erich Gamma 已提交
297 298 299 300
		if (!this.hasView) {
			return null;
		}

A
Alex Dima 已提交
301 302
		let position = this.model.validatePosition(rawPosition);
		let layoutInfo = this._configuration.editor.layoutInfo;
E
Erich Gamma 已提交
303

304 305
		let top = this._getVerticalOffsetForPosition(position.lineNumber, position.column) - this.getScrollTop();
		let left = this._view.getOffsetForColumn(position.lineNumber, position.column) + layoutInfo.glyphMarginWidth + layoutInfo.lineNumbersWidth + layoutInfo.decorationsWidth - this.getScrollLeft();
E
Erich Gamma 已提交
306 307 308 309 310 311 312 313

		return {
			top: top,
			left: left,
			height: this._configuration.editor.lineHeight
		};
	}

J
Johannes Rieken 已提交
314
	public getOffsetForColumn(lineNumber: number, column: number): number {
E
Erich Gamma 已提交
315 316 317
		if (!this.hasView) {
			return -1;
		}
A
Alex Dima 已提交
318
		return this._view.getOffsetForColumn(lineNumber, column);
E
Erich Gamma 已提交
319 320
	}

321 322 323 324
	public render(): void {
		if (!this.hasView) {
			return;
		}
325
		this._view.render(true, false);
326 327
	}

J
Johannes Rieken 已提交
328
	public applyFontInfo(target: HTMLElement): void {
329 330 331
		Configuration.applyFontInfoSlow(target, this._configuration.editor.fontInfo);
	}

A
Alex Dima 已提交
332
	_attachModel(model: ITextModel): void {
E
Erich Gamma 已提交
333 334
		this._view = null;

335
		super._attachModel(model);
E
Erich Gamma 已提交
336

337
		if (this._view) {
A
Alex Dima 已提交
338
			this.domElement.appendChild(this._view.domNode.domNode);
E
Erich Gamma 已提交
339

A
Alex Dima 已提交
340 341 342 343 344
			let keys = Object.keys(this.contentWidgets);
			for (let i = 0, len = keys.length; i < len; i++) {
				let widgetId = keys[i];
				this._view.addContentWidget(this.contentWidgets[widgetId]);
			}
E
Erich Gamma 已提交
345

A
Alex Dima 已提交
346 347 348 349 350
			keys = Object.keys(this.overlayWidgets);
			for (let i = 0, len = keys.length; i < len; i++) {
				let widgetId = keys[i];
				this._view.addOverlayWidget(this.overlayWidgets[widgetId]);
			}
E
Erich Gamma 已提交
351

A
Alex Dima 已提交
352 353
			this._view.render(false, true);
			this.hasView = true;
S
smoke:  
Sandeep Somavarapu 已提交
354
			this._view.domNode.domNode.setAttribute('data-uri', model.uri.toString());
E
Erich Gamma 已提交
355 356 357
		}
	}

358 359 360 361
	protected _scheduleAtNextAnimationFrame(callback: () => void): IDisposable {
		return dom.scheduleAtNextAnimationFrame(callback);
	}

362
	protected _createView(): void {
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
		let commandDelegate: ICommandDelegate;
		if (this.isSimpleWidget) {
			commandDelegate = {
				paste: (source: string, text: string, pasteOnNewLine: boolean, multicursorText: string[]) => {
					this.cursor.trigger(source, editorCommon.Handler.Paste, { text, pasteOnNewLine, multicursorText });
				},
				type: (source: string, text: string) => {
					this.cursor.trigger(source, editorCommon.Handler.Type, { text });
				},
				replacePreviousChar: (source: string, text: string, replaceCharCnt: number) => {
					this.cursor.trigger(source, editorCommon.Handler.ReplacePreviousChar, { text, replaceCharCnt });
				},
				compositionStart: (source: string) => {
					this.cursor.trigger(source, editorCommon.Handler.CompositionStart, undefined);
				},
				compositionEnd: (source: string) => {
					this.cursor.trigger(source, editorCommon.Handler.CompositionEnd, undefined);
				},
				cut: (source: string) => {
					this.cursor.trigger(source, editorCommon.Handler.Cut, undefined);
				}
			};
		} else {
			commandDelegate = {
				paste: (source: string, text: string, pasteOnNewLine: boolean, multicursorText: string[]) => {
					this._commandService.executeCommand(editorCommon.Handler.Paste, {
						text: text,
						pasteOnNewLine: pasteOnNewLine,
						multicursorText: multicursorText
					});
				},
				type: (source: string, text: string) => {
					this._commandService.executeCommand(editorCommon.Handler.Type, {
						text: text
					});
				},
				replacePreviousChar: (source: string, text: string, replaceCharCnt: number) => {
					this._commandService.executeCommand(editorCommon.Handler.ReplacePreviousChar, {
						text: text,
						replaceCharCnt: replaceCharCnt
					});
				},
				compositionStart: (source: string) => {
					this._commandService.executeCommand(editorCommon.Handler.CompositionStart, {});
				},
				compositionEnd: (source: string) => {
					this._commandService.executeCommand(editorCommon.Handler.CompositionEnd, {});
				},
				cut: (source: string) => {
					this._commandService.executeCommand(editorCommon.Handler.Cut, {});
				}
			};
		}

417
		this._view = new View(
418
			commandDelegate,
419
			this._configuration,
420
			this._themeService,
421
			this.viewModel,
422
			this.cursor,
423
			(editorCommand: CoreEditorCommand, args: any) => {
A
Alex Dima 已提交
424 425 426
				if (!this.cursor) {
					return;
				}
427
				editorCommand.runCoreEditorCommand(this.cursor, args);
A
Alex Dima 已提交
428
			}
429
		);
E
Erich Gamma 已提交
430

A
Alex Dima 已提交
431
		const viewEventBus = this._view.getInternalEventBus();
A
Alex Dima 已提交
432

A
Alex Dima 已提交
433
		viewEventBus.onDidGainFocus = () => {
A
Alex Dima 已提交
434 435 436
			this._onDidFocusEditorText.fire();
			// In IE, the focus is not synchronous, so we give it a little help
			this._onDidFocusEditor.fire();
A
Alex Dima 已提交
437
		};
A
Alex Dima 已提交
438

A
Alex Dima 已提交
439 440 441 442 443 444 445 446 447 448 449
		viewEventBus.onDidScroll = (e) => this._onDidScrollChange.fire(e);
		viewEventBus.onDidLoseFocus = () => this._onDidBlurEditorText.fire();
		viewEventBus.onContextMenu = (e) => this._onContextMenu.fire(e);
		viewEventBus.onMouseDown = (e) => this._onMouseDown.fire(e);
		viewEventBus.onMouseUp = (e) => this._onMouseUp.fire(e);
		viewEventBus.onMouseDrag = (e) => this._onMouseDrag.fire(e);
		viewEventBus.onMouseDrop = (e) => this._onMouseDrop.fire(e);
		viewEventBus.onKeyUp = (e) => this._onKeyUp.fire(e);
		viewEventBus.onMouseMove = (e) => this._onMouseMove.fire(e);
		viewEventBus.onMouseLeave = (e) => this._onMouseLeave.fire(e);
		viewEventBus.onKeyDown = (e) => this._onKeyDown.fire(e);
450
	}
E
Erich Gamma 已提交
451

452 453 454 455 456 457 458 459 460 461
	public restoreViewState(s: editorCommon.ICodeEditorViewState): void {
		super.restoreViewState(s);
		if (!this.cursor || !this.hasView) {
			return;
		}
		if (s && s.cursorState && s.viewState) {
			this._view.restoreState(this.viewModel.viewLayout.reduceRestoreState(s.viewState));
		}
	}

A
Alex Dima 已提交
462
	protected _detachModel(): ITextModel {
A
Alex Dima 已提交
463
		let removeDomNode: HTMLElement = null;
E
Erich Gamma 已提交
464 465 466

		if (this._view) {
			this._view.dispose();
A
Alex Dima 已提交
467
			removeDomNode = this._view.domNode.domNode;
E
Erich Gamma 已提交
468 469 470
			this._view = null;
		}

471
		let result = super._detachModel();
E
Erich Gamma 已提交
472 473 474 475 476 477 478

		if (removeDomNode) {
			this.domElement.removeChild(removeDomNode);
		}

		return result;
	}
479 480 481

	// BEGIN decorations

J
Johannes Rieken 已提交
482
	protected _registerDecorationType(key: string, options: editorCommon.IDecorationRenderOptions, parentTypeKey?: string): void {
483 484 485
		this._codeEditorService.registerDecorationType(key, options, parentTypeKey);
	}

J
Johannes Rieken 已提交
486
	protected _removeDecorationType(key: string): void {
487 488 489
		this._codeEditorService.removeDecorationType(key);
	}

490
	protected _resolveDecorationOptions(typeKey: string, writable: boolean): IModelDecorationOptions {
491 492 493 494
		return this._codeEditorService.resolveDecorationOptions(typeKey, writable);
	}

	// END decorations
495 496 497 498 499 500 501 502 503 504 505 506

	protected _triggerEditorCommand(source: string, handlerId: string, payload: any): boolean {
		const command = EditorExtensionsRegistry.getEditorCommand(handlerId);
		if (command) {
			payload = payload || {};
			payload.source = source;
			TPromise.as(command.runEditorCommand(null, this, payload)).done(null, onUnexpectedError);
			return true;
		}

		return false;
	}
E
Erich Gamma 已提交
507 508
}

509 510
class CodeEditorWidgetFocusTracker extends Disposable {

511 512
	private _hasFocus: boolean;
	private _domFocusTracker: dom.IFocusTracker;
513

M
Matt Bierner 已提交
514
	private readonly _onChange: Emitter<void> = this._register(new Emitter<void>());
515
	public readonly onChange: Event<void> = this._onChange.event;
516

J
Johannes Rieken 已提交
517
	constructor(domElement: HTMLElement) {
518 519
		super();

520 521
		this._hasFocus = false;
		this._domFocusTracker = this._register(dom.trackFocus(domElement));
522

523
		this._register(this._domFocusTracker.onDidFocus(() => {
524 525
			this._hasFocus = true;
			this._onChange.fire(void 0);
526 527
		}));
		this._register(this._domFocusTracker.onDidBlur(() => {
528 529
			this._hasFocus = false;
			this._onChange.fire(void 0);
530
		}));
531 532 533
	}

	public hasFocus(): boolean {
534
		return this._hasFocus;
535 536
	}
}
537

538
const squigglyStart = encodeURIComponent(`<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 6 3' enable-background='new 0 0 6 3' height='3' width='6'><g fill='`);
539
const squigglyEnd = encodeURIComponent(`'><polygon points='5.5,0 2.5,3 1.1,3 4.1,0'/><polygon points='4,0 6,2 6,0.6 5.4,0'/><polygon points='0,2 1,3 2.4,3 0,0.6'/></g></svg>`);
540

541 542 543
function getSquigglySVGData(color: Color) {
	return squigglyStart + encodeURIComponent(color.toString()) + squigglyEnd;
}
544

545 546 547
registerThemingParticipant((theme, collector) => {
	let errorBorderColor = theme.getColor(editorErrorBorder);
	if (errorBorderColor) {
548
		collector.addRule(`.monaco-editor .${ClassName.EditorErrorDecoration} { border-bottom: 4px double ${errorBorderColor}; }`);
549 550 551
	}
	let errorForeground = theme.getColor(editorErrorForeground);
	if (errorForeground) {
552
		collector.addRule(`.monaco-editor .${ClassName.EditorErrorDecoration} { background: url("data:image/svg+xml;utf8,${getSquigglySVGData(errorForeground)}") repeat-x bottom left; }`);
553 554
	}

555 556
	let warningBorderColor = theme.getColor(editorWarningBorder);
	if (warningBorderColor) {
557
		collector.addRule(`.monaco-editor .${ClassName.EditorWarningDecoration} { border-bottom: 4px double ${warningBorderColor}; }`);
558 559 560
	}
	let warningForeground = theme.getColor(editorWarningForeground);
	if (warningForeground) {
561
		collector.addRule(`.monaco-editor .${ClassName.EditorWarningDecoration} { background: url("data:image/svg+xml;utf8,${getSquigglySVGData(warningForeground)}") repeat-x bottom left; }`);
562 563 564
	}

	let infoBorderColor = theme.getColor(editorInfoBorder);
565 566
	if (infoBorderColor) {
		collector.addRule(`.monaco-editor .${ClassName.EditorInfoDecoration} { border-bottom: 4px double ${infoBorderColor}; }`);
567 568
	}
	let infoForeground = theme.getColor(editorInfoForeground);
569 570
	if (infoForeground) {
		collector.addRule(`.monaco-editor .${ClassName.EditorInfoDecoration} { background: url("data:image/svg+xml;utf8,${getSquigglySVGData(infoForeground)}") repeat-x bottom left; }`);
571
	}
572 573 574 575 576 577 578 579 580

	let hintBorderColor = theme.getColor(editorHintBorder);
	if (hintBorderColor) {
		collector.addRule(`.monaco-editor .${ClassName.EditorHintDecoration} { border-bottom: 4px dotted no-repeat ${hintBorderColor}; }`);
	}
	let hintForeground = theme.getColor(editorHintForeground);
	if (hintForeground) {
		collector.addRule(`.monaco-editor .${ClassName.EditorHintDecoration} { background: url("data:image/svg+xml;utf8,${getSquigglySVGData(hintForeground)}") no-repeat bottom left; }`);
	}
581
});