viewLineRenderer.ts 16.4 KB
Newer Older
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';

7
import { ViewLineToken } from 'vs/editor/common/core/viewLineToken';
J
Johannes Rieken 已提交
8
import { CharCode } from 'vs/base/common/charCode';
A
Alex Dima 已提交
9 10
import { Decoration, LineDecorationsNormalizer } from 'vs/editor/common/viewLayout/viewLineParts';
import * as strings from 'vs/base/common/strings';
A
Alex Dima 已提交
11

A
Alex Dima 已提交
12 13 14 15 16 17 18
export const enum RenderWhitespace {
	None = 0,
	Boundary = 1,
	All = 2
}

export class RenderLineInput {
19

20
	public readonly lineContent: string;
21
	public readonly mightContainRTL: boolean;
22 23
	public readonly fauxIndentLength: number;
	public readonly lineTokens: ViewLineToken[];
24 25 26 27
	public readonly lineDecorations: Decoration[];
	public readonly tabSize: number;
	public readonly spaceWidth: number;
	public readonly stopRenderingLineAfter: number;
A
Alex Dima 已提交
28
	public readonly renderWhitespace: RenderWhitespace;
29
	public readonly renderControlCharacters: boolean;
A
Alex Dima 已提交
30 31 32

	constructor(
		lineContent: string,
33
		mightContainRTL: boolean,
34 35
		fauxIndentLength: number,
		lineTokens: ViewLineToken[],
36
		lineDecorations: Decoration[],
A
Alex Dima 已提交
37
		tabSize: number,
38
		spaceWidth: number,
A
Alex Dima 已提交
39
		stopRenderingLineAfter: number,
40
		renderWhitespace: 'none' | 'boundary' | 'all',
41
		renderControlCharacters: boolean,
A
Alex Dima 已提交
42 43
	) {
		this.lineContent = lineContent;
44
		this.mightContainRTL = mightContainRTL;
45
		this.fauxIndentLength = fauxIndentLength;
46 47
		this.lineTokens = lineTokens;
		this.lineDecorations = lineDecorations;
A
Alex Dima 已提交
48
		this.tabSize = tabSize;
49
		this.spaceWidth = spaceWidth;
A
Alex Dima 已提交
50
		this.stopRenderingLineAfter = stopRenderingLineAfter;
A
Alex Dima 已提交
51 52 53 54 55 56 57
		this.renderWhitespace = (
			renderWhitespace === 'all'
				? RenderWhitespace.All
				: renderWhitespace === 'boundary'
					? RenderWhitespace.Boundary
					: RenderWhitespace.None
		);
58
		this.renderControlCharacters = renderControlCharacters;
A
Alex Dima 已提交
59 60
	}

A
Alex Dima 已提交
61
	public equals(other: RenderLineInput): boolean {
A
Alex Dima 已提交
62 63
		return (
			this.lineContent === other.lineContent
64
			&& this.mightContainRTL === other.mightContainRTL
65
			&& this.fauxIndentLength === other.fauxIndentLength
A
Alex Dima 已提交
66 67 68 69 70
			&& this.tabSize === other.tabSize
			&& this.spaceWidth === other.spaceWidth
			&& this.stopRenderingLineAfter === other.stopRenderingLineAfter
			&& this.renderWhitespace === other.renderWhitespace
			&& this.renderControlCharacters === other.renderControlCharacters
71
			&& Decoration.equalsArr(this.lineDecorations, other.lineDecorations)
72
			&& ViewLineToken.equalsArr(this.lineTokens, other.lineTokens)
A
Alex Dima 已提交
73
		);
A
Alex Dima 已提交
74
	}
75 76
}

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
export const enum CharacterMappingConstants {
	PART_INDEX_MASK = 0b11111111111111110000000000000000,
	CHAR_INDEX_MASK = 0b00000000000000001111111111111111,

	CHAR_INDEX_OFFSET = 0,
	PART_INDEX_OFFSET = 16
}

