viewImpl.ts 35.9 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
import { onUnexpectedError } from 'vs/base/common/errors';
A
Alex Dima 已提交
8
import { EmitterEvent, IEventEmitter } from 'vs/base/common/eventEmitter';
J
Johannes Rieken 已提交
9
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
A
Alex Dima 已提交
10 11
import * as browser from 'vs/base/browser/browser';
import * as dom from 'vs/base/browser/dom';
J
Johannes Rieken 已提交
12 13 14
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 已提交
15
import * as editorCommon from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
16 17 18 19
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 已提交
20
import * as editorBrowser from 'vs/editor/browser/editorBrowser';
J
Johannes Rieken 已提交
21 22 23 24 25 26
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';
27
import { CurrentLineMarginHighlightOverlay } from 'vs/editor/browser/viewParts/currentLineMarginHighlight/currentLineMarginHighlight';
J
Johannes Rieken 已提交
28 29 30 31 32
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';
33
import { Margin } from 'vs/editor/browser/viewParts/margin/margin';
J
Johannes Rieken 已提交
34
import { LinesDecorationsOverlay } from 'vs/editor/browser/viewParts/linesDecorations/linesDecorations';
35
import { MarginViewLineDecorationsOverlay } from 'vs/editor/browser/viewParts/marginDecorations/marginDecorations';
J
Johannes Rieken 已提交
36 37 38 39 40 41 42 43
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';
A
Alex Dima 已提交
44
import { ViewPart, PartFingerprint, PartFingerprints } from 'vs/editor/browser/view/viewPart';
J
Johannes Rieken 已提交
45 46
import { ViewContext, IViewEventHandler } from 'vs/editor/common/view/viewContext';
import { IViewModel } from 'vs/editor/common/viewModel/viewModel';
A
Alex Dima 已提交
47
import { RenderingContext } from 'vs/editor/common/view/renderingContext';
J
Johannes Rieken 已提交
48
import { IPointerHandlerHelper } from 'vs/editor/browser/controller/mouseHandler';
A
Alex Dima 已提交
49
import { ViewOutgoingEvents } from 'vs/editor/browser/view/viewOutgoingEvents';
50
import { ViewportData } from 'vs/editor/common/viewLayout/viewLinesViewportData';
A
Alex Dima 已提交
51
import { EditorScrollbar } from 'vs/editor/browser/viewParts/editorScrollbar/editorScrollbar';
A
Alex Dima 已提交
52
import { Minimap } from 'vs/editor/browser/viewParts/minimap/minimap';
E
Erich Gamma 已提交
53

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

J
Johannes Rieken 已提交
56
	private eventDispatcher: ViewEventDispatcher;
E
Erich Gamma 已提交
57

J
Johannes Rieken 已提交
58 59
	private listenersToRemove: IDisposable[];
	private listenersToDispose: IDisposable[];
E
Erich Gamma 已提交
60 61

	private layoutProvider: LayoutProvider;
62
	private _scrollbar: EditorScrollbar;
63
	public _context: ViewContext;
E
Erich Gamma 已提交
64 65 66 67 68 69 70 71

	// 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;
72
	private viewCursors: ViewCursors;
A
Alex Dima 已提交
73
	private viewParts: ViewPart[];
E
Erich Gamma 已提交
74 75 76 77

	private keyboardHandler: KeyboardHandler;
	private pointerHandler: PointerHandler;

A
Alex Dima 已提交
78
	private outgoingEvents: ViewOutgoingEvents;
E
Erich Gamma 已提交
79 80 81 82 83 84 85 86 87 88

	// 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 已提交
89
	private hasFocus: boolean;
E
Erich Gamma 已提交
90 91
	private _isDisposed: boolean;

J
Johannes Rieken 已提交
92
	private handleAccumulatedModelEventsTimeout: number;
A
Alex Dima 已提交
93
	private accumulatedModelEvents: EmitterEvent[];
A
Alex Dima 已提交
94
	private _renderAnimationFrame: IDisposable;
E
Erich Gamma 已提交
95

