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

import 'vs/css!./media/editor';
8
import 'vs/editor/common/view/editorColorRegistry'; // initialze editor theming partcicpants
E
Erich Gamma 已提交
9
import 'vs/css!./media/tokens';
J
Johannes Rieken 已提交
10
import { onUnexpectedError } from 'vs/base/common/errors';
11
import { TPromise } from 'vs/base/common/winjs.base';
A
Alex Dima 已提交
12 13
import * as browser from 'vs/base/browser/browser';
import * as dom from 'vs/base/browser/dom';
J
Johannes Rieken 已提交
14 15 16 17 18 19 20
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 已提交
21
import * as editorCommon from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
22 23 24
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 已提交
25
import * as editorBrowser from 'vs/editor/browser/editorBrowser';
J
Johannes Rieken 已提交
26 27
import { Colorizer } from 'vs/editor/browser/standalone/colorizer';
import { View } from 'vs/editor/browser/view/viewImpl';
J
Johannes Rieken 已提交
28
import { Disposable } from 'vs/base/common/lifecycle';
A
Alex Dima 已提交
29
import Event, { Emitter } from 'vs/base/common/event';
J
Johannes Rieken 已提交
30 31
import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { InternalEditorAction } from 'vs/editor/common/editorAction';
A
Alex Dima 已提交
32
import { ViewEventType } from 'vs/editor/browser/view/viewOutgoingEvents';
E
Erich Gamma 已提交
33

34
export abstract 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 64 65 66 67
	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 已提交
68

69
	private _codeEditorService: ICodeEditorService;
70
	private _commandService: ICommandService;
71

J
Johannes Rieken 已提交
72
	protected domElement: HTMLElement;
73
	private _focusTracker: CodeEditorWidgetFocusTracker;
E
Erich Gamma 已提交
74

J
Johannes Rieken 已提交
75
	_configuration: Configuration;
76

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

J
Johannes Rieken 已提交
80
	_view: editorBrowser.IView;
E
Erich Gamma 已提交
81 82

	constructor(
J
Johannes Rieken 已提交
83 84
		domElement: HTMLElement,
		options: editorCommon.IEditorOptions,
E
Erich Gamma 已提交
85 86
		@IInstantiationService instantiationService: IInstantiationService,
		@ICodeEditorService codeEditorService: ICodeEditorService,
87
		@ICommandService commandService: ICommandService,
88
		@IContextKeyService contextKeyService: IContextKeyService
E
Erich Gamma 已提交
89
	) {
90
		super(domElement, options, instantiationService, contextKeyService);
91
		this._codeEditorService = codeEditorService;
92
		this._commandService = commandService;
E
Erich Gamma 已提交
93

94 95 96
		this._focusTracker = new CodeEditorWidgetFocusTracker(domElement);
		this._focusTracker.onChage(() => {
			let hasFocus = this._focusTracker.hasFocus();
97

98
			if (hasFocus) {
A
Alex Dima 已提交
99
				this._onDidFocusEditor.fire();
100
			} else {
A
Alex Dima 已提交
101
				this._onDidBlurEditor.fire();
E
Erich Gamma 已提交
102 103 104 105 106 107
			}
		});

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

108 109 110
		let contributions = this._getContributions();
		for (let i = 0, len = contributions.length; i < len; i++) {
			let ctor = contributions[i];
E
Erich Gamma 已提交
111
			try {
112
				let contribution = this._instantiationService.createInstance(ctor, this);
A
Alex Dima 已提交
113
				this._contributions[contribution.getId()] = contribution;
E
Erich Gamma 已提交
114
			} catch (err) {
115
				onUnexpectedError(err);
E
Erich Gamma 已提交
116 117
			}
		}
118

119
		this._getActions().forEach((action) => {
120 121 122 123 124 125 126 127 128 129 130 131
			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 已提交
132
			this._actions[internalAction.id] = internalAction;
133
		});
134 135

		this._codeEditorService.addCodeEditor(this);
E
Erich Gamma 已提交
136 137
	}

138
	protected abstract _getContributions(): editorBrowser.IEditorContributionCtor[];
139 140
	protected abstract _getActions(): EditorAction[];

