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

J
Johannes Rieken 已提交
7 8 9
import { onUnexpectedError } from 'vs/base/common/errors';
import { EventEmitter, EmitterEvent, IEventEmitter } from 'vs/base/common/eventEmitter';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
A
Alex Dima 已提交
10 11 12
import * as timer from 'vs/base/common/timer';
import * as browser from 'vs/base/browser/browser';
import * as dom from 'vs/base/browser/dom';
J
Johannes Rieken 已提交
13 14 15
import { StyleMutator } from 'vs/base/browser/styleMutator';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { Range } from 'vs/editor/common/core/range';
A
Alex Dima 已提交
16
import * as editorCommon from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
17 18 19 20
import { ViewEventHandler } from 'vs/editor/common/viewModel/viewEventHandler';
import { Configuration } from 'vs/editor/browser/config/configuration';
import { KeyboardHandler, IKeyboardHandlerHelper } from 'vs/editor/browser/controller/keyboardHandler';
import { PointerHandler } from 'vs/editor/browser/controller/pointerHandler';
A
Alex Dima 已提交
21
import * as editorBrowser from 'vs/editor/browser/editorBrowser';
J
Johannes Rieken 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
import { ViewController, TriggerCursorHandler } from 'vs/editor/browser/view/viewController';
import { ViewEventDispatcher } from 'vs/editor/browser/view/viewEventDispatcher';
import { ContentViewOverlays, MarginViewOverlays } from 'vs/editor/browser/view/viewOverlays';
import { LayoutProvider } from 'vs/editor/browser/viewLayout/layoutProvider';
import { ViewContentWidgets } from 'vs/editor/browser/viewParts/contentWidgets/contentWidgets';
import { CurrentLineHighlightOverlay } from 'vs/editor/browser/viewParts/currentLineHighlight/currentLineHighlight';
import { DecorationsOverlay } from 'vs/editor/browser/viewParts/decorations/decorations';
import { GlyphMarginOverlay } from 'vs/editor/browser/viewParts/glyphMargin/glyphMargin';
import { LineNumbersOverlay } from 'vs/editor/browser/viewParts/lineNumbers/lineNumbers';
import { IndentGuidesOverlay } from 'vs/editor/browser/viewParts/indentGuides/indentGuides';
import { ViewLines } from 'vs/editor/browser/viewParts/lines/viewLines';
import { LinesDecorationsOverlay } from 'vs/editor/browser/viewParts/linesDecorations/linesDecorations';
import { ViewOverlayWidgets } from 'vs/editor/browser/viewParts/overlayWidgets/overlayWidgets';
import { DecorationsOverviewRuler } from 'vs/editor/browser/viewParts/overviewRuler/decorationsOverviewRuler';
import { OverviewRuler } from 'vs/editor/browser/viewParts/overviewRuler/overviewRuler';
import { Rulers } from 'vs/editor/browser/viewParts/rulers/rulers';
import { ScrollDecorationViewPart } from 'vs/editor/browser/viewParts/scrollDecoration/scrollDecoration';
import { SelectionsOverlay } from 'vs/editor/browser/viewParts/selections/selections';
import { ViewCursors } from 'vs/editor/browser/viewParts/viewCursors/viewCursors';
import { ViewZones } from 'vs/editor/browser/viewParts/viewZones/viewZones';
import { ViewPart } from 'vs/editor/browser/view/viewPart';
import { ViewContext, IViewEventHandler } from 'vs/editor/common/view/viewContext';
import { IViewModel } from 'vs/editor/common/viewModel/viewModel';
import { ViewLinesViewportData } from 'vs/editor/common/viewLayout/viewLinesViewportData';
import { IRenderingContext } from 'vs/editor/common/view/renderingContext';
import { IPointerHandlerHelper } from 'vs/editor/browser/controller/mouseHandler';
E
Erich Gamma 已提交
48

A
Alex Dima 已提交
49
export class View extends ViewEventHandler implements editorBrowser.IView, IDisposable {
E
Erich Gamma 已提交
50

J
Johannes Rieken 已提交
51
	private eventDispatcher: ViewEventDispatcher;
E
Erich Gamma 已提交
52

J
Johannes Rieken 已提交
53 54
	private listenersToRemove: IDisposable[];
	private listenersToDispose: IDisposable[];
E
Erich Gamma 已提交
55 56

	private layoutProvider: LayoutProvider;
57
	public _context: ViewContext;
E
Erich Gamma 已提交
58 59 60 61 62 63 64 65

	// The view lines
	private viewLines: ViewLines;

	// These are parts, but we must do some API related calls on them, so we keep a reference
	private viewZones: ViewZones;
	private contentWidgets: ViewContentWidgets;
	private overlayWidgets: ViewOverlayWidgets;
66
	private viewCursors: ViewCursors;
A
Alex Dima 已提交
67
	private viewParts: ViewPart[];
E
Erich Gamma 已提交
68 69 70 71

	private keyboardHandler: KeyboardHandler;
	private pointerHandler: PointerHandler;

A
Alex Dima 已提交
72
	private outgoingEventBus: EventEmitter;
E
Erich Gamma 已提交
73 74 75 76 77 78 79 80 81 82

	// Dom nodes
	private linesContent: HTMLElement;
	public domNode: HTMLElement;
	public textArea: HTMLTextAreaElement;
	private textAreaCover: HTMLElement;
	private linesContentContainer: HTMLElement;
	private overflowGuardContainer: HTMLElement;