/**
 * Provides a both direction mapping between a line's character and its rendered position.
 */
export class CharacterMapping {

	public static getPartIndex(partData: number): number {
		return (partData & CharacterMappingConstants.PART_INDEX_MASK) >>> CharacterMappingConstants.PART_INDEX_OFFSET;
	}

	public static getCharIndex(partData: number): number {
		return (partData & CharacterMappingConstants.CHAR_INDEX_MASK) >>> CharacterMappingConstants.CHAR_INDEX_OFFSET;
	}

	private readonly _data: Uint32Array;
	public readonly length: number;

	constructor(length: number) {
		this.length = length;
		this._data = new Uint32Array(this.length);
	}

	public setPartData(charOffset: number, partIndex: number, charIndex: number): void {
		let partData = (
			(partIndex << CharacterMappingConstants.PART_INDEX_OFFSET)
			| (charIndex << CharacterMappingConstants.CHAR_INDEX_OFFSET)
		) >>> 0;
		this._data[charOffset] = partData;
	}

	public charOffsetToPartData(charOffset: number): number {
		if (this.length === 0) {
			return 0;
		}
		if (charOffset < 0) {
			return this._data[0];
		}
		if (charOffset >= this.length) {
			return this._data[this.length - 1];
		}
		return this._data[charOffset];
	}

	public partDataToCharOffset(partIndex: number, partLength: number, charIndex: number): number {
		if (this.length === 0) {
			return 0;
		}

		let searchEntry = (
			(partIndex << CharacterMappingConstants.PART_INDEX_OFFSET)
			| (charIndex << CharacterMappingConstants.CHAR_INDEX_OFFSET)
		) >>> 0;

		let min = 0;
		let max = this.length - 1;
		while (min + 1 < max) {
			let mid = ((min + max) >>> 1);
			let midEntry = this._data[mid];
			if (midEntry === searchEntry) {
				return mid;
			} else if (midEntry > searchEntry) {
				max = mid;
			} else {
				min = mid;
			}
		}

		if (min === max) {
			return min;
		}

		let minEntry = this._data[min];
		let maxEntry = this._data[max];

		if (minEntry === searchEntry) {
			return min;
		}
		if (maxEntry === searchEntry) {
			return max;
		}

		let minPartIndex = CharacterMapping.getPartIndex(minEntry);
		let minCharIndex = CharacterMapping.getCharIndex(minEntry);

		let maxPartIndex = CharacterMapping.getPartIndex(maxEntry);
		let maxCharIndex: number;

		if (minPartIndex !== maxPartIndex) {
			// sitting between parts
			maxCharIndex = partLength;
		} else {
			maxCharIndex = CharacterMapping.getCharIndex(maxEntry);
		}

		let minEntryDistance = charIndex - minCharIndex;
		let maxEntryDistance = maxCharIndex - charIndex;

		if (minEntryDistance <= maxEntryDistance) {
			return min;
		}
		return max;
	}
}

A
Alex Dima 已提交
188
export class RenderLineOutput {
A
Alex Dima 已提交
189
	_renderLineOutputBrand: void;
A
Alex Dima 已提交
190

191
	readonly characterMapping: CharacterMapping;
A
Alex Dima 已提交
192
	readonly output: string;
A
Alex Dima 已提交
193

194
	constructor(characterMapping: CharacterMapping, output: string) {
195
		this.characterMapping = characterMapping;
A
Alex Dima 已提交
196 197
		this.output = output;
	}
198 199
}

A
Alex Dima 已提交
200 201
export function renderViewLine(input: RenderLineInput): RenderLineOutput {
	if (input.lineContent.length === 0) {
A
Alex Dima 已提交
202
		return new RenderLineOutput(
203
			new CharacterMapping(0),
204
			// This is basically for IE's hit test to work
205
			'<span><span>&nbsp;</span></span>'
A
Alex Dima 已提交
206
		);
207 208
	}

A
Alex Dima 已提交
209 210 211 212 213 214 215 216
	return _renderLine(resolveRenderLineInput(input));
}

