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

E
Erich Gamma 已提交
53

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

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

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

66
	private _scrollbar: EditorScrollbar;
67
	private readonly _context: ViewContext;
68
	private _selections: Selection[];
E
Erich Gamma 已提交
69 70 71 72 73 74 75 76

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

80 81
	private readonly _textAreaHandler: TextAreaHandler;
	private readonly pointerHandler: PointerHandler;
E
Erich Gamma 已提交
82

83
	private readonly outgoingEvents: ViewOutgoingEvents;
E
Erich Gamma 已提交
84 85

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

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

A
Alex Dima 已提交
93
	constructor(
94
		commandDelegate: ICommandDelegate,
A
Alex Dima 已提交
95
		configuration: IConfiguration,
96
		themeService: IThemeService,
J
Johannes Rieken 已提交
97
		model: IViewModel,
A
Alex Dima 已提交
98
		outgoingEvents: ViewOutgoingEvents
A
Alex Dima 已提交
99
	) {
E
Erich Gamma 已提交
100
		super();
101
		this._selections = [new Selection(1, 1, 1, 1)];
E
Erich Gamma 已提交
102
		this._renderAnimationFrame = null;
A
Alex Dima 已提交
103
		this.outgoingEvents = outgoingEvents;
E
Erich Gamma 已提交
104

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

A
Alex Dima 已提交
107 108
		// The view context is passed on to most classes (basically to reduce param. counts in ctors)
		this._context = new ViewContext(configuration, themeService.getColorTheme(), model);
E
Erich Gamma 已提交
109

110
		// Ensure the view is the first event handler in order to update the layout
A
Alex Dima 已提交
111
		this._context.addEventHandler(this);
112

M
Martin Aeschlimann 已提交
113
		this._register(themeService.onDidColorThemeChange(theme => {
A
Alex Dima 已提交
114
			this._context.theme.update(theme);
A
Alex Dima 已提交
115
			this._context.model.onDidColorThemeChange();
116
			this.render(true, false);
117
		}));
E
Erich Gamma 已提交
118

119 120
		this.viewParts = [];

121 122
		// Keyboard handler
		this._textAreaHandler = new TextAreaHandler(this._context, viewController, this.createTextAreaHandlerHelper());
123
		this.viewParts.push(this._textAreaHandler);
124

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

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

A
Alex Dima 已提交
135
		this.overflowGuardContainer = createFastDomNode(document.createElement('div'));
A
Alex Dima 已提交
136
		PartFingerprints.write(this.overflowGuardContainer, PartFingerprint.OverflowGuard);
A
Alex Dima 已提交
137
		this.overflowGuardContainer.setClassName('overflow-guard');
A
Alex Dima 已提交
138

139
		this._scrollbar = new EditorScrollbar(this._context, this.linesContent, this.domNode, this.overflowGuardContainer);
A
Alex Dima 已提交
140 141
		this.viewParts.push(this._scrollbar);

E
Erich Gamma 已提交
142
		// View Lines
143
		this.viewLines = new ViewLines(this._context, this.linesContent);
E
Erich Gamma 已提交
144 145

		// View Zones
146
		this.viewZones = new ViewZones(this._context);
E
Erich Gamma 已提交
147 148 149
		this.viewParts.push(this.viewZones);

		// Decorations overview ruler
M
Matt Bierner 已提交
150
		const decorationsOverviewRuler = new DecorationsOverviewRuler(this._context);
E
Erich Gamma 已提交
151 152 153
		this.viewParts.push(decorationsOverviewRuler);


M
Matt Bierner 已提交
154
		const scrollDecoration = new ScrollDecorationViewPart(this._context);
E
Erich Gamma 已提交
155 156
		this.viewParts.push(scrollDecoration);

M
Matt Bierner 已提交
157
		const contentViewOverlays = new ContentViewOverlays(this._context);
E
Erich Gamma 已提交
158
		this.viewParts.push(contentViewOverlays);
A
Alex Dima 已提交
159
		contentViewOverlays.addDynamicOverlay(new CurrentLineHighlightOverlay(this._context));
160
		contentViewOverlays.addDynamicOverlay(new SelectionsOverlay(this._context));
161
		contentViewOverlays.addDynamicOverlay(new IndentGuidesOverlay(this._context));
162
		contentViewOverlays.addDynamicOverlay(new DecorationsOverlay(this._context));
E
Erich Gamma 已提交
163

M
Matt Bierner 已提交
164
		const marginViewOverlays = new MarginViewOverlays(this._context);
E
Erich Gamma 已提交
165
		this.viewParts.push(marginViewOverlays);
A
Alex Dima 已提交
166
		marginViewOverlays.addDynamicOverlay(new CurrentLineMarginHighlightOverlay(this._context));
167
		marginViewOverlays.addDynamicOverlay(new GlyphMarginOverlay(this._context));
168
		marginViewOverlays.addDynamicOverlay(new MarginViewLineDecorationsOverlay(this._context));
169 170
		marginViewOverlays.addDynamicOverlay(new LinesDecorationsOverlay(this._context));
		marginViewOverlays.addDynamicOverlay(new LineNumbersOverlay(this._context));
E
Erich Gamma 已提交
171

M
Matt Bierner 已提交
172
		const margin = new Margin(this._context);
A
Alex Dima 已提交
173
		margin.getDomNode().appendChild(this.viewZones.marginDomNode);
A
Alex Dima 已提交
174
		margin.getDomNode().appendChild(marginViewOverlays.getDomNode());
175
		this.viewParts.push(margin);
E
Erich Gamma 已提交
176 177

		// Content widgets
178
		this.contentWidgets = new ViewContentWidgets(this._context, this.domNode);
E
Erich Gamma 已提交
179 180
		this.viewParts.push(this.contentWidgets);

181 182
		this.viewCursors = new ViewCursors(this._context);
		this.viewParts.push(this.viewCursors);
E
Erich Gamma 已提交
183 184

		// Overlay widgets
185
		this.overlayWidgets = new ViewOverlayWidgets(this._context);
E
Erich Gamma 已提交
186 187
		this.viewParts.push(this.overlayWidgets);

M
Matt Bierner 已提交
188
		const rulers = new Rulers(this._context);
189 190
		this.viewParts.push(rulers);

M
Matt Bierner 已提交
191
		const minimap = new Minimap(this._context);
A
Alex Dima 已提交
192 193
		this.viewParts.push(minimap);

E
Erich Gamma 已提交
194 195 196
		// -------------- Wire dom nodes up

		if (decorationsOverviewRuler) {
M
Matt Bierner 已提交
197
			const overviewRulerData = this._scrollbar.getOverviewRulerLayoutInfo();
E
Erich Gamma 已提交
198 199 200
			overviewRulerData.parent.insertBefore(decorationsOverviewRuler.getDomNode(), overviewRulerData.insertBefore);
		}

A
Alex Dima 已提交
201 202 203 204 205 206 207 208 209
		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());
