viewImpl.ts 35.4 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';
51
import { EditorScrollbar } from 'vs/editor/browser/viewparts/editorScrollbar/editorScrollbar';
E
Erich Gamma 已提交
52

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

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

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

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

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

	private keyboardHandler: KeyboardHandler;
	private pointerHandler: PointerHandler;

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

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

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

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

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

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

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

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

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

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

132 133
		this._scrollbar = new EditorScrollbar(this.layoutProvider.getScrollable(), configuration, this.eventDispatcher, this.linesContent, this.domNode, this.overflowGuardContainer);

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, this.layoutProvider);
E
Erich Gamma 已提交
241
		this.viewParts.push(contentViewOverlays);
242 243 244
		contentViewOverlays.addDynamicOverlay(new CurrentLineHighlightOverlay(this._context, this.layoutProvider));
		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, this.layoutProvider);
E
Erich Gamma 已提交
248
		this.viewParts.push(marginViewOverlays);
249
		marginViewOverlays.addDynamicOverlay(new CurrentLineMarginHighlightOverlay(this._context, this.layoutProvider));
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

255 256 257 258
		let margin = new Margin(this._context, this.layoutProvider);
		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, this.layoutProvider);
272 273
		this.viewParts.push(rulers);

E
Erich Gamma 已提交
274 275
		// -------------- Wire dom nodes up

276
		this.linesContentContainer = this._scrollbar.getScrollbarContainerDomNode();
E
Erich Gamma 已提交
277 278 279
		this.linesContentContainer.style.position = 'absolute';

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

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

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

