viewLineRenderer.ts 27.0 KB
Newer Older
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.
 *--------------------------------------------------------------------------------------------*/

J
Johannes Rieken 已提交
6
import { CharCode } from 'vs/base/common/charCode';
A
Alex Dima 已提交
7
import * as strings from 'vs/base/common/strings';
A
Alex Dima 已提交
8
import { IViewLineTokens } from 'vs/editor/common/core/lineTokens';
9
import { IStringBuilder, createStringBuilder } from 'vs/editor/common/core/stringBuilder';
A
Alex Dima 已提交
10
import { LineDecoration, LineDecorationsNormalizer } from 'vs/editor/common/viewLayout/lineDecorations';
11
import { InlineDecorationType } from 'vs/editor/common/viewModel/viewModel';
A
Alex Dima 已提交
12

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

A
Alex Dima 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
class LinePart {
	_linePartBrand: void;

	/**
	 * last char index of this token (not inclusive).
	 */
	public readonly endIndex: number;
	public readonly type: string;

	constructor(endIndex: number, type: string) {
		this.endIndex = endIndex;
		this.type = type;
	}
}

A
Alex Dima 已提交
34
export class RenderLineInput {
35

36
	public readonly useMonospaceOptimizations: boolean;
37
	public readonly canUseHalfwidthRightwardsArrow: boolean;
38
	public readonly lineContent: string;
A
Alex Dima 已提交
39
	public readonly continuesWithWrappedLine: boolean;
40
	public readonly isBasicASCII: boolean;
41
	public readonly containsRTL: boolean;
42
	public readonly fauxIndentLength: number;
43
	public readonly lineTokens: IViewLineTokens;
A
Alex Dima 已提交
44
	public readonly lineDecorations: LineDecoration[];
45 46 47
	public readonly tabSize: number;
	public readonly spaceWidth: number;
	public readonly stopRenderingLineAfter: number;
A
Alex Dima 已提交
48
	public readonly renderWhitespace: RenderWhitespace;
49
	public readonly renderControlCharacters: boolean;
50
	public readonly fontLigatures: boolean;
A
Alex Dima 已提交
51 52

	constructor(
53
		useMonospaceOptimizations: boolean,
54
		canUseHalfwidthRightwardsArrow: boolean,
A
Alex Dima 已提交
55
		lineContent: string,
A
Alex Dima 已提交
56
		continuesWithWrappedLine: boolean,
57
		isBasicASCII: boolean,
58
		containsRTL: boolean,
59
		fauxIndentLength: number,
60
		lineTokens: IViewLineTokens,
A
Alex Dima 已提交
61
		lineDecorations: LineDecoration[],
A
Alex Dima 已提交
62
		tabSize: number,
63
		spaceWidth: number,
A
Alex Dima 已提交
64
		stopRenderingLineAfter: number,
65
		renderWhitespace: 'none' | 'boundary' | 'all',
66
		renderControlCharacters: boolean,
67
		fontLigatures: boolean
A
Alex Dima 已提交
68
	) {
69
		this.useMonospaceOptimizations = useMonospaceOptimizations;
70
		this.canUseHalfwidthRightwardsArrow = canUseHalfwidthRightwardsArrow;
A
Alex Dima 已提交
71
		this.lineContent = lineContent;
A
Alex Dima 已提交
72
		this.continuesWithWrappedLine = continuesWithWrappedLine;
73
		this.isBasicASCII = isBasicASCII;
74
		this.containsRTL = containsRTL;
75
		this.fauxIndentLength = fauxIndentLength;
A
Alex Dima 已提交
76
		this.lineTokens = lineTokens;
77
		this.lineDecorations = lineDecorations;
A
Alex Dima 已提交
78
		this.tabSize = tabSize;
79
		this.spaceWidth = spaceWidth;
A
Alex Dima 已提交
80
		this.stopRenderingLineAfter = stopRenderingLineAfter;
A
Alex Dima 已提交
81 82 83 84 85 86 87
		this.renderWhitespace = (
			renderWhitespace === 'all'
				? RenderWhitespace.All
				: renderWhitespace === 'boundary'
					? RenderWhitespace.Boundary
					: RenderWhitespace.None
		);
88
		this.renderControlCharacters = renderControlCharacters;
89
		this.fontLigatures = fontLigatures;
A
Alex Dima 已提交
90 91
	}

A
Alex Dima 已提交
92
	public equals(other: RenderLineInput): boolean {
A
Alex Dima 已提交
93
		return (
94
			this.useMonospaceOptimizations === other.useMonospaceOptimizations
95
			&& this.canUseHalfwidthRightwardsArrow === other.canUseHalfwidthRightwardsArrow
96
			&& this.lineContent === other.lineContent
A
Alex Dima 已提交
97
			&& this.continuesWithWrappedLine === other.continuesWithWrappedLine
98
			&& this.isBasicASCII === other.isBasicASCII
99
			&& this.containsRTL === other.containsRTL
100
			&& this.fauxIndentLength === other.fauxIndentLength
A
Alex Dima 已提交
101 102 103 104 105
			&& this.tabSize === other.tabSize
			&& this.spaceWidth === other.spaceWidth
			&& this.stopRenderingLineAfter === other.stopRenderingLineAfter
			&& this.renderWhitespace === other.renderWhitespace
			&& this.renderControlCharacters === other.renderControlCharacters
106
			&& this.fontLigatures === other.fontLigatures
A
Alex Dima 已提交
107
			&& LineDecoration.equalsArr(this.lineDecorations, other.lineDecorations)
A
Alex Dima 已提交
108
			&& this.lineTokens.equals(other.lineTokens)
A
Alex Dima 已提交
109
		);
A
Alex Dima 已提交
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
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;
	}

