codeEditorWidget.ts 20.0 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 10
import { onUnexpectedError } from 'vs/base/common/errors';
import { IEventEmitter } from 'vs/base/common/eventEmitter';
A
Alex Dima 已提交
11 12
import * as browser from 'vs/base/browser/browser';
import * as dom from 'vs/base/browser/dom';
J
Johannes Rieken 已提交
13 14 15 16 17 18 19
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';
import { Range } from 'vs/editor/common/core/range';
import { Selection } from 'vs/editor/common/core/selection';
A
Alex Dima 已提交
20
import * as editorCommon from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
21 22 23
import { EditorAction } from 'vs/editor/common/editorCommonExtensions';
import { ICodeEditorService } from 'vs/editor/common/services/codeEditorService';
import { Configuration } from 'vs/editor/browser/config/configuration';
A
Alex Dima 已提交
24
import * as editorBrowser from 'vs/editor/browser/editorBrowser';
J
Johannes Rieken 已提交
25 26
import { Colorizer } from 'vs/editor/browser/standalone/colorizer';
import { View } from 'vs/editor/browser/view/viewImpl';
J
Johannes Rieken 已提交
27 28
import { Disposable } from 'vs/base/common/lifecycle';
import Event, { Emitter, fromEventEmitter } from 'vs/base/common/event';
J
Johannes Rieken 已提交
29 30
import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { InternalEditorAction } from 'vs/editor/common/editorAction';
E
Erich Gamma 已提交
31

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

J
Johannes Rieken 已提交
34 35 36 37 38 39 40 41 42
	public readonly onMouseUp: Event<editorBrowser.IEditorMouseEvent> = fromEventEmitter(this, editorCommon.EventType.MouseUp);
	public readonly onMouseDown: Event<editorBrowser.IEditorMouseEvent> = fromEventEmitter(this, editorCommon.EventType.MouseDown);
	public readonly onContextMenu: Event<editorBrowser.IEditorMouseEvent> = fromEventEmitter(this, editorCommon.EventType.ContextMenu);
	public readonly onMouseMove: Event<editorBrowser.IEditorMouseEvent> = fromEventEmitter(this, editorCommon.EventType.MouseMove);
	public readonly onMouseLeave: Event<editorBrowser.IEditorMouseEvent> = fromEventEmitter(this, editorCommon.EventType.MouseLeave);
	public readonly onKeyUp: Event<IKeyboardEvent> = fromEventEmitter(this, editorCommon.EventType.KeyUp);
	public readonly onKeyDown: Event<IKeyboardEvent> = fromEventEmitter(this, editorCommon.EventType.KeyDown);
	public readonly onDidLayoutChange: Event<editorCommon.EditorLayoutInfo> = fromEventEmitter(this, editorCommon.EventType.EditorLayout);
	public readonly onDidScrollChange: Event<editorCommon.IScrollEvent> = fromEventEmitter(this, 'scroll');
A
Alex Dima 已提交
43

44
	private _codeEditorService: ICodeEditorService;
45
	private _commandService: ICommandService;
46

J
Johannes Rieken 已提交
47
	protected domElement: HTMLElement;
48
	private _focusTracker: CodeEditorWidgetFocusTracker;
E
Erich Gamma 已提交
49

J
Johannes Rieken 已提交
50
	_configuration: Configuration;
51

J
Johannes Rieken 已提交
52 53
	private contentWidgets: { [key: string]: editorBrowser.IContentWidgetData; };
	private overlayWidgets: { [key: string]: editorBrowser.IOverlayWidgetData; };
E
Erich Gamma 已提交
54

J
Johannes Rieken 已提交
55
	_view: editorBrowser.IView;