	// Actual mutable state
J
Johannes Rieken 已提交
83
	private hasFocus: boolean;
E
Erich Gamma 已提交
84 85
	private _isDisposed: boolean;

J
Johannes Rieken 已提交
86
	private handleAccumulatedModelEventsTimeout: number;
A
Alex Dima 已提交
87
	private accumulatedModelEvents: EmitterEvent[];
A
Alex Dima 已提交
88
	private _renderAnimationFrame: IDisposable;
E
Erich Gamma 已提交
89

A
Alex Dima 已提交
90
	constructor(
91
		commandService: ICommandService,
J
Johannes Rieken 已提交
92 93 94
		configuration: Configuration,
		model: IViewModel,
		private triggerCursorHandler: TriggerCursorHandler
A
Alex Dima 已提交
95
	) {
E
Erich Gamma 已提交
96 97 98
		super();
		this._isDisposed = false;
		this._renderAnimationFrame = null;
A
Alex Dima 已提交
99
		this.outgoingEventBus = new EventEmitter();
E
Erich Gamma 已提交
100

A
Alex Dima 已提交
101
		let viewController = new ViewController(model, triggerCursorHandler, this.outgoingEventBus, commandService);
E
Erich Gamma 已提交
102 103 104 105 106

		this.listenersToRemove = [];
		this.listenersToDispose = [];

		// The event dispatcher will always go through _renderOnce before dispatching any events
J
Johannes Rieken 已提交
107
		this.eventDispatcher = new ViewEventDispatcher((callback: () => void) => this._renderOnce(callback));
E
Erich Gamma 已提交
108 109 110

		// These two dom nodes must be constructed up front, since references are needed in the layout provider (scrolling & co.)
		this.linesContent = document.createElement('div');
A
Alex Dima 已提交
111
		this.linesContent.className = editorBrowser.ClassNames.LINES_CONTENT + ' monaco-editor-background';
E
Erich Gamma 已提交
112
		this.domNode = document.createElement('div');
113
		this.domNode.className = configuration.editor.viewInfo.editorClassName;
E
Erich Gamma 已提交
114 115

		this.overflowGuardContainer = document.createElement('div');
A
Alex Dima 已提交
116
		this.overflowGuardContainer.className = editorBrowser.ClassNames.OVERFLOW_GUARD;
E
Erich Gamma 已提交
117 118 119 120 121 122 123 124 125

		// The layout provider has such responsibilities as:
		// - scrolling (i.e. viewport / full size) & co.
		// - whitespaces (a.k.a. view zones) management & co.
		// - line heights updating & co.
		this.layoutProvider = new LayoutProvider(configuration, model, this.eventDispatcher, this.linesContent, this.domNode, this.overflowGuardContainer);
		this.eventDispatcher.addEventHandler(this.layoutProvider);

		// The view context is passed on to most classes (basically to reduce param. counts in ctors)
126
		this._context = new ViewContext(
J
Johannes Rieken 已提交
127 128 129
			configuration, model, this.eventDispatcher,
			(eventHandler: IViewEventHandler) => this.eventDispatcher.addEventHandler(eventHandler),
			(eventHandler: IViewEventHandler) => this.eventDispatcher.removeEventHandler(eventHandler)
E
Erich Gamma 已提交
130 131
		);

132
		this.createTextArea();
E
Erich Gamma 已提交
133 134 135
		this.createViewParts();

		// Keyboard handler
136
		this.keyboardHandler = new KeyboardHandler(this._context, viewController, this.createKeyboardHandlerHelper());
E
Erich Gamma 已提交
137 138

		// Pointer handler
139
		this.pointerHandler = new PointerHandler(this._context, viewController, this.createPointerHandlerHelper());
E
Erich Gamma 已提交
140 141 142 143 144 145 146 147 148 149

		this.hasFocus = false;
		this.codeEditorHelper = null;

		this.eventDispatcher.addEventHandler(this);

		// The view lines rendering calls model.getLineTokens() that might emit events that its tokens have changed.
		// This delayed processing of incoming model events acts as a guard against undesired/unexpected recursion.
		this.handleAccumulatedModelEventsTimeout = -1;
		this.accumulatedModelEvents = [];
J
Johannes Rieken 已提交
150
		this.listenersToRemove.push(model.addBulkListener2((events: EmitterEvent[]) => {
E
Erich Gamma 已提交
151 152 153 154 155 156 157 158 159 160 161
			this.accumulatedModelEvents = this.accumulatedModelEvents.concat(events);
			if (this.handleAccumulatedModelEventsTimeout === -1) {
				this.handleAccumulatedModelEventsTimeout = setTimeout(() => {
					this.handleAccumulatedModelEventsTimeout = -1;
					this._flushAnyAccumulatedEvents();
				});
			}
		}));
	}

	private _flushAnyAccumulatedEvents(): void {
A
Alex Dima 已提交
162
		let toEmit = this.accumulatedModelEvents;
E
Erich Gamma 已提交
163 164 165 166 167 168
		this.accumulatedModelEvents = [];
		if (toEmit.length > 0) {
			this.eventDispatcher.emitMany(toEmit);
		}
	}

169
	private createTextArea(): void {
E
Erich Gamma 已提交
170 171
		// Text Area (The focus will always be in the textarea when the cursor is blinking)
		this.textArea = <HTMLTextAreaElement>document.createElement('textarea');
A
Alex Dima 已提交
172
		this.textArea.className = editorBrowser.ClassNames.TEXTAREA;
E
Erich Gamma 已提交
173 174 175 176
		this.textArea.setAttribute('wrap', 'off');
		this.textArea.setAttribute('autocorrect', 'off');
		this.textArea.setAttribute('autocapitalize', 'off');
		this.textArea.setAttribute('spellcheck', 'false');
177
		this.textArea.setAttribute('aria-label', this._context.configuration.editor.viewInfo.ariaLabel);
E
Erich Gamma 已提交
178 179
		this.textArea.setAttribute('role', 'textbox');
		this.textArea.setAttribute('aria-multiline', 'true');
A
Alex Dima 已提交
180 181 182
		this.textArea.setAttribute('aria-haspopup', 'false');
		this.textArea.setAttribute('aria-autocomplete', 'both');

A
Alex Dima 已提交
183 184
		StyleMutator.setTop(this.textArea, 0);
		StyleMutator.setLeft(this.textArea, 0);
E
Erich Gamma 已提交
185

A
Alex Dima 已提交
186 187
		this.listenersToDispose.push(dom.addDisposableListener(this.textArea, 'focus', () => this._setHasFocus(true)));
		this.listenersToDispose.push(dom.addDisposableListener(this.textArea, 'blur', () => this._setHasFocus(false)));
E
Erich Gamma 已提交
188 189 190 191 192

		// On top of the text area, we position a dom node to cover it up
		// (there have been reports of tiny blinking cursors)
		// (in WebKit the textarea is 1px by 1px because it cannot handle input to a 0x0 textarea)
		this.textAreaCover = document.createElement('div');
193
		if (this._context.configuration.editor.viewInfo.glyphMargin) {
A
Alex Dima 已提交
194
			this.textAreaCover.className = 'monaco-editor-background ' + editorBrowser.ClassNames.GLYPH_MARGIN + ' ' + editorBrowser.ClassNames.TEXTAREA_COVER;
E
Erich Gamma 已提交
195
		} else {
196
			if (this._context.configuration.editor.viewInfo.renderLineNumbers) {
A
Alex Dima 已提交
197
				this.textAreaCover.className = 'monaco-editor-background ' + editorBrowser.ClassNames.LINE_NUMBERS + ' ' + editorBrowser.ClassNames.TEXTAREA_COVER;
E
Erich Gamma 已提交
198
			} else {
A
Alex Dima 已提交
199
				this.textAreaCover.className = 'monaco-editor-background ' + editorBrowser.ClassNames.TEXTAREA_COVER;
E
Erich Gamma 已提交
200 201 202
			}
		}
		this.textAreaCover.style.position = 'absolute';
A
Alex Dima 已提交
203 204 205 206
		StyleMutator.setWidth(this.textAreaCover, 1);
		StyleMutator.setHeight(this.textAreaCover, 1);
		StyleMutator.setTop(this.textAreaCover, 0);
		StyleMutator.setLeft(this.textAreaCover, 0);
E
Erich Gamma 已提交
207 208 209 210 211 212
	}