	public readonly length: number;
135 136
	private readonly _data: Uint32Array;
	private readonly _absoluteOffsets: Uint32Array;
137 138

	constructor(length: number, partCount: number) {
139 140
		this.length = length;
		this._data = new Uint32Array(this.length);
141
		this._absoluteOffsets = new Uint32Array(this.length);
142 143
	}

144
	public setPartData(charOffset: number, partIndex: number, charIndex: number, partAbsoluteOffset: number): void {
145 146 147 148 149
		let partData = (
			(partIndex << CharacterMappingConstants.PART_INDEX_OFFSET)
			| (charIndex << CharacterMappingConstants.CHAR_INDEX_OFFSET)
		) >>> 0;
		this._data[charOffset] = partData;
150
		this._absoluteOffsets[charOffset] = partAbsoluteOffset + charIndex;
151 152
	}

153 154
	public getAbsoluteOffsets(): Uint32Array {
		return this._absoluteOffsets;
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
	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;
	}
}

231 232 233 234 235 236
export const enum ForeignElementType {
	None = 0,
	Before = 1,
	After = 2
}

A
Alex Dima 已提交
237
export class RenderLineOutput {
A
Alex Dima 已提交
238
	_renderLineOutputBrand: void;
A
Alex Dima 已提交
239

240
	readonly characterMapping: CharacterMapping;
241
	readonly containsRTL: boolean;
242
	readonly containsForeignElements: ForeignElementType;
A
Alex Dima 已提交
243

244
	constructor(characterMapping: CharacterMapping, containsRTL: boolean, containsForeignElements: ForeignElementType) {
245
		this.characterMapping = characterMapping;
246
		this.containsRTL = containsRTL;
247
		this.containsForeignElements = containsForeignElements;
A
Alex Dima 已提交
248
	}
249 250
}

251
export function renderViewLine(input: RenderLineInput, sb: IStringBuilder): RenderLineOutput {
A
Alex Dima 已提交
252
	if (input.lineContent.length === 0) {
253

254
		let containsForeignElements = ForeignElementType.None;
255 256

		// This is basically for IE's hit test to work
257
		let content: string = '<span><span>\u00a0</span></span>';
258 259 260 261 262 263

		if (input.lineDecorations.length > 0) {
			// This line is empty, but it contains inline decorations
			let classNames: string[] = [];
			for (let i = 0, len = input.lineDecorations.length; i < len; i++) {
				const lineDecoration = input.lineDecorations[i];
264 265 266 267 268
				if (lineDecoration.type === InlineDecorationType.Before) {
					classNames.push(input.lineDecorations[i].className);
					containsForeignElements |= ForeignElementType.Before;
				}
				if (lineDecoration.type === InlineDecorationType.After) {
269
					classNames.push(input.lineDecorations[i].className);
270
					containsForeignElements |= ForeignElementType.After;
271 272 273
				}
			}

274
			if (containsForeignElements !== ForeignElementType.None) {
275
				content = `<span><span class="${classNames.join(' ')}"></span></span>`;
276 277 278
			}
		}

279
		sb.appendASCIIString(content);
A
Alex Dima 已提交
280
		return new RenderLineOutput(
281
			new CharacterMapping(0, 0),
282
			false,
283
			containsForeignElements
A
Alex Dima 已提交
284
		);
285 286
	}

287 288 289 290 291 292 293 294
	return _renderLine(resolveRenderLineInput(input), sb);
}