210 211
		this.overflowGuardContainer.appendChild(this._textAreaHandler.textArea);
		this.overflowGuardContainer.appendChild(this._textAreaHandler.textAreaCover);
212
		this.overflowGuardContainer.appendChild(this.overlayWidgets.getDomNode());
A
Alex Dima 已提交
213 214 215
		this.overflowGuardContainer.appendChild(minimap.getDomNode());
		this.domNode.appendChild(this.overflowGuardContainer);
		this.domNode.appendChild(this.contentWidgets.overflowingContentWidgetsDomNode);
A
Alex Dima 已提交
216

A
Alex Dima 已提交
217
		this._applyLayout();
A
Alex Dima 已提交
218 219 220

		// Pointer handler
		this.pointerHandler = this._register(new PointerHandler(this._context, viewController, this.createPointerHandlerHelper()));
E
Erich Gamma 已提交
221 222 223 224 225 226
	}

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

227
	private createPointerHandlerHelper(): IPointerHandlerHelper {
E
Erich Gamma 已提交
228
		return {
A
Alex Dima 已提交
229 230
			viewDomNode: this.domNode.domNode,
			linesContentDomNode: this.linesContent.domNode,
E
Erich Gamma 已提交
231 232 233 234 235

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

236 237 238 239
			getLastRenderData: (): PointerHandlerLastRenderData => {
				const lastViewCursorsRenderData = this.viewCursors.getLastRenderData() || [];
				const lastTextareaPosition = this._textAreaHandler.getLastRenderData();
				return new PointerHandlerLastRenderData(lastViewCursorsRenderData, lastTextareaPosition);
240
			},
A
Alex Dima 已提交
241
			shouldSuppressMouseDownOnViewZone: (viewZoneId: string) => {
E
Erich Gamma 已提交
242 243
				return this.viewZones.shouldSuppressMouseDownOnViewZone(viewZoneId);
			},
244 245 246
			shouldSuppressMouseDownOnWidget: (widgetId: string) => {
				return this.contentWidgets.shouldSuppressMouseDownOnWidget(widgetId);
			},
E
Erich Gamma 已提交
247 248 249 250 251
			getPositionFromDOMInfo: (spanNode: HTMLElement, offset: number) => {
				this._flushAccumulatedAndRenderNow();
				return this.viewLines.getPositionFromDOMInfo(spanNode, offset);
			},

252
			visibleRangeForPosition: (lineNumber: number, column: number) => {
E
Erich Gamma 已提交
253
				this._flushAccumulatedAndRenderNow();
A
Alex Dima 已提交
254
				return this.viewLines.visibleRangeForPosition(new Position(lineNumber, column));
E
Erich Gamma 已提交
255 256 257 258 259 260 261 262 263
			},

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

264
	private createTextAreaHandlerHelper(): ITextAreaHandlerHelper {
E
Erich Gamma 已提交
265 266 267
		return {
			visibleRangeForPositionRelativeToEditor: (lineNumber: number, column: number) => {
				this._flushAccumulatedAndRenderNow();
A
Alex Dima 已提交
268
				return this.viewLines.visibleRangeForPosition(new Position(lineNumber, column));
E
Erich Gamma 已提交
269 270 271 272
			}
		};
	}

A
Alex Dima 已提交
273
	private _applyLayout(): void {
A
Alex Dima 已提交
274
		const options = this._context.configuration.options;
A
renames  
Alex Dima 已提交
275
		const layoutInfo = options.get(EditorOption.layoutInfo);
A
Alex Dima 已提交
276

A
Alex Dima 已提交
277 278
		this.domNode.setWidth(layoutInfo.width);
		this.domNode.setHeight(layoutInfo.height);
E
Erich Gamma 已提交
279

A
Alex Dima 已提交
280 281
		this.overflowGuardContainer.setWidth(layoutInfo.width);
		this.overflowGuardContainer.setHeight(layoutInfo.height);
E
Erich Gamma 已提交
282

A
Alex Dima 已提交
283 284
		this.linesContent.setWidth(1000000);
		this.linesContent.setHeight(1000000);
E
Erich Gamma 已提交
285
	}
286

287
	private getEditorClassName() {
M
Matt Bierner 已提交
288
		const focused = this._textAreaHandler.isFocused() ? ' focused' : '';
A
Alex Dima 已提交
289
		return this._context.configuration.options.get(EditorOption.editorClassName) + ' ' + getThemeTypeSelector(this._context.theme.type) + focused;
290 291
	}

292
	// --- begin event handlers
A
Alex Dima 已提交
293 294 295 296
	public handleEvents(events: viewEvents.ViewEvent[]): void {
		super.handleEvents(events);
		this._scheduleRender();
	}
A
Alex Dima 已提交
297
	public onConfigurationChanged(e: viewEvents.ViewConfigurationChangedEvent): boolean {
A
Alex Dima 已提交
298 299
		this.domNode.setClassName(this.getEditorClassName());
		this._applyLayout();
300
		return false;
E
Erich Gamma 已提交
301
	}
302 303 304 305
	public onCursorStateChanged(e: viewEvents.ViewCursorStateChangedEvent): boolean {
		this._selections = e.selections;
		return false;
	}
A
Alex Dima 已提交
306
	public onFocusChanged(e: viewEvents.ViewFocusChangedEvent): boolean {
S
smoke:  
Sandeep Somavarapu 已提交
307
		this.domNode.setClassName(this.getEditorClassName());
308 309
		return false;
	}
310 311 312 313
	public onThemeChanged(e: viewEvents.ViewThemeChangedEvent): boolean {
		this.domNode.setClassName(this.getEditorClassName());
		return false;
	}
314

E
Erich Gamma 已提交
315 316 317 318 319 320 321 322
	// --- end event handlers

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

A
Alex Dima 已提交
323
		this._context.removeEventHandler(this);
A
Alex Dima 已提交
324
		this.outgoingEvents.dispose();
E
Erich Gamma 已提交
325 326 327

		this.viewLines.dispose();

A
Alex Dima 已提交
328
		// Destroy view parts
A
Alex Dima 已提交
329
		for (let i = 0, len = this.viewParts.length; i < len; i++) {
E
Erich Gamma 已提交
330 331 332 333
			this.viewParts[i].dispose();
		}
		this.viewParts = [];

A
Alex Dima 已提交
334
		super.dispose();
E
Erich Gamma 已提交
335 336
	}

A
Alex Dima 已提交
337 338 339 340 341
	private _scheduleRender(): void {
		if (this._renderAnimationFrame === null) {
			this._renderAnimationFrame = dom.runAtThisOrScheduleAtNextAnimationFrame(this._onRenderScheduled.bind(this), 100);
		}
	}
A
Alex Dima 已提交
342

A
Alex Dima 已提交
343 344 345 346
	private _onRenderScheduled(): void {
		this._renderAnimationFrame = null;
		this._flushAccumulatedAndRenderNow();
	}
347

A
Alex Dima 已提交
348 349 350
	private _renderNow(): void {
		safeInvokeNoArg(() => this._actualRender());
	}
A
Alex Dima 已提交
351

A
Alex Dima 已提交
352
	private _getViewPartsToRender(): ViewPart[] {
A
Alex Dima 已提交
353
		let result: ViewPart[] = [], resultLen = 0;
A
Alex Dima 已提交
354
		for (let i = 0, len = this.viewParts.length; i < len; i++) {
M
Matt Bierner 已提交
355
			const viewPart = this.viewParts[i];
A
Alex Dima 已提交
356
			if (viewPart.shouldRender()) {
A
Alex Dima 已提交
357
				result[resultLen++] = viewPart;
A
Alex Dima 已提交
358 359 360 361
			}
		}
		return result;
	}
362

A
Alex Dima 已提交
363 364 365 366
	private _actualRender(): void {
		if (!dom.isInDOM(this.domNode.domNode)) {
			return;
		}
367

A
Alex Dima 已提交
368
		let viewPartsToRender = this._getViewPartsToRender();
A
Alex Dima 已提交
369

A
Alex Dima 已提交
370 371 372 373
		if (!this.viewLines.shouldRender() && viewPartsToRender.length === 0) {
			// Nothing to render
			return;
		}
A
Alex Dima 已提交
374

375
		const partialViewportData = this._context.viewLayout.getLinesViewportData();
A
Alex Dima 已提交
376
		this._context.model.setViewport(partialViewportData.startLineNumber, partialViewportData.endLineNumber, partialViewportData.centeredLineNumber);
377

M
Matt Bierner 已提交
378
		const viewportData = new ViewportData(
379
			this._selections,
380 381 382 383
			partialViewportData,
			this._context.viewLayout.getWhitespaceViewportData(),
			this._context.model
		);
A
Alex Dima 已提交
384

385 386 387 388 389
		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 已提交
390
		if (this.viewLines.shouldRender()) {
391
			this.viewLines.renderText(viewportData);
A
Alex Dima 已提交
392
			this.viewLines.onDidRender();
A
Alex Dima 已提交
393

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

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

A
Alex Dima 已提交
400 401
		// Render the rest of the parts
		for (let i = 0, len = viewPartsToRender.length; i < len; i++) {
M
Matt Bierner 已提交
402
			const viewPart = viewPartsToRender[i];
A
Alex Dima 已提交
403 404
			viewPart.prepareRender(renderingContext);
		}
405

A
Alex Dima 已提交
406
		for (let i = 0, len = viewPartsToRender.length; i < len; i++) {
M
Matt Bierner 已提交
407
			const viewPart = viewPartsToRender[i];
A
Alex Dima 已提交
408 409
			viewPart.render(renderingContext);
			viewPart.onDidRender();
E
Erich Gamma 已提交
410 411 412
		}
	}

A
Alex Dima 已提交
413 414
	// --- BEGIN CodeEditor helpers

415
	public delegateVerticalScrollbarMouseDown(browserEvent: IMouseEvent): void {
A
Alex Dima 已提交
416 417 418
		this._scrollbar.delegateVerticalScrollbarMouseDown(browserEvent);
	}

419
	public restoreState(scrollPosition: { scrollLeft: number; scrollTop: number; }): void {
420
		this._context.model.setScrollPosition({ scrollTop: scrollPosition.scrollTop }, ScrollType.Immediate);
421
		this._context.model.tokenizeViewport();
422 423
		this._renderNow();
		this.viewLines.updateLineWidths();
424
		this._context.model.setScrollPosition({ scrollLeft: scrollPosition.scrollLeft }, ScrollType.Immediate);
425 426
	}

A
Alex Dima 已提交
427
	public getOffsetForColumn(modelLineNumber: number, modelColumn: number): number {
M
Matt Bierner 已提交
428
		const modelPosition = this._context.model.validateModelPosition({
A
Alex Dima 已提交
429 430 431
			lineNumber: modelLineNumber,
			column: modelColumn
		});
M
Matt Bierner 已提交
432
		const viewPosition = this._context.model.coordinatesConverter.convertModelPositionToViewPosition(modelPosition);
A
Alex Dima 已提交
433
		this._flushAccumulatedAndRenderNow();
A
Alex Dima 已提交
434 435
		const visibleRange = this.viewLines.visibleRangeForPosition(new Position(viewPosition.lineNumber, viewPosition.column));
		if (!visibleRange) {
A
Alex Dima 已提交
436
			return -1;
E
Erich Gamma 已提交
437
		}
A
Alex Dima 已提交
438
		return visibleRange.left;
A
Alex Dima 已提交
439 440
	}

441
	public getTargetAtClientPoint(clientX: number, clientY: number): IMouseTarget | null {
442 443 444 445 446
		const mouseTarget = this.pointerHandler.getTargetAtClientPoint(clientX, clientY);
		if (!mouseTarget) {
			return null;
		}
		return ViewOutgoingEvents.convertViewToModelMouseTarget(mouseTarget, this._context.model.coordinatesConverter);
A
Alex Dima 已提交
447 448
	}

449 450
	public createOverviewRuler(cssClassName: string): OverviewRuler {
		return new OverviewRuler(this._context, cssClassName);
E
Erich Gamma 已提交
451 452
	}

453
	public change(callback: (changeAccessor: IViewZoneChangeAccessor) => any): boolean {
A
Alex Dima 已提交
454 455 456
		const hadAChange = this.viewZones.changeViewZones(callback);
		this._scheduleRender();
		return hadAChange;
E
Erich Gamma 已提交
457 458
	}

A
Alex Dima 已提交
459 460 461 462 463
	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 已提交
464
				const viewPart = this.viewParts[i];
A
Alex Dima 已提交
465 466 467 468 469 470 471 472 473 474 475
				viewPart.forceShouldRender();
			}
		}
		if (now) {
			this._flushAccumulatedAndRenderNow();
		} else {
			this._scheduleRender();
		}
	}

	public focus(): void {
476
		this._textAreaHandler.focusTextArea();
A
Alex Dima 已提交
477 478 479
	}

	public isFocused(): boolean {
480
		return this._textAreaHandler.isFocused();
A
Alex Dima 已提交
481 482
	}