J
Johannes Rieken 已提交
141
	protected _createConfiguration(options: editorCommon.ICodeEditorWidgetCreationOptions): CommonEditorConfiguration {
142
		return new Configuration(options, this.domElement);
E
Erich Gamma 已提交
143 144 145
	}

	public dispose(): void {
146 147
		this._codeEditorService.removeCodeEditor(this);

E
Erich Gamma 已提交
148 149 150
		this.contentWidgets = {};
		this.overlayWidgets = {};

151
		this._focusTracker.dispose();
E
Erich Gamma 已提交
152 153 154
		super.dispose();
	}

J
Johannes Rieken 已提交
155
	public updateOptions(newOptions: editorCommon.IEditorOptions): void {
156
		let oldTheme = this._configuration.editor.viewInfo.theme;
157
		super.updateOptions(newOptions);
158
		let newTheme = this._configuration.editor.viewInfo.theme;
159 160 161 162 163 164

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

J
Johannes Rieken 已提交
165
	public colorizeModelLine(lineNumber: number, model: editorCommon.IModel = this.model): string {
E
Erich Gamma 已提交
166 167 168
		if (!model) {
			return '';
		}
A
Alex Dima 已提交
169
		let content = model.getLineContent(lineNumber);
A
Alex Dima 已提交
170 171
		model.forceTokenization(lineNumber);
		let tokens = model.getLineTokens(lineNumber);
A
Alex Dima 已提交
172 173
		let inflatedTokens = tokens.inflate();
		let tabSize = model.getOptions().tabSize;
174
		return Colorizer.colorizeLine(content, model.mightContainRTL(), inflatedTokens, tabSize);
E
Erich Gamma 已提交
175
	}
A
Alex Dima 已提交
176
	public getView(): editorBrowser.IView {
E
Erich Gamma 已提交
177 178 179 180 181 182 183
		return this._view;
	}

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

187
	public getCenteredRangeInViewport(): Range {
E
Erich Gamma 已提交
188 189 190
		if (!this.hasView) {
			return null;
		}
A
Alex Dima 已提交
191
		return this.viewModel.getCenteredRangeInViewport();
E
Erich Gamma 已提交
192 193
	}

A
Alex Dima 已提交
194
	protected _getCompletelyVisibleViewRange(): Range {
195 196 197
		if (!this.hasView) {
			return null;
		}
A
Alex Dima 已提交
198 199 200 201 202 203
		return this._view.getCodeEditorHelper().getCompletelyVisibleViewRange();
	}

	public getCompletelyVisibleLinesRangeInViewport(): Range {
		const viewRange = this._getCompletelyVisibleViewRange();
		return this.viewModel.coordinatesConverter.convertViewRangeToModelRange(viewRange);
204 205
	}

206
	public getScrollWidth(): number {
E
Erich Gamma 已提交
207
		if (!this.hasView) {
208
			return -1;
E
Erich Gamma 已提交
209
		}
210 211 212 213 214
		return this._view.getCodeEditorHelper().getScrollWidth();
	}
	public getScrollLeft(): number {
		if (!this.hasView) {
			return -1;
E
Erich Gamma 已提交
215
		}
216
		return this._view.getCodeEditorHelper().getScrollLeft();
E
Erich Gamma 已提交
217 218
	}

219
	public getScrollHeight(): number {
E
Erich Gamma 已提交
220 221 222
		if (!this.hasView) {
			return -1;
		}
223
		return this._view.getCodeEditorHelper().getScrollHeight();
E
Erich Gamma 已提交
224
	}
225
	public getScrollTop(): number {
E
Erich Gamma 已提交
226
		if (!this.hasView) {
227
			return -1;
E
Erich Gamma 已提交
228
		}
229
		return this._view.getCodeEditorHelper().getScrollTop();
E
Erich Gamma 已提交
230 231
	}

J
Johannes Rieken 已提交
232
	public setScrollLeft(newScrollLeft: number): void {
E
Erich Gamma 已提交
233 234 235 236 237 238
		if (!this.hasView) {
			return;
		}
		if (typeof newScrollLeft !== 'number') {
			throw new Error('Invalid arguments');
		}
239 240 241
		this._view.getCodeEditorHelper().setScrollPosition({
			scrollLeft: newScrollLeft
		});
E
Erich Gamma 已提交
242
	}
J
Johannes Rieken 已提交
243
	public setScrollTop(newScrollTop: number): void {
E
Erich Gamma 已提交
244
		if (!this.hasView) {
245
			return;
E
Erich Gamma 已提交
246
		}
247 248 249 250 251 252
		if (typeof newScrollTop !== 'number') {
			throw new Error('Invalid arguments');
		}
		this._view.getCodeEditorHelper().setScrollPosition({
			scrollTop: newScrollTop
		});
E
Erich Gamma 已提交
253
	}
254
	public setScrollPosition(position: editorCommon.INewScrollPosition): void {
E
Erich Gamma 已提交
255
		if (!this.hasView) {
256
			return;
E
Erich Gamma 已提交
257
		}
258
		this._view.getCodeEditorHelper().setScrollPosition(position);
E
Erich Gamma 已提交
259 260
	}

J
Johannes Rieken 已提交
261
	public delegateVerticalScrollbarMouseDown(browserEvent: MouseEvent): void {
E
Erich Gamma 已提交
262
		if (!this.hasView) {
263
			return;
E
Erich Gamma 已提交
264
		}
265
		this._view.getCodeEditorHelper().delegateVerticalScrollbarMouseDown(browserEvent);
E
Erich Gamma 已提交
266 267
	}

A
Alex Dima 已提交
268
	public saveViewState(): editorCommon.ICodeEditorViewState {
E
Erich Gamma 已提交
269 270 271
		if (!this.cursor || !this.hasView) {
			return null;
		}
J
Johannes Rieken 已提交
272
		let contributionsState: { [key: string]: any } = {};
A
Alex Dima 已提交
273 274 275 276 277

		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];
278 279 280 281 282
			if (typeof contribution.saveViewState === 'function') {
				contributionsState[id] = contribution.saveViewState();
			}
		}