A
Alex Dima 已提交
96
	constructor(
97
		commandService: ICommandService,
J
Johannes Rieken 已提交
98 99 100
		configuration: Configuration,
		model: IViewModel,
		private triggerCursorHandler: TriggerCursorHandler
A
Alex Dima 已提交
101
	) {
E
Erich Gamma 已提交
102 103 104
		super();
		this._isDisposed = false;
		this._renderAnimationFrame = null;
A
Alex Dima 已提交
105
		this.outgoingEvents = new ViewOutgoingEvents(model);
E
Erich Gamma 已提交
106

A
Alex Dima 已提交
107
		let viewController = new ViewController(model, triggerCursorHandler, this.outgoingEvents, commandService);
E
Erich Gamma 已提交
108 109 110 111 112

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

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

		// 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 已提交
117
		this.linesContent.className = editorBrowser.ClassNames.LINES_CONTENT + ' monaco-editor-background';
A
Alex Dima 已提交
118
		this.linesContent.style.position = 'absolute';
E
Erich Gamma 已提交
119
		this.domNode = document.createElement('div');
120
		this.domNode.className = configuration.editor.viewInfo.editorClassName;
E
Erich Gamma 已提交
121 122

		this.overflowGuardContainer = document.createElement('div');
A
Alex Dima 已提交
123
		PartFingerprints.write(this.overflowGuardContainer, PartFingerprint.OverflowGuard);
A
Alex Dima 已提交
124
		this.overflowGuardContainer.className = editorBrowser.ClassNames.OVERFLOW_GUARD;
E
Erich Gamma 已提交
125 126 127 128 129

		// 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.
A
Alex Dima 已提交
130
		this.layoutProvider = new LayoutProvider(configuration, model.getLineCount(), this.eventDispatcher);
E
Erich Gamma 已提交
131

A
Alex Dima 已提交
132
		this._scrollbar = new EditorScrollbar(this.layoutProvider.getScrollable(), configuration, this.linesContent, this.domNode, this.overflowGuardContainer);
133

E
Erich Gamma 已提交
134
		// The view context is passed on to most classes (basically to reduce param. counts in ctors)
135
		this._context = new ViewContext(
J
Johannes Rieken 已提交
136 137 138
			configuration, model, this.eventDispatcher,
			(eventHandler: IViewEventHandler) => this.eventDispatcher.addEventHandler(eventHandler),
			(eventHandler: IViewEventHandler) => this.eventDispatcher.removeEventHandler(eventHandler)
E
Erich Gamma 已提交
139 140
		);

141
		this.createTextArea();
E
Erich Gamma 已提交
142 143 144
		this.createViewParts();

		// Keyboard handler
145
		this.keyboardHandler = new KeyboardHandler(this._context, viewController, this.createKeyboardHandlerHelper());
E
Erich Gamma 已提交
146 147

		// Pointer handler
148
		this.pointerHandler = new PointerHandler(this._context, viewController, this.createPointerHandlerHelper());
E
Erich Gamma 已提交
149 150 151 152 153 154 155 156 157 158

		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 已提交
159
		this.listenersToRemove.push(model.addBulkListener2((events: EmitterEvent[]) => {
E
Erich Gamma 已提交
160 161 162 163 164 165 166 167 168 169 170
			this.accumulatedModelEvents = this.accumulatedModelEvents.concat(events);
			if (this.handleAccumulatedModelEventsTimeout === -1) {
				this.handleAccumulatedModelEventsTimeout = setTimeout(() => {
					this.handleAccumulatedModelEventsTimeout = -1;
					this._flushAnyAccumulatedEvents();
				});
			}
		}));
	}

	private _flushAnyAccumulatedEvents(): void {
A
Alex Dima 已提交
171
		let toEmit = this.accumulatedModelEvents;
E
Erich Gamma 已提交
172 173 174 175 176 177
		this.accumulatedModelEvents = [];
		if (toEmit.length > 0) {
			this.eventDispatcher.emitMany(toEmit);
		}
	}

178
	private createTextArea(): void {
E
Erich Gamma 已提交
179 180
		// Text Area (The focus will always be in the textarea when the cursor is blinking)
		this.textArea = <HTMLTextAreaElement>document.createElement('textarea');
A
Alex Dima 已提交
181
		PartFingerprints.write(this.textArea, PartFingerprint.TextArea);
A
Alex Dima 已提交
182
		this.textArea.className = editorBrowser.ClassNames.TEXTAREA;
E
Erich Gamma 已提交
183 184 185 186
		this.textArea.setAttribute('wrap', 'off');
		this.textArea.setAttribute('autocorrect', 'off');
		this.textArea.setAttribute('autocapitalize', 'off');
		this.textArea.setAttribute('spellcheck', 'false');
187
		this.textArea.setAttribute('aria-label', this._context.configuration.editor.viewInfo.ariaLabel);
E
Erich Gamma 已提交
188 189
		this.textArea.setAttribute('role', 'textbox');
		this.textArea.setAttribute('aria-multiline', 'true');
A
Alex Dima 已提交
190 191 192
		this.textArea.setAttribute('aria-haspopup', 'false');
		this.textArea.setAttribute('aria-autocomplete', 'both');

A
Alex Dima 已提交
193 194
		StyleMutator.setTop(this.textArea, 0);
		StyleMutator.setLeft(this.textArea, 0);
E
Erich Gamma 已提交
195

A
Alex Dima 已提交
196 197
		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 已提交
198 199 200 201 202

		// 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');
203
		if (this._context.configuration.editor.viewInfo.glyphMargin) {
A
Alex Dima 已提交
204
			this.textAreaCover.className = 'monaco-editor-background ' + editorBrowser.ClassNames.GLYPH_MARGIN + ' ' + editorBrowser.ClassNames.TEXTAREA_COVER;
E
Erich Gamma 已提交
205
		} else {
206
			if (this._context.configuration.editor.viewInfo.renderLineNumbers) {
A
Alex Dima 已提交
207
				this.textAreaCover.className = 'monaco-editor-background ' + editorBrowser.ClassNames.LINE_NUMBERS + ' ' + editorBrowser.ClassNames.TEXTAREA_COVER;
E
Erich Gamma 已提交
208
			} else {
A
Alex Dima 已提交
209
				this.textAreaCover.className = 'monaco-editor-background ' + editorBrowser.ClassNames.TEXTAREA_COVER;
E
Erich Gamma 已提交
210 211 212
			}
		}
		this.textAreaCover.style.position = 'absolute';
A
Alex Dima 已提交
213 214 215 216
		StyleMutator.setWidth(this.textAreaCover, 1);
		StyleMutator.setHeight(this.textAreaCover, 1);
		StyleMutator.setTop(this.textAreaCover, 0);
		StyleMutator.setLeft(this.textAreaCover, 0);
E
Erich Gamma 已提交
217 218 219 220 221 222
	}

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

		// View Lines
