src/vs/editor/common/languages/textToHtmlTokenizer.ts

170 LOC · 142 covered · 28 uncovered · 24 ranges · 8 concepts · 7 introducers · 5 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the filesrc/vs/editor/common/encodedTokenAttributes.ts · 193 LOCcommon/encodedTokenAttri…src/vs/editor/test/common/core/testLineToken.ts · 156 LOCcore/testLineToken.tstextToHtmlTokenizer.ts ×1 · 2 introduced LOCtextToHtmlTokenizer.ts ×…textToHtmlTokenizer.ts ×2 · 27 introduced LOCtextToHtmlTokenizer.ts ×…textToHtmlTokenizer.test|title=Editor Modes - textToHtmlTokenizer tokenizeLineToHTML|occurrence=1 · 0 introduced LOCtextToHtmlTokenizer.test…encodedTokenAttributes.ts ×4 · 17 introduced LOCencodedTokenAttributes.t…textToHtmlTokenizer.ts ×2 · 19 introduced LOCtextToHtmlTokenizer.ts ×…textToHtmlTokenizer.ts ×3 · 5 introduced LOCtextToHtmlTokenizer.ts ×…textToHtmlTokenizer.ts ×11 · 71 introduced LOCtextToHtmlTokenizer.ts ×…textToHtmlTokenizer.ts ×4 · 26 introduced LOCtextToHtmlTokenizer.ts ×…textToHtmlTokenizer.test|title=Editor Modes - textToHtmlTokenizer TextToHtmlTokenizer 1|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/editor/test/common/modes/textToHtmlTokenizer.test|title=Editor Modes - textToHtmlTokenizer TextToHtmlTokenizer 1|occurrence=1textToHtmlTokenizer.test…textToHtmlTokenizer.test|title=Editor Modes - textToHtmlTokenizer TextToHtmlTokenizer 2|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/editor/test/common/modes/textToHtmlTokenizer.test|title=Editor Modes - textToHtmlTokenizer TextToHtmlTokenizer 2|occurrence=1textToHtmlTokenizer.test…textToHtmlTokenizer.test|title=Editor Modes - textToHtmlTokenizer tokenizeLineToHTML handle spaces #35954|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/editor/test/common/modes/textToHtmlTokenizer.test|title=Editor Modes - textToHtmlTokenizer tokenizeLineToHTML handle spaces #35954|occurrence=1textToHtmlTokenizer.test…textToHtmlTokenizer.test|title=Editor Modes - textToHtmlTokenizer tokenizeLineToHTML with tabs and non-zero startOffset #263387|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/editor/test/common/modes/textToHtmlTokenizer.test|title=Editor Modes - textToHtmlTokenizer tokenizeLineToHTML with tabs and non-zero startOffset #263387|occurrence=1textToHtmlTokenizer.test…textToHtmlTokenizer.test|title=Editor Modes - textToHtmlTokenizer tokenizeLineToHTML|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/editor/test/common/modes/textToHtmlTokenizer.test|title=Editor Modes - textToHtmlTokenizer tokenizeLineToHTML|occurrence=1textToHtmlTokenizer.test…Focused file · src/vs/editor/common/languages/textToHtmlTokenizer.ts · 170 LOClanguages/textToHtmlToke…

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- textToHtmlTokenizer.ts ×4
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { CharCode } from '../../../base/common/charCode.js';
7 > import * as strings from '../../../base/common/strings.js';
8 > import { IViewLineTokens, LineTokens } from '../tokens/lineTokens.js';
9 > import { ILanguageIdCodec, IState, ITokenizationSupport, TokenizationRegistry } from '../languages.js';
10 > import { LanguageId } from '../encodedTokenAttributes.js';
11 > import { NullState, nullTokenizeEncoded } from './nullTokenize.js';
12 > import { ILanguageService } from './language.js';
13 >
14 > export type IReducedTokenizationSupport = Omit<ITokenizationSupport, 'tokenize'>;
15 >
16 > const fallback: IReducedTokenizationSupport = {
17 > getInitialState: () => NullState,
18 > tokenizeEncoded: (buffer: string, hasEOL: boolean, state: IState) => nullTokenizeEncoded(LanguageId.Null, state)
19 > };
20 >
21 > export function tokenizeToStringSync(languageService: ILanguageService, text: string, languageId: string): string {
22 return _tokenizeToString(text, languageService.languageIdCodec, TokenizationRegistry.get(languageId) || fallback);
23 }
25 export async function tokenizeToString(languageService: ILanguageService, text: string, languageId: string | null): Promise<string> {
26 if (!languageId) {
27 return _tokenizeToString(text, languageService.languageIdCodec, fallback);
28 }
29 const tokenizationSupport = await TokenizationRegistry.getOrCreate(languageId);
30 return _tokenizeToString(text, languageService.languageIdCodec, tokenizationSupport || fallback);
31 }
33 > export function tokenizeLineToHTML(text: string, viewLineTokens: IViewLineTokens, colorMap: string[], startOffset: number, endOffset: number, tabSize: number, useNbsp: boolean): string {
34 > let result = `<div>`; textToHtmlTokenizer.ts ×11
35 > let charIndex = 0;
36 > let width = 0;
37 >
38 > let prevIsSpace = true;
39 >
40 > for (let tokenIndex = 0, tokenCount = viewLineTokens.getCount(); tokenIndex < tokenCount; tokenIndex++) {
41 > const tokenEndIndex = viewLineTokens.getEndOffset(tokenIndex);
42 > let partContent = '';
43 >
44 > for (; charIndex < tokenEndIndex && charIndex < endOffset; charIndex++) {
45 > const charCode = text.charCodeAt(charIndex);
46 > const isTab = charCode === CharCode.Tab;
47 >
48 > width += strings.isFullWidthCharacter(charCode) ? 2 : (isTab ? 0 : 1);
49 >
50 > if (charIndex < startOffset) {
51 > if (isTab) { textToHtmlTokenizer.ts ×3
52 > const remainder = width % tabSize; textToHtmlTokenizer.ts ×2
53 > width += remainder === 0 ? tabSize : tabSize - remainder;
54 > }
56 > }
58 > switch (charCode) {
59 > case CharCode.Tab: {
60 > const remainder = width % tabSize; textToHtmlTokenizer.ts ×2
61 > const insertSpacesCount = remainder === 0 ? tabSize : tabSize - remainder;
62 > width += insertSpacesCount;
63 > let spacesRemaining = insertSpacesCount;
64 > while (spacesRemaining > 0) {
65 > if (useNbsp && prevIsSpace) {
66 > partContent += '&#160;';
67 > prevIsSpace = false;
68 > } else {
69 > partContent += ' ';
70 > prevIsSpace = true;
71 > }
72 > spacesRemaining--;
73 > }
74 > break;
75 > }
76 > case CharCode.LessThan: textToHtmlTokenizer.ts ×11
77 partContent += '&lt;';
78 prevIsSpace = false;
79 break;
81 > case CharCode.GreaterThan:
82 partContent += '&gt;';
83 prevIsSpace = false;
84 break;
86 > case CharCode.Ampersand:
87 partContent += '&amp;';
88 prevIsSpace = false;
89 break;
91 > case CharCode.Null:
92 partContent += '&#00;';
93 prevIsSpace = false;
94 break;
96 > case CharCode.UTF8_BOM:
97 > case CharCode.LINE_SEPARATOR:
98 > case CharCode.PARAGRAPH_SEPARATOR:
99 > case CharCode.NEXT_LINE:
100 partContent += '\ufffd';
101 prevIsSpace = false;
102 break;
104 > case CharCode.CarriageReturn:
105 // zero width space, because carriage return would introduce a line break
106 partContent += '&#8203';
107 prevIsSpace = false;
108 break;
110 > case CharCode.Space:
111 > if (useNbsp && prevIsSpace) { encodedTokenAttributes.ts ×4
112 > partContent += '&#160;';
113 > prevIsSpace = false;
114 > } else {
115 > partContent += ' ';
116 > prevIsSpace = true;
117 > }
118 > break;
120 > default:
121 > partContent += String.fromCharCode(charCode);
122 > prevIsSpace = false;
123 > }
124 > }
125 >
126 > if (tokenEndIndex <= startOffset) {
127 > continue; textToHtmlTokenizer.ts ×3
128 > }
130 > result += `<span style="${viewLineTokens.getInlineStyle(tokenIndex, colorMap)}">${partContent}</span>`;
131 >
132 > if (tokenEndIndex > endOffset || charIndex >= endOffset || startOffset >= endOffset) {
133 > break;
134 > }
135 > }
136 >
137 > result += `</div>`;
138 > return result;
139 > }
141 > export function _tokenizeToString(text: string, languageIdCodec: ILanguageIdCodec, tokenizationSupport: IReducedTokenizationSupport): string {
142 > let result = `<div class="monaco-tokenized-source">`; textToHtmlTokenizer.ts ×2
143 > const lines = strings.splitLines(text);
144 > let currentState = tokenizationSupport.getInitialState();
145 > for (let i = 0, len = lines.length; i < len; i++) {
146 > const line = lines[i];
147 >
148 > if (i > 0) {
149 > result += `<br/>`; textToHtmlTokenizer.ts ×1
150 > }
152 > const tokenizationResult = tokenizationSupport.tokenizeEncoded(line, true, currentState);
153 > LineTokens.convertToEndOffset(tokenizationResult.tokens, line.length);
154 > const lineTokens = new LineTokens(tokenizationResult.tokens, line, languageIdCodec);
155 > const viewLineTokens = lineTokens.inflate();
156 >
157 > let startOffset = 0;
158 > for (let j = 0, lenJ = viewLineTokens.getCount(); j < lenJ; j++) {
159 > const type = viewLineTokens.getClassName(j);
160 > const endIndex = viewLineTokens.getEndOffset(j);
161 > result += `<span class="${type}">${strings.escape(line.substring(startOffset, endIndex))}</span>`;
162 > startOffset = endIndex;
163 > }
164 >
165 > currentState = tokenizationResult.endState;
166 > }
167 >
168 > result += `</div>`;
169 > return result;
170 > }