A
Alex Dima 已提交
283 284
		let cursorState = this.cursor.saveState();
		let viewState = this._view.saveState();
E
Erich Gamma 已提交
285 286
		return {
			cursorState: cursorState,
287 288
			viewState: viewState,
			contributionsState: contributionsState
E
Erich Gamma 已提交
289 290 291
		};
	}

A
Alex Dima 已提交
292
	public restoreViewState(s: editorCommon.ICodeEditorViewState): void {
E
Erich Gamma 已提交
293 294 295
		if (!this.cursor || !this.hasView) {
			return;
		}
B
Benjamin Pasero 已提交
296
		if (s && s.cursorState && s.viewState) {
A
Alex Dima 已提交
297 298
			let codeEditorState = <editorCommon.ICodeEditorViewState>s;
			let cursorState = <any>codeEditorState.cursorState;
B
Benjamin Pasero 已提交
299 300 301 302 303
			if (Array.isArray(cursorState)) {
				this.cursor.restoreState(<editorCommon.ICursorState[]>cursorState);
			} else {
				// Backwards compatibility
				this.cursor.restoreState([<editorCommon.ICursorState>cursorState]);
E
Erich Gamma 已提交
304
			}
B
Benjamin Pasero 已提交
305
			this._view.restoreState(codeEditorState.viewState);
306

B
Benjamin Pasero 已提交
307
			let contributionsState = s.contributionsState || {};
A
Alex Dima 已提交
308 309 310 311
			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];
312 313 314 315
				if (typeof contribution.restoreViewState === 'function') {
					contribution.restoreViewState(contributionsState[id]);
				}
			}
E
Erich Gamma 已提交
316 317 318
		}
	}

J
Johannes Rieken 已提交
319
	public layout(dimension?: editorCommon.IDimension): void {
E
Erich Gamma 已提交
320
		this._configuration.observeReferenceElement(dimension);
321
		this.render();
E
Erich Gamma 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334
	}

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

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

335
	public hasWidgetFocus(): boolean {
336
		return this._focusTracker && this._focusTracker.hasFocus();
337 338
	}

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

A
Alex Dima 已提交
367
	public removeContentWidget(widget: editorBrowser.IContentWidget): void {
A
Alex Dima 已提交
368
		let widgetId = widget.getId();
E
Erich Gamma 已提交
369
		if (this.contentWidgets.hasOwnProperty(widgetId)) {
A
Alex Dima 已提交
370
			let widgetData = this.contentWidgets[widgetId];
E
Erich Gamma 已提交
371 372 373 374 375 376 377
			delete this.contentWidgets[widgetId];
			if (this.hasView) {
				this._view.removeContentWidget(widgetData);
			}
		}
	}