class ResolvedRenderLineInput {
	constructor(
		public readonly lineContent: string,
		public readonly len: number,
		public readonly isOverflowing: boolean,
217
		public readonly tokens: ViewLineToken[],
A
Alex Dima 已提交
218 219
		public readonly lineDecorations: Decoration[],
		public readonly tabSize: number,
220
		public readonly emitLTRDir: boolean,
A
Alex Dima 已提交
221 222 223 224 225
		public readonly spaceWidth: number,
		public readonly renderWhitespace: RenderWhitespace,
		public readonly renderControlCharacters: boolean,
	) {
		//
226
	}
A
Alex Dima 已提交
227
}
228

A
Alex Dima 已提交
229 230
function resolveRenderLineInput(input: RenderLineInput): ResolvedRenderLineInput {
	const lineContent = input.lineContent;
231

A
Alex Dima 已提交
232 233
	let isOverflowing: boolean;
	let len: number;
A
Alex Dima 已提交
234

A
Alex Dima 已提交
235 236 237 238 239 240 241
	if (input.stopRenderingLineAfter !== -1 && input.stopRenderingLineAfter < lineContent.length) {
		isOverflowing = true;
		len = input.stopRenderingLineAfter;
	} else {
		isOverflowing = false;
		len = lineContent.length;
	}
242

243
	let tokens = removeOverflowing(input.lineTokens, len);
A
Alex Dima 已提交
244
	if (input.renderWhitespace === RenderWhitespace.All || input.renderWhitespace === RenderWhitespace.Boundary) {
245
		tokens = _applyRenderWhitespace(lineContent, len, tokens, input.fauxIndentLength, input.tabSize, input.renderWhitespace === RenderWhitespace.Boundary);
A
Alex Dima 已提交
246 247 248 249
	}
	if (input.lineDecorations.length > 0) {
		tokens = _applyInlineDecorations(lineContent, len, tokens, input.lineDecorations);
	}
A
Alex Dima 已提交
250

251 252 253 254 255
	let emitLTRDir = false;
	if (input.mightContainRTL) {
		emitLTRDir = strings.containsRTL(lineContent);
	}

A
Alex Dima 已提交
256 257 258 259 260 261 262
	return new ResolvedRenderLineInput(
		lineContent,
		len,
		isOverflowing,
		tokens,
		input.lineDecorations,
		input.tabSize,
263
		emitLTRDir,
A
Alex Dima 已提交
264 265 266 267
		input.spaceWidth,
		input.renderWhitespace,
		input.renderControlCharacters
	);
268 269
}

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
function removeOverflowing(tokens: ViewLineToken[], len: number): ViewLineToken[] {
	if (tokens.length === 0) {
		return tokens;
	}
	if (tokens[tokens.length - 1].endIndex === len) {
		return tokens;
	}
	let result: ViewLineToken[] = [];
	for (let tokenIndex = 0, tokensLen = tokens.length; tokenIndex < tokensLen; tokenIndex++) {
		const endIndex = tokens[tokenIndex].endIndex;
		if (endIndex === len) {
			result[tokenIndex] = tokens[tokenIndex];
			break;
		}
		if (endIndex > len) {
			result[tokenIndex] = new ViewLineToken(len, tokens[tokenIndex].type);
			break;
		}
		result[tokenIndex] = tokens[tokenIndex];
	}
	return result;
}