	private createViewParts(): void {
		this.viewParts = [];

		// View Lines
213
		this.viewLines = new ViewLines(this._context, this.layoutProvider);
E
Erich Gamma 已提交
214 215

		// View Zones
216
		this.viewZones = new ViewZones(this._context, this.layoutProvider);
E
Erich Gamma 已提交
217 218 219
		this.viewParts.push(this.viewZones);

		// Decorations overview ruler
A
Alex Dima 已提交
220
		let decorationsOverviewRuler = new DecorationsOverviewRuler(
J
Johannes Rieken 已提交
221 222
			this._context, this.layoutProvider.getScrollHeight(),
			(lineNumber: number) => this.layoutProvider.getVerticalOffsetForLineNumber(lineNumber)
E
Erich Gamma 已提交
223 224 225 226
		);
		this.viewParts.push(decorationsOverviewRuler);


A
Alex Dima 已提交
227
		let scrollDecoration = new ScrollDecorationViewPart(this._context);
E
Erich Gamma 已提交
228 229
		this.viewParts.push(scrollDecoration);

A
Alex Dima 已提交
230
		let contentViewOverlays = new ContentViewOverlays(this._context, this.layoutProvider);
E
Erich Gamma 已提交
231
		this.viewParts.push(contentViewOverlays);
232 233 234
		contentViewOverlays.addDynamicOverlay(new CurrentLineHighlightOverlay(this._context, this.layoutProvider));
		contentViewOverlays.addDynamicOverlay(new SelectionsOverlay(this._context));
		contentViewOverlays.addDynamicOverlay(new DecorationsOverlay(this._context));
235
		contentViewOverlays.addDynamicOverlay(new IndentGuidesOverlay(this._context));
E
Erich Gamma 已提交
236

A
Alex Dima 已提交
237
		let marginViewOverlays = new MarginViewOverlays(this._context, this.layoutProvider);
E
Erich Gamma 已提交
238
		this.viewParts.push(marginViewOverlays);
239 240 241
		marginViewOverlays.addDynamicOverlay(new GlyphMarginOverlay(this._context));
		marginViewOverlays.addDynamicOverlay(new LinesDecorationsOverlay(this._context));
		marginViewOverlays.addDynamicOverlay(new LineNumbersOverlay(this._context));
E
Erich Gamma 已提交
242 243 244


		// Content widgets
245
		this.contentWidgets = new ViewContentWidgets(this._context, this.domNode);
E
Erich Gamma 已提交
246 247
		this.viewParts.push(this.contentWidgets);

248 249
		this.viewCursors = new ViewCursors(this._context);
		this.viewParts.push(this.viewCursors);
E
Erich Gamma 已提交
250 251

		// Overlay widgets
252
		this.overlayWidgets = new ViewOverlayWidgets(this._context);
E
Erich Gamma 已提交
253 254
		this.viewParts.push(this.overlayWidgets);

A
Alex Dima 已提交
255
		let rulers = new Rulers(this._context, this.layoutProvider);
256 257
		this.viewParts.push(rulers);

E
Erich Gamma 已提交
258 259 260 261 262 263
		// -------------- Wire dom nodes up

		this.linesContentContainer = this.layoutProvider.getScrollbarContainerDomNode();
		this.linesContentContainer.style.position = 'absolute';

		if (decorationsOverviewRuler) {
A
Alex Dima 已提交
264
			let overviewRulerData = this.layoutProvider.getOverviewRulerInsertData();
E
Erich Gamma 已提交
265 266 267 268
			overviewRulerData.parent.insertBefore(decorationsOverviewRuler.getDomNode(), overviewRulerData.insertBefore);
		}

		this.linesContent.appendChild(contentViewOverlays.getDomNode());
269
		this.linesContent.appendChild(rulers.domNode);
E
Erich Gamma 已提交
270
		this.linesContent.appendChild(this.viewZones.domNode);
A
Alex Dima 已提交
271
		this.linesContent.appendChild(this.viewLines.getDomNode());
E
Erich Gamma 已提交
272
		this.linesContent.appendChild(this.contentWidgets.domNode);
273
		this.linesContent.appendChild(this.viewCursors.getDomNode());
274
		this.overflowGuardContainer.appendChild(marginViewOverlays.getDomNode());
E
Erich Gamma 已提交
275 276 277 278 279 280
		this.overflowGuardContainer.appendChild(this.linesContentContainer);
		this.overflowGuardContainer.appendChild(scrollDecoration.getDomNode());
		this.overflowGuardContainer.appendChild(this.overlayWidgets.domNode);
		this.overflowGuardContainer.appendChild(this.textArea);
		this.overflowGuardContainer.appendChild(this.textAreaCover);
		this.domNode.appendChild(this.overflowGuardContainer);
281
		this.domNode.appendChild(this.contentWidgets.overflowingContentWidgetsDomNode);
E
Erich Gamma 已提交
282 283 284 285 286 287 288
	}

	private _flushAccumulatedAndRenderNow(): void {
		this._flushAnyAccumulatedEvents();
		this._renderNow();
	}

289
	private createPointerHandlerHelper(): IPointerHandlerHelper {
E
Erich Gamma 已提交
290 291 292 293 294 295 296 297 298 299 300
		return {
			viewDomNode: this.domNode,
			linesContentDomNode: this.linesContent,

			focusTextArea: () => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.focusTextArea: View is disposed');
				}
				this.focus();
			},

301 302 303 304
			isDirty: (): boolean => {
				return (this.accumulatedModelEvents.length > 0);
			},

E
Erich Gamma 已提交
305 306 307 308 309 310
			getScrollLeft: () => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.getScrollLeft: View is disposed');
				}
				return this.layoutProvider.getScrollLeft();
			},