A
Alex Dima 已提交
378
	public addOverlayWidget(widget: editorBrowser.IOverlayWidget): void {
A
Alex Dima 已提交
379
		let widgetData: editorBrowser.IOverlayWidgetData = {
E
Erich Gamma 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
			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 已提交
395
	public layoutOverlayWidget(widget: editorBrowser.IOverlayWidget): void {
A
Alex Dima 已提交
396
		let widgetId = widget.getId();
E
Erich Gamma 已提交
397
		if (this.overlayWidgets.hasOwnProperty(widgetId)) {
A
Alex Dima 已提交
398
			let widgetData = this.overlayWidgets[widgetId];
E
Erich Gamma 已提交
399 400 401 402 403 404 405
			widgetData.position = widget.getPosition();
			if (this.hasView) {
				this._view.layoutOverlayWidget(widgetData);
			}
		}
	}

A
Alex Dima 已提交
406
	public removeOverlayWidget(widget: editorBrowser.IOverlayWidget): void {
A
Alex Dima 已提交
407
		let widgetId = widget.getId();
E
Erich Gamma 已提交
408
		if (this.overlayWidgets.hasOwnProperty(widgetId)) {
A
Alex Dima 已提交
409
			let widgetData = this.overlayWidgets[widgetId];
E
Erich Gamma 已提交
410 411 412 413 414 415 416
			delete this.overlayWidgets[widgetId];
			if (this.hasView) {
				this._view.removeOverlayWidget(widgetData);
			}
		}
	}

J
Johannes Rieken 已提交
417
	public changeViewZones(callback: (accessor: editorBrowser.IViewZoneChangeAccessor) => void): void {
E
Erich Gamma 已提交
418 419 420
		if (!this.hasView) {
			return;
		}
A
Alex Dima 已提交
421
		let hasChanges = this._view.change(callback);
E
Erich Gamma 已提交
422
		if (hasChanges) {
A
Alex Dima 已提交
423
			this._onDidChangeViewZones.fire();
E
Erich Gamma 已提交
424 425 426
		}
	}

A
Alex Dima 已提交
427
	public getWhitespaces(): editorCommon.IEditorWhitespace[] {
E
Erich Gamma 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
		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);
	}

448 449 450 451 452 453 454
	public getTargetAtClientPoint(clientX: number, clientY: number): editorBrowser.IMouseTarget {
		if (!this.hasView) {
			return null;
		}
		return this._view.getCodeEditorHelper().getTargetAtClientPoint(clientX, clientY);
	}

J
Johannes Rieken 已提交
455
	public getScrolledVisiblePosition(rawPosition: editorCommon.IPosition): { top: number; left: number; height: number; } {
E
Erich Gamma 已提交
456 457 458 459
		if (!this.hasView) {
			return null;
		}

A
Alex Dima 已提交
460 461 462
		let position = this.model.validatePosition(rawPosition);
		let helper = this._view.getCodeEditorHelper();
		let layoutInfo = this._configuration.editor.layoutInfo;
E
Erich Gamma 已提交
463

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

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

J
Johannes Rieken 已提交
474
	public getOffsetForColumn(lineNumber: number, column: number): number {
E
Erich Gamma 已提交
475 476 477 478 479 480
		if (!this.hasView) {
			return -1;
		}
		return this._view.getCodeEditorHelper().getOffsetForColumn(lineNumber, column);
	}

481 482 483 484
	public render(): void {
		if (!this.hasView) {
			return;
		}
485
		this._view.render(true, false);
486 487
	}

J
Johannes Rieken 已提交
488
	public setHiddenAreas(ranges: editorCommon.IRange[]): void {
M
Martin Aeschlimann 已提交
489 490 491 492 493
		if (this.viewModel) {
			this.viewModel.setHiddenAreas(ranges);
		}
	}

J
Johannes Rieken 已提交
494
	public setAriaActiveDescendant(id: string): void {
A
Alex Dima 已提交
495 496 497 498 499 500
		if (!this.hasView) {
			return;
		}
		this._view.setAriaActiveDescendant(id);
	}

J
Johannes Rieken 已提交
501
	public applyFontInfo(target: HTMLElement): void {
502 503 504
		Configuration.applyFontInfoSlow(target, this._configuration.editor.fontInfo);
	}

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

508
		super._attachModel(model);
E
Erich Gamma 已提交
509

510
		if (this._view) {
A
Alex Dima 已提交
511
			this.domElement.appendChild(this._view.domNode.domNode);
E
Erich Gamma 已提交
512

A
Alex Dima 已提交
513 514 515 516 517
			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 已提交
518

A
Alex Dima 已提交
519 520 521 522 523
			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 已提交
524

A
Alex Dima 已提交
525 526
			this._view.render(false, true);
			this.hasView = true;
E
Erich Gamma 已提交
527 528 529
		}
	}

530
	protected _enableEmptySelectionClipboard(): boolean {
A
Alex Dima 已提交
531
		return browser.enableEmptySelectionClipboard;
E
Erich Gamma 已提交
532 533
	}

534 535
	protected _createView(): void {
		this._view = new View(
536
			this._commandService,
537 538
			this._configuration,
			this.viewModel,
J
Johannes Rieken 已提交
539
			(source: string, handlerId: string, payload: any) => {
A
Alex Dima 已提交
540 541 542 543 544
				if (!this.cursor) {
					return;
				}
				this.cursor.trigger(source, handlerId, payload);
			}
545
		);
E
Erich Gamma 已提交
546

A
Alex Dima 已提交
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 604 605 606 607
		this.listenersToRemove.push(this._view.getInternalEventBus().addBulkListener((events) => {
			for (let i = 0, len = events.length; i < len; i++) {
				let eventType = events[i].type;
				let e = events[i].data;

				switch (eventType) {
					case ViewEventType.ViewFocusGained:
						this._onDidFocusEditorText.fire();
						// In IE, the focus is not synchronous, so we give it a little help
						this._onDidFocusEditor.fire();
						break;

					case ViewEventType.EditorScroll:
						this._onDidScrollChange.fire(e);
						break;

					case ViewEventType.ViewFocusLost:
						this._onDidBlurEditorText.fire();
						break;

					case ViewEventType.ContextMenu:
						this._onContextMenu.fire(e);
						break;

					case ViewEventType.MouseDown:
						this._onMouseDown.fire(e);
						break;

					case ViewEventType.MouseUp:
						this._onMouseUp.fire(e);
						break;

					case ViewEventType.MouseDrag:
						this._onMouseDrag.fire(e);
						break;

					case ViewEventType.MouseDrop:
						this._onMouseDrop.fire(e);
						break;

					case ViewEventType.KeyUp:
						this._onKeyUp.fire(e);
						break;

					case ViewEventType.MouseMove:
						this._onMouseMove.fire(e);
						break;

					case ViewEventType.MouseLeave:
						this._onMouseLeave.fire(e);
						break;

					case ViewEventType.KeyDown:
						this._onKeyDown.fire(e);
						break;

					default:
					// console.warn("Unhandled view event: ", e);
				}
			}
		}));
608
	}
E
Erich Gamma 已提交
609

A
Alex Dima 已提交
610
	protected _detachModel(): editorCommon.IModel {
A
Alex Dima 已提交
611
		let removeDomNode: HTMLElement = null;
E
Erich Gamma 已提交
612 613 614

		if (this._view) {
			this._view.dispose();
A
Alex Dima 已提交
615
			removeDomNode = this._view.domNode.domNode;
E
Erich Gamma 已提交
616 617 618
			this._view = null;
		}

619
		let result = super._detachModel();
E
Erich Gamma 已提交
620 621 622 623 624 625 626

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

		return result;
	}
627 628 629

	// BEGIN decorations

J
Johannes Rieken 已提交
630
	protected _registerDecorationType(key: string, options: editorCommon.IDecorationRenderOptions, parentTypeKey?: string): void {
631 632 633
		this._codeEditorService.registerDecorationType(key, options, parentTypeKey);
	}

J
Johannes Rieken 已提交
634
	protected _removeDecorationType(key: string): void {
635 636 637
		this._codeEditorService.removeDecorationType(key);
	}

J
Johannes Rieken 已提交
638
	protected _resolveDecorationOptions(typeKey: string, writable: boolean): editorCommon.IModelDecorationOptions {
639 640 641 642
		return this._codeEditorService.resolveDecorationOptions(typeKey, writable);
	}

	// END decorations
E
Erich Gamma 已提交
643 644
}

