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

A
Alex Dima 已提交
6
import * as dom from 'vs/base/browser/dom';
A
Alex Dima 已提交
7
import { FastDomNode, createFastDomNode } from 'vs/base/browser/fastDomNode';
A
Alex Dima 已提交
8 9 10 11
import { IMouseEvent } from 'vs/base/browser/mouseEvent';
import { onUnexpectedError } from 'vs/base/common/errors';
import { IDisposable } from 'vs/base/common/lifecycle';
import { IPointerHandlerHelper } from 'vs/editor/browser/controller/mouseHandler';
J
Johannes Rieken 已提交
12
import { PointerHandler } from 'vs/editor/browser/controller/pointerHandler';
A
Alex Dima 已提交
13
import { ITextAreaHandlerHelper, TextAreaHandler } from 'vs/editor/browser/controller/textAreaHandler';
I
isidor 已提交
14
import { IContentWidget, IContentWidgetPosition, IOverlayWidget, IOverlayWidgetPosition, IMouseTarget, IViewZoneChangeAccessor, IEditorAriaOptions } from 'vs/editor/browser/editorBrowser';
A
Alex Dima 已提交
15 16
import { ICommandDelegate, ViewController } from 'vs/editor/browser/view/viewController';
import { ViewOutgoingEvents } from 'vs/editor/browser/view/viewOutgoingEvents';
J
Johannes Rieken 已提交
17
import { ContentViewOverlays, MarginViewOverlays } from 'vs/editor/browser/view/viewOverlays';
A
Alex Dima 已提交
18
import { PartFingerprint, PartFingerprints, ViewPart } from 'vs/editor/browser/view/viewPart';
J
Johannes Rieken 已提交
19
import { ViewContentWidgets } from 'vs/editor/browser/viewParts/contentWidgets/contentWidgets';
20
import { CurrentLineHighlightOverlay, CurrentLineMarginHighlightOverlay } from 'vs/editor/browser/viewParts/currentLineHighlight/currentLineHighlight';
J
Johannes Rieken 已提交
21
import { DecorationsOverlay } from 'vs/editor/browser/viewParts/decorations/decorations';
A
Alex Dima 已提交
22
import { EditorScrollbar } from 'vs/editor/browser/viewParts/editorScrollbar/editorScrollbar';
J
Johannes Rieken 已提交
23 24
import { GlyphMarginOverlay } from 'vs/editor/browser/viewParts/glyphMargin/glyphMargin';
import { IndentGuidesOverlay } from 'vs/editor/browser/viewParts/indentGuides/indentGuides';
A
Alex Dima 已提交
25
import { LineNumbersOverlay } from 'vs/editor/browser/viewParts/lineNumbers/lineNumbers';
J
Johannes Rieken 已提交
26 27
import { ViewLines } from 'vs/editor/browser/viewParts/lines/viewLines';
import { LinesDecorationsOverlay } from 'vs/editor/browser/viewParts/linesDecorations/linesDecorations';
A
Alex Dima 已提交
28
import { Margin } from 'vs/editor/browser/viewParts/margin/margin';
29
import { MarginViewLineDecorationsOverlay } from 'vs/editor/browser/viewParts/marginDecorations/marginDecorations';
A
Alex Dima 已提交
30
import { Minimap } from 'vs/editor/browser/viewParts/minimap/minimap';
J
Johannes Rieken 已提交
31 32 33 34 35 36 37 38
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 已提交
39 40
import { Cursor } from 'vs/editor/common/controller/cursor';
import { Position } from 'vs/editor/common/core/position';
41
import { Range } from 'vs/editor/common/core/range';
A
Alex Dima 已提交
42
import { IConfiguration } from 'vs/editor/common/editorCommon';
A
Alex Dima 已提交
43
import { RenderingContext } from 'vs/editor/common/view/renderingContext';
A
Alex Dima 已提交
44 45
import { ViewContext } from 'vs/editor/common/view/viewContext';
import { ViewEventDispatcher } from 'vs/editor/common/view/viewEventDispatcher';
46
import * as viewEvents from 'vs/editor/common/view/viewEvents';
A
Alex Dima 已提交
47 48 49
import { ViewportData } from 'vs/editor/common/viewLayout/viewLinesViewportData';
import { ViewEventHandler } from 'vs/editor/common/viewModel/viewEventHandler';
import { IViewModel } from 'vs/editor/common/viewModel/viewModel';
50
import { IThemeService, getThemeTypeSelector } from 'vs/platform/theme/common/themeService';
A
renames  
Alex Dima 已提交
51
import { EditorOption } from 'vs/editor/common/config/editorOptions';
52
import { PointerHandlerLastRenderData } from 'vs/editor/browser/controller/mouseTarget';
53

