viewLine.ts 12.3 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';

A
Alex Dima 已提交
7
import * as browser from 'vs/base/browser/browser';
A
Alex Dima 已提交
8
import {FastDomNode, createFastDomNode} from 'vs/base/browser/styleMutator';
9
import {IConfigurationChangedEvent} from 'vs/editor/common/editorCommon';
A
Alex Dima 已提交
10
import {LineParts, createLineParts, getColumnOfLinePartOffset} from 'vs/editor/common/viewLayout/viewLineParts';
A
Alex Dima 已提交
11
import {renderLine, RenderLineInput} from 'vs/editor/common/viewLayout/viewLineRenderer';
A
Alex Dima 已提交
12
import {ClassNames} from 'vs/editor/browser/editorBrowser';
A
Alex Dima 已提交
13
import {IVisibleLineData} from 'vs/editor/browser/view/viewLayer';
A
Alex Dima 已提交
14
import {RangeUtil} from 'vs/editor/browser/viewParts/lines/rangeUtil';
A
Alex Dima 已提交
15
import {ViewContext} from 'vs/editor/common/view/viewContext';
16
import {HorizontalRange} from 'vs/editor/common/view/renderingContext';
17
import {InlineDecoration} from 'vs/editor/common/viewModel/viewModel';
E
Erich Gamma 已提交
18

A
Alex Dima 已提交
19
export class ViewLine implements IVisibleLineData {
E
Erich Gamma 已提交
20

A
Alex Dima 已提交
21
	protected _context:ViewContext;
A
Alex Dima 已提交
22
	private _renderWhitespace: boolean;
23
	private _indentGuides: boolean;
24
	private _spaceWidth: number;
A
Alex Dima 已提交
25 26 27
	private _lineHeight: number;
	private _stopRenderingLineAfter: number;

A
Alex Dima 已提交
28
	private _domNode: FastDomNode;
E
Erich Gamma 已提交
29

A
Alex Dima 已提交
30
	private _lineParts: LineParts;
E
Erich Gamma 已提交
31 32 33 34

	private _isInvalid: boolean;
	private _isMaybeInvalid: boolean;

A
Alex Dima 已提交
35
	protected _charOffsetInPart:number[];
E
Erich Gamma 已提交
36 37 38
	private _lastRenderedPartIndex:number;
	private _cachedWidth: number;

A
Alex Dima 已提交
39
	constructor(context:ViewContext) {
E
Erich Gamma 已提交
40
		this._context = context;
41 42
		this._renderWhitespace = this._context.configuration.editor.viewInfo.renderWhitespace;
		this._indentGuides = this._context.configuration.editor.viewInfo.indentGuides;
43
		this._spaceWidth = this._context.configuration.editor.fontInfo.spaceWidth;
A
Alex Dima 已提交
44
		this._lineHeight = this._context.configuration.editor.lineHeight;
45
		this._stopRenderingLineAfter = this._context.configuration.editor.viewInfo.stopRenderingLineAfter;
A
Alex Dima 已提交
46

E
Erich Gamma 已提交
47 48 49 50 51 52 53 54
		this._domNode = null;
		this._isInvalid = true;
		this._isMaybeInvalid = false;
		this._lineParts = null;
		this._charOffsetInPart = [];
		this._lastRenderedPartIndex = 0;
	}

A
Alex Dima 已提交
55 56
	// --- begin IVisibleLineData

E
Erich Gamma 已提交
57
	public getDomNode(): HTMLElement {
A
Alex Dima 已提交
58 59 60 61
		if (!this._domNode) {
			return null;
		}
		return this._domNode.domNode;
E
Erich Gamma 已提交
62 63
	}
	public setDomNode(domNode:HTMLElement): void {
A
Alex Dima 已提交
64
		this._domNode = createFastDomNode(domNode);
E
Erich Gamma 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	}