E
Erich Gamma 已提交
56 57

	constructor(
J
Johannes Rieken 已提交
58 59
		domElement: HTMLElement,
		options: editorCommon.IEditorOptions,
E
Erich Gamma 已提交
60 61
		@IInstantiationService instantiationService: IInstantiationService,
		@ICodeEditorService codeEditorService: ICodeEditorService,
62
		@ICommandService commandService: ICommandService,
63
		@IContextKeyService contextKeyService: IContextKeyService
E
Erich Gamma 已提交
64
	) {
65
		super(domElement, options, instantiationService, contextKeyService);
66
		this._codeEditorService = codeEditorService;
67
		this._commandService = commandService;
E
Erich Gamma 已提交
68

69 70 71
		this._focusTracker = new CodeEditorWidgetFocusTracker(domElement);
		this._focusTracker.onChage(() => {
			let hasFocus = this._focusTracker.hasFocus();
72

73
			if (hasFocus) {
A
Alex Dima 已提交
74
				this.emit(editorCommon.EventType.EditorFocus, {});
75
			} else {
A
Alex Dima 已提交
76
				this.emit(editorCommon.EventType.EditorBlur, {});
E
Erich Gamma 已提交
77 78 79 80 81 82
			}
		});

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

83 84 85
		let contributions = this._getContributions();
		for (let i = 0, len = contributions.length; i < len; i++) {
			let ctor = contributions[i];
E
Erich Gamma 已提交
86
			try {
87
				let contribution = this._instantiationService.createInstance(ctor, this);
A
Alex Dima 已提交
88
				this._contributions[contribution.getId()] = contribution;
E
Erich Gamma 已提交
89
			} catch (err) {
90
				onUnexpectedError(err);
E
Erich Gamma 已提交
91 92
			}
		}
93

94
		this._getActions().forEach((action) => {
95
			let internalAction = new InternalEditorAction(action, this, this._instantiationService, this._contextKeyService);
A
Alex Dima 已提交
96
			this._actions[internalAction.id] = internalAction;
97
		});
98 99

		this._codeEditorService.addCodeEditor(this);
E
Erich Gamma 已提交
100 101
	}

102
	protected abstract _getContributions(): editorBrowser.IEditorContributionCtor[];
103 104
	protected abstract _getActions(): EditorAction[];

J
Johannes Rieken 已提交
105
	protected _createConfiguration(options: editorCommon.ICodeEditorWidgetCreationOptions): CommonEditorConfiguration {
106
		return new Configuration(options, this.domElement);
E
Erich Gamma 已提交
107 108 109
	}

	public dispose(): void {
110 111
		this._codeEditorService.removeCodeEditor(this);

E
Erich Gamma 已提交
112 113 114
		this.contentWidgets = {};
		this.overlayWidgets = {};

115
		this._focusTracker.dispose();
E
Erich Gamma 已提交
116 117 118
		super.dispose();
	}

J
Johannes Rieken 已提交
119
	public updateOptions(newOptions: editorCommon.IEditorOptions): void {
120
		let oldTheme = this._configuration.editor.viewInfo.theme;
121
		super.updateOptions(newOptions);
122
		let newTheme = this._configuration.editor.viewInfo.theme;
123 124 125 126 127 128

		if (oldTheme !== newTheme) {
			this.render();
		}
	}

J
Johannes Rieken 已提交
129
	public colorizeModelLine(lineNumber: number, model: editorCommon.IModel = this.model): string {
E
Erich Gamma 已提交
130 131 132
		if (!model) {
			return '';
		}
A
Alex Dima 已提交
133 134 135 136
		let content = model.getLineContent(lineNumber);
		let tokens = model.getLineTokens(lineNumber, false);
		let inflatedTokens = tokens.inflate();
		let tabSize = model.getOptions().tabSize;
137
		return Colorizer.colorizeLine(content, inflatedTokens, tabSize);
E
Erich Gamma 已提交
138
	}
A
Alex Dima 已提交
139
	public getView(): editorBrowser.IView {
E
Erich Gamma 已提交
140 141 142 143 144 145 146 147 148 149
		return this._view;
	}

	public getDomNode(): HTMLElement {
		if (!this.hasView) {
			return null;
		}
		return this._view.domNode;
	}

150
	public getCenteredRangeInViewport(): Range {
E
Erich Gamma 已提交
151 152 153 154 155 156
		if (!this.hasView) {
			return null;
		}
		return this._view.getCenteredRangeInViewport();
	}

157
	public getCompletelyVisibleLinesRangeInViewport(): Range {
158 159 160
		if (!this.hasView) {
			return null;
		}
161
		return this._view.getCompletelyVisibleLinesRangeInViewport();
162 163
	}