E
Erich Gamma 已提交
54

A
Alex Dima 已提交
55
export interface IContentWidgetData {
56 57
	widget: IContentWidget;
	position: IContentWidgetPosition | null;
A
Alex Dima 已提交
58 59 60
}

export interface IOverlayWidgetData {
61 62
	widget: IOverlayWidget;
	position: IOverlayWidgetPosition | null;
A
Alex Dima 已提交
63 64 65
}

export class View extends ViewEventHandler {
E
Erich Gamma 已提交
66

67
	private readonly eventDispatcher: ViewEventDispatcher;
E
Erich Gamma 已提交
68

69
	private _scrollbar: EditorScrollbar;
70 71
	private readonly _context: ViewContext;
	private readonly _cursor: Cursor;
E
Erich Gamma 已提交
72 73 74 75 76 77 78 79

	// 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;
80
	private viewCursors: ViewCursors;
A
Alex Dima 已提交
81
	private viewParts: ViewPart[];
E
Erich Gamma 已提交
82

83 84
	private readonly _textAreaHandler: TextAreaHandler;
	private readonly pointerHandler: PointerHandler;
E
Erich Gamma 已提交
85

86
	private readonly outgoingEvents: ViewOutgoingEvents;
E
Erich Gamma 已提交
87 88

	// Dom nodes
A
Alex Dima 已提交
89 90 91
	private linesContent: FastDomNode<HTMLElement>;
	public domNode: FastDomNode<HTMLElement>;
	private overflowGuardContainer: FastDomNode<HTMLElement>;
E
Erich Gamma 已提交
92 93