	public onContentChanged(): void {
		this._isInvalid = true;
	}
	public onLinesInsertedAbove(): void {
		this._isMaybeInvalid = true;
	}
	public onLinesDeletedAbove(): void {
		this._isMaybeInvalid = true;
	}
	public onLineChangedAbove(): void {
		this._isMaybeInvalid = true;
	}
	public onTokensChanged(): void {
		this._isMaybeInvalid = true;
	}
	public onModelDecorationsChanged(): void {
		this._isMaybeInvalid = true;
	}
A
Alex Dima 已提交
85
	public onConfigurationChanged(e:IConfigurationChangedEvent): void {
86 87
		if (e.viewInfo.renderWhitespace) {
			this._renderWhitespace = this._context.configuration.editor.viewInfo.renderWhitespace;
A
Alex Dima 已提交
88
		}
89 90
		if (e.viewInfo.indentGuides) {
			this._indentGuides = this._context.configuration.editor.viewInfo.indentGuides;
91
		}
92 93
		if (e.fontInfo) {
			this._spaceWidth = this._context.configuration.editor.fontInfo.spaceWidth;
94
		}
A
Alex Dima 已提交
95 96 97
		if (e.lineHeight) {
			this._lineHeight = this._context.configuration.editor.lineHeight;
		}
98 99
		if (e.viewInfo.stopRenderingLineAfter) {
			this._stopRenderingLineAfter = this._context.configuration.editor.viewInfo.stopRenderingLineAfter;
A
Alex Dima 已提交
100
		}
E
Erich Gamma 已提交
101 102 103
		this._isInvalid = true;
	}

104
	public shouldUpdateHTML(startLineNumber:number, lineNumber:number, inlineDecorations:InlineDecoration[]): boolean {
A
Alex Dima 已提交
105
		let newLineParts:LineParts = null;
E
Erich Gamma 已提交
106 107 108

		if (this._isMaybeInvalid || this._isInvalid) {
			// Compute new line parts only if there is some evidence that something might have changed
A
Alex Dima 已提交
109 110
			newLineParts = createLineParts(
				lineNumber,
111
				this._context.model.getLineMinColumn(lineNumber),
A
Alex Dima 已提交
112
				this._context.model.getLineContent(lineNumber),
113
				this._context.model.getTabSize(),
A
Alex Dima 已提交
114 115
				this._context.model.getLineTokens(lineNumber),
				inlineDecorations,
116 117
				this._renderWhitespace,
				this._indentGuides
A
Alex Dima 已提交
118
			);
E
Erich Gamma 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
		}

		// Decide if isMaybeInvalid flips isInvalid to true
		if (this._isMaybeInvalid) {
			if (!this._isInvalid) {
				if (!this._lineParts || !this._lineParts.equals(newLineParts)) {
					this._isInvalid = true;
				}
			}
			this._isMaybeInvalid = false;
		}

		if (this._isInvalid) {
			this._lineParts = newLineParts;
		}

		return this._isInvalid;
	}

	public getLineOuterHTML(out:string[], lineNumber:number, deltaTop:number): void {
		out.push('<div lineNumber="');
		out.push(lineNumber.toString());
		out.push('" style="top:');
		out.push(deltaTop.toString());
		out.push('px;height:');
A
Alex Dima 已提交
144
		out.push(this._lineHeight.toString());
E
Erich Gamma 已提交
145
		out.push('px;" class="');
A
Alex Dima 已提交
146
		out.push(ClassNames.VIEW_LINE);
E
Erich Gamma 已提交
147 148 149 150 151 152 153
		out.push('">');
		out.push(this.getLineInnerHTML(lineNumber));
		out.push('</div>');
	}

	public getLineInnerHTML(lineNumber: number): string {
		this._isInvalid = false;
A
Alex Dima 已提交
154
		return this._render(lineNumber, this._lineParts);
E
Erich Gamma 已提交
155 156 157
	}

	public layoutLine(lineNumber:number, deltaTop:number): void {
A
Alex Dima 已提交
158 159
		this._domNode.setLineNumber(String(lineNumber));
		this._domNode.setTop(deltaTop);
A
Alex Dima 已提交
160
		this._domNode.setHeight(this._lineHeight);
E
Erich Gamma 已提交
161 162
	}

A
Alex Dima 已提交
163 164
	// --- end IVisibleLineData

A
Alex Dima 已提交
165
	private _render(lineNumber:number, lineParts:LineParts): string {
E
Erich Gamma 已提交
166

A
Alex Dima 已提交
167
		this._cachedWidth = -1;
E
Erich Gamma 已提交
168

A
Alex Dima 已提交
169 170 171
		let r = renderLine(new RenderLineInput(
			this._context.model.getLineContent(lineNumber),
			this._context.model.getTabSize(),
172
			this._spaceWidth,
A
Alex Dima 已提交
173 174
			this._stopRenderingLineAfter,
			this._renderWhitespace,
A
Alex Dima 已提交
175 176
			lineParts.getParts()
		));
E
Erich Gamma 已提交
177 178 179 180 181 182 183 184 185

		this._charOffsetInPart = r.charOffsetInPart;
		this._lastRenderedPartIndex = r.lastRenderedPartIndex;

		return r.output;
	}

	// --- Reading from the DOM methods

186
	protected _getReadingTarget(): HTMLElement {
A
Alex Dima 已提交
187
		return <HTMLSpanElement>this._domNode.domNode.firstChild;
E
Erich Gamma 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
	}

	/**
	 * Width of the line in pixels
	 */
	public getWidth(): number {
		if (this._cachedWidth === -1) {
			this._cachedWidth = this._getReadingTarget().offsetWidth;
		}
		return this._cachedWidth;
	}

