textToHtmlTokenizer.test.ts 6.5 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 assert from 'assert';
8 9
import { TokenizationRegistry, IState, LanguageIdentifier, ColorId, FontStyle, MetadataConsts } from 'vs/editor/common/modes';
import { tokenizeToString, tokenizeLineToHTML } from 'vs/editor/common/modes/textToHtmlTokenizer';
J
Johannes Rieken 已提交
10
import { MockMode } from 'vs/editor/test/common/mocks/mockMode';
A
Tweaks  
Alex Dima 已提交
11
import { TokenizationResult2 } from 'vs/editor/common/core/token';
A
Alex Dima 已提交
12
import { ViewLineToken, ViewLineTokens } from 'vs/editor/test/common/core/viewLineToken';
E
Erich Gamma 已提交
13 14

suite('Editor Modes - textToHtmlTokenizer', () => {
A
Alex Dima 已提交
15 16 17 18
	function toStr(pieces: { className: string; text: string }[]): string {
		let resultArr = pieces.map((t) => `<span class="${t.className}">${t.text}</span>`);
		return resultArr.join('');
	}
E
Erich Gamma 已提交
19

A
Alex Dima 已提交
20 21
	test('TextToHtmlTokenizer 1', () => {
		let mode = new Mode();
E
Erich Gamma 已提交
22

A
Alex Dima 已提交
23 24 25 26 27 28 29 30 31 32
		let actual = tokenizeToString('.abc..def...gh', mode.getId());
		let expected = [
			{ className: 'mtk7', text: '.' },
			{ className: 'mtk9', text: 'abc' },
			{ className: 'mtk7', text: '..' },
			{ className: 'mtk9', text: 'def' },
			{ className: 'mtk7', text: '...' },
			{ className: 'mtk9', text: 'gh' },
		];
		let expectedStr = `<div class="monaco-tokenized-source">${toStr(expected)}</div>`;
E
Erich Gamma 已提交
33

A
Alex Dima 已提交
34
		assert.equal(actual, expectedStr);
E
Erich Gamma 已提交
35

A
Alex Dima 已提交
36 37
		mode.dispose();
	});
E
Erich Gamma 已提交
38

A
Alex Dima 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	test('TextToHtmlTokenizer 2', () => {
		let mode = new Mode();

		let actual = tokenizeToString('.abc..def...gh\n.abc..def...gh', mode.getId());
		let expected1 = [
			{ className: 'mtk7', text: '.' },
			{ className: 'mtk9', text: 'abc' },
			{ className: 'mtk7', text: '..' },
			{ className: 'mtk9', text: 'def' },
			{ className: 'mtk7', text: '...' },
			{ className: 'mtk9', text: 'gh' },
		];
		let expected2 = [
			{ className: 'mtk7', text: '.' },
			{ className: 'mtk9', text: 'abc' },
			{ className: 'mtk7', text: '..' },
			{ className: 'mtk9', text: 'def' },
			{ className: 'mtk7', text: '...' },
			{ className: 'mtk9', text: 'gh' },
		];
		let expectedStr1 = toStr(expected1);
		let expectedStr2 = toStr(expected2);
		let expectedStr = `<div class="monaco-tokenized-source">${expectedStr1}<br/>${expectedStr2}</div>`;

		assert.equal(actual, expectedStr);

		mode.dispose();
E
Erich Gamma 已提交
66 67
	});

68 69
	test('tokenizeLineToHTML', () => {
		const text = 'Ciao hello world!';
A
Alex Dima 已提交
70
		const lineTokens = new ViewLineTokens([
71 72 73 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
			new ViewLineToken(
				4,
				(
					(3 << MetadataConsts.FOREGROUND_OFFSET)
					| ((FontStyle.Bold | FontStyle.Italic) << MetadataConsts.FONT_STYLE_OFFSET)
				) >>> 0
			),
			new ViewLineToken(
				5,
				(
					(1 << MetadataConsts.FOREGROUND_OFFSET)
				) >>> 0
			),
			new ViewLineToken(
				10,
				(
					(4 << MetadataConsts.FOREGROUND_OFFSET)
				) >>> 0
			),
			new ViewLineToken(
				11,
				(
					(1 << MetadataConsts.FOREGROUND_OFFSET)
				) >>> 0
			),
			new ViewLineToken(
				17,
				(
					(5 << MetadataConsts.FOREGROUND_OFFSET)
					| ((FontStyle.Underline) << MetadataConsts.FONT_STYLE_OFFSET)
				) >>> 0
			)
A
Alex Dima 已提交
103
		]);
104 105 106 107 108 109 110
		const colorMap = [null, '#000000', '#ffffff', '#ff0000', '#00ff00', '#0000ff'];

		assert.equal(
			tokenizeLineToHTML(text, lineTokens, colorMap, 0, 17, 4),
			[
				'<div>',
				'<span style="color: #ff0000;font-style: italic;font-weight: bold;">Ciao</span>',
R
rebornix 已提交
111
				'<span style="color: #000000;"> </span>',
112
				'<span style="color: #00ff00;">hello</span>',
R
rebornix 已提交
113
				'<span style="color: #000000;"> </span>',
114 115 116 117 118 119 120 121 122 123
				'<span style="color: #0000ff;text-decoration: underline;">world!</span>',
				'</div>'
			].join('')
		);

		assert.equal(
			tokenizeLineToHTML(text, lineTokens, colorMap, 0, 12, 4),
			[
				'<div>',
				'<span style="color: #ff0000;font-style: italic;font-weight: bold;">Ciao</span>',
R
rebornix 已提交
124
				'<span style="color: #000000;"> </span>',
125
				'<span style="color: #00ff00;">hello</span>',
R
rebornix 已提交
126
				'<span style="color: #000000;"> </span>',
127 128 129 130 131 132 133 134 135 136
				'<span style="color: #0000ff;text-decoration: underline;">w</span>',
				'</div>'
			].join('')
		);

		assert.equal(
			tokenizeLineToHTML(text, lineTokens, colorMap, 0, 11, 4),
			[
				'<div>',
				'<span style="color: #ff0000;font-style: italic;font-weight: bold;">Ciao</span>',
R
rebornix 已提交
137
				'<span style="color: #000000;"> </span>',
138
				'<span style="color: #00ff00;">hello</span>',
R
rebornix 已提交
139
				'<span style="color: #000000;"> </span>',
140 141 142 143 144 145 146 147 148
				'</div>'
			].join('')
		);

		assert.equal(
			tokenizeLineToHTML(text, lineTokens, colorMap, 1, 11, 4),
			[
				'<div>',
				'<span style="color: #ff0000;font-style: italic;font-weight: bold;">iao</span>',
R
rebornix 已提交
149
				'<span style="color: #000000;"> </span>',
150
				'<span style="color: #00ff00;">hello</span>',
R
rebornix 已提交
151
				'<span style="color: #000000;"> </span>',
152 153 154 155 156 157 158 159
				'</div>'
			].join('')
		);

		assert.equal(
			tokenizeLineToHTML(text, lineTokens, colorMap, 4, 11, 4),
			[
				'<div>',
R
rebornix 已提交
160
				'<span style="color: #000000;"> </span>',
161
				'<span style="color: #00ff00;">hello</span>',
R
rebornix 已提交
162
				'<span style="color: #000000;"> </span>',
163 164 165 166 167 168 169 170 171
				'</div>'
			].join('')
		);

		assert.equal(
			tokenizeLineToHTML(text, lineTokens, colorMap, 5, 11, 4),
			[
				'<div>',
				'<span style="color: #00ff00;">hello</span>',
R
rebornix 已提交
172
				'<span style="color: #000000;"> </span>',
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
				'</div>'
			].join('')
		);

		assert.equal(
			tokenizeLineToHTML(text, lineTokens, colorMap, 5, 10, 4),
			[
				'<div>',
				'<span style="color: #00ff00;">hello</span>',
				'</div>'
			].join('')
		);

		assert.equal(
			tokenizeLineToHTML(text, lineTokens, colorMap, 6, 9, 4),
			[
				'<div>',
				'<span style="color: #00ff00;">ell</span>',
				'</div>'
			].join('')
		);
	});

E
Erich Gamma 已提交
196 197
});