export class RenderLineOutput2 {
	constructor(
		public readonly characterMapping: CharacterMapping,
		public readonly html: string,
		public readonly containsRTL: boolean,
295
		public readonly containsForeignElements: ForeignElementType
296 297 298 299 300 301 302 303
	) {
	}
}

export function renderViewLine2(input: RenderLineInput): RenderLineOutput2 {
	let sb = createStringBuilder(10000);
	let out = renderViewLine(input, sb);
	return new RenderLineOutput2(out.characterMapping, sb.build(), out.containsRTL, out.containsForeignElements);
A
Alex Dima 已提交
304 305 306 307
}

class ResolvedRenderLineInput {
	constructor(
308
		public readonly fontIsMonospace: boolean,
309
		public readonly canUseHalfwidthRightwardsArrow: boolean,
A
Alex Dima 已提交
310 311 312
		public readonly lineContent: string,
		public readonly len: number,
		public readonly isOverflowing: boolean,
A
Alex Dima 已提交
313
		public readonly parts: LinePart[],
314
		public readonly containsForeignElements: ForeignElementType,
A
Alex Dima 已提交
315
		public readonly tabSize: number,
316
		public readonly containsRTL: boolean,
A
Alex Dima 已提交
317 318 319 320 321
		public readonly spaceWidth: number,
		public readonly renderWhitespace: RenderWhitespace,
		public readonly renderControlCharacters: boolean,
	) {
		//
322
	}
A
Alex Dima 已提交
323
}
324

A
Alex Dima 已提交
325
function resolveRenderLineInput(input: RenderLineInput): ResolvedRenderLineInput {
326
	const useMonospaceOptimizations = input.useMonospaceOptimizations;
A
Alex Dima 已提交
327
	const lineContent = input.lineContent;
328

A
Alex Dima 已提交
329 330
	let isOverflowing: boolean;
	let len: number;
A
Alex Dima 已提交
331

A
Alex Dima 已提交
332 333 334 335 336 337 338
	if (input.stopRenderingLineAfter !== -1 && input.stopRenderingLineAfter < lineContent.length) {
		isOverflowing = true;
		len = input.stopRenderingLineAfter;
	} else {
		isOverflowing = false;
		len = lineContent.length;
	}
339

340
	let tokens = transformAndRemoveOverflowing(input.lineTokens, input.fauxIndentLength, len);
A
Alex Dima 已提交
341
	if (input.renderWhitespace === RenderWhitespace.All || input.renderWhitespace === RenderWhitespace.Boundary) {
A
Alex Dima 已提交
342
		tokens = _applyRenderWhitespace(lineContent, len, input.continuesWithWrappedLine, tokens, input.fauxIndentLength, input.tabSize, useMonospaceOptimizations, input.renderWhitespace === RenderWhitespace.Boundary);
A
Alex Dima 已提交
343
	}
344
	let containsForeignElements = ForeignElementType.None;
A
Alex Dima 已提交
345
	if (input.lineDecorations.length > 0) {
346 347
		for (let i = 0, len = input.lineDecorations.length; i < len; i++) {
			const lineDecoration = input.lineDecorations[i];
348 349 350 351 352 353 354
			if (lineDecoration.type === InlineDecorationType.RegularAffectingLetterSpacing) {
				// Pretend there are foreign elements... although not 100% accurate.
				containsForeignElements |= ForeignElementType.Before;
			} else if (lineDecoration.type === InlineDecorationType.Before) {
				containsForeignElements |= ForeignElementType.Before;
			} else if (lineDecoration.type === InlineDecorationType.After) {
				containsForeignElements |= ForeignElementType.After;
355 356
			}
		}
A
Alex Dima 已提交
357 358
		tokens = _applyInlineDecorations(lineContent, len, tokens, input.lineDecorations);
	}
359 360 361
	if (!input.containsRTL) {
		// We can never split RTL text, as it ruins the rendering
		tokens = splitLargeTokens(lineContent, tokens, !input.isBasicASCII || input.fontLigatures);
362 363
	}

A
Alex Dima 已提交
364
	return new ResolvedRenderLineInput(
365
		useMonospaceOptimizations,
366
		input.canUseHalfwidthRightwardsArrow,
A
Alex Dima 已提交
367 368 369 370
		lineContent,
		len,
		isOverflowing,
		tokens,
371
		containsForeignElements,
A
Alex Dima 已提交
372
		input.tabSize,
373
		input.containsRTL,
A
Alex Dima 已提交
374 375 376 377
		input.spaceWidth,
		input.renderWhitespace,
		input.renderControlCharacters
	);
378 379
}