164
	public getScrollWidth(): number {
E
Erich Gamma 已提交
165
		if (!this.hasView) {
166
			return -1;
E
Erich Gamma 已提交
167
		}
168 169 170 171 172
		return this._view.getCodeEditorHelper().getScrollWidth();
	}
	public getScrollLeft(): number {
		if (!this.hasView) {
			return -1;
E
Erich Gamma 已提交
173
		}
174
		return this._view.getCodeEditorHelper().getScrollLeft();
E
Erich Gamma 已提交
175 176
	}

177
	public getScrollHeight(): number {
E
Erich Gamma 已提交
178 179 180
		if (!this.hasView) {
			return -1;
		}
181
		return this._view.getCodeEditorHelper().getScrollHeight();
E
Erich Gamma 已提交
182
	}
183
	public getScrollTop(): number {
E
Erich Gamma 已提交
184
		if (!this.hasView) {
185
			return -1;
E
Erich Gamma 已提交
186
		}
187
		return this._view.getCodeEditorHelper().getScrollTop();
E
Erich Gamma 已提交
188 189
	}

J
Johannes Rieken 已提交
190
	public setScrollLeft(newScrollLeft: number): void {
E
Erich Gamma 已提交
191 192 193 194 195 196
		if (!this.hasView) {
			return;
		}
		if (typeof newScrollLeft !== 'number') {
			throw new Error('Invalid arguments');
		}
197 198 199
		this._view.getCodeEditorHelper().setScrollPosition({
			scrollLeft: newScrollLeft
		});
E
Erich Gamma 已提交
200
	}
J
Johannes Rieken 已提交
201
	public setScrollTop(newScrollTop: number): void {
E
Erich Gamma 已提交
202
		if (!this.hasView) {
203
			return;
E
Erich Gamma 已提交
204
		}
205 206 207 208 209 210
		if (typeof newScrollTop !== 'number') {
			throw new Error('Invalid arguments');
		}
		this._view.getCodeEditorHelper().setScrollPosition({
			scrollTop: newScrollTop
		});
E
Erich Gamma 已提交
211
	}
212
	public setScrollPosition(position: editorCommon.INewScrollPosition): void {
E
Erich Gamma 已提交
213
		if (!this.hasView) {
214
			return;
E
Erich Gamma 已提交
215
		}
216
		this._view.getCodeEditorHelper().setScrollPosition(position);
E
Erich Gamma 已提交
217 218
	}

J
Johannes Rieken 已提交
219
	public delegateVerticalScrollbarMouseDown(browserEvent: MouseEvent): void {
E
Erich Gamma 已提交
220
		if (!this.hasView) {
221
			return;
E
Erich Gamma 已提交
222
		}
223
		this._view.getCodeEditorHelper().delegateVerticalScrollbarMouseDown(browserEvent);
E
Erich Gamma 已提交
224 225
	}

A
Alex Dima 已提交
226
	public saveViewState(): editorCommon.ICodeEditorViewState {
E
Erich Gamma 已提交
227 228 229
		if (!this.cursor || !this.hasView) {
			return null;
		}
J
Johannes Rieken 已提交
230
		let contributionsState: { [key: string]: any } = {};
A
Alex Dima 已提交
231 232 233 234 235

		let keys = Object.keys(this._contributions);
		for (let i = 0, len = keys.length; i < len; i++) {
			let id = keys[i];
			let contribution = this._contributions[id];
236 237 238 239 240
			if (typeof contribution.saveViewState === 'function') {
				contributionsState[id] = contribution.saveViewState();
			}
		}

A
Alex Dima 已提交
241 242
		let cursorState = this.cursor.saveState();
		let viewState = this._view.saveState();
E
Erich Gamma 已提交
243 244
		return {
			cursorState: cursorState,
245 246
			viewState: viewState,
			contributionsState: contributionsState
E
Erich Gamma 已提交
247 248 249
		};
	}

