codeEditorWidget.ts 20.5 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';
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';
E
Erich Gamma 已提交
13
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
14
import {ICommandService} from 'vs/platform/commands/common/commands';
15
import {IContextKeyService} from 'vs/platform/contextkey/common/contextkey';
A
Alex Dima 已提交
16
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
17
import {CommonCodeEditor} from 'vs/editor/common/commonCodeEditor';
18
import {CommonEditorConfiguration} from 'vs/editor/common/config/commonEditorConfig';
A
Alex Dima 已提交
19 20 21 22 23 24 25 26 27 28
import {Range} from 'vs/editor/common/core/range';
import {Selection} from 'vs/editor/common/core/selection';
import * as editorCommon from 'vs/editor/common/editorCommon';
import {CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions';
import {ICodeEditorService} from 'vs/editor/common/services/codeEditorService';
import {Configuration} from 'vs/editor/browser/config/configuration';
import * as editorBrowser from 'vs/editor/browser/editorBrowser';
import {EditorBrowserRegistry} from 'vs/editor/browser/editorBrowserExtensions';
import {Colorizer} from 'vs/editor/browser/standalone/colorizer';
import {View} from 'vs/editor/browser/view/viewImpl';
A
Alex Dima 已提交
29
import {Disposable, IDisposable} from 'vs/base/common/lifecycle';
30
import Event, {Emitter} from 'vs/base/common/event';
A
Alex Dima 已提交
31
import {IKeyboardEvent} from 'vs/base/browser/keyboardEvent';
A
Alex Dima 已提交
32
import {InternalEditorAction} from 'vs/editor/common/editorAction';
E
Erich Gamma 已提交
33

A
Alex Dima 已提交
34
export class CodeEditorWidget extends CommonCodeEditor implements editorBrowser.ICodeEditor {
E
Erich Gamma 已提交
35

A
Alex Dima 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	public onMouseUp(listener: (e:editorBrowser.IEditorMouseEvent)=>void): IDisposable {
		return this.addListener2(editorCommon.EventType.MouseUp, listener);
	}
	public onMouseDown(listener: (e:editorBrowser.IEditorMouseEvent)=>void): IDisposable {
		return this.addListener2(editorCommon.EventType.MouseDown, listener);
	}
	public onContextMenu(listener: (e:editorBrowser.IEditorMouseEvent)=>void): IDisposable {
		return this.addListener2(editorCommon.EventType.ContextMenu, listener);
	}
	public onMouseMove(listener: (e:editorBrowser.IEditorMouseEvent)=>void): IDisposable {
		return this.addListener2(editorCommon.EventType.MouseMove, listener);
	}
	public onMouseLeave(listener: (e:editorBrowser.IEditorMouseEvent)=>void): IDisposable {
		return this.addListener2(editorCommon.EventType.MouseLeave, listener);
	}
	public onKeyUp(listener: (e:IKeyboardEvent)=>void): IDisposable {
		return this.addListener2(editorCommon.EventType.KeyUp, listener);
	}
	public onKeyDown(listener: (e:IKeyboardEvent)=>void): IDisposable {
		return this.addListener2(editorCommon.EventType.KeyDown, listener);
	}
	public onDidLayoutChange(listener: (e:editorCommon.EditorLayoutInfo)=>void): IDisposable {
		return this.addListener2(editorCommon.EventType.EditorLayout, listener);
	}
	public onDidScrollChange(listener: (e:editorCommon.IScrollEvent)=>void): IDisposable {
		return this.addListener2('scroll', listener);
	}

64
	protected domElement:HTMLElement;
65
	private _focusTracker: CodeEditorWidgetFocusTracker;
E
Erich Gamma 已提交
66

67 68
	_configuration:Configuration;

A
Alex Dima 已提交
69 70
	private contentWidgets:{ [key:string]:editorBrowser.IContentWidgetData; };
	private overlayWidgets:{ [key:string]:editorBrowser.IOverlayWidgetData; };
E
Erich Gamma 已提交
71

A
Alex Dima 已提交
72
	_view:editorBrowser.IView;
E
Erich Gamma 已提交
73 74 75

	constructor(
		domElement:HTMLElement,
76
		options:editorCommon.IEditorOptions,
E
Erich Gamma 已提交
77 78
		@IInstantiationService instantiationService: IInstantiationService,
		@ICodeEditorService codeEditorService: ICodeEditorService,
79
		@ICommandService commandService: ICommandService,
80
		@IContextKeyService contextKeyService: IContextKeyService,
E
Erich Gamma 已提交
81 82
		@ITelemetryService telemetryService: ITelemetryService
	) {
83
		super(domElement, options, instantiationService, codeEditorService, commandService, contextKeyService, telemetryService);
E
Erich Gamma 已提交
84

85 86 87
		this._focusTracker = new CodeEditorWidgetFocusTracker(domElement);
		this._focusTracker.onChage(() => {
			let hasFocus = this._focusTracker.hasFocus();
88

89
			if (hasFocus) {
E
Erich Gamma 已提交
90
				this._editorFocusContextKey.set(true);
A
Alex Dima 已提交
91
				this.emit(editorCommon.EventType.EditorFocus, {});
92
			} else {
E
Erich Gamma 已提交
93
				this._editorFocusContextKey.reset();
A
Alex Dima 已提交
94
				this.emit(editorCommon.EventType.EditorBlur, {});
E
Erich Gamma 已提交
95 96 97 98 99 100
			}
		});

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

A
Alex Dima 已提交
101 102
		let contributionDescriptors = [].concat(EditorBrowserRegistry.getEditorContributions()).concat(CommonEditorRegistry.getEditorContributions());
		for (let i = 0, len = contributionDescriptors.length; i < len; i++) {
E
Erich Gamma 已提交
103
			try {
A
Alex Dima 已提交
104 105
				let contribution = contributionDescriptors[i].createInstance(this._instantiationService, this);
				this._contributions[contribution.getId()] = contribution;
E
Erich Gamma 已提交
106
			} catch (err) {
107
				onUnexpectedError(err);
E
Erich Gamma 已提交
108 109
			}
		}
110 111

		CommonEditorRegistry.getEditorActions().forEach((action) => {
112
			let internalAction = new InternalEditorAction(action, this, this._instantiationService, this._contextKeyService);
A
Alex Dima 已提交
113
			this._actions[internalAction.id] = internalAction;
114
		});
E
Erich Gamma 已提交
115 116
	}

117 118
	protected _createConfiguration(options:editorCommon.ICodeEditorWidgetCreationOptions): CommonEditorConfiguration {
		return new Configuration(options, this.domElement);
E
Erich Gamma 已提交
119 120 121 122 123 124
	}

	public dispose(): void {
		this.contentWidgets = {};
		this.overlayWidgets = {};

125
		this._focusTracker.dispose();
E
Erich Gamma 已提交
126 127 128
		super.dispose();
	}

129
	public updateOptions(newOptions:editorCommon.IEditorOptions): void {
130
		let oldTheme = this._configuration.editor.viewInfo.theme;
131
		super.updateOptions(newOptions);
132
		let newTheme = this._configuration.editor.viewInfo.theme;
133 134 135 136 137 138

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

A
Alex Dima 已提交
139
	public colorizeModelLine(lineNumber:number, model:editorCommon.IModel = this.model): string {
E
Erich Gamma 已提交
140 141 142 143 144
		if (!model) {
			return '';
		}
		var content = model.getLineContent(lineNumber);
		var tokens = model.getLineTokens(lineNumber, false);
A
Alex Dima 已提交
145
		var inflatedTokens = tokens.inflate();
146 147
		var tabSize = model.getOptions().tabSize;
		return Colorizer.colorizeLine(content, inflatedTokens, tabSize);
E
Erich Gamma 已提交
148
	}
A
Alex Dima 已提交
149
	public getView(): editorBrowser.IView {
E
Erich Gamma 已提交
150 151 152 153 154 155 156 157 158 159
		return this._view;
	}

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

160
	public getCenteredRangeInViewport(): Range {
E
Erich Gamma 已提交
161 162 163 164 165 166
		if (!this.hasView) {
			return null;
		}
		return this._view.getCenteredRangeInViewport();
	}

167 168 169 170 171 172 173
	public getVisibleRangeInViewport(): Range {
		if (!this.hasView) {
			return null;
		}
		return this._view.getVisibleRangeInViewport();
	}

174
	public getScrollWidth(): number {
E
Erich Gamma 已提交
175
		if (!this.hasView) {
176
			return -1;
E
Erich Gamma 已提交
177
		}
178 179 180 181 182
		return this._view.getCodeEditorHelper().getScrollWidth();
	}
	public getScrollLeft(): number {
		if (!this.hasView) {
			return -1;
E
Erich Gamma 已提交
183
		}
184
		return this._view.getCodeEditorHelper().getScrollLeft();
E
Erich Gamma 已提交
185 186
	}

187
	public getScrollHeight(): number {
E
Erich Gamma 已提交
188 189 190
		if (!this.hasView) {
			return -1;
		}
191
		return this._view.getCodeEditorHelper().getScrollHeight();
E
Erich Gamma 已提交
192
	}
193
	public getScrollTop(): number {
E
Erich Gamma 已提交
194
		if (!this.hasView) {
195
			return -1;
E
Erich Gamma 已提交
196
		}
197
		return this._view.getCodeEditorHelper().getScrollTop();
E
Erich Gamma 已提交
198 199 200 201 202 203 204 205 206
	}

	public setScrollLeft(newScrollLeft:number): void {
		if (!this.hasView) {
			return;
		}
		if (typeof newScrollLeft !== 'number') {
			throw new Error('Invalid arguments');
		}
207 208 209
		this._view.getCodeEditorHelper().setScrollPosition({
			scrollLeft: newScrollLeft
		});
E
Erich Gamma 已提交
210
	}
211
	public setScrollTop(newScrollTop:number): void {
E
Erich Gamma 已提交
212
		if (!this.hasView) {
213
			return;
E
Erich Gamma 已提交
214
		}
215 216 217 218 219 220
		if (typeof newScrollTop !== 'number') {
			throw new Error('Invalid arguments');
		}
		this._view.getCodeEditorHelper().setScrollPosition({
			scrollTop: newScrollTop
		});
E
Erich Gamma 已提交
221
	}
222
	public setScrollPosition(position: editorCommon.INewScrollPosition): void {
E
Erich Gamma 已提交
223
		if (!this.hasView) {
224
			return;
E
Erich Gamma 已提交
225
		}
226
		this._view.getCodeEditorHelper().setScrollPosition(position);
E
Erich Gamma 已提交
227 228
	}

229
	public delegateVerticalScrollbarMouseDown(browserEvent:MouseEvent): void {
E
Erich Gamma 已提交
230
		if (!this.hasView) {
231
			return;
E
Erich Gamma 已提交
232
		}
233
		this._view.getCodeEditorHelper().delegateVerticalScrollbarMouseDown(browserEvent);
E
Erich Gamma 已提交
234 235
	}

A
Alex Dima 已提交
236
	public saveViewState(): editorCommon.ICodeEditorViewState {
E
Erich Gamma 已提交
237 238 239
		if (!this.cursor || !this.hasView) {
			return null;
		}
240
		let contributionsState: {[key:string]:any} = {};
A
Alex Dima 已提交
241 242 243 244 245

		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];
246 247 248 249 250
			if (typeof contribution.saveViewState === 'function') {
				contributionsState[id] = contribution.saveViewState();
			}
		}

A
Alex Dima 已提交
251 252
		let cursorState = this.cursor.saveState();
		let viewState = this._view.saveState();
E
Erich Gamma 已提交
253 254
		return {
			cursorState: cursorState,
255 256
			viewState: viewState,
			contributionsState: contributionsState
E
Erich Gamma 已提交
257 258 259
		};
	}

A
Alex Dima 已提交
260
	public restoreViewState(state:editorCommon.IEditorViewState): void {
E
Erich Gamma 已提交
261 262 263 264 265
		if (!this.cursor || !this.hasView) {
			return;
		}
		var s = <any>state;
		if (s && s.cursorState && s.viewState) {
A
Alex Dima 已提交
266
			var codeEditorState = <editorCommon.ICodeEditorViewState>s;
E
Erich Gamma 已提交
267 268
			var cursorState = <any>codeEditorState.cursorState;
			if (Array.isArray(cursorState)) {
A
Alex Dima 已提交
269
				this.cursor.restoreState(<editorCommon.ICursorState[]>cursorState);
E
Erich Gamma 已提交
270 271
			} else {
				// Backwards compatibility
A
Alex Dima 已提交
272
				this.cursor.restoreState([<editorCommon.ICursorState>cursorState]);
E
Erich Gamma 已提交
273 274
			}
			this._view.restoreState(codeEditorState.viewState);
275 276

			let contributionsState = s.contributionsState || {};
A
Alex Dima 已提交
277 278 279 280
			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];
281 282 283 284
				if (typeof contribution.restoreViewState === 'function') {
					contribution.restoreViewState(contributionsState[id]);
				}
			}
E
Erich Gamma 已提交
285 286 287
		}
	}