311 312 313 314 315 316 317
			getScrollTop: () => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.getScrollTop: View is disposed');
				}
				return this.layoutProvider.getScrollTop();
			},

J
Johannes Rieken 已提交
318
			setScrollPosition: (position: editorCommon.INewScrollPosition) => {
E
Erich Gamma 已提交
319
				if (this._isDisposed) {
320
					throw new Error('ViewImpl.pointerHandler.setScrollPosition: View is disposed');
E
Erich Gamma 已提交
321
				}
322
				this.layoutProvider.setScrollPosition(position);
E
Erich Gamma 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
			},

			isAfterLines: (verticalOffset: number) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.isAfterLines: View is disposed');
				}
				return this.layoutProvider.isAfterLines(verticalOffset);
			},
			getLineNumberAtVerticalOffset: (verticalOffset: number) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.getLineNumberAtVerticalOffset: View is disposed');
				}
				return this.layoutProvider.getLineNumberAtVerticalOffset(verticalOffset);
			},
			getVerticalOffsetForLineNumber: (lineNumber: number) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.getVerticalOffsetForLineNumber: View is disposed');
				}
				return this.layoutProvider.getVerticalOffsetForLineNumber(lineNumber);
			},
			getWhitespaceAtVerticalOffset: (verticalOffset: number) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.getWhitespaceAtVerticalOffset: View is disposed');
				}
				return this.layoutProvider.getWhitespaceAtVerticalOffset(verticalOffset);
			},
349 350 351 352 353 354
			getLastViewCursorsRenderData: () => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.getLastViewCursorsRenderData: View is disposed');
				}
				return this.viewCursors.getLastRenderData() || [];
			},
E
Erich Gamma 已提交
355 356 357 358 359 360
			shouldSuppressMouseDownOnViewZone: (viewZoneId: number) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.shouldSuppressMouseDownOnViewZone: View is disposed');
				}
				return this.viewZones.shouldSuppressMouseDownOnViewZone(viewZoneId);
			},
361 362 363 364 365 366
			shouldSuppressMouseDownOnWidget: (widgetId: string) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.shouldSuppressMouseDownOnWidget: View is disposed');
				}
				return this.contentWidgets.shouldSuppressMouseDownOnWidget(widgetId);
			},
E
Erich Gamma 已提交
367 368 369 370 371 372 373 374 375 376 377 378 379
			getPositionFromDOMInfo: (spanNode: HTMLElement, offset: number) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.getPositionFromDOMInfo: View is disposed');
				}
				this._flushAccumulatedAndRenderNow();
				return this.viewLines.getPositionFromDOMInfo(spanNode, offset);
			},

			visibleRangeForPosition2: (lineNumber: number, column: number) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.visibleRangeForPosition2: View is disposed');
				}
				this._flushAccumulatedAndRenderNow();
A
Alex Dima 已提交
380
				let visibleRanges = this.viewLines.visibleRangesForRange2(new Range(lineNumber, column, lineNumber, column), 0);
E
Erich Gamma 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
				if (!visibleRanges) {
					return null;
				}
				return visibleRanges[0];
			},

			getLineWidth: (lineNumber: number) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.getLineWidth: View is disposed');
				}
				this._flushAccumulatedAndRenderNow();
				return this.viewLines.getLineWidth(lineNumber);
			}
		};
	}

397
	private createKeyboardHandlerHelper(): IKeyboardHandlerHelper {
E
Erich Gamma 已提交
398 399 400 401 402 403 404 405
		return {
			viewDomNode: this.domNode,
			textArea: this.textArea,
			visibleRangeForPositionRelativeToEditor: (lineNumber: number, column: number) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.keyboardHandler.visibleRangeForPositionRelativeToEditor: View is disposed');
				}
				this._flushAccumulatedAndRenderNow();
A
Alex Dima 已提交
406 407
				let linesViewPortData = this.layoutProvider.getLinesViewportData();
				let visibleRanges = this.viewLines.visibleRangesForRange2(new Range(lineNumber, column, lineNumber, column), linesViewPortData.visibleRangesDeltaTop);
E
Erich Gamma 已提交
408 409 410 411
				if (!visibleRanges) {
					return null;
				}
				return visibleRanges[0];
412 413 414
			},
			flushAnyAccumulatedEvents: () => {
				this._flushAnyAccumulatedEvents();
E
Erich Gamma 已提交
415 416 417 418
			}
		};
	}

J
Johannes Rieken 已提交
419
	public setAriaActiveDescendant(id: string): void {
A
Alex Dima 已提交
420 421 422 423 424 425 426 427 428 429 430 431 432
		if (id) {
			this.textArea.setAttribute('role', 'combobox');
			if (this.textArea.getAttribute('aria-activedescendant') !== id) {
				this.textArea.setAttribute('aria-haspopup', 'true');
				this.textArea.setAttribute('aria-activedescendant', id);
			}
		} else {
			this.textArea.setAttribute('role', 'textbox');
			this.textArea.removeAttribute('aria-activedescendant');
			this.textArea.removeAttribute('aria-haspopup');
		}
	}

E
Erich Gamma 已提交
433 434
	// --- begin event handlers

J
Johannes Rieken 已提交
435
	public onLayoutChanged(layoutInfo: editorCommon.EditorLayoutInfo): boolean {
A
Alex Dima 已提交
436
		if (browser.isChrome) {
A
tslint  
Alex Dima 已提交
437
			/* tslint:disable:no-unused-variable */
E
Erich Gamma 已提交
438 439
			// Access overflowGuardContainer.clientWidth to prevent relayouting bug in Chrome
			// See Bug 19676: Editor misses a layout event
A
Alex Dima 已提交
440
			let clientWidth = this.overflowGuardContainer.clientWidth + 'px';
A
tslint  
Alex Dima 已提交
441
			/* tslint:enable:no-unused-variable */
E
Erich Gamma 已提交
442
		}
A
Alex Dima 已提交
443 444
		StyleMutator.setWidth(this.domNode, layoutInfo.width);
		StyleMutator.setHeight(this.domNode, layoutInfo.height);
E
Erich Gamma 已提交
445

A
Alex Dima 已提交
446 447
		StyleMutator.setWidth(this.overflowGuardContainer, layoutInfo.width);
		StyleMutator.setHeight(this.overflowGuardContainer, layoutInfo.height);
E
Erich Gamma 已提交
448

A
Alex Dima 已提交
449 450
		StyleMutator.setWidth(this.linesContent, 1000000);
		StyleMutator.setHeight(this.linesContent, 1000000);
E
Erich Gamma 已提交
451

A
Alex Dima 已提交
452 453 454
		StyleMutator.setLeft(this.linesContentContainer, layoutInfo.contentLeft);
		StyleMutator.setWidth(this.linesContentContainer, layoutInfo.contentWidth);
		StyleMutator.setHeight(this.linesContentContainer, layoutInfo.contentHeight);
E
Erich Gamma 已提交
455

A
Alex Dima 已提交
456
		this.outgoingEventBus.emit(editorCommon.EventType.ViewLayoutChanged, layoutInfo);
E
Erich Gamma 已提交
457 458
		return false;
	}