function _applyRenderWhitespace(lineContent: string, len: number, tokens: ViewLineToken[], fauxIndentLength: number, tabSize: number, onlyBoundary: boolean): ViewLineToken[] {
A
Alex Dima 已提交
294

295
	let result: ViewLineToken[] = [], resultLen = 0;
A
Alex Dima 已提交
296 297 298
	let tokenIndex = 0;
	let tokenType = tokens[tokenIndex].type;
	let tokenEndIndex = tokens[tokenIndex].endIndex;
299

A
Alex Dima 已提交
300
	if (fauxIndentLength > 0) {
301
		result[resultLen++] = new ViewLineToken(fauxIndentLength, '');
302 303
	}

A
Alex Dima 已提交
304 305 306 307 308 309 310 311 312
	let firstNonWhitespaceIndex = strings.firstNonWhitespaceIndex(lineContent);
	let lastNonWhitespaceIndex: number;
	if (firstNonWhitespaceIndex === -1) {
		// The entire line is whitespace
		firstNonWhitespaceIndex = len;
		lastNonWhitespaceIndex = len;
	} else {
		lastNonWhitespaceIndex = strings.lastNonWhitespaceIndex(lineContent);
	}
313

A
Alex Dima 已提交
314 315 316 317 318 319 320 321
	let tmpIndent = 0;
	for (let charIndex = 0; charIndex < fauxIndentLength; charIndex++) {
		const chCode = lineContent.charCodeAt(charIndex);
		if (chCode === CharCode.Tab) {
			tmpIndent = tabSize;
		} else {
			tmpIndent++;
		}
322
	}
A
Alex Dima 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
	tmpIndent = tmpIndent % tabSize;

	let wasInWhitespace = false;
	for (let charIndex = fauxIndentLength; charIndex < len; charIndex++) {
		const chCode = lineContent.charCodeAt(charIndex);

		let isInWhitespace: boolean;
		if (charIndex < firstNonWhitespaceIndex || charIndex > lastNonWhitespaceIndex) {
			// in leading or trailing whitespace
			isInWhitespace = true;
		} else if (chCode === CharCode.Tab) {
			// a tab character is rendered both in all and boundary cases
			isInWhitespace = true;
		} else if (chCode === CharCode.Space) {
			// hit a space character
			if (onlyBoundary) {
				// rendering only boundary whitespace
				if (wasInWhitespace) {
					isInWhitespace = true;
				} else {
					const nextChCode = (charIndex + 1 < len ? lineContent.charCodeAt(charIndex + 1) : CharCode.Null);
					isInWhitespace = (nextChCode === CharCode.Space || nextChCode === CharCode.Tab);
				}
			} else {
				isInWhitespace = true;
			}
		} else {
			isInWhitespace = false;
		}

		if (wasInWhitespace) {
			// was in whitespace token
			if (!isInWhitespace || tmpIndent >= tabSize) {
				// leaving whitespace token or entering a new indent
357
				result[resultLen++] = new ViewLineToken(charIndex, 'vs-whitespace');
A
Alex Dima 已提交
358 359 360 361 362
				tmpIndent = tmpIndent % tabSize;
			}
		} else {
			// was in regular token
			if (charIndex === tokenEndIndex || (isInWhitespace && charIndex > fauxIndentLength)) {
363
				result[resultLen++] = new ViewLineToken(charIndex, tokenType);
A
Alex Dima 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
				tmpIndent = tmpIndent % tabSize;
			}
		}

		if (chCode === CharCode.Tab) {
			tmpIndent = tabSize;
		} else {
			tmpIndent++;
		}

		wasInWhitespace = isInWhitespace;

		if (charIndex === tokenEndIndex) {
			tokenIndex++;
			tokenType = tokens[tokenIndex].type;
			tokenEndIndex = tokens[tokenIndex].endIndex;
		}
	}

	if (wasInWhitespace) {
		// was in whitespace token
385
		result[resultLen++] = new ViewLineToken(len, 'vs-whitespace');
A
Alex Dima 已提交
386 387
	} else {
		// was in regular token
388
		result[resultLen++] = new ViewLineToken(len, tokenType);
A
Alex Dima 已提交
389 390 391
	}

	return result;
392 393
}