223
		this.viewLines = new ViewLines(this._context, this.layoutProvider);
E
Erich Gamma 已提交
224 225

		// View Zones
226
		this.viewZones = new ViewZones(this._context, this.layoutProvider);
E
Erich Gamma 已提交
227 228 229
		this.viewParts.push(this.viewZones);

		// Decorations overview ruler
A
Alex Dima 已提交
230
		let decorationsOverviewRuler = new DecorationsOverviewRuler(
J
Johannes Rieken 已提交
231 232
			this._context, this.layoutProvider.getScrollHeight(),
			(lineNumber: number) => this.layoutProvider.getVerticalOffsetForLineNumber(lineNumber)
E
Erich Gamma 已提交
233 234 235 236
		);
		this.viewParts.push(decorationsOverviewRuler);


A
Alex Dima 已提交
237
		let scrollDecoration = new ScrollDecorationViewPart(this._context);
E
Erich Gamma 已提交
238 239
		this.viewParts.push(scrollDecoration);

A
Alex Dima 已提交
240
		let contentViewOverlays = new ContentViewOverlays(this._context);
E
Erich Gamma 已提交
241
		this.viewParts.push(contentViewOverlays);
A
Alex Dima 已提交
242
		contentViewOverlays.addDynamicOverlay(new CurrentLineHighlightOverlay(this._context));
243 244
		contentViewOverlays.addDynamicOverlay(new SelectionsOverlay(this._context));
		contentViewOverlays.addDynamicOverlay(new DecorationsOverlay(this._context));
245
		contentViewOverlays.addDynamicOverlay(new IndentGuidesOverlay(this._context));
E
Erich Gamma 已提交
246

A
Alex Dima 已提交
247
		let marginViewOverlays = new MarginViewOverlays(this._context);
E
Erich Gamma 已提交
248
		this.viewParts.push(marginViewOverlays);
A
Alex Dima 已提交
249
		marginViewOverlays.addDynamicOverlay(new CurrentLineMarginHighlightOverlay(this._context));
250
		marginViewOverlays.addDynamicOverlay(new GlyphMarginOverlay(this._context));
251
		marginViewOverlays.addDynamicOverlay(new MarginViewLineDecorationsOverlay(this._context));
252 253
		marginViewOverlays.addDynamicOverlay(new LinesDecorationsOverlay(this._context));
		marginViewOverlays.addDynamicOverlay(new LineNumbersOverlay(this._context));
E
Erich Gamma 已提交
254

A
Alex Dima 已提交
255
		let margin = new Margin(this._context);
256 257 258
		margin.domNode.appendChild(this.viewZones.marginDomNode);
		margin.domNode.appendChild(marginViewOverlays.getDomNode());
		this.viewParts.push(margin);
E
Erich Gamma 已提交
259 260

		// Content widgets
261
		this.contentWidgets = new ViewContentWidgets(this._context, this.domNode);
E
Erich Gamma 已提交
262 263
		this.viewParts.push(this.contentWidgets);

264 265
		this.viewCursors = new ViewCursors(this._context);
		this.viewParts.push(this.viewCursors);
E
Erich Gamma 已提交
266 267

		// Overlay widgets
268
		this.overlayWidgets = new ViewOverlayWidgets(this._context);
E
Erich Gamma 已提交
269 270
		this.viewParts.push(this.overlayWidgets);

A
Alex Dima 已提交
271
		let rulers = new Rulers(this._context);
272 273
		this.viewParts.push(rulers);

274
		let minimap = new Minimap(this._context, this.layoutProvider);
A
Alex Dima 已提交
275 276
		this.viewParts.push(minimap);

E
Erich Gamma 已提交
277 278
		// -------------- Wire dom nodes up

279
		this.linesContentContainer = this._scrollbar.getScrollbarContainerDomNode();
E
Erich Gamma 已提交
280 281 282
		this.linesContentContainer.style.position = 'absolute';

		if (decorationsOverviewRuler) {
283
			let overviewRulerData = this._scrollbar.getOverviewRulerLayoutInfo();
E
Erich Gamma 已提交
284 285 286 287
			overviewRulerData.parent.insertBefore(decorationsOverviewRuler.getDomNode(), overviewRulerData.insertBefore);
		}

		this.linesContent.appendChild(contentViewOverlays.getDomNode());
288
		this.linesContent.appendChild(rulers.domNode);
E
Erich Gamma 已提交
289
		this.linesContent.appendChild(this.viewZones.domNode);
A
Alex Dima 已提交
290
		this.linesContent.appendChild(this.viewLines.getDomNode());
E
Erich Gamma 已提交
291
		this.linesContent.appendChild(this.contentWidgets.domNode);
292
		this.linesContent.appendChild(this.viewCursors.getDomNode());