645 646
class CodeEditorWidgetFocusTracker extends Disposable {

647 648
	private _hasFocus: boolean;
	private _domFocusTracker: dom.IFocusTracker;
649 650 651 652

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

J
Johannes Rieken 已提交
653
	constructor(domElement: HTMLElement) {
654 655
		super();

656 657
		this._hasFocus = false;
		this._domFocusTracker = this._register(dom.trackFocus(domElement));
658

659 660 661
		this._domFocusTracker.addFocusListener(() => {
			this._hasFocus = true;
			this._onChange.fire(void 0);
662
		});
663 664 665
		this._domFocusTracker.addBlurListener(() => {
			this._hasFocus = false;
			this._onChange.fire(void 0);
666 667 668 669
		});
	}

	public hasFocus(): boolean {
670
		return this._hasFocus;
671 672 673
	}
}

A
Alex Dima 已提交
674
class OverlayWidget2 implements editorBrowser.IOverlayWidget {
E
Erich Gamma 已提交
675 676

	private _id: string;
A
Alex Dima 已提交
677
	private _position: editorBrowser.IOverlayWidgetPosition;
E
Erich Gamma 已提交
678 679
	private _domNode: HTMLElement;

J
Johannes Rieken 已提交
680
	constructor(id: string, position: editorBrowser.IOverlayWidgetPosition) {
E
Erich Gamma 已提交
681 682 683
		this._id = id;
		this._position = position;
		this._domNode = document.createElement('div');
J
Johannes Rieken 已提交
684
		this._domNode.className = this._id.replace(/\./g, '-').replace(/[^a-z0-9\-]/, '');
E
Erich Gamma 已提交
685 686 687 688 689 690 691 692 693 694
	}

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

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

A
Alex Dima 已提交
695
	public getPosition(): editorBrowser.IOverlayWidgetPosition {
E
Erich Gamma 已提交
696 697 698 699 700 701 702 703
		return this._position;
	}
}

