-
Notifications
You must be signed in to change notification settings - Fork 116
/
themedTokenizer.ts
51 lines (42 loc) · 1.6 KB
/
themedTokenizer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import { IGrammar, StateStack } from '../main';
import { EncodedTokenAttributes } from '../encodedTokenAttributes';
import { applyStateStackDiff, diffStateStacksRefEq } from '../diffStateStacks';
export interface IThemedToken {
content: string;
color: string;
}
export function tokenizeWithTheme(colorMap: string[], fileContents: string, grammar: IGrammar): IThemedToken[] {
const lines = fileContents.split(/\r\n|\r|\n/);
let ruleStack: StateStack | null = null;
let actual: IThemedToken[] = [], actualLen = 0;
for (let i = 0, len = lines.length; i < len; i++) {
const line = lines[i];
const result = grammar.tokenizeLine2(line, ruleStack);
const tokensLength = result.tokens.length / 2;
for (let j = 0; j < tokensLength; j++) {
const startIndex = result.tokens[2 * j];
const nextStartIndex = j + 1 < tokensLength ? result.tokens[2 * j + 2] : line.length;
const tokenText = line.substring(startIndex, nextStartIndex);
if (tokenText === '') {
continue;
}
const metadata = result.tokens[2 * j + 1];
const foreground = EncodedTokenAttributes.getForeground(metadata);
const foregroundColor = colorMap[foreground];
actual[actualLen++] = {
content: tokenText,
color: foregroundColor
};
}
if (ruleStack) {
const diff = diffStateStacksRefEq(ruleStack, result.ruleStack);
ruleStack = applyStateStackDiff(ruleStack, diff);
} else {
ruleStack = result.ruleStack;
}
}
return actual;
}