	/**
	 * Visible ranges for a model range
	 */
203
	public getVisibleRangesForRange(startColumn:number, endColumn:number, clientRectDeltaLeft:number, endNode:HTMLElement): HorizontalRange[] {
204 205 206 207
		startColumn = startColumn|0; // @perf
		endColumn = endColumn|0; // @perf
		clientRectDeltaLeft = clientRectDeltaLeft|0; // @perf
		const stopRenderingLineAfter = this._stopRenderingLineAfter|0; // @perf
E
Erich Gamma 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221

		if (stopRenderingLineAfter !== -1 && startColumn > stopRenderingLineAfter && endColumn > stopRenderingLineAfter) {
			// This range is obviously not visible
			return null;
		}

		if (stopRenderingLineAfter !== -1 && startColumn > stopRenderingLineAfter) {
			startColumn = stopRenderingLineAfter;
		}

		if (stopRenderingLineAfter !== -1 && endColumn > stopRenderingLineAfter) {
			endColumn = stopRenderingLineAfter;
		}

222
		return this._readVisibleRangesForRange(startColumn, endColumn, clientRectDeltaLeft, endNode);
E
Erich Gamma 已提交
223 224
	}

225
	protected _readVisibleRangesForRange(startColumn:number, endColumn:number, clientRectDeltaLeft:number, endNode:HTMLElement): HorizontalRange[] {
E
Erich Gamma 已提交
226
		if (startColumn === endColumn) {
227
			return this._readRawVisibleRangesForPosition(startColumn, clientRectDeltaLeft, endNode);
E
Erich Gamma 已提交
228
		} else {
229
			return this._readRawVisibleRangesForRange(startColumn, endColumn, clientRectDeltaLeft, endNode);
E
Erich Gamma 已提交
230 231 232
		}
	}

233
	protected _readRawVisibleRangesForPosition(column:number, clientRectDeltaLeft:number, endNode:HTMLElement): HorizontalRange[] {
E
Erich Gamma 已提交
234 235 236

		if (this._charOffsetInPart.length === 0) {
			// This line is empty
A
Alex Dima 已提交
237
			return [new HorizontalRange(0, 0)];
E
Erich Gamma 已提交
238 239
		}

A
Alex Dima 已提交
240 241
		let partIndex = findIndexInArrayWithMax(this._lineParts, column - 1, this._lastRenderedPartIndex);
		let charOffsetInPart = this._charOffsetInPart[column - 1];
E
Erich Gamma 已提交
242

243
		return RangeUtil.readHorizontalRanges(this._getReadingTarget(), partIndex, charOffsetInPart, partIndex, charOffsetInPart, clientRectDeltaLeft, this._getScaleRatio(), endNode);
E
Erich Gamma 已提交
244 245
	}

246
	private _readRawVisibleRangesForRange(startColumn:number, endColumn:number, clientRectDeltaLeft:number, endNode:HTMLElement): HorizontalRange[] {
E
Erich Gamma 已提交
247 248 249 250

		if (startColumn === 1 && endColumn === this._charOffsetInPart.length) {
			// This branch helps IE with bidi text & gives a performance boost to other browsers when reading visible ranges for an entire line

A
Alex Dima 已提交
251
			return [new HorizontalRange(0, this.getWidth())];
E
Erich Gamma 已提交
252 253
		}

A
Alex Dima 已提交
254 255 256 257
		let startPartIndex = findIndexInArrayWithMax(this._lineParts, startColumn - 1, this._lastRenderedPartIndex);
		let startCharOffsetInPart = this._charOffsetInPart[startColumn - 1];
		let endPartIndex = findIndexInArrayWithMax(this._lineParts, endColumn - 1, this._lastRenderedPartIndex);
		let endCharOffsetInPart = this._charOffsetInPart[endColumn - 1];
E
Erich Gamma 已提交
258

259
		return RangeUtil.readHorizontalRanges(this._getReadingTarget(), startPartIndex, startCharOffsetInPart, endPartIndex, endCharOffsetInPart, clientRectDeltaLeft, this._getScaleRatio(), endNode);
E
Erich Gamma 已提交
260 261
	}

262 263
	protected _getScaleRatio(): number {
		return 1;
E
Erich Gamma 已提交
264 265
	}

A
Alex Dima 已提交
266 267 268
	/**
	 * Returns the column for the text found at a specific offset inside a rendered dom node
	 */
E
Erich Gamma 已提交
269
	public getColumnOfNodeOffset(lineNumber:number, spanNode:HTMLElement, offset:number): number {
A
Alex Dima 已提交
270 271
		let spanNodeTextContentLength = spanNode.textContent.length;

A
Alex Dima 已提交
272
		let spanIndex = -1;
E
Erich Gamma 已提交
273 274 275 276
		while (spanNode) {
			spanNode = <HTMLElement>spanNode.previousSibling;
			spanIndex++;
		}
A
Alex Dima 已提交
277
		let lineParts = this._lineParts.getParts();
E
Erich Gamma 已提交
278

A
Alex Dima 已提交
279 280 281 282 283 284 285 286 287
		return getColumnOfLinePartOffset(
			this._stopRenderingLineAfter,
			lineParts,
			this._context.model.getLineMaxColumn(lineNumber),
			this._charOffsetInPart,
			spanIndex,
			spanNodeTextContentLength,
			offset
		);
E
Erich Gamma 已提交
288 289 290 291 292
	}
}