293
		this.overflowGuardContainer.appendChild(margin.domNode);
E
Erich Gamma 已提交
294 295 296 297 298
		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);
299
		this.overflowGuardContainer.appendChild(minimap.getDomNode());
E
Erich Gamma 已提交
300
		this.domNode.appendChild(this.overflowGuardContainer);
301
		this.domNode.appendChild(this.contentWidgets.overflowingContentWidgetsDomNode);
E
Erich Gamma 已提交
302 303 304 305 306 307 308
	}

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

309
	private createPointerHandlerHelper(): IPointerHandlerHelper {
E
Erich Gamma 已提交
310 311 312 313 314 315 316 317 318 319 320
		return {
			viewDomNode: this.domNode,
			linesContentDomNode: this.linesContent,

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

321 322 323 324
			isDirty: (): boolean => {
				return (this.accumulatedModelEvents.length > 0);
			},

E
Erich Gamma 已提交
325 326 327 328 329 330
			getScrollLeft: () => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.getScrollLeft: View is disposed');
				}
				return this.layoutProvider.getScrollLeft();
			},
331 332 333 334 335 336 337
			getScrollTop: () => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.getScrollTop: View is disposed');
				}
				return this.layoutProvider.getScrollTop();
			},

J
Johannes Rieken 已提交
338
			setScrollPosition: (position: editorCommon.INewScrollPosition) => {
E
Erich Gamma 已提交
339
				if (this._isDisposed) {
340
					throw new Error('ViewImpl.pointerHandler.setScrollPosition: View is disposed');
E
Erich Gamma 已提交
341
				}
342
				this.layoutProvider.setScrollPosition(position);
E
Erich Gamma 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
			},

			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);
			},
369 370 371 372 373 374
			getLastViewCursorsRenderData: () => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.getLastViewCursorsRenderData: View is disposed');
				}
				return this.viewCursors.getLastRenderData() || [];
			},
E
Erich Gamma 已提交
375 376 377 378 379 380
			shouldSuppressMouseDownOnViewZone: (viewZoneId: number) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.shouldSuppressMouseDownOnViewZone: View is disposed');
				}
				return this.viewZones.shouldSuppressMouseDownOnViewZone(viewZoneId);
			},
381 382 383 384 385 386
			shouldSuppressMouseDownOnWidget: (widgetId: string) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.shouldSuppressMouseDownOnWidget: View is disposed');
				}
				return this.contentWidgets.shouldSuppressMouseDownOnWidget(widgetId);
			},
E
Erich Gamma 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399
			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 已提交
400
				let visibleRanges = this.viewLines.visibleRangesForRange2(new Range(lineNumber, column, lineNumber, column), 0);
E
Erich Gamma 已提交
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
				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);
			}
		};
	}

417
	private createKeyboardHandlerHelper(): IKeyboardHandlerHelper {
E
Erich Gamma 已提交
418 419 420 421 422 423 424 425
		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 已提交
426 427
				let linesViewPortData = this.layoutProvider.getLinesViewportData();
				let visibleRanges = this.viewLines.visibleRangesForRange2(new Range(lineNumber, column, lineNumber, column), linesViewPortData.visibleRangesDeltaTop);
E
Erich Gamma 已提交
428 429 430 431
				if (!visibleRanges) {
					return null;
				}
				return visibleRanges[0];
432 433 434
			},
			flushAnyAccumulatedEvents: () => {
				this._flushAnyAccumulatedEvents();
E
Erich Gamma 已提交
435 436 437 438
			}
		};
	}