	// Actual mutable state
A
Alex Dima 已提交
94
	private _renderAnimationFrame: IDisposable | null;
E
Erich Gamma 已提交
95

A
Alex Dima 已提交
96
	constructor(
97
		commandDelegate: ICommandDelegate,
A
Alex Dima 已提交
98
		configuration: IConfiguration,
99
		themeService: IThemeService,
J
Johannes Rieken 已提交
100
		model: IViewModel,
101
		cursor: Cursor,
A
Alex Dima 已提交
102
		outgoingEvents: ViewOutgoingEvents
A
Alex Dima 已提交
103
	) {
E
Erich Gamma 已提交
104
		super();
105
		this._cursor = cursor;
E
Erich Gamma 已提交
106
		this._renderAnimationFrame = null;
A
Alex Dima 已提交
107
		this.outgoingEvents = outgoingEvents;
E
Erich Gamma 已提交
108

M
Matt Bierner 已提交
109
		const viewController = new ViewController(configuration, model, this.outgoingEvents, commandDelegate);
E
Erich Gamma 已提交
110 111

		// 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 116
		// Ensure the view is the first event handler in order to update the layout
		this.eventDispatcher.addEventHandler(this);

E
Erich Gamma 已提交
117
		// The view context is passed on to most classes (basically to reduce param. counts in ctors)
M
Martin Aeschlimann 已提交
118
		this._context = new ViewContext(configuration, themeService.getColorTheme(), model, this.eventDispatcher);
119

M
Martin Aeschlimann 已提交
120
		this._register(themeService.onDidColorThemeChange(theme => {
A
Alex Dima 已提交
121
			this._context.theme.update(theme);
122
			this.eventDispatcher.emit(new viewEvents.ViewThemeChangedEvent());
123
			this.render(true, false);
124
		}));
E
Erich Gamma 已提交
125

126 127
		this.viewParts = [];

128 129
		// Keyboard handler
		this._textAreaHandler = new TextAreaHandler(this._context, viewController, this.createTextAreaHandlerHelper());
130
		this.viewParts.push(this._textAreaHandler);
131

A
Alex Dima 已提交
132
		// These two dom nodes must be constructed up front, since references are needed in the layout provider (scrolling & co.)
A
Alex Dima 已提交
133
		this.linesContent = createFastDomNode(document.createElement('div'));
A
Alex Dima 已提交
134
		this.linesContent.setClassName('lines-content' + ' monaco-editor-background');
A
Alex Dima 已提交
135
		this.linesContent.setPosition('absolute');
A
Alex Dima 已提交
136

A
Alex Dima 已提交
137
		this.domNode = createFastDomNode(document.createElement('div'));
138
		this.domNode.setClassName(this.getEditorClassName());
I
isidor 已提交
139 140
		// Set role 'code' for better screen reader support https://github.com/microsoft/vscode/issues/93438
		this.domNode.setAttribute('role', 'code');
A
Alex Dima 已提交
141

A
Alex Dima 已提交
142
		this.overflowGuardContainer = createFastDomNode(document.createElement('div'));
A
Alex Dima 已提交
143
		PartFingerprints.write(this.overflowGuardContainer, PartFingerprint.OverflowGuard);
A
Alex Dima 已提交
144
		this.overflowGuardContainer.setClassName('overflow-guard');
A
Alex Dima 已提交
145

146
		this._scrollbar = new EditorScrollbar(this._context, this.linesContent, this.domNode, this.overflowGuardContainer);
A
Alex Dima 已提交
147 148
		this.viewParts.push(this._scrollbar);

E
Erich Gamma 已提交
149
		// View Lines
150
		this.viewLines = new ViewLines(this._context, this.linesContent);
E
Erich Gamma 已提交
151 152

		// View Zones
153
		this.viewZones = new ViewZones(this._context);
E
Erich Gamma 已提交
154 155 156
		this.viewParts.push(this.viewZones);

		// Decorations overview ruler
M
Matt Bierner 已提交
157
		const decorationsOverviewRuler = new DecorationsOverviewRuler(this._context);
E
Erich Gamma 已提交
158 159 160
		this.viewParts.push(decorationsOverviewRuler);


M
Matt Bierner 已提交
161
		const scrollDecoration = new ScrollDecorationViewPart(this._context);
E
Erich Gamma 已提交
162 163
		this.viewParts.push(scrollDecoration);

M
Matt Bierner 已提交
164
		const contentViewOverlays = new ContentViewOverlays(this._context);
E
Erich Gamma 已提交
165
		this.viewParts.push(contentViewOverlays);
A
Alex Dima 已提交
166
		contentViewOverlays.addDynamicOverlay(new CurrentLineHighlightOverlay(this._context));
167
		contentViewOverlays.addDynamicOverlay(new SelectionsOverlay(this._context));
168
		contentViewOverlays.addDynamicOverlay(new IndentGuidesOverlay(this._context));
169
		contentViewOverlays.addDynamicOverlay(new DecorationsOverlay(this._context));
E
Erich Gamma 已提交
170

M
Matt Bierner 已提交
171
		const marginViewOverlays = new MarginViewOverlays(this._context);
E
Erich Gamma 已提交
172
		this.viewParts.push(marginViewOverlays);
A
Alex Dima 已提交
173
		marginViewOverlays.addDynamicOverlay(new CurrentLineMarginHighlightOverlay(this._context));
174
		marginViewOverlays.addDynamicOverlay(new GlyphMarginOverlay(this._context));
175
		marginViewOverlays.addDynamicOverlay(new MarginViewLineDecorationsOverlay(this._context));
176 177
		marginViewOverlays.addDynamicOverlay(new LinesDecorationsOverlay(this._context));
		marginViewOverlays.addDynamicOverlay(new LineNumbersOverlay(this._context));
E
Erich Gamma 已提交
178

M
Matt Bierner 已提交
179
		const margin = new Margin(this._context);
A
Alex Dima 已提交
180
		margin.getDomNode().appendChild(this.viewZones.marginDomNode);
A
Alex Dima 已提交
181
		margin.getDomNode().appendChild(marginViewOverlays.getDomNode());
182
		this.viewParts.push(margin);
E
Erich Gamma 已提交
183 184

		// Content widgets
185
		this.contentWidgets = new ViewContentWidgets(this._context, this.domNode);
E
Erich Gamma 已提交
186 187
		this.viewParts.push(this.contentWidgets);

188 189
		this.viewCursors = new ViewCursors(this._context);
		this.viewParts.push(this.viewCursors);
E
Erich Gamma 已提交
190 191

		// Overlay widgets
192
		this.overlayWidgets = new ViewOverlayWidgets(this._context);
E
Erich Gamma 已提交
193 194
		this.viewParts.push(this.overlayWidgets);

M
Matt Bierner 已提交
195
		const rulers = new Rulers(this._context);
196 197
		this.viewParts.push(rulers);

M
Matt Bierner 已提交
198
		const minimap = new Minimap(this._context);
A
Alex Dima 已提交
199 200
		this.viewParts.push(minimap);

E
Erich Gamma 已提交
201 202 203
		// -------------- Wire dom nodes up

		if (decorationsOverviewRuler) {
M
Matt Bierner 已提交
204
			const overviewRulerData = this._scrollbar.getOverviewRulerLayoutInfo();
E
Erich Gamma 已提交
205 206 207
			overviewRulerData.parent.insertBefore(decorationsOverviewRuler.getDomNode(), overviewRulerData.insertBefore);
		}

A
Alex Dima 已提交
208 209 210 211 212 213 214 215 216
		this.linesContent.appendChild(contentViewOverlays.getDomNode());
		this.linesContent.appendChild(rulers.domNode);
		this.linesContent.appendChild(this.viewZones.domNode);
		this.linesContent.appendChild(this.viewLines.getDomNode());
		this.linesContent.appendChild(this.contentWidgets.domNode);
		this.linesContent.appendChild(this.viewCursors.getDomNode());
		this.overflowGuardContainer.appendChild(margin.getDomNode());
		this.overflowGuardContainer.appendChild(this._scrollbar.getDomNode());
		this.overflowGuardContainer.appendChild(scrollDecoration.getDomNode());
217 218
		this.overflowGuardContainer.appendChild(this._textAreaHandler.textArea);
		this.overflowGuardContainer.appendChild(this._textAreaHandler.textAreaCover);
219
		this.overflowGuardContainer.appendChild(this.overlayWidgets.getDomNode());
A
Alex Dima 已提交
220 221 222
		this.overflowGuardContainer.appendChild(minimap.getDomNode());
		this.domNode.appendChild(this.overflowGuardContainer);
		this.domNode.appendChild(this.contentWidgets.overflowingContentWidgetsDomNode);
A
Alex Dima 已提交
223

A
Alex Dima 已提交
224
		this._applyLayout();
A
Alex Dima 已提交
225 226 227 228

		// Pointer handler
		this.pointerHandler = this._register(new PointerHandler(this._context, viewController, this.createPointerHandlerHelper()));

A
Alex Dima 已提交
229
		this._register(model.addViewEventListener((events: viewEvents.ViewEvent[]) => {
A
Alex Dima 已提交
230 231 232
			this.eventDispatcher.emitMany(events);
		}));

A
Alex Dima 已提交
233
		this._register(this._cursor.addViewEventListener((events: viewEvents.ViewEvent[]) => {
A
Alex Dima 已提交
234 235
			this.eventDispatcher.emitMany(events);
		}));
E
Erich Gamma 已提交
236 237 238 239 240 241
	}

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

242
	private createPointerHandlerHelper(): IPointerHandlerHelper {
E
Erich Gamma 已提交
243
		return {
A
Alex Dima 已提交
244 245
			viewDomNode: this.domNode.domNode,
			linesContentDomNode: this.linesContent.domNode,
E
Erich Gamma 已提交
246 247 248 249 250

			focusTextArea: () => {
				this.focus();
			},

251 252 253 254
			getLastRenderData: (): PointerHandlerLastRenderData => {
				const lastViewCursorsRenderData = this.viewCursors.getLastRenderData() || [];
				const lastTextareaPosition = this._textAreaHandler.getLastRenderData();
				return new PointerHandlerLastRenderData(lastViewCursorsRenderData, lastTextareaPosition);
255
			},
A
Alex Dima 已提交
256
			shouldSuppressMouseDownOnViewZone: (viewZoneId: string) => {
E
Erich Gamma 已提交
257 258
				return this.viewZones.shouldSuppressMouseDownOnViewZone(viewZoneId);
			},
259 260 261
			shouldSuppressMouseDownOnWidget: (widgetId: string) => {
				return this.contentWidgets.shouldSuppressMouseDownOnWidget(widgetId);
			},
E
Erich Gamma 已提交
262 263 264 265 266
			getPositionFromDOMInfo: (spanNode: HTMLElement, offset: number) => {
				this._flushAccumulatedAndRenderNow();
				return this.viewLines.getPositionFromDOMInfo(spanNode, offset);
			},

267
			visibleRangeForPosition: (lineNumber: number, column: number) => {
E
Erich Gamma 已提交
268
				this._flushAccumulatedAndRenderNow();
A
Alex Dima 已提交
269
				return this.viewLines.visibleRangeForPosition(new Position(lineNumber, column));
E
Erich Gamma 已提交
270 271 272 273 274 275 276 277 278
			},

			getLineWidth: (lineNumber: number) => {
				this._flushAccumulatedAndRenderNow();
				return this.viewLines.getLineWidth(lineNumber);
			}
		};
	}

279
	private createTextAreaHandlerHelper(): ITextAreaHandlerHelper {
E
Erich Gamma 已提交
280 281 282
		return {
			visibleRangeForPositionRelativeToEditor: (lineNumber: number, column: number) => {
				this._flushAccumulatedAndRenderNow();
A
Alex Dima 已提交
283
				return this.viewLines.visibleRangeForPosition(new Position(lineNumber, column));
E
Erich Gamma 已提交
284 285 286 287
			}
		};
	}

A
Alex Dima 已提交
288
	private _applyLayout(): void {
A
Alex Dima 已提交
289
		const options = this._context.configuration.options;
A
renames  
Alex Dima 已提交
290
		const layoutInfo = options.get(EditorOption.layoutInfo);
A
Alex Dima 已提交
291

A
Alex Dima 已提交
292 293
		this.domNode.setWidth(layoutInfo.width);
		this.domNode.setHeight(layoutInfo.height);
E
Erich Gamma 已提交
294

A
Alex Dima 已提交
295 296
		this.overflowGuardContainer.setWidth(layoutInfo.width);
		this.overflowGuardContainer.setHeight(layoutInfo.height);
E
Erich Gamma 已提交
297

A
Alex Dima 已提交
298 299
		this.linesContent.setWidth(1000000);
		this.linesContent.setHeight(1000000);
E
Erich Gamma 已提交
300
	}
301

302
	private getEditorClassName() {
M
Matt Bierner 已提交
303
		const focused = this._textAreaHandler.isFocused() ? ' focused' : '';
A
Alex Dima 已提交
304
		return this._context.configuration.options.get(EditorOption.editorClassName) + ' ' + getThemeTypeSelector(this._context.theme.type) + focused;
305 306
	}

307 308
	// --- begin event handlers

A
Alex Dima 已提交
309
	public onConfigurationChanged(e: viewEvents.ViewConfigurationChangedEvent): boolean {
A
Alex Dima 已提交
310 311
		this.domNode.setClassName(this.getEditorClassName());
		this._applyLayout();
312
		return false;
E
Erich Gamma 已提交
313
	}
314 315 316 317
	public onContentSizeChanged(e: viewEvents.ViewContentSizeChangedEvent): boolean {
		this.outgoingEvents.emitContentSizeChange(e);
		return false;
	}
A
Alex Dima 已提交
318
	public onFocusChanged(e: viewEvents.ViewFocusChangedEvent): boolean {
S
smoke:  
Sandeep Somavarapu 已提交
319
		this.domNode.setClassName(this.getEditorClassName());
320
		this._context.model.setHasFocus(e.isFocused);
A
Alex Dima 已提交
321
		if (e.isFocused) {
A
Alex Dima 已提交
322
			this.outgoingEvents.emitViewFocusGained();
E
Erich Gamma 已提交
323
		} else {
A
Alex Dima 已提交
324
			this.outgoingEvents.emitViewFocusLost();
E
Erich Gamma 已提交
325 326 327
		}
		return false;
	}
328 329 330 331
	public onScrollChanged(e: viewEvents.ViewScrollChangedEvent): boolean {
		this.outgoingEvents.emitScrollChanged(e);
		return false;
	}
332 333 334 335
	public onThemeChanged(e: viewEvents.ViewThemeChangedEvent): boolean {
		this.domNode.setClassName(this.getEditorClassName());
		return false;
	}
336

E
Erich Gamma 已提交
337 338 339 340 341 342 343 344 345
	// --- end event handlers