A
Alex Dima 已提交
459
	public onConfigurationChanged(e: editorCommon.IConfigurationChangedEvent): boolean {
460 461
		if (e.viewInfo.editorClassName) {
			this.domNode.className = this._context.configuration.editor.viewInfo.editorClassName;
E
Erich Gamma 已提交
462
		}
463 464
		if (e.viewInfo.ariaLabel) {
			this.textArea.setAttribute('aria-label', this._context.configuration.editor.viewInfo.ariaLabel);
465
		}
E
Erich Gamma 已提交
466 467
		return false;
	}
J
Johannes Rieken 已提交
468
	public onScrollChanged(e: editorCommon.IScrollEvent): boolean {
A
Alex Dima 已提交
469
		this.outgoingEventBus.emit('scroll', e);
470
		return false;
E
Erich Gamma 已提交
471
	}
J
Johannes Rieken 已提交
472
	public onViewFocusChanged(isFocused: boolean): boolean {
A
Alex Dima 已提交
473
		dom.toggleClass(this.domNode, 'focused', isFocused);
E
Erich Gamma 已提交
474
		if (isFocused) {
A
Alex Dima 已提交
475
			this.outgoingEventBus.emit(editorCommon.EventType.ViewFocusGained, {});
E
Erich Gamma 已提交
476
		} else {
A
Alex Dima 已提交
477
			this.outgoingEventBus.emit(editorCommon.EventType.ViewFocusLost, {});
E
Erich Gamma 已提交
478 479 480
		}
		return false;
	}
481 482 483 484 485 486 487 488 489 490 491 492 493

	public onCursorRevealRange(e: editorCommon.IViewRevealRangeEvent): boolean {
		return e.revealCursor ? this.revealCursor() : false;
	}

	public onCursorScrollRequest(e: editorCommon.ICursorScrollRequestEvent): boolean {
		return e.revealCursor ? this.revealCursor() : false;
	}

	private revealCursor(): boolean {
		this.triggerCursorHandler('revealCursor', editorCommon.Handler.CursorMove, { to: editorCommon.CursorMovePosition.ViewPortIfOutside });
		return false;
	}
E
Erich Gamma 已提交
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
	// --- end event handlers

	public dispose(): void {
		this._isDisposed = true;
		if (this.handleAccumulatedModelEventsTimeout !== -1) {
			clearTimeout(this.handleAccumulatedModelEventsTimeout);
			this.handleAccumulatedModelEventsTimeout = -1;
		}
		if (this._renderAnimationFrame !== null) {
			this._renderAnimationFrame.dispose();
			this._renderAnimationFrame = null;
		}
		this.accumulatedModelEvents = [];

		this.eventDispatcher.removeEventHandler(this);
		this.outgoingEventBus.dispose();
A
Alex Dima 已提交
510
		this.listenersToRemove = dispose(this.listenersToRemove);
J
Joao Moreno 已提交
511
		this.listenersToDispose = dispose(this.listenersToDispose);
E
Erich Gamma 已提交
512 513 514 515 516 517 518

		this.keyboardHandler.dispose();
		this.pointerHandler.dispose();

		this.viewLines.dispose();

		// Destroy IViewPart second
A
Alex Dima 已提交
519
		for (let i = 0, len = this.viewParts.length; i < len; i++) {
E
Erich Gamma 已提交
520 521 522 523 524 525 526 527 528
			this.viewParts[i].dispose();
		}
		this.viewParts = [];

		this.layoutProvider.dispose();
	}

	// --- begin Code Editor APIs

J
Johannes Rieken 已提交
529
	private codeEditorHelper: editorBrowser.ICodeEditorHelper;
A
Alex Dima 已提交
530
	public getCodeEditorHelper(): editorBrowser.ICodeEditorHelper {
E
Erich Gamma 已提交
531 532
		if (!this.codeEditorHelper) {
			this.codeEditorHelper = {
533
				getScrollWidth: () => {
E
Erich Gamma 已提交
534
					if (this._isDisposed) {
535
						throw new Error('ViewImpl.codeEditorHelper.getScrollWidth: View is disposed');
E
Erich Gamma 已提交
536
					}
537
					return this.layoutProvider.getScrollWidth();
E
Erich Gamma 已提交
538 539 540 541 542 543 544
				},
				getScrollLeft: () => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.getScrollLeft: View is disposed');
					}
					return this.layoutProvider.getScrollLeft();
				},
545

E
Erich Gamma 已提交
546 547 548 549 550 551
				getScrollHeight: () => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.getScrollHeight: View is disposed');
					}
					return this.layoutProvider.getScrollHeight();
				},
552
				getScrollTop: () => {
E
Erich Gamma 已提交
553
					if (this._isDisposed) {
554
						throw new Error('ViewImpl.codeEditorHelper.getScrollTop: View is disposed');
E
Erich Gamma 已提交
555
					}
556
					return this.layoutProvider.getScrollTop();
E
Erich Gamma 已提交
557
				},
558

J
Johannes Rieken 已提交
559
				setScrollPosition: (position: editorCommon.INewScrollPosition) => {
560 561 562 563 564 565
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.setScrollPosition: View is disposed');
					}
					this.layoutProvider.setScrollPosition(position);
				},

J
Johannes Rieken 已提交
566
				getVerticalOffsetForPosition: (modelLineNumber: number, modelColumn: number) => {
E
Erich Gamma 已提交
567 568 569
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.getVerticalOffsetForPosition: View is disposed');
					}
A
Alex Dima 已提交
570
					let modelPosition = this._context.model.validateModelPosition({
E
Erich Gamma 已提交
571 572 573
						lineNumber: modelLineNumber,
						column: modelColumn
					});