export enum EditCursorState {
	EndOfLastEditOperation = 0
}

A
Alex Dima 已提交
704 705
class SingleEditOperation {

706
	range: Range;
A
Alex Dima 已提交
707 708 709
	text: string;
	forceMoveMarkers: boolean;

J
Johannes Rieken 已提交
710
	constructor(source: editorCommon.ISingleEditOperation) {
A
Alex Dima 已提交
711 712 713 714 715 716 717
		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 已提交
718
export class CommandRunner implements editorCommon.ICommand {
E
Erich Gamma 已提交
719

A
Alex Dima 已提交
720
	private _ops: SingleEditOperation[];
E
Erich Gamma 已提交
721 722
	private _editCursorState: EditCursorState;

A
Alex Dima 已提交
723
	constructor(ops: editorCommon.ISingleEditOperation[], editCursorState: EditCursorState) {
A
Alex Dima 已提交
724
		this._ops = ops.map(op => new SingleEditOperation(op));
E
Erich Gamma 已提交
725 726 727
		this._editCursorState = editCursorState;
	}

A
Alex Dima 已提交
728
	public getEditOperations(model: editorCommon.ITokenizedModel, builder: editorCommon.IEditOperationBuilder): void {
E
Erich Gamma 已提交
729 730 731 732 733 734 735 736 737 738
		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 已提交
739 740 741
		let resultOps: editorCommon.ISingleEditOperation[] = [];
		let previousOp = this._ops[0];
		for (let i = 1; i < this._ops.length; i++) {
E
Erich Gamma 已提交
742 743 744 745 746 747 748 749 750 751 752
			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 已提交
753
		for (let i = 0; i < resultOps.length; i++) {
E
Erich Gamma 已提交
754 755 756 757
			builder.addEditOperation(Range.lift(resultOps[i].range), resultOps[i].text);
		}
	}

758
	public computeCursorState(model: editorCommon.ITokenizedModel, helper: editorCommon.ICursorStateComputerData): Selection {
A
Alex Dima 已提交
759 760
		let inverseEditOperations = helper.getInverseEditOperations();
		let srcRange = inverseEditOperations[inverseEditOperations.length - 1].range;
A
Alex Dima 已提交
761
		return new Selection(
E
Erich Gamma 已提交
762 763 764 765 766 767 768
			srcRange.endLineNumber,
			srcRange.endColumn,
			srcRange.endLineNumber,
			srcRange.endColumn
		);
	}
}