483 484 485 486
	public refreshFocusState() {
		this._textAreaHandler.refreshFocusState();
	}

I
isidor 已提交
487 488
	public setAriaOptions(options: IEditorAriaOptions): void {
		this._textAreaHandler.setAriaOptions(options);
I
isidor 已提交
489 490
	}

A
Alex Dima 已提交
491
	public addContentWidget(widgetData: IContentWidgetData): void {
A
Alex Dima 已提交
492 493 494
		this.contentWidgets.addWidget(widgetData.widget);
		this.layoutContentWidget(widgetData);
		this._scheduleRender();
E
Erich Gamma 已提交
495 496
	}

A
Alex Dima 已提交
497
	public layoutContentWidget(widgetData: IContentWidgetData): void {
498 499 500 501 502 503 504
		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 已提交
505
		const newPreference = widgetData.position ? widgetData.position.preference : null;
506
		this.contentWidgets.setWidgetPosition(widgetData.widget, newRange, newPreference);
A
Alex Dima 已提交
507
		this._scheduleRender();
E
Erich Gamma 已提交
508 509
	}

A
Alex Dima 已提交
510
	public removeContentWidget(widgetData: IContentWidgetData): void {
A
Alex Dima 已提交
511 512
		this.contentWidgets.removeWidget(widgetData.widget);
		this._scheduleRender();
E
Erich Gamma 已提交
513 514
	}