A
Alex Dima 已提交
574
					let viewPosition = this._context.model.convertModelPositionToViewPosition(modelPosition.lineNumber, modelPosition.column);
E
Erich Gamma 已提交
575 576 577 578 579 580 581 582 583 584 585 586
					return this.layoutProvider.getVerticalOffsetForLineNumber(viewPosition.lineNumber);
				},
				delegateVerticalScrollbarMouseDown: (browserEvent: MouseEvent) => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.delegateVerticalScrollbarMouseDown: View is disposed');
					}
					this.layoutProvider.delegateVerticalScrollbarMouseDown(browserEvent);
				},
				getOffsetForColumn: (modelLineNumber: number, modelColumn: number) => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.getOffsetForColumn: View is disposed');
					}
A
Alex Dima 已提交
587
					let modelPosition = this._context.model.validateModelPosition({
E
Erich Gamma 已提交
588 589 590
						lineNumber: modelLineNumber,
						column: modelColumn
					});
A
Alex Dima 已提交
591
					let viewPosition = this._context.model.convertModelPositionToViewPosition(modelPosition.lineNumber, modelPosition.column);
E
Erich Gamma 已提交
592
					this._flushAccumulatedAndRenderNow();
A
Alex Dima 已提交
593
					let visibleRanges = this.viewLines.visibleRangesForRange2(new Range(viewPosition.lineNumber, viewPosition.column, viewPosition.lineNumber, viewPosition.column), 0);
E
Erich Gamma 已提交
594 595 596 597 598 599 600 601 602 603
					if (!visibleRanges) {
						return -1;
					}
					return visibleRanges[0].left;
				}
			};
		}
		return this.codeEditorHelper;
	}

604
	public getCenteredRangeInViewport(): Range {
E
Erich Gamma 已提交
605 606 607
		if (this._isDisposed) {
			throw new Error('ViewImpl.getCenteredRangeInViewport: View is disposed');
		}
A
Alex Dima 已提交
608 609 610
		let viewLineNumber = this.layoutProvider.getCenteredViewLineNumberInViewport();
		let viewModel = this._context.model;
		let currentCenteredViewRange = new Range(viewLineNumber, 1, viewLineNumber, viewModel.getLineMaxColumn(viewLineNumber));
E
Erich Gamma 已提交
611 612 613
		return viewModel.convertViewRangeToModelRange(currentCenteredViewRange);
	}

614
	public getCompletelyVisibleLinesRangeInViewport(): Range {
615
		if (this._isDisposed) {
616
			throw new Error('ViewImpl.getVisibleRangeInViewportExcludingPartialRenderedLines: View is disposed');
617
		}
618 619
		let completelyVisibleLinesRange = this.layoutProvider.getLinesViewportData().completelyVisibleLinesRange;
		return this._context.model.convertViewRangeToModelRange(completelyVisibleLinesRange);
620 621
	}

J
Johannes Rieken 已提交
622 623 624
	//	public getLineInfoProvider():view.ILineInfoProvider {
	//		return this.viewLines;
	//	}
E
Erich Gamma 已提交
625

A
Alex Dima 已提交
626
	public getInternalEventBus(): IEventEmitter {
E
Erich Gamma 已提交
627 628 629 630 631 632
		if (this._isDisposed) {
			throw new Error('ViewImpl.getInternalEventBus: View is disposed');
		}
		return this.outgoingEventBus;
	}

A
Alex Dima 已提交
633
	public saveState(): editorCommon.IViewState {
E
Erich Gamma 已提交
634 635 636 637 638 639
		if (this._isDisposed) {
			throw new Error('ViewImpl.saveState: View is disposed');
		}
		return this.layoutProvider.saveState();
	}

A
Alex Dima 已提交
640
	public restoreState(state: editorCommon.IViewState): void {
E
Erich Gamma 已提交
641 642 643 644 645 646 647 648 649 650 651
		if (this._isDisposed) {
			throw new Error('ViewImpl.restoreState: View is disposed');
		}
		this._flushAnyAccumulatedEvents();
		return this.layoutProvider.restoreState(state);
	}

	public focus(): void {
		if (this._isDisposed) {
			throw new Error('ViewImpl.focus: View is disposed');
		}
652
		this.keyboardHandler.focusTextArea();
E
Erich Gamma 已提交
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669

		// IE does not trigger the focus event immediately, so we must help it a little bit
		this._setHasFocus(true);
	}

	public isFocused(): boolean {
		if (this._isDisposed) {
			throw new Error('ViewImpl.isFocused: View is disposed');
		}
		return this.hasFocus;
	}

	public createOverviewRuler(cssClassName: string, minimumHeight: number, maximumHeight: number): OverviewRuler {
		if (this._isDisposed) {
			throw new Error('ViewImpl.createOverviewRuler: View is disposed');
		}
		return new OverviewRuler(
J
Johannes Rieken 已提交
670 671
			this._context, cssClassName, this.layoutProvider.getScrollHeight(), minimumHeight, maximumHeight,
			(lineNumber: number) => this.layoutProvider.getVerticalOffsetForLineNumber(lineNumber)
E
Erich Gamma 已提交
672 673 674
		);
	}

A
Alex Dima 已提交
675
	public change(callback: (changeAccessor: editorBrowser.IViewZoneChangeAccessor) => any): boolean {
E
Erich Gamma 已提交
676 677 678
		if (this._isDisposed) {
			throw new Error('ViewImpl.change: View is disposed');
		}
A
Alex Dima 已提交
679
		let zonesHaveChanged = false;
E
Erich Gamma 已提交
680 681 682
		this._renderOnce(() => {
			// Handle events to avoid "adjusting" newly inserted view zones
			this._flushAnyAccumulatedEvents();
A
Alex Dima 已提交
683
			let changeAccessor: editorBrowser.IViewZoneChangeAccessor = {
J
Johannes Rieken 已提交
684
				addZone: (zone: editorBrowser.IViewZone): number => {
E
Erich Gamma 已提交
685 686 687
					zonesHaveChanged = true;
					return this.viewZones.addZone(zone);
				},
J
Johannes Rieken 已提交
688
				removeZone: (id: number): void => {
689 690 691
					if (!id) {
						return;
					}
E
Erich Gamma 已提交
692 693 694
					zonesHaveChanged = this.viewZones.removeZone(id) || zonesHaveChanged;
				},
				layoutZone: (id: number): void => {
695 696 697
					if (!id) {
						return;
					}
E
Erich Gamma 已提交
698 699 700 701
					zonesHaveChanged = this.viewZones.layoutZone(id) || zonesHaveChanged;
				}
			};

A
Alex Dima 已提交
702
			let r: any = safeInvoke1Arg(callback, changeAccessor);
E
Erich Gamma 已提交
703 704 705 706 707 708

			// Invalidate changeAccessor
			changeAccessor.addZone = null;
			changeAccessor.removeZone = null;

			if (zonesHaveChanged) {
709
				this._context.privateViewEventBus.emit(editorCommon.EventType.ViewZonesChanged, null);
E
Erich Gamma 已提交
710 711 712 713 714 715 716
			}

			return r;
		});
		return zonesHaveChanged;
	}