J
Johannes Rieken 已提交
439
	public setAriaActiveDescendant(id: string): void {
A
Alex Dima 已提交
440 441 442 443 444 445 446 447 448 449 450 451 452
		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 已提交
453 454
	// --- begin event handlers

A
Alex Dima 已提交
455
	public onModelFlushed(): boolean {
A
Alex Dima 已提交
456
		this.layoutProvider.onModelFlushed(this._context.model.getLineCount());
A
Alex Dima 已提交
457 458 459 460 461 462 463 464 465 466
		return false;
	}
	public onModelLinesDeleted(e: editorCommon.IViewLinesDeletedEvent): boolean {
		this.layoutProvider.onModelLinesDeleted(e);
		return false;
	}
	public onModelLinesInserted(e: editorCommon.IViewLinesInsertedEvent): boolean {
		this.layoutProvider.onModelLinesInserted(e);
		return false;
	}
J
Johannes Rieken 已提交
467
	public onLayoutChanged(layoutInfo: editorCommon.EditorLayoutInfo): boolean {
A
Alex Dima 已提交
468
		if (browser.isChrome) {
A
tslint  
Alex Dima 已提交
469
			/* tslint:disable:no-unused-variable */
E
Erich Gamma 已提交
470 471
			// Access overflowGuardContainer.clientWidth to prevent relayouting bug in Chrome
			// See Bug 19676: Editor misses a layout event
A
Alex Dima 已提交
472
			let clientWidth = this.overflowGuardContainer.clientWidth + 'px';
A
tslint  
Alex Dima 已提交
473
			/* tslint:enable:no-unused-variable */
E
Erich Gamma 已提交
474
		}
A
Alex Dima 已提交
475 476
		StyleMutator.setWidth(this.domNode, layoutInfo.width);
		StyleMutator.setHeight(this.domNode, layoutInfo.height);
E
Erich Gamma 已提交
477

A
Alex Dima 已提交
478 479
		StyleMutator.setWidth(this.overflowGuardContainer, layoutInfo.width);
		StyleMutator.setHeight(this.overflowGuardContainer, layoutInfo.height);
E
Erich Gamma 已提交
480

A
Alex Dima 已提交
481 482
		StyleMutator.setWidth(this.linesContent, 1000000);
		StyleMutator.setHeight(this.linesContent, 1000000);
E
Erich Gamma 已提交
483

A
Alex Dima 已提交
484 485 486
		StyleMutator.setLeft(this.linesContentContainer, layoutInfo.contentLeft);
		StyleMutator.setWidth(this.linesContentContainer, layoutInfo.contentWidth);
		StyleMutator.setHeight(this.linesContentContainer, layoutInfo.contentHeight);
E
Erich Gamma 已提交
487

A
Alex Dima 已提交
488
		this.outgoingEvents.emitViewLayoutChanged(layoutInfo);
E
Erich Gamma 已提交
489 490
		return false;
	}
A
Alex Dima 已提交
491
	public onConfigurationChanged(e: editorCommon.IConfigurationChangedEvent): boolean {
492 493
		if (e.viewInfo.editorClassName) {
			this.domNode.className = this._context.configuration.editor.viewInfo.editorClassName;
E
Erich Gamma 已提交
494
		}
495 496
		if (e.viewInfo.ariaLabel) {
			this.textArea.setAttribute('aria-label', this._context.configuration.editor.viewInfo.ariaLabel);
497
		}
A
Alex Dima 已提交
498
		this.layoutProvider.onConfigurationChanged(e);
E
Erich Gamma 已提交
499 500
		return false;
	}
J
Johannes Rieken 已提交
501
	public onScrollChanged(e: editorCommon.IScrollEvent): boolean {
A
Alex Dima 已提交
502
		this.outgoingEvents.emitScrollChanged(e);
503
		return false;
E
Erich Gamma 已提交
504
	}
J
Johannes Rieken 已提交
505
	public onViewFocusChanged(isFocused: boolean): boolean {
A
Alex Dima 已提交
506
		dom.toggleClass(this.domNode, 'focused', isFocused);
E
Erich Gamma 已提交
507
		if (isFocused) {
A
Alex Dima 已提交
508
			this.outgoingEvents.emitViewFocusGained();
E
Erich Gamma 已提交
509
		} else {
A
Alex Dima 已提交
510
			this.outgoingEvents.emitViewFocusLost();
E
Erich Gamma 已提交
511 512 513
		}
		return false;
	}
514 515 516 517 518 519 520 521 522 523 524 525 526

	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 已提交
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
	// --- 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);
A
Alex Dima 已提交
542
		this.outgoingEvents.dispose();
A
Alex Dima 已提交
543
		this.listenersToRemove = dispose(this.listenersToRemove);
J
Joao Moreno 已提交
544
		this.listenersToDispose = dispose(this.listenersToDispose);
E
Erich Gamma 已提交
545 546 547 548 549 550 551

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

		this.viewLines.dispose();

		// Destroy IViewPart second
A
Alex Dima 已提交
552
		for (let i = 0, len = this.viewParts.length; i < len; i++) {
E
Erich Gamma 已提交
553 554 555 556
			this.viewParts[i].dispose();
		}
		this.viewParts = [];

557
		this._scrollbar.dispose();
E
Erich Gamma 已提交
558 559 560 561 562
		this.layoutProvider.dispose();
	}

	// --- begin Code Editor APIs

J
Johannes Rieken 已提交
563
	private codeEditorHelper: editorBrowser.ICodeEditorHelper;
A
Alex Dima 已提交
564
	public getCodeEditorHelper(): editorBrowser.ICodeEditorHelper {
E
Erich Gamma 已提交
565 566
		if (!this.codeEditorHelper) {
			this.codeEditorHelper = {
567
				getScrollWidth: () => {
E
Erich Gamma 已提交
568
					if (this._isDisposed) {
569
						throw new Error('ViewImpl.codeEditorHelper.getScrollWidth: View is disposed');
E
Erich Gamma 已提交
570
					}
571
					return this.layoutProvider.getScrollWidth();
E
Erich Gamma 已提交
572 573 574 575 576 577 578
				},
				getScrollLeft: () => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.getScrollLeft: View is disposed');
					}
					return this.layoutProvider.getScrollLeft();
				},
579

E
Erich Gamma 已提交
580 581 582 583 584 585
				getScrollHeight: () => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.getScrollHeight: View is disposed');
					}
					return this.layoutProvider.getScrollHeight();
				},
586
				getScrollTop: () => {
E
Erich Gamma 已提交
587
					if (this._isDisposed) {
588
						throw new Error('ViewImpl.codeEditorHelper.getScrollTop: View is disposed');
E
Erich Gamma 已提交
589
					}
590
					return this.layoutProvider.getScrollTop();
E
Erich Gamma 已提交
591
				},
592