A
Alex Dima 已提交
250
	public restoreViewState(s: editorCommon.ICodeEditorViewState): void {
E
Erich Gamma 已提交
251 252 253
		if (!this.cursor || !this.hasView) {
			return;
		}
B
Benjamin Pasero 已提交
254
		if (s && s.cursorState && s.viewState) {
A
Alex Dima 已提交
255 256
			let codeEditorState = <editorCommon.ICodeEditorViewState>s;
			let cursorState = <any>codeEditorState.cursorState;
B
Benjamin Pasero 已提交
257 258 259 260 261
			if (Array.isArray(cursorState)) {
				this.cursor.restoreState(<editorCommon.ICursorState[]>cursorState);
			} else {
				// Backwards compatibility
				this.cursor.restoreState([<editorCommon.ICursorState>cursorState]);
E
Erich Gamma 已提交
262
			}
B
Benjamin Pasero 已提交
263
			this._view.restoreState(codeEditorState.viewState);
264

B
Benjamin Pasero 已提交
265
			let contributionsState = s.contributionsState || {};
A
Alex Dima 已提交
266 267 268 269
			let keys = Object.keys(this._contributions);
			for (let i = 0, len = keys.length; i < len; i++) {
				let id = keys[i];
				let contribution = this._contributions[id];
270 271 272 273
				if (typeof contribution.restoreViewState === 'function') {
					contribution.restoreViewState(contributionsState[id]);
				}
			}
E
Erich Gamma 已提交
274 275 276
		}
	}

J
Johannes Rieken 已提交
277
	public layout(dimension?: editorCommon.IDimension): void {
E
Erich Gamma 已提交
278
		this._configuration.observeReferenceElement(dimension);
279
		this.render();
E
Erich Gamma 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292
	}

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

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

293
	public hasWidgetFocus(): boolean {
294
		return this._focusTracker && this._focusTracker.hasFocus();
295 296
	}