394
function _applyInlineDecorations(lineContent: string, len: number, tokens: ViewLineToken[], _lineDecorations: Decoration[]): ViewLineToken[] {
A
Alex Dima 已提交
395 396 397
	_lineDecorations.sort(Decoration.compare);
	const lineDecorations = LineDecorationsNormalizer.normalize(_lineDecorations);
	const lineDecorationsLen = lineDecorations.length;
A
Alex Dima 已提交
398

A
Alex Dima 已提交
399
	let lineDecorationIndex = 0;
400
	let result: ViewLineToken[] = [], resultLen = 0, lastResultEndIndex = 0;
A
Alex Dima 已提交
401 402 403 404
	for (let tokenIndex = 0, len = tokens.length; tokenIndex < len; tokenIndex++) {
		const token = tokens[tokenIndex];
		const tokenEndIndex = token.endIndex;
		const tokenType = token.type;
405

A
Alex Dima 已提交
406 407
		while (lineDecorationIndex < lineDecorationsLen && lineDecorations[lineDecorationIndex].startOffset < tokenEndIndex) {
			const lineDecoration = lineDecorations[lineDecorationIndex];
408

A
Alex Dima 已提交
409 410
			if (lineDecoration.startOffset > lastResultEndIndex) {
				lastResultEndIndex = lineDecoration.startOffset;
411
				result[resultLen++] = new ViewLineToken(lastResultEndIndex, tokenType);
A
Alex Dima 已提交
412
			}
A
Alex Dima 已提交
413

A
Alex Dima 已提交
414 415
			if (lineDecoration.endOffset + 1 < tokenEndIndex) {
				lastResultEndIndex = lineDecoration.endOffset + 1;
416
				result[resultLen++] = new ViewLineToken(lastResultEndIndex, tokenType + ' ' + lineDecoration.className);
A
Alex Dima 已提交
417 418 419 420 421
				lineDecorationIndex++;
			} else {
				break;
			}
		}
422

A
Alex Dima 已提交
423 424
		if (tokenEndIndex > lastResultEndIndex) {
			lastResultEndIndex = tokenEndIndex;
425
			result[resultLen++] = new ViewLineToken(lastResultEndIndex, tokenType);
426
		}
A
Alex Dima 已提交
427
	}
428

A
Alex Dima 已提交
429 430 431 432 433 434 435 436 437
	return result;
}