A
Alex Dima 已提交
288
	public layout(dimension?:editorCommon.IDimension): void {
E
Erich Gamma 已提交
289
		this._configuration.observeReferenceElement(dimension);
290
		this.render();
E
Erich Gamma 已提交
291 292 293 294 295 296 297 298 299
	}

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

300 301 302 303 304 305 306 307
	public beginForcedWidgetFocus(): void {
		this._focusTracker.beginForcedFocus();
	}

	public endForcedWidgetFocus(): void {
		this._focusTracker.endForcedFocus();
	}

E
Erich Gamma 已提交
308 309 310 311
	public isFocused(): boolean {
		return this.hasView && this._view.isFocused();
	}

312
	public hasWidgetFocus(): boolean {
313
		return this._focusTracker.hasFocus();
314 315
	}

A
Alex Dima 已提交
316 317
	public addContentWidget(widget: editorBrowser.IContentWidget): void {
		var widgetData: editorBrowser.IContentWidgetData = {
E
Erich Gamma 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
			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 已提交
333
	public layoutContentWidget(widget: editorBrowser.IContentWidget): void {
E
Erich Gamma 已提交
334 335 336 337 338 339 340 341 342 343
		var widgetId = widget.getId();
		if (this.contentWidgets.hasOwnProperty(widgetId)) {
			var widgetData = this.contentWidgets[widgetId];
			widgetData.position = widget.getPosition();
			if (this.hasView) {
				this._view.layoutContentWidget(widgetData);
			}
		}
	}

A
Alex Dima 已提交
344
	public removeContentWidget(widget: editorBrowser.IContentWidget): void {
E
Erich Gamma 已提交
345 346 347 348 349 350 351 352 353 354
		var widgetId = widget.getId();
		if (this.contentWidgets.hasOwnProperty(widgetId)) {
			var widgetData = this.contentWidgets[widgetId];
			delete this.contentWidgets[widgetId];
			if (this.hasView) {
				this._view.removeContentWidget(widgetData);
			}
		}
	}

A
Alex Dima 已提交
355 356
	public addOverlayWidget(widget: editorBrowser.IOverlayWidget): void {
		var widgetData: editorBrowser.IOverlayWidgetData = {
E
Erich Gamma 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
			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 已提交
372
	public layoutOverlayWidget(widget: editorBrowser.IOverlayWidget): void {
E
Erich Gamma 已提交
373 374 375 376 377 378 379 380 381 382
		var widgetId = widget.getId();
		if (this.overlayWidgets.hasOwnProperty(widgetId)) {
			var widgetData = this.overlayWidgets[widgetId];
			widgetData.position = widget.getPosition();
			if (this.hasView) {
				this._view.layoutOverlayWidget(widgetData);
			}
		}
	}

A
Alex Dima 已提交
383
	public removeOverlayWidget(widget: editorBrowser.IOverlayWidget): void {
E
Erich Gamma 已提交
384 385 386 387 388 389 390 391 392 393
		var widgetId = widget.getId();
		if (this.overlayWidgets.hasOwnProperty(widgetId)) {
			var widgetData = this.overlayWidgets[widgetId];
			delete this.overlayWidgets[widgetId];
			if (this.hasView) {
				this._view.removeOverlayWidget(widgetData);
			}
		}
	}

A
Alex Dima 已提交
394
	public changeViewZones(callback:(accessor:editorBrowser.IViewZoneChangeAccessor)=>void): void {
E
Erich Gamma 已提交
395 396 397 398 399 400
		if (!this.hasView) {
//			console.warn('Cannot change view zones on editor that is not attached to a model, since there is no view.');
			return;
		}
		var hasChanges = this._view.change(callback);
		if (hasChanges) {
A
Alex Dima 已提交
401
			this.emit(editorCommon.EventType.ViewZonesChanged);
E
Erich Gamma 已提交
402 403 404
		}
	}

A
Alex Dima 已提交
405
	public getWhitespaces(): editorCommon.IEditorWhitespace[] {
E
Erich Gamma 已提交
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
		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);
	}

A
Alex Dima 已提交
426
	public getScrolledVisiblePosition(rawPosition:editorCommon.IPosition): { top:number; left:number; height:number; } {
E
Erich Gamma 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
		if (!this.hasView) {
			return null;
		}

		var position = this.model.validatePosition(rawPosition);
		var helper = this._view.getCodeEditorHelper();
		var layoutInfo = this._configuration.editor.layoutInfo;

		var top = helper.getVerticalOffsetForPosition(position.lineNumber, position.column) - helper.getScrollTop();
		var left = helper.getOffsetForColumn(position.lineNumber, position.column) + layoutInfo.glyphMarginWidth + layoutInfo.lineNumbersWidth + layoutInfo.decorationsWidth - helper.getScrollLeft();

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

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

452 453 454 455
	public render(): void {
		if (!this.hasView) {
			return;
		}
456
		this._view.render(true, false);
457 458
	}

A
Alex Dima 已提交
459
	public setHiddenAreas(ranges:editorCommon.IRange[]): void {
M
Martin Aeschlimann 已提交
460 461 462 463 464
		if (this.viewModel) {
			this.viewModel.setHiddenAreas(ranges);
		}
	}

A
Alex Dima 已提交
465 466 467 468 469 470 471
	public setAriaActiveDescendant(id:string): void {
		if (!this.hasView) {
			return;
		}
		this._view.setAriaActiveDescendant(id);
	}

472 473 474 475
	public applyFontInfo(target:HTMLElement): void {
		Configuration.applyFontInfoSlow(target, this._configuration.editor.fontInfo);
	}

A
Alex Dima 已提交
476
	_attachModel(model:editorCommon.IModel): void {
E
Erich Gamma 已提交
477 478
		this._view = null;

479
		super._attachModel(model);
E
Erich Gamma 已提交
480

481
		if (this._view) {
E
Erich Gamma 已提交
482 483 484 485
			this.domElement.appendChild(this._view.domNode);

			this._view.renderOnce(() => {

A
Alex Dima 已提交
486 487 488 489
				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 已提交
490 491
				}

A
Alex Dima 已提交
492 493 494 495
				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 已提交
496 497
				}

498
				this._view.render(false, true);
E
Erich Gamma 已提交
499 500 501 502 503
				this.hasView = true;
			});
		}
	}

504
	protected _enableEmptySelectionClipboard(): boolean {
A
Alex Dima 已提交
505
		return browser.enableEmptySelectionClipboard;
E
Erich Gamma 已提交
506 507
	}

508 509
	protected _createView(): void {
		this._view = new View(
510
			this._contextKeyService,
511
			this._commandService,
512 513
			this._configuration,
			this.viewModel,
A
Alex Dima 已提交
514 515 516 517 518 519
			(source:string, handlerId:string, payload:any) => {
				if (!this.cursor) {
					return;
				}
				this.cursor.trigger(source, handlerId, payload);
			}
520 521
		);
	}
E
Erich Gamma 已提交
522

523 524 525
	protected _getViewInternalEventBus(): IEventEmitter {
		return this._view.getInternalEventBus();
	}
E
Erich Gamma 已提交
526

A
Alex Dima 已提交
527
	protected _detachModel(): editorCommon.IModel {
E
Erich Gamma 已提交
528 529 530 531 532 533 534 535
		var removeDomNode:HTMLElement = null;

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

536
		let result = super._detachModel();
E
Erich Gamma 已提交
537 538 539 540 541 542 543 544 545

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

		return result;
	}
}

546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
class CodeEditorWidgetFocusTracker extends Disposable {

	private _forcedWidgetFocusCount: number;
	private _focusTrackerHasFocus: boolean;
	private _focusTracker: dom.IFocusTracker;
	private _actualHasFocus: boolean;

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

	constructor(domElement:HTMLElement) {
		super();

		this._focusTrackerHasFocus = false;
		this._forcedWidgetFocusCount = 0;
		this._actualHasFocus = false;
		this._focusTracker = this._register(dom.trackFocus(domElement));

		this._focusTracker.addFocusListener(() => {
			this._focusTrackerHasFocus = true;
			this._update();
		});
		this._focusTracker.addBlurListener(() => {
			this._focusTrackerHasFocus = false;
			this._update();
		});
	}

	public hasFocus(): boolean {
		return this._actualHasFocus;
	}

	public beginForcedFocus(): void {
		this._forcedWidgetFocusCount++;
		this._update();
	}

	public endForcedFocus(): void {
		this._forcedWidgetFocusCount--;
		this._update();
	}

	private _update(): void {
		let newActualHasFocus = this._focusTrackerHasFocus;
		if (this._forcedWidgetFocusCount > 0) {
			newActualHasFocus = true;
		}

		if (this._actualHasFocus === newActualHasFocus) {
			// no change
			return;
		}

		this._actualHasFocus = newActualHasFocus;
		this._onChange.fire(void 0);
	}
}

A
Alex Dima 已提交
604
class OverlayWidget2 implements editorBrowser.IOverlayWidget {
E
Erich Gamma 已提交
605 606

	private _id: string;
A
Alex Dima 已提交
607
	private _position: editorBrowser.IOverlayWidgetPosition;
E
Erich Gamma 已提交
608 609
	private _domNode: HTMLElement;

A
Alex Dima 已提交
610
	constructor(id:string, position:editorBrowser.IOverlayWidgetPosition) {
E
Erich Gamma 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624
		this._id = id;
		this._position = position;
		this._domNode = document.createElement('div');
		this._domNode.className = this._id.replace(/\./g, '-').replace(/[^a-z0-9\-]/,'');
	}

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

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

A
Alex Dima 已提交
625
	public getPosition(): editorBrowser.IOverlayWidgetPosition {
E
Erich Gamma 已提交
626 627 628 629 630 631 632 633
		return this._position;
	}
}

export enum EditCursorState {
	EndOfLastEditOperation = 0
}

A
Alex Dima 已提交
634 635
class SingleEditOperation {

636
	range: Range;
A
Alex Dima 已提交
637 638 639 640 641 642 643 644 645 646 647
	text: string;
	forceMoveMarkers: boolean;

	constructor(source:editorCommon.ISingleEditOperation) {
		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 已提交
648
export class CommandRunner implements editorCommon.ICommand {
E
Erich Gamma 已提交
649

A
Alex Dima 已提交
650
	private _ops: SingleEditOperation[];
E
Erich Gamma 已提交
651 652
	private _editCursorState: EditCursorState;

A
Alex Dima 已提交
653
	constructor(ops: editorCommon.ISingleEditOperation[], editCursorState: EditCursorState) {
A
Alex Dima 已提交
654
		this._ops = ops.map(op => new SingleEditOperation(op));
E
Erich Gamma 已提交
655 656 657
		this._editCursorState = editCursorState;
	}

A
Alex Dima 已提交
658
	public getEditOperations(model: editorCommon.ITokenizedModel, builder: editorCommon.IEditOperationBuilder): void {
E
Erich Gamma 已提交
659 660 661 662 663 664 665 666 667 668
		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 已提交
669
		var resultOps:editorCommon.ISingleEditOperation[] = [];
E
Erich Gamma 已提交
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
		var previousOp = this._ops[0];
		for (var i = 1; i < this._ops.length; i++) {
			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);

		for (var i = 0; i < resultOps.length; i++) {
			builder.addEditOperation(Range.lift(resultOps[i].range), resultOps[i].text);
		}
	}

688
	public computeCursorState(model: editorCommon.ITokenizedModel, helper: editorCommon.ICursorStateComputerData): Selection {
E
Erich Gamma 已提交
689 690
		var inverseEditOperations = helper.getInverseEditOperations();
		var srcRange = inverseEditOperations[inverseEditOperations.length - 1].range;
A
Alex Dima 已提交
691
		return new Selection(
E
Erich Gamma 已提交
692 693 694 695 696 697 698
			srcRange.endLineNumber,
			srcRange.endColumn,
			srcRange.endLineNumber,
			srcRange.endColumn
		);
	}
}