class IEViewLine extends ViewLine {

A
Alex Dima 已提交
293
	constructor(context:ViewContext) {
E
Erich Gamma 已提交
294 295 296
		super(context);
	}

297 298
	protected _getScaleRatio(): number {
		return screen.logicalXDPI / screen.deviceXDPI;
E
Erich Gamma 已提交
299 300 301 302 303
	}
}

class WebKitViewLine extends ViewLine {

A
Alex Dima 已提交
304
	constructor(context:ViewContext) {
E
Erich Gamma 已提交
305 306 307
		super(context);
	}

308 309
	protected _readVisibleRangesForRange(startColumn:number, endColumn:number, clientRectDeltaLeft:number, endNode:HTMLElement): HorizontalRange[] {
		let output = super._readVisibleRangesForRange(startColumn, endColumn, clientRectDeltaLeft, endNode);
E
Erich Gamma 已提交
310 311 312 313 314

		if (!output || output.length === 0 || startColumn === endColumn || (startColumn === 1 && endColumn === this._charOffsetInPart.length)) {
			return output;
		}

315
		// WebKit is buggy and returns an expanded range (to contain words in some cases)
E
Erich Gamma 已提交
316 317 318 319
		// The last client rect is enlarged (I think)

		// This is an attempt to patch things up
		// Find position of previous column
320
		let beforeEndVisibleRanges = this._readRawVisibleRangesForPosition(endColumn - 1, clientRectDeltaLeft, endNode);
E
Erich Gamma 已提交
321
		// Find position of last column
322
		let endVisibleRanges = this._readRawVisibleRangesForPosition(endColumn, clientRectDeltaLeft, endNode);
E
Erich Gamma 已提交
323 324

		if (beforeEndVisibleRanges && beforeEndVisibleRanges.length > 0 && endVisibleRanges && endVisibleRanges.length > 0) {
A
Alex Dima 已提交
325 326 327 328
			let beforeEndVisibleRange = beforeEndVisibleRanges[0];
			let endVisibleRange = endVisibleRanges[0];
			let isLTR = (beforeEndVisibleRange.left <= endVisibleRange.left);
			let lastRange = output[output.length - 1];
E
Erich Gamma 已提交
329

A
Andre Weinand 已提交
330
			if (isLTR && lastRange.left < endVisibleRange.left) {
E
Erich Gamma 已提交
331 332 333 334 335 336 337 338 339 340
				// Trim down the width of the last visible range to not go after the last column's position
				lastRange.width = endVisibleRange.left - lastRange.left;
			}
		}

		return output;
	}
}


A
Alex Dima 已提交
341
function findIndexInArrayWithMax(lineParts:LineParts, desiredIndex: number, maxResult:number): number {
A
Alex Dima 已提交
342
	let r = lineParts.findIndexOfOffset(desiredIndex);
E
Erich Gamma 已提交
343 344 345
	return r <= maxResult ? r : maxResult;
}

A
Alex Dima 已提交
346
export let createLine: (context: ViewContext) => ViewLine = (function() {
E
Erich Gamma 已提交
347 348 349 350
	if (window.screen && window.screen.deviceXDPI && (navigator.userAgent.indexOf('Trident/6.0') >= 0 || navigator.userAgent.indexOf('Trident/5.0') >= 0)) {
		// IE11 doesn't need the screen.logicalXDPI / screen.deviceXDPI ratio multiplication
		// for TextRange.getClientRects() anymore
		return createIELine;
A
Alex Dima 已提交
351
	} else if (browser.isWebKit) {
E
Erich Gamma 已提交
352 353 354 355 356
		return createWebKitLine;
	}
	return createNormalLine;
})();

A
Alex Dima 已提交
357
function createIELine(context: ViewContext): ViewLine {
E
Erich Gamma 已提交
358 359 360
	return new IEViewLine(context);
}

A
Alex Dima 已提交
361
function createWebKitLine(context: ViewContext): ViewLine {
E
Erich Gamma 已提交
362 363 364
	return new WebKitViewLine(context);
}

A
Alex Dima 已提交
365
function createNormalLine(context: ViewContext): ViewLine {
E
Erich Gamma 已提交
366 367 368
	return new ViewLine(context);
}