305
	private createPointerHandlerHelper(): IPointerHandlerHelper {
E
Erich Gamma 已提交
306 307 308 309 310 311 312 313 314 315 316
		return {
			viewDomNode: this.domNode,
			linesContentDomNode: this.linesContent,

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

317 318 319 320
			isDirty: (): boolean => {
				return (this.accumulatedModelEvents.length > 0);
			},

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

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

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

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

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

J
Johannes Rieken 已提交
451
	public onLayoutChanged(layoutInfo: editorCommon.EditorLayoutInfo): boolean {
A
Alex Dima 已提交
452
		if (browser.isChrome) {
A
tslint  
Alex Dima 已提交
453
			/* tslint:disable:no-unused-variable */
E
Erich Gamma 已提交
454 455
			// Access overflowGuardContainer.clientWidth to prevent relayouting bug in Chrome
			// See Bug 19676: Editor misses a layout event
A
Alex Dima 已提交
456
			let clientWidth = this.overflowGuardContainer.clientWidth + 'px';
A
tslint  
Alex Dima 已提交
457
			/* tslint:enable:no-unused-variable */
E
Erich Gamma 已提交
458
		}
A
Alex Dima 已提交
459 460
		StyleMutator.setWidth(this.domNode, layoutInfo.width);
		StyleMutator.setHeight(this.domNode, layoutInfo.height);
E
Erich Gamma 已提交
461

A
Alex Dima 已提交
462 463
		StyleMutator.setWidth(this.overflowGuardContainer, layoutInfo.width);
		StyleMutator.setHeight(this.overflowGuardContainer, layoutInfo.height);
E
Erich Gamma 已提交
464

A
Alex Dima 已提交
465 466
		StyleMutator.setWidth(this.linesContent, 1000000);
		StyleMutator.setHeight(this.linesContent, 1000000);
E
Erich Gamma 已提交
467

A
Alex Dima 已提交
468 469 470
		StyleMutator.setLeft(this.linesContentContainer, layoutInfo.contentLeft);
		StyleMutator.setWidth(this.linesContentContainer, layoutInfo.contentWidth);
		StyleMutator.setHeight(this.linesContentContainer, layoutInfo.contentHeight);
E
Erich Gamma 已提交
471

A
Alex Dima 已提交
472
		this.outgoingEvents.emitViewLayoutChanged(layoutInfo);
E
Erich Gamma 已提交
473 474
		return false;
	}
A
Alex Dima 已提交
475
	public onConfigurationChanged(e: editorCommon.IConfigurationChangedEvent): boolean {
476 477
		if (e.viewInfo.editorClassName) {
			this.domNode.className = this._context.configuration.editor.viewInfo.editorClassName;
E
Erich Gamma 已提交
478
		}
479 480
		if (e.viewInfo.ariaLabel) {
			this.textArea.setAttribute('aria-label', this._context.configuration.editor.viewInfo.ariaLabel);
481
		}
E
Erich Gamma 已提交
482 483
		return false;
	}
J
Johannes Rieken 已提交
484
	public onScrollChanged(e: editorCommon.IScrollEvent): boolean {
A
Alex Dima 已提交
485
		this.outgoingEvents.emitScrollChanged(e);
486
		return false;
E
Erich Gamma 已提交
487
	}
J
Johannes Rieken 已提交
488
	public onViewFocusChanged(isFocused: boolean): boolean {
A
Alex Dima 已提交
489
		dom.toggleClass(this.domNode, 'focused', isFocused);
E
Erich Gamma 已提交
490
		if (isFocused) {
A
Alex Dima 已提交
491
			this.outgoingEvents.emitViewFocusGained();
E
Erich Gamma 已提交
492
		} else {
A
Alex Dima 已提交
493
			this.outgoingEvents.emitViewFocusLost();
E
Erich Gamma 已提交
494 495 496
		}
		return false;
	}
497 498 499 500 501 502 503 504 505 506 507 508 509

	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 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
	// --- 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 已提交
525
		this.outgoingEvents.dispose();
A
Alex Dima 已提交
526
		this.listenersToRemove = dispose(this.listenersToRemove);
J
Joao Moreno 已提交
527
		this.listenersToDispose = dispose(this.listenersToDispose);
E
Erich Gamma 已提交
528 529 530 531 532 533 534

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

		this.viewLines.dispose();

		// Destroy IViewPart second
A
Alex Dima 已提交
535
		for (let i = 0, len = this.viewParts.length; i < len; i++) {
E
Erich Gamma 已提交
536 537 538 539
			this.viewParts[i].dispose();
		}
		this.viewParts = [];

540
		this._scrollbar.dispose();
E
Erich Gamma 已提交
541 542 543 544 545
		this.layoutProvider.dispose();
	}

	// --- begin Code Editor APIs

J
Johannes Rieken 已提交
546
	private codeEditorHelper: editorBrowser.ICodeEditorHelper;
A
Alex Dima 已提交
547
	public getCodeEditorHelper(): editorBrowser.ICodeEditorHelper {
E
Erich Gamma 已提交
548 549
		if (!this.codeEditorHelper) {
			this.codeEditorHelper = {
550
				getScrollWidth: () => {
E
Erich Gamma 已提交
551
					if (this._isDisposed) {
552
						throw new Error('ViewImpl.codeEditorHelper.getScrollWidth: View is disposed');
E
Erich Gamma 已提交
553
					}
554
					return this.layoutProvider.getScrollWidth();
E
Erich Gamma 已提交
555 556 557 558 559 560 561
				},
				getScrollLeft: () => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.getScrollLeft: View is disposed');
					}
					return this.layoutProvider.getScrollLeft();
				},
562

E
Erich Gamma 已提交
563 564 565 566 567 568
				getScrollHeight: () => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.getScrollHeight: View is disposed');
					}
					return this.layoutProvider.getScrollHeight();
				},
569
				getScrollTop: () => {
E
Erich Gamma 已提交
570
					if (this._isDisposed) {
571
						throw new Error('ViewImpl.codeEditorHelper.getScrollTop: View is disposed');
E
Erich Gamma 已提交
572
					}
573
					return this.layoutProvider.getScrollTop();
E
Erich Gamma 已提交
574
				},
575

J
Johannes Rieken 已提交
576
				setScrollPosition: (position: editorCommon.INewScrollPosition) => {
577 578 579 580 581 582
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.setScrollPosition: View is disposed');
					}
					this.layoutProvider.setScrollPosition(position);
				},