J
Johannes Rieken 已提交
593
				setScrollPosition: (position: editorCommon.INewScrollPosition) => {
594 595 596 597 598 599
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.setScrollPosition: View is disposed');
					}
					this.layoutProvider.setScrollPosition(position);
				},

J
Johannes Rieken 已提交
600
				getVerticalOffsetForPosition: (modelLineNumber: number, modelColumn: number) => {
E
Erich Gamma 已提交
601 602 603
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.getVerticalOffsetForPosition: View is disposed');
					}
A
Alex Dima 已提交
604
					let modelPosition = this._context.model.validateModelPosition({
E
Erich Gamma 已提交
605 606 607
						lineNumber: modelLineNumber,
						column: modelColumn
					});
A
Alex Dima 已提交
608
					let viewPosition = this._context.model.coordinatesConverter.convertModelPositionToViewPosition(modelPosition);
E
Erich Gamma 已提交
609 610 611 612 613 614
					return this.layoutProvider.getVerticalOffsetForLineNumber(viewPosition.lineNumber);
				},
				delegateVerticalScrollbarMouseDown: (browserEvent: MouseEvent) => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.delegateVerticalScrollbarMouseDown: View is disposed');
					}
615
					this._scrollbar.delegateVerticalScrollbarMouseDown(browserEvent);
E
Erich Gamma 已提交
616 617 618 619 620
				},
				getOffsetForColumn: (modelLineNumber: number, modelColumn: number) => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.getOffsetForColumn: View is disposed');
					}
A
Alex Dima 已提交
621
					let modelPosition = this._context.model.validateModelPosition({
E
Erich Gamma 已提交
622 623 624
						lineNumber: modelLineNumber,
						column: modelColumn
					});
A
Alex Dima 已提交
625
					let viewPosition = this._context.model.coordinatesConverter.convertModelPositionToViewPosition(modelPosition);
E
Erich Gamma 已提交
626
					this._flushAccumulatedAndRenderNow();
A
Alex Dima 已提交
627
					let visibleRanges = this.viewLines.visibleRangesForRange2(new Range(viewPosition.lineNumber, viewPosition.column, viewPosition.lineNumber, viewPosition.column), 0);
E
Erich Gamma 已提交
628 629 630 631
					if (!visibleRanges) {
						return -1;
					}
					return visibleRanges[0].left;
632 633 634 635 636 637 638
				},

				getTargetAtClientPoint: (clientX: number, clientY: number): editorBrowser.IMouseTarget => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.getTargetAtClientPoint: View is disposed');
					}
					return this.pointerHandler.getTargetAtClientPoint(clientX, clientY);
E
Erich Gamma 已提交
639
				}
640

E
Erich Gamma 已提交
641 642 643 644 645
			};
		}
		return this.codeEditorHelper;
	}

646
	public getCompletelyVisibleLinesRangeInViewport(): Range {
647
		if (this._isDisposed) {
648
			throw new Error('ViewImpl.getCompletelyVisibleLinesRangeInViewport: View is disposed');
649
		}
650 651 652 653 654 655 656 657 658 659 660

		let partialData = this.layoutProvider.getLinesViewportData();
		let startLineNumber = partialData.startLineNumber === partialData.endLineNumber || partialData.relativeVerticalOffset[0] >= partialData.viewportTop ? partialData.startLineNumber : partialData.startLineNumber + 1;
		let endLineNumber = partialData.relativeVerticalOffset[partialData.relativeVerticalOffset.length - 1] + this._context.configuration.editor.lineHeight <= partialData.viewportTop + partialData.viewportHeight ? partialData.endLineNumber : partialData.endLineNumber - 1;
		let completelyVisibleLinesRange = new Range(
			startLineNumber,
			1,
			endLineNumber,
			this._context.model.getLineMaxColumn(endLineNumber)
		);

A
Alex Dima 已提交
661
		return this._context.model.coordinatesConverter.convertViewRangeToModelRange(completelyVisibleLinesRange);
662 663
	}

A
Alex Dima 已提交
664
	public getInternalEventBus(): IEventEmitter {
E
Erich Gamma 已提交
665 666 667
		if (this._isDisposed) {
			throw new Error('ViewImpl.getInternalEventBus: View is disposed');
		}
A
Alex Dima 已提交
668
		return this.outgoingEvents.getInternalEventBus();
E
Erich Gamma 已提交
669 670
	}

A
Alex Dima 已提交
671
	public saveState(): editorCommon.IViewState {
E
Erich Gamma 已提交
672 673 674 675 676 677
		if (this._isDisposed) {
			throw new Error('ViewImpl.saveState: View is disposed');
		}
		return this.layoutProvider.saveState();
	}

A
Alex Dima 已提交
678
	public restoreState(state: editorCommon.IViewState): void {
E
Erich Gamma 已提交
679 680 681 682 683 684 685 686 687 688 689
		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');
		}
690
		this.keyboardHandler.focusTextArea();
E
Erich Gamma 已提交
691 692

		// IE does not trigger the focus event immediately, so we must help it a little bit
693 694 695
		if (document.activeElement === this.textArea) {
			this._setHasFocus(true);
		}
E
Erich Gamma 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708 709
	}

	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 已提交