A
Alex Dima 已提交
515
	public addOverlayWidget(widgetData: IOverlayWidgetData): void {
A
Alex Dima 已提交
516 517 518
		this.overlayWidgets.addWidget(widgetData.widget);
		this.layoutOverlayWidget(widgetData);
		this._scheduleRender();
E
Erich Gamma 已提交
519 520
	}

A
Alex Dima 已提交
521
	public layoutOverlayWidget(widgetData: IOverlayWidgetData): void {
M
Matt Bierner 已提交
522 523
		const newPreference = widgetData.position ? widgetData.position.preference : null;
		const shouldRender = this.overlayWidgets.setWidgetPosition(widgetData.widget, newPreference);
524 525 526
		if (shouldRender) {
			this._scheduleRender();
		}
E
Erich Gamma 已提交
527 528
	}

A
Alex Dima 已提交
529
	public removeOverlayWidget(widgetData: IOverlayWidgetData): void {
A
Alex Dima 已提交
530 531
		this.overlayWidgets.removeWidget(widgetData.widget);
		this._scheduleRender();
E
Erich Gamma 已提交
532 533
	}

A
Alex Dima 已提交
534
	// --- END CodeEditor helpers
E
Erich Gamma 已提交
535 536 537

}

J
Johannes Rieken 已提交
538
function safeInvokeNoArg(func: Function): any {
A
Alex Dima 已提交
539 540
	try {
		return func();
J
Johannes Rieken 已提交
541
	} catch (e) {
A
Alex Dima 已提交
542 543 544
		onUnexpectedError(e);
	}
}