380 381 382 383
/**
 * In the rendering phase, characters are always looped until token.endIndex.
 * Ensure that all tokens end before `len` and the last one ends precisely at `len`.
 */
384
function transformAndRemoveOverflowing(tokens: IViewLineTokens, fauxIndentLength: number, len: number): LinePart[] {
385 386 387 388 389 390 391
	let result: LinePart[] = [], resultLen = 0;

	// The faux indent part of the line should have no token type
	if (fauxIndentLength > 0) {
		result[resultLen++] = new LinePart(fauxIndentLength, '');
	}

A
Alex Dima 已提交
392
	for (let tokenIndex = 0, tokensLen = tokens.getCount(); tokenIndex < tokensLen; tokenIndex++) {
393
		const endIndex = tokens.getEndOffset(tokenIndex);
394 395 396 397
		if (endIndex <= fauxIndentLength) {
			// The faux indent part of the line should have no token type
			continue;
		}
398
		const type = tokens.getClassName(tokenIndex);
A
Alex Dima 已提交
399
		if (endIndex >= len) {
400
			result[resultLen++] = new LinePart(len, type);
401 402
			break;
		}
403
		result[resultLen++] = new LinePart(endIndex, type);
404
	}
405

406 407 408
	return result;
}

409 410 411 412 413 414 415 416 417 418 419 420
/**
 * written as a const enum to get value inlining.
 */
const enum Constants {
	LongToken = 50
}

/**
 * See https://github.com/Microsoft/vscode/issues/6885.
 * It appears that having very large spans causes very slow reading of character positions.
 * So here we try to avoid that.
 */
421
function splitLargeTokens(lineContent: string, tokens: LinePart[], onlyAtSpaces: boolean): LinePart[] {
422
	let lastTokenEndIndex = 0;
A
Alex Dima 已提交
423
	let result: LinePart[] = [], resultLen = 0;
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

	if (onlyAtSpaces) {
		// Split only at spaces => we need to walk each character
		for (let i = 0, len = tokens.length; i < len; i++) {
			const token = tokens[i];
			const tokenEndIndex = token.endIndex;
			if (lastTokenEndIndex + Constants.LongToken < tokenEndIndex) {
				const tokenType = token.type;

				let lastSpaceOffset = -1;
				let currTokenStart = lastTokenEndIndex;
				for (let j = lastTokenEndIndex; j < tokenEndIndex; j++) {
					if (lineContent.charCodeAt(j) === CharCode.Space) {
						lastSpaceOffset = j;
					}
					if (lastSpaceOffset !== -1 && j - currTokenStart >= Constants.LongToken) {
						// Split at `lastSpaceOffset` + 1
						result[resultLen++] = new LinePart(lastSpaceOffset + 1, tokenType);
						currTokenStart = lastSpaceOffset + 1;
						lastSpaceOffset = -1;
					}
				}
				if (currTokenStart !== tokenEndIndex) {
					result[resultLen++] = new LinePart(tokenEndIndex, tokenType);
				}
			} else {
				result[resultLen++] = token;
451
			}
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472

			lastTokenEndIndex = tokenEndIndex;
		}
	} else {
		// Split anywhere => we don't need to walk each character
		for (let i = 0, len = tokens.length; i < len; i++) {
			const token = tokens[i];
			const tokenEndIndex = token.endIndex;
			let diff = (tokenEndIndex - lastTokenEndIndex);
			if (diff > Constants.LongToken) {
				const tokenType = token.type;
				const piecesCount = Math.ceil(diff / Constants.LongToken);
				for (let j = 1; j < piecesCount; j++) {
					let pieceEndIndex = lastTokenEndIndex + (j * Constants.LongToken);
					result[resultLen++] = new LinePart(pieceEndIndex, tokenType);
				}
				result[resultLen++] = new LinePart(tokenEndIndex, tokenType);
			} else {
				result[resultLen++] = token;
			}
			lastTokenEndIndex = tokenEndIndex;
473 474 475 476 477 478 479 480 481 482 483
		}
	}

	return result;
}