J
Johannes Rieken 已提交
583
				getVerticalOffsetForPosition: (modelLineNumber: number, modelColumn: number) => {
E
Erich Gamma 已提交
584 585 586
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.getVerticalOffsetForPosition: 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.coordinatesConverter.convertModelPositionToViewPosition(modelPosition);
E
Erich Gamma 已提交
592 593 594 595 596 597
					return this.layoutProvider.getVerticalOffsetForLineNumber(viewPosition.lineNumber);
				},
				delegateVerticalScrollbarMouseDown: (browserEvent: MouseEvent) => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.delegateVerticalScrollbarMouseDown: View is disposed');
					}
598
					this._scrollbar.delegateVerticalScrollbarMouseDown(browserEvent);
E
Erich Gamma 已提交
599 600 601 602 603
				},
				getOffsetForColumn: (modelLineNumber: number, modelColumn: number) => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.getOffsetForColumn: 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
					this._flushAccumulatedAndRenderNow();
A
Alex Dima 已提交
610
					let visibleRanges = this.viewLines.visibleRangesForRange2(new Range(viewPosition.lineNumber, viewPosition.column, viewPosition.lineNumber, viewPosition.column), 0);
E
Erich Gamma 已提交
611 612 613 614
					if (!visibleRanges) {
						return -1;
					}
					return visibleRanges[0].left;
615 616 617 618 619 620 621
				},

				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 已提交
622
				}
623

E
Erich Gamma 已提交
624 625 626 627 628
			};
		}
		return this.codeEditorHelper;
	}

629
	public getCompletelyVisibleLinesRangeInViewport(): Range {
630
		if (this._isDisposed) {
631
			throw new Error('ViewImpl.getCompletelyVisibleLinesRangeInViewport: View is disposed');
632
		}
633 634 635 636 637 638 639 640 641 642 643

		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 已提交
644
		return this._context.model.coordinatesConverter.convertViewRangeToModelRange(completelyVisibleLinesRange);
645 646
	}

A
Alex Dima 已提交
647
	public getInternalEventBus(): IEventEmitter {
E
Erich Gamma 已提交
648 649 650
		if (this._isDisposed) {
			throw new Error('ViewImpl.getInternalEventBus: View is disposed');
		}
A
Alex Dima 已提交
651
		return this.outgoingEvents.getInternalEventBus();
E
Erich Gamma 已提交
652 653
	}

A
Alex Dima 已提交
654
	public saveState(): editorCommon.IViewState {
E
Erich Gamma 已提交
655 656 657 658 659 660
		if (this._isDisposed) {
			throw new Error('ViewImpl.saveState: View is disposed');
		}
		return this.layoutProvider.saveState();
	}

A
Alex Dima 已提交
661
	public restoreState(state: editorCommon.IViewState): void {
E
Erich Gamma 已提交
662 663 664 665 666 667 668 669 670 671 672
		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');
		}
673
		this.keyboardHandler.focusTextArea();
E
Erich Gamma 已提交
674 675

		// IE does not trigger the focus event immediately, so we must help it a little bit
676 677 678
		if (document.activeElement === this.textArea) {
			this._setHasFocus(true);
		}
E
Erich Gamma 已提交
679 680 681 682 683 684 685 686 687 688 689 690 691 692
	}

	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 已提交
693 694
			this._context, cssClassName, this.layoutProvider.getScrollHeight(), minimumHeight, maximumHeight,
			(lineNumber: number) => this.layoutProvider.getVerticalOffsetForLineNumber(lineNumber)
E
Erich Gamma 已提交
695 696 697
		);
	}