	public dispose(): void {
		if (this._renderAnimationFrame !== null) {
			this._renderAnimationFrame.dispose();
			this._renderAnimationFrame = null;
		}

		this.eventDispatcher.removeEventHandler(this);
A
Alex Dima 已提交
346
		this.outgoingEvents.dispose();
E
Erich Gamma 已提交
347 348 349

		this.viewLines.dispose();

A
Alex Dima 已提交
350
		// Destroy view parts
A
Alex Dima 已提交
351
		for (let i = 0, len = this.viewParts.length; i < len; i++) {
E
Erich Gamma 已提交
352 353 354 355
			this.viewParts[i].dispose();
		}
		this.viewParts = [];

A
Alex Dima 已提交
356
		super.dispose();
E
Erich Gamma 已提交
357 358
	}

359
	private _renderOnce<T>(callback: () => T): T {
M
Matt Bierner 已提交
360
		const r = safeInvokeNoArg(callback);
A
Alex Dima 已提交
361 362 363
		this._scheduleRender();
		return r;
	}
E
Erich Gamma 已提交
364

A
Alex Dima 已提交
365 366 367 368 369
	private _scheduleRender(): void {
		if (this._renderAnimationFrame === null) {
			this._renderAnimationFrame = dom.runAtThisOrScheduleAtNextAnimationFrame(this._onRenderScheduled.bind(this), 100);
		}
	}
A
Alex Dima 已提交
370

A
Alex Dima 已提交
371 372 373 374
	private _onRenderScheduled(): void {
		this._renderAnimationFrame = null;
		this._flushAccumulatedAndRenderNow();
	}
375

A
Alex Dima 已提交
376 377 378
	private _renderNow(): void {
		safeInvokeNoArg(() => this._actualRender());
	}
A
Alex Dima 已提交
379

A
Alex Dima 已提交
380
	private _getViewPartsToRender(): ViewPart[] {
A
Alex Dima 已提交
381
		let result: ViewPart[] = [], resultLen = 0;
A
Alex Dima 已提交
382
		for (let i = 0, len = this.viewParts.length; i < len; i++) {
M
Matt Bierner 已提交
383
			const viewPart = this.viewParts[i];
A
Alex Dima 已提交
384
			if (viewPart.shouldRender()) {
A
Alex Dima 已提交
385
				result[resultLen++] = viewPart;
A
Alex Dima 已提交
386 387 388 389
			}
		}
		return result;
	}
390

A
Alex Dima 已提交
391 392 393 394
	private _actualRender(): void {
		if (!dom.isInDOM(this.domNode.domNode)) {
			return;
		}
395

A
Alex Dima 已提交
396
		let viewPartsToRender = this._getViewPartsToRender();
A
Alex Dima 已提交
397

A
Alex Dima 已提交
398 399 400 401
		if (!this.viewLines.shouldRender() && viewPartsToRender.length === 0) {
			// Nothing to render
			return;
		}
A
Alex Dima 已提交
402

403
		const partialViewportData = this._context.viewLayout.getLinesViewportData();
A
Alex Dima 已提交
404
		this._context.model.setViewport(partialViewportData.startLineNumber, partialViewportData.endLineNumber, partialViewportData.centeredLineNumber);
405

M
Matt Bierner 已提交
406
		const viewportData = new ViewportData(
407 408 409 410 411
			this._cursor.getViewSelections(),
			partialViewportData,
			this._context.viewLayout.getWhitespaceViewportData(),
			this._context.model
		);
A
Alex Dima 已提交
412

413 414 415 416 417
		if (this.contentWidgets.shouldRender()) {
			// Give the content widgets a chance to set their max width before a possible synchronous layout
			this.contentWidgets.onBeforeRender(viewportData);
		}

A
Alex Dima 已提交
418
		if (this.viewLines.shouldRender()) {
419
			this.viewLines.renderText(viewportData);
A
Alex Dima 已提交
420
			this.viewLines.onDidRender();
A
Alex Dima 已提交
421

A
Alex Dima 已提交
422 423 424
			// Rendering of viewLines might cause scroll events to occur, so collect view parts to render again
			viewPartsToRender = this._getViewPartsToRender();
		}
A
Alex Dima 已提交
425

M
Matt Bierner 已提交
426
		const renderingContext = new RenderingContext(this._context.viewLayout, viewportData, this.viewLines);
A
Alex Dima 已提交
427

A
Alex Dima 已提交
428 429
		// Render the rest of the parts
		for (let i = 0, len = viewPartsToRender.length; i < len; i++) {
M
Matt Bierner 已提交
430
			const viewPart = viewPartsToRender[i];
A
Alex Dima 已提交
431 432
			viewPart.prepareRender(renderingContext);
		}
433

A
Alex Dima 已提交
434
		for (let i = 0, len = viewPartsToRender.length; i < len; i++) {
M
Matt Bierner 已提交
435
			const viewPart = viewPartsToRender[i];
A
Alex Dima 已提交
436 437
			viewPart.render(renderingContext);
			viewPart.onDidRender();
E
Erich Gamma 已提交
438 439 440
		}
	}

A
Alex Dima 已提交
441 442
	// --- BEGIN CodeEditor helpers

443
	public delegateVerticalScrollbarMouseDown(browserEvent: IMouseEvent): void {
A
Alex Dima 已提交
444 445 446
		this._scrollbar.delegateVerticalScrollbarMouseDown(browserEvent);
	}

447 448
	public restoreState(scrollPosition: { scrollLeft: number; scrollTop: number; }): void {
		this._context.viewLayout.setScrollPositionNow({ scrollTop: scrollPosition.scrollTop });
449
		this._context.model.tokenizeViewport();
450 451 452 453 454
		this._renderNow();
		this.viewLines.updateLineWidths();
		this._context.viewLayout.setScrollPositionNow({ scrollLeft: scrollPosition.scrollLeft });
	}

A
Alex Dima 已提交
455
	public getOffsetForColumn(modelLineNumber: number, modelColumn: number): number {
M
Matt Bierner 已提交
456
		const modelPosition = this._context.model.validateModelPosition({
A
Alex Dima 已提交
457 458 459
			lineNumber: modelLineNumber,
			column: modelColumn
		});
M
Matt Bierner 已提交
460
		const viewPosition = this._context.model.coordinatesConverter.convertModelPositionToViewPosition(modelPosition);
A
Alex Dima 已提交
461
		this._flushAccumulatedAndRenderNow();
A
Alex Dima 已提交
462 463
		const visibleRange = this.viewLines.visibleRangeForPosition(new Position(viewPosition.lineNumber, viewPosition.column));
		if (!visibleRange) {
A
Alex Dima 已提交
464
			return -1;
E
Erich Gamma 已提交
465
		}
A
Alex Dima 已提交
466
		return visibleRange.left;
A
Alex Dima 已提交
467 468
	}

469
	public getTargetAtClientPoint(clientX: number, clientY: number): IMouseTarget | null {
470 471 472 473 474
		const mouseTarget = this.pointerHandler.getTargetAtClientPoint(clientX, clientY);
		if (!mouseTarget) {
			return null;
		}
		return ViewOutgoingEvents.convertViewToModelMouseTarget(mouseTarget, this._context.model.coordinatesConverter);
A
Alex Dima 已提交
475 476
	}

477 478
	public createOverviewRuler(cssClassName: string): OverviewRuler {
		return new OverviewRuler(this._context, cssClassName);
E
Erich Gamma 已提交
479 480
	}

481 482 483
	public change(callback: (changeAccessor: IViewZoneChangeAccessor) => any): boolean {
		return this._renderOnce(() => {
			const zonesHaveChanged = this.viewZones.changeViewZones(callback);
E
Erich Gamma 已提交
484
			if (zonesHaveChanged) {
485
				this._context.viewLayout.onHeightMaybeChanged();
486
				this._context.privateViewEventBus.emit(new viewEvents.ViewZonesChangedEvent());
E
Erich Gamma 已提交
487
			}
488
			return zonesHaveChanged;
E
Erich Gamma 已提交
489 490 491
		});
	}

A
Alex Dima 已提交
492 493 494 495 496
	public render(now: boolean, everything: boolean): void {
		if (everything) {
			// Force everything to render...
			this.viewLines.forceShouldRender();
			for (let i = 0, len = this.viewParts.length; i < len; i++) {
M
Matt Bierner 已提交
497
				const viewPart = this.viewParts[i];
A
Alex Dima 已提交
498 499 500 501 502 503 504 505 506 507 508
				viewPart.forceShouldRender();
			}
		}
		if (now) {
			this._flushAccumulatedAndRenderNow();
		} else {
			this._scheduleRender();
		}
	}