/**
 * Whitespace is rendered by "replacing" tokens with a special-purpose `vs-whitespace` type that is later recognized in the rendering phase.
 * Moreover, a token is created for every visual indent because on some fonts the glyphs used for rendering whitespace (&rarr; or &middot;) do not have the same width as &nbsp;.
 * The rendering phase will generate `style="width:..."` for these tokens.
 */
A
Alex Dima 已提交
484
function _applyRenderWhitespace(lineContent: string, len: number, continuesWithWrappedLine: boolean, tokens: LinePart[], fauxIndentLength: number, tabSize: number, useMonospaceOptimizations: boolean, onlyBoundary: boolean): LinePart[] {
A
Alex Dima 已提交
485

A
Alex Dima 已提交
486
	let result: LinePart[] = [], resultLen = 0;
A
Alex Dima 已提交
487 488 489
	let tokenIndex = 0;
	let tokenType = tokens[tokenIndex].type;
	let tokenEndIndex = tokens[tokenIndex].endIndex;
A
Alex Dima 已提交
490
	const tokensLength = tokens.length;
491

A
Alex Dima 已提交
492 493 494 495 496 497 498 499 500
	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);
	}
501

A
Alex Dima 已提交
502 503 504 505 506
	let tmpIndent = 0;
	for (let charIndex = 0; charIndex < fauxIndentLength; charIndex++) {
		const chCode = lineContent.charCodeAt(charIndex);
		if (chCode === CharCode.Tab) {
			tmpIndent = tabSize;
507 508
		} else if (strings.isFullWidthCharacter(chCode)) {
			tmpIndent += 2;
A
Alex Dima 已提交
509 510 511
		} else {
			tmpIndent++;
		}
512
	}
A
Alex Dima 已提交
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
	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
545
			if (!isInWhitespace || (!useMonospaceOptimizations && tmpIndent >= tabSize)) {
A
Alex Dima 已提交
546
				// leaving whitespace token or entering a new indent
A
Alex Dima 已提交
547
				result[resultLen++] = new LinePart(charIndex, 'vs-whitespace');
A
Alex Dima 已提交
548 549 550 551 552
				tmpIndent = tmpIndent % tabSize;
			}
		} else {
			// was in regular token
			if (charIndex === tokenEndIndex || (isInWhitespace && charIndex > fauxIndentLength)) {
A
Alex Dima 已提交
553
				result[resultLen++] = new LinePart(charIndex, tokenType);
A
Alex Dima 已提交
554 555 556 557 558 559
				tmpIndent = tmpIndent % tabSize;
			}
		}

		if (chCode === CharCode.Tab) {
			tmpIndent = tabSize;
560 561
		} else if (strings.isFullWidthCharacter(chCode)) {
			tmpIndent += 2;
A
Alex Dima 已提交
562 563 564 565 566 567 568 569
		} else {
			tmpIndent++;
		}

		wasInWhitespace = isInWhitespace;

		if (charIndex === tokenEndIndex) {
			tokenIndex++;
A
Alex Dima 已提交
570 571 572 573
			if (tokenIndex < tokensLength) {
				tokenType = tokens[tokenIndex].type;
				tokenEndIndex = tokens[tokenIndex].endIndex;
			}
A
Alex Dima 已提交
574 575 576
		}
	}

A
Alex Dima 已提交
577
	let generateWhitespace = false;