A
Alex Dima 已提交
698
	public change(callback: (changeAccessor: editorBrowser.IViewZoneChangeAccessor) => any): boolean {
E
Erich Gamma 已提交
699 700 701
		if (this._isDisposed) {
			throw new Error('ViewImpl.change: View is disposed');
		}
A
Alex Dima 已提交
702
		let zonesHaveChanged = false;
A
Alex Dima 已提交
703

E
Erich Gamma 已提交
704 705 706
		this._renderOnce(() => {
			// Handle events to avoid "adjusting" newly inserted view zones
			this._flushAnyAccumulatedEvents();
A
Alex Dima 已提交
707
			let changeAccessor: editorBrowser.IViewZoneChangeAccessor = {
J
Johannes Rieken 已提交
708
				addZone: (zone: editorBrowser.IViewZone): number => {
E
Erich Gamma 已提交
709 710 711
					zonesHaveChanged = true;
					return this.viewZones.addZone(zone);
				},
J
Johannes Rieken 已提交
712
				removeZone: (id: number): void => {
713 714 715
					if (!id) {
						return;
					}
E
Erich Gamma 已提交
716 717 718
					zonesHaveChanged = this.viewZones.removeZone(id) || zonesHaveChanged;
				},
				layoutZone: (id: number): void => {
719 720 721
					if (!id) {
						return;
					}
E
Erich Gamma 已提交
722 723 724 725
					zonesHaveChanged = this.viewZones.layoutZone(id) || zonesHaveChanged;
				}
			};

A
Alex Dima 已提交
726
			safeInvoke1Arg(callback, changeAccessor);
E
Erich Gamma 已提交
727 728 729 730 731 732

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

			if (zonesHaveChanged) {
A
Alex Dima 已提交
733
				this.layoutProvider.onHeightMaybeChanged();
734
				this._context.privateViewEventBus.emit(editorCommon.EventType.ViewZonesChanged, null);
E
Erich Gamma 已提交
735 736 737 738 739
			}
		});
		return zonesHaveChanged;
	}

J
Johannes Rieken 已提交
740
	public getWhitespaces(): editorCommon.IEditorWhitespace[] {
E
Erich Gamma 已提交
741 742 743 744 745 746
		if (this._isDisposed) {
			throw new Error('ViewImpl.getWhitespaces: View is disposed');
		}
		return this.layoutProvider.getWhitespaces();
	}

A
Alex Dima 已提交
747
	public addContentWidget(widgetData: editorBrowser.IContentWidgetData): void {
E
Erich Gamma 已提交
748 749 750
		if (this._isDisposed) {
			throw new Error('ViewImpl.addContentWidget: View is disposed');
		}
A
Alex Dima 已提交
751 752 753
		this.contentWidgets.addWidget(widgetData.widget);
		this.layoutContentWidget(widgetData);
		this._scheduleRender();
E
Erich Gamma 已提交
754 755
	}

A
Alex Dima 已提交
756
	public layoutContentWidget(widgetData: editorBrowser.IContentWidgetData): void {
E
Erich Gamma 已提交
757 758 759
		if (this._isDisposed) {
			throw new Error('ViewImpl.layoutContentWidget: View is disposed');
		}
760

A
Alex Dima 已提交
761 762 763 764
		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 已提交
765 766
	}

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

A
Alex Dima 已提交
775
	public addOverlayWidget(widgetData: editorBrowser.IOverlayWidgetData): void {
E
Erich Gamma 已提交
776 777 778
		if (this._isDisposed) {
			throw new Error('ViewImpl.addOverlayWidget: View is disposed');
		}
A
Alex Dima 已提交
779 780 781
		this.overlayWidgets.addWidget(widgetData.widget);
		this.layoutOverlayWidget(widgetData);
		this._scheduleRender();
E
Erich Gamma 已提交
782 783
	}