710 711
			this._context, cssClassName, this.layoutProvider.getScrollHeight(), minimumHeight, maximumHeight,
			(lineNumber: number) => this.layoutProvider.getVerticalOffsetForLineNumber(lineNumber)
E
Erich Gamma 已提交
712 713 714
		);
	}

A
Alex Dima 已提交
715
	public change(callback: (changeAccessor: editorBrowser.IViewZoneChangeAccessor) => any): boolean {
E
Erich Gamma 已提交
716 717 718
		if (this._isDisposed) {
			throw new Error('ViewImpl.change: View is disposed');
		}
A
Alex Dima 已提交
719
		let zonesHaveChanged = false;
A
Alex Dima 已提交
720

E
Erich Gamma 已提交
721 722 723
		this._renderOnce(() => {
			// Handle events to avoid "adjusting" newly inserted view zones
			this._flushAnyAccumulatedEvents();
A
Alex Dima 已提交
724
			let changeAccessor: editorBrowser.IViewZoneChangeAccessor = {
J
Johannes Rieken 已提交
725
				addZone: (zone: editorBrowser.IViewZone): number => {
E
Erich Gamma 已提交
726 727 728
					zonesHaveChanged = true;
					return this.viewZones.addZone(zone);
				},
J
Johannes Rieken 已提交
729
				removeZone: (id: number): void => {
730 731 732
					if (!id) {
						return;
					}
E
Erich Gamma 已提交
733 734 735
					zonesHaveChanged = this.viewZones.removeZone(id) || zonesHaveChanged;
				},
				layoutZone: (id: number): void => {
736 737 738
					if (!id) {
						return;
					}
E
Erich Gamma 已提交
739 740 741 742
					zonesHaveChanged = this.viewZones.layoutZone(id) || zonesHaveChanged;
				}
			};

A
Alex Dima 已提交
743
			safeInvoke1Arg(callback, changeAccessor);
E
Erich Gamma 已提交
744 745 746 747 748 749

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

			if (zonesHaveChanged) {
A
Alex Dima 已提交
750
				this.layoutProvider.onHeightMaybeChanged();
751
				this._context.privateViewEventBus.emit(editorCommon.EventType.ViewZonesChanged, null);
E
Erich Gamma 已提交
752 753 754 755 756
			}
		});
		return zonesHaveChanged;
	}

J
Johannes Rieken 已提交
757
	public getWhitespaces(): editorCommon.IEditorWhitespace[] {
E
Erich Gamma 已提交
758 759 760 761 762 763
		if (this._isDisposed) {
			throw new Error('ViewImpl.getWhitespaces: View is disposed');
		}
		return this.layoutProvider.getWhitespaces();
	}

A
Alex Dima 已提交
764
	public addContentWidget(widgetData: editorBrowser.IContentWidgetData): void {
E
Erich Gamma 已提交
765 766 767
		if (this._isDisposed) {
			throw new Error('ViewImpl.addContentWidget: View is disposed');
		}
A
Alex Dima 已提交
768 769 770
		this.contentWidgets.addWidget(widgetData.widget);
		this.layoutContentWidget(widgetData);
		this._scheduleRender();
E
Erich Gamma 已提交
771 772
	}

A
Alex Dima 已提交
773
	public layoutContentWidget(widgetData: editorBrowser.IContentWidgetData): void {
E
Erich Gamma 已提交
774 775 776
		if (this._isDisposed) {
			throw new Error('ViewImpl.layoutContentWidget: View is disposed');
		}
777

A
Alex Dima 已提交
778 779 780 781
		let newPosition = widgetData.position ? widgetData.position.position : null;
		let newPreference = widgetData.position ? widgetData.position.preference : null;
		this.contentWidgets.setWidgetPosition(widgetData.widget, newPosition, newPreference);
		this._scheduleRender();
E
Erich Gamma 已提交
782 783
	}

A
Alex Dima 已提交
784
	public removeContentWidget(widgetData: editorBrowser.IContentWidgetData): void {
E
Erich Gamma 已提交
785 786 787
		if (this._isDisposed) {
			throw new Error('ViewImpl.removeContentWidget: View is disposed');
		}
A
Alex Dima 已提交
788 789
		this.contentWidgets.removeWidget(widgetData.widget);
		this._scheduleRender();
E
Erich Gamma 已提交
790 791
	}

A
Alex Dima 已提交
792
	public addOverlayWidget(widgetData: editorBrowser.IOverlayWidgetData): void {
E
Erich Gamma 已提交
793 794 795
		if (this._isDisposed) {
			throw new Error('ViewImpl.addOverlayWidget: View is disposed');
		}
A
Alex Dima 已提交
796 797 798
		this.overlayWidgets.addWidget(widgetData.widget);
		this.layoutOverlayWidget(widgetData);
		this._scheduleRender();
E
Erich Gamma 已提交
799 800
	}