A
Alex Dima 已提交
578 579
	if (wasInWhitespace) {
		// was in whitespace token
A
Alex Dima 已提交
580 581 582 583 584 585 586 587 588 589
		if (continuesWithWrappedLine && onlyBoundary) {
			let lastCharCode = (len > 0 ? lineContent.charCodeAt(len - 1) : CharCode.Null);
			let prevCharCode = (len > 1 ? lineContent.charCodeAt(len - 2) : CharCode.Null);
			let isSingleTrailingSpace = (lastCharCode === CharCode.Space && (prevCharCode !== CharCode.Space && prevCharCode !== CharCode.Tab));
			if (!isSingleTrailingSpace) {
				generateWhitespace = true;
			}
		} else {
			generateWhitespace = true;
		}
A
Alex Dima 已提交
590 591
	}

A
Alex Dima 已提交
592 593
	result[resultLen++] = new LinePart(len, generateWhitespace ? 'vs-whitespace' : tokenType);

A
Alex Dima 已提交
594
	return result;
595 596
}

597 598 599 600
/**
 * Inline decorations are "merged" on top of tokens.
 * Special care must be taken when multiple inline decorations are at play and they overlap.
 */
A
Alex Dima 已提交
601 602
function _applyInlineDecorations(lineContent: string, len: number, tokens: LinePart[], _lineDecorations: LineDecoration[]): LinePart[] {
	_lineDecorations.sort(LineDecoration.compare);
603
	const lineDecorations = LineDecorationsNormalizer.normalize(lineContent, _lineDecorations);
A
Alex Dima 已提交
604
	const lineDecorationsLen = lineDecorations.length;
A
Alex Dima 已提交
605

A
Alex Dima 已提交
606
	let lineDecorationIndex = 0;
A
Alex Dima 已提交
607
	let result: LinePart[] = [], resultLen = 0, lastResultEndIndex = 0;
A
Alex Dima 已提交
608 609 610 611
	for (let tokenIndex = 0, len = tokens.length; tokenIndex < len; tokenIndex++) {
		const token = tokens[tokenIndex];
		const tokenEndIndex = token.endIndex;
		const tokenType = token.type;
612

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

A
Alex Dima 已提交
616 617
			if (lineDecoration.startOffset > lastResultEndIndex) {
				lastResultEndIndex = lineDecoration.startOffset;
A
Alex Dima 已提交
618
				result[resultLen++] = new LinePart(lastResultEndIndex, tokenType);
A
Alex Dima 已提交
619
			}
A
Alex Dima 已提交
620

621
			if (lineDecoration.endOffset + 1 <= tokenEndIndex) {
622
				// This line decoration ends before this token ends
A
Alex Dima 已提交
623
				lastResultEndIndex = lineDecoration.endOffset + 1;
A
Alex Dima 已提交
624
				result[resultLen++] = new LinePart(lastResultEndIndex, tokenType + ' ' + lineDecoration.className);
A
Alex Dima 已提交
625 626
				lineDecorationIndex++;
			} else {
627 628
				// This line decoration continues on to the next token
				lastResultEndIndex = tokenEndIndex;
A
Alex Dima 已提交
629
				result[resultLen++] = new LinePart(lastResultEndIndex, tokenType + ' ' + lineDecoration.className);
A
Alex Dima 已提交
630 631 632
				break;
			}
		}
633

A
Alex Dima 已提交
634 635
		if (tokenEndIndex > lastResultEndIndex) {
			lastResultEndIndex = tokenEndIndex;
A
Alex Dima 已提交
636
			result[resultLen++] = new LinePart(lastResultEndIndex, tokenType);
637
		}
A
Alex Dima 已提交
638
	}
639

640 641 642 643 644 645 646 647
	const lastTokenEndIndex = tokens[tokens.length - 1].endIndex;
	if (lineDecorationIndex < lineDecorationsLen && lineDecorations[lineDecorationIndex].startOffset === lastTokenEndIndex) {
		let classNames: string[] = [];
		while (lineDecorationIndex < lineDecorationsLen && lineDecorations[lineDecorationIndex].startOffset === lastTokenEndIndex) {
			classNames.push(lineDecorations[lineDecorationIndex].className);
			lineDecorationIndex++;
		}
		result[resultLen++] = new LinePart(lastResultEndIndex, classNames.join(' '));
648 649
	}

A
Alex Dima 已提交
650 651 652
	return result;
}

