Skip to content

Commit 2d58406

Browse files
committed
get rid of TokenClassification
1 parent 5e0189c commit 2d58406

3 files changed

Lines changed: 20 additions & 67 deletions

File tree

src/vs/platform/theme/common/tokenClassificationRegistry.ts

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,15 @@ import { Event, Emitter } from 'vs/base/common/event';
1313
import { IJSONSchema, IJSONSchemaMap } from 'vs/base/common/jsonSchema';
1414

1515
export const TOKEN_TYPE_WILDCARD = '*';
16-
export const TOKEN_TYPE_WILDCARD_NUM = -1;
1716

1817
// qualified string [type|*](.modifier)*
1918
export type TokenClassificationString = string;
2019

2120
export const typeAndModifierIdPattern = '^\\w+[-_\\w+]*$';
2221
export const fontStylePattern = '^(\\s*(-?italic|-?bold|-?underline))*\\s*$';
2322

24-
export interface TokenClassification {
25-
type: number;
26-
modifiers: number;
27-
}
28-
2923
export interface TokenSelector {
30-
match(classification: TokenClassification): number;
24+
match(type: string, modifiers: string[]): number;
3125
asString(): string;
3226
}
3327

@@ -138,8 +132,6 @@ export interface ITokenClassificationRegistry {
138132
*/
139133
registerTokenModifier(id: string, description: string): void;
140134

141-
getTokenClassification(type: string, modifiers: string[]): TokenClassification | undefined;
142-
143135
/**
144136
* Parses a token selector from a selector string.
145137
* @param selectorString selector string in the form (*|type)(.modifier)*
@@ -241,8 +233,6 @@ class TokenClassificationRegistry implements ITokenClassificationRegistry {
241233
constructor() {
242234
this.tokenTypeById = {};
243235
this.tokenModifierById = {};
244-
245-
this.tokenTypeById[TOKEN_TYPE_WILDCARD] = { num: TOKEN_TYPE_WILDCARD_NUM, id: TOKEN_TYPE_WILDCARD, description: '', deprecationMessage: undefined };
246236
}
247237

248238
public registerTokenType(id: string, description: string, deprecationMessage?: string): void {
@@ -270,42 +260,27 @@ class TokenClassificationRegistry implements ITokenClassificationRegistry {
270260
this.tokenStylingSchema.properties[`*.${id}`] = getStylingSchemeEntry(description, deprecationMessage);
271261
}
272262

273-
public getTokenClassification(type: string, modifiers: string[]): TokenClassification | undefined {
274-
const tokenTypeDesc = this.tokenTypeById[type];
275-
if (!tokenTypeDesc) {
276-
return undefined;
277-
}
278-
let allModifierBits = 0;
279-
for (const modifier of modifiers) {
280-
const tokenModifierDesc = this.tokenModifierById[modifier];
281-
if (tokenModifierDesc) {
282-
allModifierBits |= tokenModifierDesc.num;
283-
}
284-
}
285-
return { type: tokenTypeDesc.num, modifiers: allModifierBits };
286-
}
287-
288-
289263
public parseTokenSelector(selectorString: string): TokenSelector {
290-
const [type, ...modifiers] = selectorString.split('.');
264+
const [selectorType, ...selectorModifiers] = selectorString.split('.');
291265

292-
const selectorClassification = this.getTokenClassification(type, modifiers);
293-
if (!selectorClassification) {
266+
if (!selectorType) {
294267
return {
295268
match: () => -1,
296269
asString: () => selectorString
297270
};
298271
}
299-
const score = getTokenStylingScore(selectorClassification);
272+
const score = selectorModifiers.length + ((selectorString !== TOKEN_TYPE_WILDCARD) ? 1 : 0);
300273

301274
return {
302-
match: (classification: TokenClassification) => {
303-
if (selectorClassification.type !== TOKEN_TYPE_WILDCARD_NUM && selectorClassification.type !== classification.type) {
275+
match: (type: string, modifiers: string[]) => {
276+
if (selectorType !== TOKEN_TYPE_WILDCARD && selectorType !== type) {
304277
return -1;
305278
}
306-
const selectorModifier = selectorClassification.modifiers;
307-
if ((classification.modifiers & selectorModifier) !== selectorModifier) {
308-
return -1;
279+
// all selector modifiers must be present
280+
for (const selectorModifier of selectorModifiers) {
281+
if (modifiers.indexOf(selectorModifier) === -1) {
282+
return -1;
283+
}
309284
}
310285
return score;
311286
},
@@ -432,16 +407,6 @@ export function getTokenClassificationRegistry(): ITokenClassificationRegistry {
432407
return tokenClassificationRegistry;
433408
}
434409

435-
function bitCount(u: number) {
436-
// https://blogs.msdn.microsoft.com/jeuge/2005/06/08/bit-fiddling-3/
437-
const uCount = u - ((u >> 1) & 0o33333333333) - ((u >> 2) & 0o11111111111);
438-
return ((uCount + (uCount >> 3)) & 0o30707070707) % 63;
439-
}
440-
441-
function getTokenStylingScore(classification: TokenClassification) {
442-
return bitCount(classification.modifiers) + ((classification.type !== TOKEN_TYPE_WILDCARD_NUM) ? 1 : 0);
443-
}
444-
445410
function getStylingSchemeEntry(description?: string, deprecationMessage?: string): IJSONSchema {
446411
return {
447412
description,

src/vs/workbench/services/themes/common/colorThemeData.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { getParseErrorMessage } from 'vs/base/common/jsonErrorMessages';
1919
import { URI } from 'vs/base/common/uri';
2020
import { parse as parsePList } from 'vs/workbench/services/themes/common/plistParser';
2121
import { startsWith } from 'vs/base/common/strings';
22-
import { TokenStyle, TokenClassification, ProbeScope, TokenStylingRule, getTokenClassificationRegistry, TokenStyleValue, TokenStyleData } from 'vs/platform/theme/common/tokenClassificationRegistry';
22+
import { TokenStyle, ProbeScope, TokenStylingRule, getTokenClassificationRegistry, TokenStyleValue, TokenStyleData } from 'vs/platform/theme/common/tokenClassificationRegistry';
2323
import { MatcherWithPriority, Matcher, createMatchers } from 'vs/workbench/services/themes/common/textMateScopeMatcher';
2424
import { IExtensionResourceLoaderService } from 'vs/workbench/services/extensionResourceLoader/common/extensionResourceLoader';
2525
import { CharCode } from 'vs/base/common/charCode';
@@ -124,7 +124,7 @@ export class ColorThemeData implements IColorTheme {
124124
return color;
125125
}
126126

127-
public getTokenStyle(classification: TokenClassification, useDefault = true, definitions: TokenStyleDefinitions = {}): TokenStyle | undefined {
127+
public getTokenStyle(type: string, modifiers: string[], useDefault = true, definitions: TokenStyleDefinitions = {}): TokenStyle | undefined {
128128
let result: any = {
129129
foreground: undefined,
130130
bold: undefined,
@@ -158,7 +158,7 @@ export class ColorThemeData implements IColorTheme {
158158
}
159159
if (this.tokenStylingRules === undefined) {
160160
for (const rule of tokenClassificationRegistry.getTokenStylingDefaultRules()) {
161-
const matchScore = rule.selector.match(classification);
161+
const matchScore = rule.selector.match(type, modifiers);
162162
if (matchScore >= 0) {
163163
let style: TokenStyle | undefined;
164164
if (rule.defaults.scopesToProbe) {
@@ -178,14 +178,14 @@ export class ColorThemeData implements IColorTheme {
178178
}
179179
} else {
180180
for (const rule of this.tokenStylingRules) {
181-
const matchScore = rule.selector.match(classification);
181+
const matchScore = rule.selector.match(type, modifiers);
182182
if (matchScore >= 0) {
183183
_processStyle(matchScore, rule.style, rule);
184184
}
185185
}
186186
}
187187
for (const rule of this.customTokenStylingRules) {
188-
const matchScore = rule.selector.match(classification);
188+
const matchScore = rule.selector.match(type, modifiers);
189189
if (matchScore >= 0) {
190190
_processStyle(matchScore, rule.style, rule);
191191
}
@@ -202,10 +202,7 @@ export class ColorThemeData implements IColorTheme {
202202
return undefined;
203203
} else if (typeof tokenStyleValue === 'string') {
204204
const [type, ...modifiers] = tokenStyleValue.split('.');
205-
const classification = tokenClassificationRegistry.getTokenClassification(type, modifiers);
206-
if (classification) {
207-
return this.getTokenStyle(classification);
208-
}
205+
return this.getTokenStyle(type, modifiers);
209206
} else if (typeof tokenStyleValue === 'object') {
210207
return tokenStyleValue;
211208
}
@@ -243,11 +240,7 @@ export class ColorThemeData implements IColorTheme {
243240
}
244241

245242
public getTokenStyleMetadata(type: string, modifiers: string[], useDefault = true, definitions: TokenStyleDefinitions = {}): ITokenStyle | undefined {
246-
const classification = tokenClassificationRegistry.getTokenClassification(type, modifiers);
247-
if (!classification) {
248-
return undefined;
249-
}
250-
const style = this.getTokenStyle(classification, useDefault, definitions);
243+
const style = this.getTokenStyle(type, modifiers, useDefault, definitions);
251244
if (!style) {
252245
return undefined;
253246
}

src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { ColorThemeData } from 'vs/workbench/services/themes/common/colorThemeData';
77
import * as assert from 'assert';
88
import { ITokenColorCustomizations } from 'vs/workbench/services/themes/common/workbenchThemeService';
9-
import { TokenStyle, getTokenClassificationRegistry } from 'vs/platform/theme/common/tokenClassificationRegistry';
9+
import { TokenStyle } from 'vs/platform/theme/common/tokenClassificationRegistry';
1010
import { Color } from 'vs/base/common/color';
1111
import { isString } from 'vs/base/common/types';
1212
import { FileService } from 'vs/platform/files/common/fileService';
@@ -18,8 +18,6 @@ import { getPathFromAmdModule } from 'vs/base/common/amd';
1818
import { ExtensionResourceLoaderService } from 'vs/workbench/services/extensionResourceLoader/electron-browser/extensionResourceLoaderService';
1919
import { ITokenStyle } from 'vs/platform/theme/common/themeService';
2020

21-
let tokenClassificationRegistry = getTokenClassificationRegistry();
22-
2321
const undefinedStyle = { bold: undefined, underline: undefined, italic: undefined };
2422
const unsetStyle = { bold: false, underline: false, italic: false };
2523

@@ -73,10 +71,7 @@ function assertTokenStyles(themeData: ColorThemeData, expected: { [qualifiedClas
7371
for (let qualifiedClassifier in expected) {
7472
const [type, ...modifiers] = qualifiedClassifier.split('.');
7573

76-
const classification = tokenClassificationRegistry.getTokenClassification(type, modifiers);
77-
assert.ok(classification, 'Classification not found');
78-
79-
const tokenStyle = themeData.getTokenStyle(classification!);
74+
const tokenStyle = themeData.getTokenStyle(type, modifiers);
8075
const expectedTokenStyle = expected[qualifiedClassifier];
8176
assertTokenStyle(tokenStyle, expectedTokenStyle, qualifiedClassifier);
8277

0 commit comments

Comments
 (0)