A
Alex Dima 已提交
801
	public layoutOverlayWidget(widgetData: editorBrowser.IOverlayWidgetData): void {
E
Erich Gamma 已提交
802 803 804
		if (this._isDisposed) {
			throw new Error('ViewImpl.layoutOverlayWidget: View is disposed');
		}
805 806 807 808 809 810

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

A
Alex Dima 已提交
813
	public removeOverlayWidget(widgetData: editorBrowser.IOverlayWidgetData): void {
E
Erich Gamma 已提交
814 815 816
		if (this._isDisposed) {
			throw new Error('ViewImpl.removeOverlayWidget: View is disposed');
		}
A
Alex Dima 已提交
817 818
		this.overlayWidgets.removeWidget(widgetData.widget);
		this._scheduleRender();
E
Erich Gamma 已提交
819 820
	}

J
Johannes Rieken 已提交
821
	public render(now: boolean, everything: boolean): void {
E
Erich Gamma 已提交
822 823 824
		if (this._isDisposed) {
			throw new Error('ViewImpl.render: View is disposed');
		}
825 826 827 828
		if (everything) {
			// Force a render with a layout event
			this.layoutProvider.emitLayoutChangedEvent();
		}
829 830 831
		if (now) {
			this._flushAccumulatedAndRenderNow();
		}
E
Erich Gamma 已提交
832 833 834 835 836 837 838 839
	}

	// --- end Code Editor APIs

	private _renderOnce(callback: () => any): any {
		if (this._isDisposed) {
			throw new Error('ViewImpl._renderOnce: View is disposed');
		}
A
Alex Dima 已提交
840
		return this.outgoingEvents.deferredEmit(() => {
A
Alex Dima 已提交
841 842
			let r = safeInvokeNoArg(callback);
			this._scheduleRender();
E
Erich Gamma 已提交
843 844 845 846 847 848 849 850 851
			return r;
		});
	}

	private _scheduleRender(): void {
		if (this._isDisposed) {
			throw new Error('ViewImpl._scheduleRender: View is disposed');
		}
		if (this._renderAnimationFrame === null) {
A
Alex Dima 已提交
852
			this._renderAnimationFrame = dom.runAtThisOrScheduleAtNextAnimationFrame(this._onRenderScheduled.bind(this), 100);
E
Erich Gamma 已提交
853 854 855 856 857 858 859 860 861
		}
	}

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

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

865
	private _getViewPartsToRender(): ViewPart[] {
J
Johannes Rieken 已提交
866
		let result: ViewPart[] = [];
867 868 869 870 871 872 873 874 875
		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 已提交
876
	private _actualRender(): void {
A
Alex Dima 已提交
877
		if (!dom.isInDOM(this.domNode)) {
E
Erich Gamma 已提交
878 879 880
			return;
		}

881
		let viewPartsToRender = this._getViewPartsToRender();
E
Erich Gamma 已提交
882

A
Alex Dima 已提交
883 884
		if (!this.viewLines.shouldRender() && viewPartsToRender.length === 0) {
			// Nothing to render
885
			this.keyboardHandler.writeToTextArea();
A
Alex Dima 已提交
886 887
			return;
		}
E
Erich Gamma 已提交
888

889
		let partialViewportData = this.layoutProvider.getLinesViewportData();
A
Alex Dima 已提交
890
		this._context.model.setViewport(partialViewportData.startLineNumber, partialViewportData.endLineNumber, partialViewportData.centeredLineNumber);
891 892

		let viewportData = new ViewportData(partialViewportData, this._context.model);
E
Erich Gamma 已提交
893

A
Alex Dima 已提交
894
		if (this.viewLines.shouldRender()) {
895
			this.viewLines.renderText(viewportData, () => {
896 897
				this.keyboardHandler.writeToTextArea();
			});
A
Alex Dima 已提交
898
			this.viewLines.onDidRender();
899 900 901

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

906
		let renderingContext = new RenderingContext(this.viewLines, this.layoutProvider, viewportData);
A
Alex Dima 已提交
907

A
Alex Dima 已提交
908 909 910 911 912
		// 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 已提交
913

A
Alex Dima 已提交
914 915 916 917
		for (let i = 0, len = viewPartsToRender.length; i < len; i++) {
			let viewPart = viewPartsToRender[i];
			viewPart.render(renderingContext);
			viewPart.onDidRender();
E
Erich Gamma 已提交
918 919
		}

920
		// Render the scrollbar
921
		this._scrollbar.renderScrollbar();
E
Erich Gamma 已提交
922 923
	}

J
Johannes Rieken 已提交
924
	private _setHasFocus(newHasFocus: boolean): void {
E
Erich Gamma 已提交
925 926
		if (this.hasFocus !== newHasFocus) {
			this.hasFocus = newHasFocus;
927
			this._context.privateViewEventBus.emit(editorCommon.EventType.ViewFocusChanged, this.hasFocus);
E
Erich Gamma 已提交
928 929 930 931
		}
	}
}

J
Johannes Rieken 已提交
932
function safeInvokeNoArg(func: Function): any {
A
Alex Dima 已提交
933 934
	try {
		return func();
J
Johannes Rieken 已提交
935
	} catch (e) {
A
Alex Dima 已提交
936 937 938 939
		onUnexpectedError(e);
	}
}

J
Johannes Rieken 已提交
940
function safeInvoke1Arg(func: Function, arg1: any): any {
A
Alex Dima 已提交
941 942
	try {
		return func(arg1);
J
Johannes Rieken 已提交
943
	} catch (e) {
A
Alex Dima 已提交
944 945 946
		onUnexpectedError(e);
	}
}