A
Alex Dima 已提交
784
	public layoutOverlayWidget(widgetData: editorBrowser.IOverlayWidgetData): void {
E
Erich Gamma 已提交
785 786 787
		if (this._isDisposed) {
			throw new Error('ViewImpl.layoutOverlayWidget: View is disposed');
		}
788 789 790 791 792 793

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

A
Alex Dima 已提交
796
	public removeOverlayWidget(widgetData: editorBrowser.IOverlayWidgetData): void {
E
Erich Gamma 已提交
797 798 799
		if (this._isDisposed) {
			throw new Error('ViewImpl.removeOverlayWidget: View is disposed');
		}
A
Alex Dima 已提交
800 801
		this.overlayWidgets.removeWidget(widgetData.widget);
		this._scheduleRender();
E
Erich Gamma 已提交
802 803
	}

J
Johannes Rieken 已提交
804
	public render(now: boolean, everything: boolean): void {
E
Erich Gamma 已提交
805 806 807
		if (this._isDisposed) {
			throw new Error('ViewImpl.render: View is disposed');
		}
808 809 810 811
		if (everything) {
			// Force a render with a layout event
			this.layoutProvider.emitLayoutChangedEvent();
		}
812 813 814
		if (now) {
			this._flushAccumulatedAndRenderNow();
		}
E
Erich Gamma 已提交
815 816 817 818 819 820 821 822
	}

	// --- end Code Editor APIs

	private _renderOnce(callback: () => any): any {
		if (this._isDisposed) {
			throw new Error('ViewImpl._renderOnce: View is disposed');
		}
A
Alex Dima 已提交
823
		return this.outgoingEvents.deferredEmit(() => {
A
Alex Dima 已提交
824 825
			let r = safeInvokeNoArg(callback);
			this._scheduleRender();
E
Erich Gamma 已提交
826 827 828 829 830 831 832 833 834
			return r;
		});
	}

	private _scheduleRender(): void {
		if (this._isDisposed) {
			throw new Error('ViewImpl._scheduleRender: View is disposed');
		}
		if (this._renderAnimationFrame === null) {
A
Alex Dima 已提交
835
			this._renderAnimationFrame = dom.runAtThisOrScheduleAtNextAnimationFrame(this._onRenderScheduled.bind(this), 100);
E
Erich Gamma 已提交
836 837 838 839 840 841 842 843 844
		}
	}

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

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

848
	private _getViewPartsToRender(): ViewPart[] {
J
Johannes Rieken 已提交
849
		let result: ViewPart[] = [];
850 851 852 853 854 855 856 857 858
		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 已提交
859
	private _actualRender(): void {
A
Alex Dima 已提交
860
		if (!dom.isInDOM(this.domNode)) {
E
Erich Gamma 已提交
861 862 863
			return;
		}

864
		let viewPartsToRender = this._getViewPartsToRender();
E
Erich Gamma 已提交
865

A
Alex Dima 已提交
866 867
		if (!this.viewLines.shouldRender() && viewPartsToRender.length === 0) {
			// Nothing to render
868
			this.keyboardHandler.writeToTextArea();
A
Alex Dima 已提交
869 870
			return;
		}
E
Erich Gamma 已提交
871

872
		let partialViewportData = this.layoutProvider.getLinesViewportData();
A
Alex Dima 已提交
873
		this._context.model.setViewport(partialViewportData.startLineNumber, partialViewportData.endLineNumber, partialViewportData.centeredLineNumber);
874 875

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

A
Alex Dima 已提交
877
		if (this.viewLines.shouldRender()) {
878
			this.viewLines.renderText(viewportData, () => {
879 880
				this.keyboardHandler.writeToTextArea();
			});
A
Alex Dima 已提交
881
			this.viewLines.onDidRender();
882 883 884

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

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

A
Alex Dima 已提交
891 892 893 894 895
		// 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 已提交
896

A
Alex Dima 已提交
897 898 899 900
		for (let i = 0, len = viewPartsToRender.length; i < len; i++) {
			let viewPart = viewPartsToRender[i];
			viewPart.render(renderingContext);
			viewPart.onDidRender();
E
Erich Gamma 已提交
901 902
		}

903
		// Render the scrollbar
904
		this._scrollbar.renderScrollbar();
E
Erich Gamma 已提交
905 906
	}

J
Johannes Rieken 已提交
907
	private _setHasFocus(newHasFocus: boolean): void {
E
Erich Gamma 已提交
908 909
		if (this.hasFocus !== newHasFocus) {
			this.hasFocus = newHasFocus;
910
			this._context.privateViewEventBus.emit(editorCommon.EventType.ViewFocusChanged, this.hasFocus);
E
Erich Gamma 已提交
911 912 913 914
		}
	}
}

J
Johannes Rieken 已提交
915
function safeInvokeNoArg(func: Function): any {
A
Alex Dima 已提交
916 917
	try {
		return func();
J
Johannes Rieken 已提交
918
	} catch (e) {
A
Alex Dima 已提交
919 920 921 922
		onUnexpectedError(e);
	}
}

J
Johannes Rieken 已提交
923
function safeInvoke1Arg(func: Function, arg1: any): any {
A
Alex Dima 已提交
924 925
	try {
		return func(arg1);
J
Johannes Rieken 已提交
926
	} catch (e) {
A
Alex Dima 已提交
927 928 929
		onUnexpectedError(e);
	}
}