	public focus(): void {
509
		this._textAreaHandler.focusTextArea();
A
Alex Dima 已提交
510 511 512
	}

	public isFocused(): boolean {
513
		return this._textAreaHandler.isFocused();
A
Alex Dima 已提交
514 515
	}

516 517 518 519
	public refreshFocusState() {
		this._textAreaHandler.refreshFocusState();
	}

I
isidor 已提交
520 521
	public setAriaOptions(options: IEditorAriaOptions): void {
		this._textAreaHandler.setAriaOptions(options);
I
isidor 已提交
522 523
	}

A
Alex Dima 已提交
524
	public addContentWidget(widgetData: IContentWidgetData): void {
A
Alex Dima 已提交
525 526 527
		this.contentWidgets.addWidget(widgetData.widget);
		this.layoutContentWidget(widgetData);
		this._scheduleRender();
E
Erich Gamma 已提交
528 529
	}

A
Alex Dima 已提交
530
	public layoutContentWidget(widgetData: IContentWidgetData): void {
531 532 533 534 535 536 537
		let newRange = widgetData.position ? widgetData.position.range || null : null;
		if (newRange === null) {
			const newPosition = widgetData.position ? widgetData.position.position : null;
			if (newPosition !== null) {
				newRange = new Range(newPosition.lineNumber, newPosition.column, newPosition.lineNumber, newPosition.column);
			}
		}
M
Matt Bierner 已提交
538
		const newPreference = widgetData.position ? widgetData.position.preference : null;
539
		this.contentWidgets.setWidgetPosition(widgetData.widget, newRange, newPreference);
A
Alex Dima 已提交
540
		this._scheduleRender();
E
Erich Gamma 已提交
541 542
	}

A
Alex Dima 已提交
543
	public removeContentWidget(widgetData: IContentWidgetData): void {
A
Alex Dima 已提交
544 545
		this.contentWidgets.removeWidget(widgetData.widget);
		this._scheduleRender();
E
Erich Gamma 已提交
546 547
	}

A
Alex Dima 已提交
548
	public addOverlayWidget(widgetData: IOverlayWidgetData): void {
A
Alex Dima 已提交
549 550 551
		this.overlayWidgets.addWidget(widgetData.widget);
		this.layoutOverlayWidget(widgetData);
		this._scheduleRender();
E
Erich Gamma 已提交
552 553
	}

A
Alex Dima 已提交
554
	public layoutOverlayWidget(widgetData: IOverlayWidgetData): void {
M
Matt Bierner 已提交
555 556
		const newPreference = widgetData.position ? widgetData.position.preference : null;
		const shouldRender = this.overlayWidgets.setWidgetPosition(widgetData.widget, newPreference);
557 558 559
		if (shouldRender) {
			this._scheduleRender();
		}
E
Erich Gamma 已提交
560 561
	}

A
Alex Dima 已提交
562
	public removeOverlayWidget(widgetData: IOverlayWidgetData): void {
A
Alex Dima 已提交
563 564
		this.overlayWidgets.removeWidget(widgetData.widget);
		this._scheduleRender();
E
Erich Gamma 已提交
565 566
	}

A
Alex Dima 已提交
567
	// --- END CodeEditor helpers
E
Erich Gamma 已提交
568 569 570

}

J
Johannes Rieken 已提交
571
function safeInvokeNoArg(func: Function): any {
A
Alex Dima 已提交
572 573
	try {
		return func();
J
Johannes Rieken 已提交
574
	} catch (e) {
A
Alex Dima 已提交
575 576 577
		onUnexpectedError(e);
	}
}