function _renderLine(input: ResolvedRenderLineInput): RenderLineOutput {
	const lineContent = input.lineContent;
	const len = input.len;
	const isOverflowing = input.isOverflowing;
	const tokens = input.tokens;
	const tabSize = input.tabSize;
438
	const emitLTRDir = input.emitLTRDir;
A
Alex Dima 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
	const spaceWidth = input.spaceWidth;
	const renderWhitespace = input.renderWhitespace;
	const renderControlCharacters = input.renderControlCharacters;

	const characterMapping = new CharacterMapping(len + 1);

	let charIndex = 0;
	let tabsCharDelta = 0;
	let charOffsetInPart = 0;

	let out = '<span>';
	for (let tokenIndex = 0, tokensLen = tokens.length; tokenIndex < tokensLen; tokenIndex++) {
		const token = tokens[tokenIndex];
		const tokenEndIndex = token.endIndex;
		const tokenType = token.type;
		const tokenRendersWhitespace = (renderWhitespace !== RenderWhitespace.None && (tokenType.indexOf('vs-whitespace') >= 0));
A
Alex Dima 已提交
455
		charOffsetInPart = 0;
A
Alex Dima 已提交
456 457

		if (tokenRendersWhitespace) {
458 459 460

			let partContentCnt = 0;
			let partContent = '';
A
Alex Dima 已提交
461 462 463
			for (; charIndex < tokenEndIndex; charIndex++) {
				characterMapping.setPartData(charIndex, tokenIndex, charOffsetInPart);
				const charCode = lineContent.charCodeAt(charIndex);
A
Alex Dima 已提交
464

A
Alex Dima 已提交
465
				if (charCode === CharCode.Tab) {
A
Alex Dima 已提交
466 467 468 469
					let insertSpacesCount = tabSize - (charIndex + tabsCharDelta) % tabSize;
					tabsCharDelta += insertSpacesCount - 1;
					charOffsetInPart += insertSpacesCount - 1;
					if (insertSpacesCount > 0) {
470
						partContent += '&rarr;';
471
						partContentCnt++;
A
Alex Dima 已提交
472 473 474
						insertSpacesCount--;
					}
					while (insertSpacesCount > 0) {
475 476
						partContent += '&nbsp;';
						partContentCnt++;
A
Alex Dima 已提交
477 478
						insertSpacesCount--;
					}
479
				} else {
A
Alex Dima 已提交
480
					// must be CharCode.Space
481
					partContent += '&middot;';
482 483 484
					partContentCnt++;
				}

J
Johannes Rieken 已提交
485
				charOffsetInPart++;
A
Alex Dima 已提交
486
			}
A
Alex Dima 已提交
487 488 489

			out += `<span class="${tokenType}" style="width:${spaceWidth * partContentCnt}px">${partContent}</span>`;

490
		} else {
491
			let partContent = '';
492

A
Alex Dima 已提交
493 494 495
			for (; charIndex < tokenEndIndex; charIndex++) {
				characterMapping.setPartData(charIndex, tokenIndex, charOffsetInPart);
				const charCode = lineContent.charCodeAt(charIndex);
496 497

				switch (charCode) {
A
Alex Dima 已提交
498
					case CharCode.Tab:
499 500 501 502
						let insertSpacesCount = tabSize - (charIndex + tabsCharDelta) % tabSize;
						tabsCharDelta += insertSpacesCount - 1;
						charOffsetInPart += insertSpacesCount - 1;
						while (insertSpacesCount > 0) {
503
							partContent += '&nbsp;';
504 505 506 507
							insertSpacesCount--;
						}
						break;

A
Alex Dima 已提交
508
					case CharCode.Space:
509
						partContent += '&nbsp;';
510 511
						break;

A
Alex Dima 已提交
512
					case CharCode.LessThan:
513
						partContent += '&lt;';
514 515
						break;

A
Alex Dima 已提交
516
					case CharCode.GreaterThan:
517
						partContent += '&gt;';
518 519
						break;

A
Alex Dima 已提交
520
					case CharCode.Ampersand:
521
						partContent += '&amp;';
522 523
						break;

A
Alex Dima 已提交
524
					case CharCode.Null:
525
						partContent += '&#00;';
526 527
						break;

A
Alex Dima 已提交
528 529
					case CharCode.UTF8_BOM:
					case CharCode.LINE_SEPARATOR_2028:
530
						partContent += '\ufffd';
531 532
						break;

A
Alex Dima 已提交
533
					case CharCode.CarriageReturn:
534
						// zero width space, because carriage return would introduce a line break
535
						partContent += '&#8203';
536 537 538
						break;

					default:
A
Alex Dima 已提交
539 540
						if (renderControlCharacters && charCode < 32) {
							partContent += String.fromCharCode(9216 + charCode);
541
						} else {
A
Alex Dima 已提交
542
							partContent += String.fromCharCode(charCode);;
543
						}
544 545
				}

J
Johannes Rieken 已提交
546
				charOffsetInPart++;
A
Alex Dima 已提交
547
			}
A
Alex Dima 已提交
548

549 550 551 552 553
			if (emitLTRDir) {
				out += `<span dir="ltr" class="${tokenType}">${partContent}</span>`;
			} else {
				out += `<span class="${tokenType}">${partContent}</span>`;
			}
A
Alex Dima 已提交
554

A
Alex Dima 已提交
555
		}
556 557 558 559
	}

	// When getting client rects for the last character, we will position the
	// text range at the end of the span, insteaf of at the beginning of next span
A
Alex Dima 已提交
560
	characterMapping.setPartData(len, tokens.length - 1, charOffsetInPart);
561

A
Alex Dima 已提交
562 563
	if (isOverflowing) {
		out += `<span class="">&hellip;</span>`;
564
	}
A
Alex Dima 已提交
565

566 567
	out += '</span>';

A
Alex Dima 已提交
568
	return new RenderLineOutput(characterMapping, out);
569
}