653 654 655 656
/**
 * This function is on purpose not split up into multiple functions to allow runtime type inference (i.e. performance reasons).
 * Notice how all the needed data is fully resolved and passed in (i.e. no other calls).
 */
657
function _renderLine(input: ResolvedRenderLineInput, sb: IStringBuilder): RenderLineOutput {
658
	const fontIsMonospace = input.fontIsMonospace;
659
	const canUseHalfwidthRightwardsArrow = input.canUseHalfwidthRightwardsArrow;
660
	const containsForeignElements = input.containsForeignElements;
A
Alex Dima 已提交
661 662 663
	const lineContent = input.lineContent;
	const len = input.len;
	const isOverflowing = input.isOverflowing;
A
Alex Dima 已提交
664
	const parts = input.parts;
A
Alex Dima 已提交
665
	const tabSize = input.tabSize;
666
	const containsRTL = input.containsRTL;
A
Alex Dima 已提交
667 668 669 670
	const spaceWidth = input.spaceWidth;
	const renderWhitespace = input.renderWhitespace;
	const renderControlCharacters = input.renderControlCharacters;

A
Alex Dima 已提交
671
	const characterMapping = new CharacterMapping(len + 1, parts.length);
A
Alex Dima 已提交
672 673 674 675 676

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

677 678 679
	let prevPartContentCnt = 0;
	let partAbsoluteOffset = 0;

680 681
	sb.appendASCIIString('<span>');

A
Alex Dima 已提交
682
	for (let partIndex = 0, tokensLen = parts.length; partIndex < tokensLen; partIndex++) {
683 684
		partAbsoluteOffset += prevPartContentCnt;

A
Alex Dima 已提交
685 686 687 688
		const part = parts[partIndex];
		const partEndIndex = part.endIndex;
		const partType = part.type;
		const partRendersWhitespace = (renderWhitespace !== RenderWhitespace.None && (partType.indexOf('vs-whitespace') >= 0));
A
Alex Dima 已提交
689
		charOffsetInPart = 0;
A
Alex Dima 已提交
690

691 692 693 694
		sb.appendASCIIString('<span class="');
		sb.appendASCIIString(partType);
		sb.appendASCII(CharCode.DoubleQuote);

A
Alex Dima 已提交
695
		if (partRendersWhitespace) {
696 697

			let partContentCnt = 0;
698 699 700 701 702 703 704 705 706 707 708 709
			{
				let _charIndex = charIndex;
				let _tabsCharDelta = tabsCharDelta;

				for (; _charIndex < partEndIndex; _charIndex++) {
					const charCode = lineContent.charCodeAt(_charIndex);

					if (charCode === CharCode.Tab) {
						let insertSpacesCount = tabSize - (_charIndex + _tabsCharDelta) % tabSize;
						_tabsCharDelta += insertSpacesCount - 1;
						partContentCnt += insertSpacesCount;
					} else {
710
						// must be CharCode.Space
711 712 713 714 715
						partContentCnt++;
					}
				}
			}

716 717 718 719 720 721 722
			if (!fontIsMonospace) {
				const partIsOnlyWhitespace = (partType === 'vs-whitespace');
				if (partIsOnlyWhitespace || !containsForeignElements) {
					sb.appendASCIIString(' style="width:');
					sb.appendASCIIString(String(spaceWidth * partContentCnt));
					sb.appendASCIIString('px"');
				}
723 724 725
			}
			sb.appendASCII(CharCode.GreaterThan);

A
Alex Dima 已提交
726
			for (; charIndex < partEndIndex; charIndex++) {
727
				characterMapping.setPartData(charIndex, partIndex, charOffsetInPart, partAbsoluteOffset);
A
Alex Dima 已提交
728
				const charCode = lineContent.charCodeAt(charIndex);
A
Alex Dima 已提交
729

A
Alex Dima 已提交
730
				if (charCode === CharCode.Tab) {
A
Alex Dima 已提交
731 732 733 734
					let insertSpacesCount = tabSize - (charIndex + tabsCharDelta) % tabSize;
					tabsCharDelta += insertSpacesCount - 1;
					charOffsetInPart += insertSpacesCount - 1;
					if (insertSpacesCount > 0) {
735
						if (!canUseHalfwidthRightwardsArrow || insertSpacesCount > 1) {
736 737
							sb.write1(0x2192); // RIGHTWARDS ARROW
						} else {
738
							sb.write1(0xFFEB); // HALFWIDTH RIGHTWARDS ARROW
739
						}
A
Alex Dima 已提交
740 741 742
						insertSpacesCount--;
					}
					while (insertSpacesCount > 0) {
743
						sb.write1(0xA0); // &nbsp;
A
Alex Dima 已提交
744 745
						insertSpacesCount--;
					}
746
				} else {
A
Alex Dima 已提交
747
					// must be CharCode.Space
748
					sb.write1(0xB7); // &middot;
749 750
				}

J
Johannes Rieken 已提交
751
				charOffsetInPart++;
A
Alex Dima 已提交
752
			}
A
Alex Dima 已提交
753

754
			prevPartContentCnt = partContentCnt;
A
Alex Dima 已提交
755

756
		} else {
757 758

			let partContentCnt = 0;
759 760 761 762 763

			if (containsRTL) {
				sb.appendASCIIString(' dir="ltr"');
			}
			sb.appendASCII(CharCode.GreaterThan);
764

A
Alex Dima 已提交
765
			for (; charIndex < partEndIndex; charIndex++) {
766
				characterMapping.setPartData(charIndex, partIndex, charOffsetInPart, partAbsoluteOffset);
A
Alex Dima 已提交
767
				const charCode = lineContent.charCodeAt(charIndex);
768 769

				switch (charCode) {
A
Alex Dima 已提交
770
					case CharCode.Tab:
771 772 773 774
						let insertSpacesCount = tabSize - (charIndex + tabsCharDelta) % tabSize;
						tabsCharDelta += insertSpacesCount - 1;
						charOffsetInPart += insertSpacesCount - 1;
						while (insertSpacesCount > 0) {
775
							sb.write1(0xA0); // &nbsp;
776
							partContentCnt++;
777 778 779 780
							insertSpacesCount--;
						}
						break;

A
Alex Dima 已提交
781
					case CharCode.Space:
782
						sb.write1(0xA0); // &nbsp;
783
						partContentCnt++;
784 785
						break;

A
Alex Dima 已提交
786
					case CharCode.LessThan:
787
						sb.appendASCIIString('&lt;');
788
						partContentCnt++;
789 790
						break;

A
Alex Dima 已提交
791
					case CharCode.GreaterThan:
792
						sb.appendASCIIString('&gt;');
793
						partContentCnt++;
794 795
						break;

A
Alex Dima 已提交
796
					case CharCode.Ampersand:
797
						sb.appendASCIIString('&amp;');
798
						partContentCnt++;
799 800
						break;

A
Alex Dima 已提交
801
					case CharCode.Null:
802
						sb.appendASCIIString('&#00;');
803
						partContentCnt++;
804 805
						break;

A
Alex Dima 已提交
806 807
					case CharCode.UTF8_BOM:
					case CharCode.LINE_SEPARATOR_2028:
808
						sb.write1(0xFFFD);
809
						partContentCnt++;
810 811 812
						break;

					default:
813 814 815
						if (strings.isFullWidthCharacter(charCode)) {
							tabsCharDelta++;
						}
A
Alex Dima 已提交
816
						if (renderControlCharacters && charCode < 32) {
817
							sb.write1(9216 + charCode);
818
							partContentCnt++;
819
						} else {
820
							sb.write1(charCode);
821
							partContentCnt++;
822
						}
823 824
				}

J
Johannes Rieken 已提交
825
				charOffsetInPart++;
A
Alex Dima 已提交
826
			}
A
Alex Dima 已提交
827

828
			prevPartContentCnt = partContentCnt;
A
Alex Dima 已提交
829
		}
830 831 832

		sb.appendASCIIString('</span>');

833 834 835 836
	}

	// 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
837
	characterMapping.setPartData(len, parts.length - 1, charOffsetInPart, partAbsoluteOffset);
838

A
Alex Dima 已提交
839
	if (isOverflowing) {
840
		sb.appendASCIIString('<span>&hellip;</span>');
841
	}
A
Alex Dima 已提交
842

843
	sb.appendASCIIString('</span>');
844

845
	return new RenderLineOutput(characterMapping, containsRTL, containsForeignElements);
846
}