J
Johannes Rieken 已提交
717
	public getWhitespaces(): editorCommon.IEditorWhitespace[] {
E
Erich Gamma 已提交
718 719 720 721 722 723
		if (this._isDisposed) {
			throw new Error('ViewImpl.getWhitespaces: View is disposed');
		}
		return this.layoutProvider.getWhitespaces();
	}

A
Alex Dima 已提交
724
	public addContentWidget(widgetData: editorBrowser.IContentWidgetData): void {
E
Erich Gamma 已提交
725 726 727 728 729 730 731 732 733
		if (this._isDisposed) {
			throw new Error('ViewImpl.addContentWidget: View is disposed');
		}
		this._renderOnce(() => {
			this.contentWidgets.addWidget(widgetData.widget);
			this.layoutContentWidget(widgetData);
		});
	}

A
Alex Dima 已提交
734
	public layoutContentWidget(widgetData: editorBrowser.IContentWidgetData): void {
E
Erich Gamma 已提交
735 736 737
		if (this._isDisposed) {
			throw new Error('ViewImpl.layoutContentWidget: View is disposed');
		}
738

739 740 741 742 743
		this._renderOnce(() => {
			let newPosition = widgetData.position ? widgetData.position.position : null;
			let newPreference = widgetData.position ? widgetData.position.preference : null;
			this.contentWidgets.setWidgetPosition(widgetData.widget, newPosition, newPreference);
		});
E
Erich Gamma 已提交
744 745
	}

A
Alex Dima 已提交
746
	public removeContentWidget(widgetData: editorBrowser.IContentWidgetData): void {
E
Erich Gamma 已提交
747 748 749 750 751 752 753 754
		if (this._isDisposed) {
			throw new Error('ViewImpl.removeContentWidget: View is disposed');
		}
		this._renderOnce(() => {
			this.contentWidgets.removeWidget(widgetData.widget);
		});
	}

A
Alex Dima 已提交
755
	public addOverlayWidget(widgetData: editorBrowser.IOverlayWidgetData): void {
E
Erich Gamma 已提交
756 757 758 759 760 761 762 763 764
		if (this._isDisposed) {
			throw new Error('ViewImpl.addOverlayWidget: View is disposed');
		}
		this._renderOnce(() => {
			this.overlayWidgets.addWidget(widgetData.widget);
			this.layoutOverlayWidget(widgetData);
		});
	}

A
Alex Dima 已提交
765
	public layoutOverlayWidget(widgetData: editorBrowser.IOverlayWidgetData): void {
E
Erich Gamma 已提交
766 767 768
		if (this._isDisposed) {
			throw new Error('ViewImpl.layoutOverlayWidget: View is disposed');
		}
769 770 771 772 773 774

		let newPreference = widgetData.position ? widgetData.position.preference : null;
		let shouldRender = this.overlayWidgets.setWidgetPosition(widgetData.widget, newPreference);
		if (shouldRender) {
			this._scheduleRender();
		}
E
Erich Gamma 已提交
775 776
	}

A
Alex Dima 已提交
777
	public removeOverlayWidget(widgetData: editorBrowser.IOverlayWidgetData): void {
E
Erich Gamma 已提交
778 779 780 781 782 783 784 785
		if (this._isDisposed) {
			throw new Error('ViewImpl.removeOverlayWidget: View is disposed');
		}
		this._renderOnce(() => {
			this.overlayWidgets.removeWidget(widgetData.widget);
		});
	}

J
Johannes Rieken 已提交
786
	public render(now: boolean, everything: boolean): void {
E
Erich Gamma 已提交
787 788 789
		if (this._isDisposed) {
			throw new Error('ViewImpl.render: View is disposed');
		}
790 791 792 793
		if (everything) {
			// Force a render with a layout event
			this.layoutProvider.emitLayoutChangedEvent();
		}
794 795 796
		if (now) {
			this._flushAccumulatedAndRenderNow();
		}
E
Erich Gamma 已提交
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
	}

	public renderOnce(callback: () => any): any {
		if (this._isDisposed) {
			throw new Error('ViewImpl.renderOnce: View is disposed');
		}
		return this._renderOnce(callback);
	}

	// --- end Code Editor APIs

	private _renderOnce(callback: () => any): any {
		if (this._isDisposed) {
			throw new Error('ViewImpl._renderOnce: View is disposed');
		}
		return this.outgoingEventBus.deferredEmit(() => {
A
Alex Dima 已提交
813 814
			let r = safeInvokeNoArg(callback);
			this._scheduleRender();
E
Erich Gamma 已提交
815 816 817 818 819 820 821 822 823
			return r;
		});
	}

	private _scheduleRender(): void {
		if (this._isDisposed) {
			throw new Error('ViewImpl._scheduleRender: View is disposed');
		}
		if (this._renderAnimationFrame === null) {
A
Alex Dima 已提交
824
			this._renderAnimationFrame = dom.runAtThisOrScheduleAtNextAnimationFrame(this._onRenderScheduled.bind(this), 100);
E
Erich Gamma 已提交
825 826 827 828 829 830 831 832 833
		}
	}

	private _onRenderScheduled(): void {
		this._renderAnimationFrame = null;
		this._flushAccumulatedAndRenderNow();
	}

	private _renderNow(): void {
A
Alex Dima 已提交
834
		safeInvokeNoArg(() => this._actualRender());
E
Erich Gamma 已提交
835 836
	}