A
Alex Dima 已提交
198
class Mode extends MockMode {
A
Alex Dima 已提交
199

200
	private static readonly _id = new LanguageIdentifier('textToHtmlTokenizerMode', 3);
A
Alex Dima 已提交
201

E
Erich Gamma 已提交
202
	constructor() {
A
Alex Dima 已提交
203 204
		super(Mode._id);
		this._register(TokenizationRegistry.register(this.getId(), {
A
Alex Dima 已提交
205
			getInitialState: (): IState => null,
A
Alex Dima 已提交
206
			tokenize: undefined,
A
Tweaks  
Alex Dima 已提交
207
			tokenize2: (line: string, state: IState): TokenizationResult2 => {
A
Alex Dima 已提交
208 209
				let tokensArr: number[] = [];
				let prevColor: ColorId = -1;
A
Alex Dima 已提交
210
				for (let i = 0; i < line.length; i++) {
A
Alex Dima 已提交
211 212 213 214 215 216
					let colorId = line.charAt(i) === '.' ? 7 : 9;
					if (prevColor !== colorId) {
						tokensArr.push(i);
						tokensArr.push((
							colorId << MetadataConsts.FOREGROUND_OFFSET
						) >>> 0);
A
Alex Dima 已提交
217
					}
A
Alex Dima 已提交
218 219 220 221 222 223
					prevColor = colorId;
				}

				let tokens = new Uint32Array(tokensArr.length);
				for (let i = 0; i < tokens.length; i++) {
					tokens[i] = tokensArr[i];
A
Alex Dima 已提交
224
				}
A
Tweaks  
Alex Dima 已提交
225
				return new TokenizationResult2(tokens, null);
A
Alex Dima 已提交
226
			}
A
Alex Dima 已提交
227
		}));
E
Erich Gamma 已提交
228 229
	}
}