A
Alex Dima 已提交
297
	public addContentWidget(widget: editorBrowser.IContentWidget): void {
A
Alex Dima 已提交
298
		let widgetData: editorBrowser.IContentWidgetData = {
E
Erich Gamma 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
			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 已提交
314
	public layoutContentWidget(widget: editorBrowser.IContentWidget): void {
A
Alex Dima 已提交
315
		let widgetId = widget.getId();
E
Erich Gamma 已提交
316
		if (this.contentWidgets.hasOwnProperty(widgetId)) {
A
Alex Dima 已提交
317
			let widgetData = this.contentWidgets[widgetId];
E
Erich Gamma 已提交
318 319 320 321 322 323 324
			widgetData.position = widget.getPosition();
			if (this.hasView) {
				this._view.layoutContentWidget(widgetData);
			}
		}
	}

A
Alex Dima 已提交
325
	public removeContentWidget(widget: editorBrowser.IContentWidget): void {
A
Alex Dima 已提交
326
		let widgetId = widget.getId();
E
Erich Gamma 已提交
327
		if (this.contentWidgets.hasOwnProperty(widgetId)) {
A
Alex Dima 已提交
328
			let widgetData = this.contentWidgets[widgetId];
E
Erich Gamma 已提交
329 330 331 332 333 334 335
			delete this.contentWidgets[widgetId];
			if (this.hasView) {
				this._view.removeContentWidget(widgetData);
			}
		}
	}

A
Alex Dima 已提交
336
	public addOverlayWidget(widget: editorBrowser.IOverlayWidget): void {
A
Alex Dima 已提交
337
		let widgetData: editorBrowser.IOverlayWidgetData = {
E
Erich Gamma 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
			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 已提交
353
	public layoutOverlayWidget(widget: editorBrowser.IOverlayWidget): void {
A
Alex Dima 已提交
354
		let widgetId = widget.getId();
E
Erich Gamma 已提交
355
		if (this.overlayWidgets.hasOwnProperty(widgetId)) {
A
Alex Dima 已提交
356
			let widgetData = this.overlayWidgets[widgetId];
E
Erich Gamma 已提交
357 358 359 360 361 362 363
			widgetData.position = widget.getPosition();
			if (this.hasView) {
				this._view.layoutOverlayWidget(widgetData);
			}
		}
	}

A
Alex Dima 已提交
364
	public removeOverlayWidget(widget: editorBrowser.IOverlayWidget): void {
A
Alex Dima 已提交
365
		let widgetId = widget.getId();
E
Erich Gamma 已提交
366
		if (this.overlayWidgets.hasOwnProperty(widgetId)) {
A
Alex Dima 已提交
367
			let widgetData = this.overlayWidgets[widgetId];
E
Erich Gamma 已提交
368 369 370 371 372 373 374
			delete this.overlayWidgets[widgetId];
			if (this.hasView) {
				this._view.removeOverlayWidget(widgetData);
			}
		}
	}

J
Johannes Rieken 已提交
375
	public changeViewZones(callback: (accessor: editorBrowser.IViewZoneChangeAccessor) => void): void {
E
Erich Gamma 已提交
376
		if (!this.hasView) {
J
Johannes Rieken 已提交
377
			//			console.warn('Cannot change view zones on editor that is not attached to a model, since there is no view.');
E
Erich Gamma 已提交
378 379
			return;
		}
A
Alex Dima 已提交
380
		let hasChanges = this._view.change(callback);
E
Erich Gamma 已提交
381
		if (hasChanges) {
A
Alex Dima 已提交
382
			this.emit(editorCommon.EventType.ViewZonesChanged);
E
Erich Gamma 已提交
383 384 385
		}
	}

A
Alex Dima 已提交
386
	public getWhitespaces(): editorCommon.IEditorWhitespace[] {
E
Erich Gamma 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
		if (!this.hasView) {
			return [];
		}
		return this._view.getWhitespaces();
	}

	public getTopForLineNumber(lineNumber: number): number {
		if (!this.hasView) {
			return -1;
		}
		return this._view.getCodeEditorHelper().getVerticalOffsetForPosition(lineNumber, 1);
	}

	public getTopForPosition(lineNumber: number, column: number): number {
		if (!this.hasView) {
			return -1;
		}
		return this._view.getCodeEditorHelper().getVerticalOffsetForPosition(lineNumber, column);
	}

J
Johannes Rieken 已提交
407
	public getScrolledVisiblePosition(rawPosition: editorCommon.IPosition): { top: number; left: number; height: number; } {
E
Erich Gamma 已提交
408 409 410 411
		if (!this.hasView) {
			return null;
		}

A
Alex Dima 已提交
412 413 414
		let position = this.model.validatePosition(rawPosition);
		let helper = this._view.getCodeEditorHelper();
		let layoutInfo = this._configuration.editor.layoutInfo;
E
Erich Gamma 已提交
415

A
Alex Dima 已提交
416 417
		let top = helper.getVerticalOffsetForPosition(position.lineNumber, position.column) - helper.getScrollTop();
		let left = helper.getOffsetForColumn(position.lineNumber, position.column) + layoutInfo.glyphMarginWidth + layoutInfo.lineNumbersWidth + layoutInfo.decorationsWidth - helper.getScrollLeft();
E
Erich Gamma 已提交
418 419 420 421 422 423 424 425

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

J
Johannes Rieken 已提交
426
	public getOffsetForColumn(lineNumber: number, column: number): number {
E
Erich Gamma 已提交
427 428 429 430 431 432
		if (!this.hasView) {
			return -1;
		}
		return this._view.getCodeEditorHelper().getOffsetForColumn(lineNumber, column);
	}

433 434 435 436
	public render(): void {
		if (!this.hasView) {
			return;
		}
437
		this._view.render(true, false);
438 439
	}

J
Johannes Rieken 已提交
440
	public setHiddenAreas(ranges: editorCommon.IRange[]): void {
M
Martin Aeschlimann 已提交
441 442 443 444 445
		if (this.viewModel) {
			this.viewModel.setHiddenAreas(ranges);
		}
	}

J
Johannes Rieken 已提交
446
	public setAriaActiveDescendant(id: string): void {
A
Alex Dima 已提交
447 448 449 450 451 452
		if (!this.hasView) {
			return;
		}
		this._view.setAriaActiveDescendant(id);
	}

J
Johannes Rieken 已提交
453
	public applyFontInfo(target: HTMLElement): void {
454 455 456
		Configuration.applyFontInfoSlow(target, this._configuration.editor.fontInfo);
	}

J
Johannes Rieken 已提交
457
	_attachModel(model: editorCommon.IModel): void {
E
Erich Gamma 已提交
458 459
		this._view = null;

460
		super._attachModel(model);
E
Erich Gamma 已提交
461

462
		if (this._view) {
E
Erich Gamma 已提交
463 464
			this.domElement.appendChild(this._view.domNode);

A
Alex Dima 已提交
465 466 467 468 469
			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 已提交
470

A
Alex Dima 已提交
471 472 473 474 475
			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 已提交
476

A
Alex Dima 已提交
477 478
			this._view.render(false, true);
			this.hasView = true;
E
Erich Gamma 已提交
479 480 481
		}
	}

482
	protected _enableEmptySelectionClipboard(): boolean {
A
Alex Dima 已提交
483
		return browser.enableEmptySelectionClipboard;
E
Erich Gamma 已提交
484 485
	}

486 487
	protected _createView(): void {
		this._view = new View(
488
			this._commandService,
489 490
			this._configuration,
			this.viewModel,
J
Johannes Rieken 已提交
491
			(source: string, handlerId: string, payload: any) => {
A
Alex Dima 已提交
492 493 494 495 496
				if (!this.cursor) {
					return;
				}
				this.cursor.trigger(source, handlerId, payload);
			}
497 498
		);
	}
E
Erich Gamma 已提交
499

500 501 502
	protected _getViewInternalEventBus(): IEventEmitter {
		return this._view.getInternalEventBus();
	}
E
Erich Gamma 已提交
503

A
Alex Dima 已提交
504
	protected _detachModel(): editorCommon.IModel {
A
Alex Dima 已提交
505
		let removeDomNode: HTMLElement = null;
E
Erich Gamma 已提交
506 507 508 509 510 511 512

		if (this._view) {
			this._view.dispose();
			removeDomNode = this._view.domNode;
			this._view = null;
		}

513
		let result = super._detachModel();
E
Erich Gamma 已提交
514 515 516 517 518 519 520

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

		return result;
	}
521 522 523

	// BEGIN decorations

J
Johannes Rieken 已提交
524
	protected _registerDecorationType(key: string, options: editorCommon.IDecorationRenderOptions, parentTypeKey?: string): void {
525 526 527
		this._codeEditorService.registerDecorationType(key, options, parentTypeKey);
	}

J
Johannes Rieken 已提交
528
	protected _removeDecorationType(key: string): void {
529 530 531
		this._codeEditorService.removeDecorationType(key);
	}

J
Johannes Rieken 已提交
532
	protected _resolveDecorationOptions(typeKey: string, writable: boolean): editorCommon.IModelDecorationOptions {
533 534 535 536
		return this._codeEditorService.resolveDecorationOptions(typeKey, writable);
	}

	// END decorations
E
Erich Gamma 已提交
537 538
}

539 540
class CodeEditorWidgetFocusTracker extends Disposable {

541 542
	private _hasFocus: boolean;
	private _domFocusTracker: dom.IFocusTracker;
543 544 545 546

	private _onChange: Emitter<void> = this._register(new Emitter<void>());
	public onChage: Event<void> = this._onChange.event;

J
Johannes Rieken 已提交
547
	constructor(domElement: HTMLElement) {
548 549
		super();

550 551
		this._hasFocus = false;
		this._domFocusTracker = this._register(dom.trackFocus(domElement));
552

553 554 555
		this._domFocusTracker.addFocusListener(() => {
			this._hasFocus = true;
			this._onChange.fire(void 0);
556
		});
557 558 559
		this._domFocusTracker.addBlurListener(() => {
			this._hasFocus = false;
			this._onChange.fire(void 0);
560 561 562 563
		});
	}

	public hasFocus(): boolean {
564
		return this._hasFocus;
565 566 567
	}
}

A
Alex Dima 已提交
568
class OverlayWidget2 implements editorBrowser.IOverlayWidget {
E
Erich Gamma 已提交
569 570

	private _id: string;
A
Alex Dima 已提交
571
	private _position: editorBrowser.IOverlayWidgetPosition;
E
Erich Gamma 已提交
572 573
	private _domNode: HTMLElement;

J
Johannes Rieken 已提交
574
	constructor(id: string, position: editorBrowser.IOverlayWidgetPosition) {
E
Erich Gamma 已提交
575 576 577
		this._id = id;
		this._position = position;
		this._domNode = document.createElement('div');
J
Johannes Rieken 已提交
578
		this._domNode.className = this._id.replace(/\./g, '-').replace(/[^a-z0-9\-]/, '');
E
Erich Gamma 已提交
579 580 581 582 583 584 585 586 587 588
	}

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

	public getDomNode(): HTMLElement {
		return this._domNode;
	}

A
Alex Dima 已提交
589
	public getPosition(): editorBrowser.IOverlayWidgetPosition {
E
Erich Gamma 已提交
590 591 592 593 594 595 596 597
		return this._position;
	}
}

export enum EditCursorState {
	EndOfLastEditOperation = 0
}

A
Alex Dima 已提交
598 599
class SingleEditOperation {

600
	range: Range;
A
Alex Dima 已提交
601 602 603
	text: string;
	forceMoveMarkers: boolean;

J
Johannes Rieken 已提交
604
	constructor(source: editorCommon.ISingleEditOperation) {
A
Alex Dima 已提交
605 606 607 608 609 610 611
		this.range = new Range(source.range.startLineNumber, source.range.startColumn, source.range.endLineNumber, source.range.endColumn);
		this.text = source.text;
		this.forceMoveMarkers = source.forceMoveMarkers || false;
	}

}

A
Alex Dima 已提交
612
export class CommandRunner implements editorCommon.ICommand {
E
Erich Gamma 已提交
613

A
Alex Dima 已提交
614
	private _ops: SingleEditOperation[];
E
Erich Gamma 已提交
615 616
	private _editCursorState: EditCursorState;

A
Alex Dima 已提交
617
	constructor(ops: editorCommon.ISingleEditOperation[], editCursorState: EditCursorState) {
A
Alex Dima 已提交
618
		this._ops = ops.map(op => new SingleEditOperation(op));
E
Erich Gamma 已提交
619 620 621
		this._editCursorState = editCursorState;
	}

A
Alex Dima 已提交
622
	public getEditOperations(model: editorCommon.ITokenizedModel, builder: editorCommon.IEditOperationBuilder): void {
E
Erich Gamma 已提交
623 624 625 626 627 628 629 630 631 632
		if (this._ops.length === 0) {
			return;
		}

		// Sort them in ascending order by range starts
		this._ops.sort((o1, o2) => {
			return Range.compareRangesUsingStarts(o1.range, o2.range);
		});

		// Merge operations that touch each other
A
Alex Dima 已提交
633 634 635
		let resultOps: editorCommon.ISingleEditOperation[] = [];
		let previousOp = this._ops[0];
		for (let i = 1; i < this._ops.length; i++) {
E
Erich Gamma 已提交
636 637 638 639 640 641 642 643 644 645 646
			if (previousOp.range.endLineNumber === this._ops[i].range.startLineNumber && previousOp.range.endColumn === this._ops[i].range.startColumn) {
				// These operations are one after another and can be merged
				previousOp.range = Range.plusRange(previousOp.range, this._ops[i].range);
				previousOp.text = previousOp.text + this._ops[i].text;
			} else {
				resultOps.push(previousOp);
				previousOp = this._ops[i];
			}
		}
		resultOps.push(previousOp);

A
Alex Dima 已提交
647
		for (let i = 0; i < resultOps.length; i++) {
E
Erich Gamma 已提交
648 649 650 651
			builder.addEditOperation(Range.lift(resultOps[i].range), resultOps[i].text);
		}
	}

652
	public computeCursorState(model: editorCommon.ITokenizedModel, helper: editorCommon.ICursorStateComputerData): Selection {
A
Alex Dima 已提交
653 654
		let inverseEditOperations = helper.getInverseEditOperations();
		let srcRange = inverseEditOperations[inverseEditOperations.length - 1].range;
A
Alex Dima 已提交
655
		return new Selection(
E
Erich Gamma 已提交
656 657 658 659 660 661 662
			srcRange.endLineNumber,
			srcRange.endColumn,
			srcRange.endLineNumber,
			srcRange.endColumn
		);
	}
}