J
Johannes Rieken 已提交
837
	private createRenderingContext(linesViewportData: ViewLinesViewportData): IRenderingContext {
E
Erich Gamma 已提交
838

A
Alex Dima 已提交
839
		let vInfo = this.layoutProvider.getCurrentViewport();
E
Erich Gamma 已提交
840

A
Alex Dima 已提交
841
		let deltaTop = linesViewportData.visibleRangesDeltaTop;
E
Erich Gamma 已提交
842

A
Alex Dima 已提交
843
		let r: IRenderingContext = {
E
Erich Gamma 已提交
844 845 846 847 848 849 850 851 852 853 854 855
			linesViewportData: linesViewportData,
			scrollWidth: this.layoutProvider.getScrollWidth(),
			scrollHeight: this.layoutProvider.getScrollHeight(),

			visibleRange: linesViewportData.visibleRange,
			bigNumbersDelta: linesViewportData.bigNumbersDelta,

			viewportWidth: vInfo.width,
			viewportHeight: vInfo.height,
			viewportLeft: vInfo.left,
			viewportTop: vInfo.top,

J
Johannes Rieken 已提交
856
			getScrolledTopFromAbsoluteTop: (absoluteTop: number) => {
E
Erich Gamma 已提交
857 858 859
				return this.layoutProvider.getScrolledTopFromAbsoluteTop(absoluteTop);
			},

J
Johannes Rieken 已提交
860
			getViewportVerticalOffsetForLineNumber: (lineNumber: number) => {
A
Alex Dima 已提交
861 862
				let verticalOffset = this.layoutProvider.getVerticalOffsetForLineNumber(lineNumber);
				let scrolledTop = this.layoutProvider.getScrolledTopFromAbsoluteTop(verticalOffset);
E
Erich Gamma 已提交
863 864 865 866 867
				return scrolledTop;
			},

			getDecorationsInViewport: () => linesViewportData.getDecorationsInViewport(),

J
Johannes Rieken 已提交
868
			linesVisibleRangesForRange: (range: editorCommon.IRange, includeNewLines: boolean) => {
E
Erich Gamma 已提交
869 870 871
				return this.viewLines.linesVisibleRangesForRange(range, includeNewLines);
			},

J
Johannes Rieken 已提交
872
			visibleRangeForPosition: (position: editorCommon.IPosition) => {
A
Alex Dima 已提交
873
				let visibleRanges = this.viewLines.visibleRangesForRange2(new Range(position.lineNumber, position.column, position.lineNumber, position.column), deltaTop);
E
Erich Gamma 已提交
874 875 876 877 878 879
				if (!visibleRanges) {
					return null;
				}
				return visibleRanges[0];
			},

J
Johannes Rieken 已提交
880
			lineIsVisible: (lineNumber: number) => {
E
Erich Gamma 已提交
881 882 883 884 885 886
				return linesViewportData.visibleRange.startLineNumber <= lineNumber && lineNumber <= linesViewportData.visibleRange.endLineNumber;
			}
		};
		return r;
	}

887
	private _getViewPartsToRender(): ViewPart[] {
J
Johannes Rieken 已提交
888
		let result: ViewPart[] = [];
889 890 891 892 893 894 895 896 897
		for (let i = 0, len = this.viewParts.length; i < len; i++) {
			let viewPart = this.viewParts[i];
			if (viewPart.shouldRender()) {
				result.push(viewPart);
			}
		}
		return result;
	}

A
Alex Dima 已提交
898
	private _actualRender(): void {
A
Alex Dima 已提交
899
		if (!dom.isInDOM(this.domNode)) {
E
Erich Gamma 已提交
900 901
			return;
		}
A
Alex Dima 已提交
902
		let t = timer.start(timer.Topic.EDITOR, 'View.render');
E
Erich Gamma 已提交
903

904
		let viewPartsToRender = this._getViewPartsToRender();
E
Erich Gamma 已提交
905

A
Alex Dima 已提交
906 907
		if (!this.viewLines.shouldRender() && viewPartsToRender.length === 0) {
			// Nothing to render
908
			this.keyboardHandler.writeToTextArea();
A
Alex Dima 已提交
909 910 911
			t.stop();
			return;
		}
E
Erich Gamma 已提交
912

A
Alex Dima 已提交
913
		let linesViewportData = this.layoutProvider.getLinesViewportData();
E
Erich Gamma 已提交
914

A
Alex Dima 已提交
915
		if (this.viewLines.shouldRender()) {
916 917 918
			this.viewLines.renderText(linesViewportData, () => {
				this.keyboardHandler.writeToTextArea();
			});
A
Alex Dima 已提交
919
			this.viewLines.onDidRender();
920 921 922

			// Rendering of viewLines might cause scroll events to occur, so collect view parts to render again
			viewPartsToRender = this._getViewPartsToRender();
923 924
		} else {
			this.keyboardHandler.writeToTextArea();
A
Alex Dima 已提交
925
		}
E
Erich Gamma 已提交
926

A
Alex Dima 已提交
927
		let renderingContext = this.createRenderingContext(linesViewportData);
A
Alex Dima 已提交
928

A
Alex Dima 已提交
929 930 931 932 933
		// Render the rest of the parts
		for (let i = 0, len = viewPartsToRender.length; i < len; i++) {
			let viewPart = viewPartsToRender[i];
			viewPart.prepareRender(renderingContext);
		}
A
Alex Dima 已提交
934

A
Alex Dima 已提交
935 936 937 938
		for (let i = 0, len = viewPartsToRender.length; i < len; i++) {
			let viewPart = viewPartsToRender[i];
			viewPart.render(renderingContext);
			viewPart.onDidRender();
E
Erich Gamma 已提交
939 940
		}

941 942 943
		// Render the scrollbar
		this.layoutProvider.renderScrollbar();

E
Erich Gamma 已提交
944 945 946
		t.stop();
	}

J
Johannes Rieken 已提交
947
	private _setHasFocus(newHasFocus: boolean): void {
E
Erich Gamma 已提交
948 949
		if (this.hasFocus !== newHasFocus) {
			this.hasFocus = newHasFocus;
950
			this._context.privateViewEventBus.emit(editorCommon.EventType.ViewFocusChanged, this.hasFocus);
E
Erich Gamma 已提交
951 952 953 954
		}
	}
}

J
Johannes Rieken 已提交
955
function safeInvokeNoArg(func: Function): any {
A
Alex Dima 已提交
956 957
	try {
		return func();
J
Johannes Rieken 已提交
958
	} catch (e) {
A
Alex Dima 已提交
959 960 961 962
		onUnexpectedError(e);
	}
}

J
Johannes Rieken 已提交
963
function safeInvoke1Arg(func: Function, arg1: any): any {
A
Alex Dima 已提交
964 965
	try {
		return func(arg1);
J
Johannes Rieken 已提交
966
	} catch (e) {
A
Alex Dima 已提交
967 968 969
		onUnexpectedError(e);
	}
}