提交 2d584060 编写于 作者: M Martin Aeschlimann

get rid of TokenClassification

上级 5e0189c8
...@@ -13,7 +13,6 @@ import { Event, Emitter } from 'vs/base/common/event'; ...@@ -13,7 +13,6 @@ import { Event, Emitter } from 'vs/base/common/event';
import { IJSONSchema, IJSONSchemaMap } from 'vs/base/common/jsonSchema'; import { IJSONSchema, IJSONSchemaMap } from 'vs/base/common/jsonSchema';
export const TOKEN_TYPE_WILDCARD = '*'; export const TOKEN_TYPE_WILDCARD = '*';
export const TOKEN_TYPE_WILDCARD_NUM = -1;
// qualified string [type|*](.modifier)* // qualified string [type|*](.modifier)*
export type TokenClassificationString = string; export type TokenClassificationString = string;
...@@ -21,13 +20,8 @@ export type TokenClassificationString = string; ...@@ -21,13 +20,8 @@ export type TokenClassificationString = string;
export const typeAndModifierIdPattern = '^\\w+[-_\\w+]*$'; export const typeAndModifierIdPattern = '^\\w+[-_\\w+]*$';
export const fontStylePattern = '^(\\s*(-?italic|-?bold|-?underline))*\\s*$'; export const fontStylePattern = '^(\\s*(-?italic|-?bold|-?underline))*\\s*$';
export interface TokenClassification {
type: number;
modifiers: number;
}
export interface TokenSelector { export interface TokenSelector {
match(classification: TokenClassification): number; match(type: string, modifiers: string[]): number;
asString(): string; asString(): string;
} }
...@@ -138,8 +132,6 @@ export interface ITokenClassificationRegistry { ...@@ -138,8 +132,6 @@ export interface ITokenClassificationRegistry {
*/ */
registerTokenModifier(id: string, description: string): void; registerTokenModifier(id: string, description: string): void;
getTokenClassification(type: string, modifiers: string[]): TokenClassification | undefined;
/** /**
* Parses a token selector from a selector string. * Parses a token selector from a selector string.
* @param selectorString selector string in the form (*|type)(.modifier)* * @param selectorString selector string in the form (*|type)(.modifier)*
...@@ -241,8 +233,6 @@ class TokenClassificationRegistry implements ITokenClassificationRegistry { ...@@ -241,8 +233,6 @@ class TokenClassificationRegistry implements ITokenClassificationRegistry {
constructor() { constructor() {
this.tokenTypeById = {}; this.tokenTypeById = {};
this.tokenModifierById = {}; this.tokenModifierById = {};
this.tokenTypeById[TOKEN_TYPE_WILDCARD] = { num: TOKEN_TYPE_WILDCARD_NUM, id: TOKEN_TYPE_WILDCARD, description: '', deprecationMessage: undefined };
} }
public registerTokenType(id: string, description: string, deprecationMessage?: string): void { public registerTokenType(id: string, description: string, deprecationMessage?: string): void {
...@@ -270,42 +260,27 @@ class TokenClassificationRegistry implements ITokenClassificationRegistry { ...@@ -270,42 +260,27 @@ class TokenClassificationRegistry implements ITokenClassificationRegistry {
this.tokenStylingSchema.properties[`*.${id}`] = getStylingSchemeEntry(description, deprecationMessage); this.tokenStylingSchema.properties[`*.${id}`] = getStylingSchemeEntry(description, deprecationMessage);
} }
public getTokenClassification(type: string, modifiers: string[]): TokenClassification | undefined {
const tokenTypeDesc = this.tokenTypeById[type];
if (!tokenTypeDesc) {
return undefined;
}
let allModifierBits = 0;
for (const modifier of modifiers) {
const tokenModifierDesc = this.tokenModifierById[modifier];
if (tokenModifierDesc) {
allModifierBits |= tokenModifierDesc.num;
}
}
return { type: tokenTypeDesc.num, modifiers: allModifierBits };
}
public parseTokenSelector(selectorString: string): TokenSelector { public parseTokenSelector(selectorString: string): TokenSelector {
const [type, ...modifiers] = selectorString.split('.'); const [selectorType, ...selectorModifiers] = selectorString.split('.');
const selectorClassification = this.getTokenClassification(type, modifiers); if (!selectorType) {
if (!selectorClassification) {
return { return {
match: () => -1, match: () => -1,
asString: () => selectorString asString: () => selectorString
}; };
} }
const score = getTokenStylingScore(selectorClassification); const score = selectorModifiers.length + ((selectorString !== TOKEN_TYPE_WILDCARD) ? 1 : 0);
return { return {
match: (classification: TokenClassification) => { match: (type: string, modifiers: string[]) => {
if (selectorClassification.type !== TOKEN_TYPE_WILDCARD_NUM && selectorClassification.type !== classification.type) { if (selectorType !== TOKEN_TYPE_WILDCARD && selectorType !== type) {
return -1; return -1;
} }
const selectorModifier = selectorClassification.modifiers; // all selector modifiers must be present
if ((classification.modifiers & selectorModifier) !== selectorModifier) { for (const selectorModifier of selectorModifiers) {
return -1; if (modifiers.indexOf(selectorModifier) === -1) {
return -1;
}
} }
return score; return score;
}, },
...@@ -432,16 +407,6 @@ export function getTokenClassificationRegistry(): ITokenClassificationRegistry { ...@@ -432,16 +407,6 @@ export function getTokenClassificationRegistry(): ITokenClassificationRegistry {
return tokenClassificationRegistry; return tokenClassificationRegistry;
} }
function bitCount(u: number) {
// https://blogs.msdn.microsoft.com/jeuge/2005/06/08/bit-fiddling-3/
const uCount = u - ((u >> 1) & 0o33333333333) - ((u >> 2) & 0o11111111111);
return ((uCount + (uCount >> 3)) & 0o30707070707) % 63;
}
function getTokenStylingScore(classification: TokenClassification) {
return bitCount(classification.modifiers) + ((classification.type !== TOKEN_TYPE_WILDCARD_NUM) ? 1 : 0);
}
function getStylingSchemeEntry(description?: string, deprecationMessage?: string): IJSONSchema { function getStylingSchemeEntry(description?: string, deprecationMessage?: string): IJSONSchema {
return { return {
description, description,
......
...@@ -19,7 +19,7 @@ import { getParseErrorMessage } from 'vs/base/common/jsonErrorMessages'; ...@@ -19,7 +19,7 @@ import { getParseErrorMessage } from 'vs/base/common/jsonErrorMessages';
import { URI } from 'vs/base/common/uri'; import { URI } from 'vs/base/common/uri';
import { parse as parsePList } from 'vs/workbench/services/themes/common/plistParser'; import { parse as parsePList } from 'vs/workbench/services/themes/common/plistParser';
import { startsWith } from 'vs/base/common/strings'; import { startsWith } from 'vs/base/common/strings';
import { TokenStyle, TokenClassification, ProbeScope, TokenStylingRule, getTokenClassificationRegistry, TokenStyleValue, TokenStyleData } from 'vs/platform/theme/common/tokenClassificationRegistry'; import { TokenStyle, ProbeScope, TokenStylingRule, getTokenClassificationRegistry, TokenStyleValue, TokenStyleData } from 'vs/platform/theme/common/tokenClassificationRegistry';
import { MatcherWithPriority, Matcher, createMatchers } from 'vs/workbench/services/themes/common/textMateScopeMatcher'; import { MatcherWithPriority, Matcher, createMatchers } from 'vs/workbench/services/themes/common/textMateScopeMatcher';
import { IExtensionResourceLoaderService } from 'vs/workbench/services/extensionResourceLoader/common/extensionResourceLoader'; import { IExtensionResourceLoaderService } from 'vs/workbench/services/extensionResourceLoader/common/extensionResourceLoader';
import { CharCode } from 'vs/base/common/charCode'; import { CharCode } from 'vs/base/common/charCode';
...@@ -124,7 +124,7 @@ export class ColorThemeData implements IColorTheme { ...@@ -124,7 +124,7 @@ export class ColorThemeData implements IColorTheme {
return color; return color;
} }
public getTokenStyle(classification: TokenClassification, useDefault = true, definitions: TokenStyleDefinitions = {}): TokenStyle | undefined { public getTokenStyle(type: string, modifiers: string[], useDefault = true, definitions: TokenStyleDefinitions = {}): TokenStyle | undefined {
let result: any = { let result: any = {
foreground: undefined, foreground: undefined,
bold: undefined, bold: undefined,
...@@ -158,7 +158,7 @@ export class ColorThemeData implements IColorTheme { ...@@ -158,7 +158,7 @@ export class ColorThemeData implements IColorTheme {
} }
if (this.tokenStylingRules === undefined) { if (this.tokenStylingRules === undefined) {
for (const rule of tokenClassificationRegistry.getTokenStylingDefaultRules()) { for (const rule of tokenClassificationRegistry.getTokenStylingDefaultRules()) {
const matchScore = rule.selector.match(classification); const matchScore = rule.selector.match(type, modifiers);
if (matchScore >= 0) { if (matchScore >= 0) {
let style: TokenStyle | undefined; let style: TokenStyle | undefined;
if (rule.defaults.scopesToProbe) { if (rule.defaults.scopesToProbe) {
...@@ -178,14 +178,14 @@ export class ColorThemeData implements IColorTheme { ...@@ -178,14 +178,14 @@ export class ColorThemeData implements IColorTheme {
} }
} else { } else {
for (const rule of this.tokenStylingRules) { for (const rule of this.tokenStylingRules) {
const matchScore = rule.selector.match(classification); const matchScore = rule.selector.match(type, modifiers);
if (matchScore >= 0) { if (matchScore >= 0) {
_processStyle(matchScore, rule.style, rule); _processStyle(matchScore, rule.style, rule);
} }
} }
} }
for (const rule of this.customTokenStylingRules) { for (const rule of this.customTokenStylingRules) {
const matchScore = rule.selector.match(classification); const matchScore = rule.selector.match(type, modifiers);
if (matchScore >= 0) { if (matchScore >= 0) {
_processStyle(matchScore, rule.style, rule); _processStyle(matchScore, rule.style, rule);
} }
...@@ -202,10 +202,7 @@ export class ColorThemeData implements IColorTheme { ...@@ -202,10 +202,7 @@ export class ColorThemeData implements IColorTheme {
return undefined; return undefined;
} else if (typeof tokenStyleValue === 'string') { } else if (typeof tokenStyleValue === 'string') {
const [type, ...modifiers] = tokenStyleValue.split('.'); const [type, ...modifiers] = tokenStyleValue.split('.');
const classification = tokenClassificationRegistry.getTokenClassification(type, modifiers); return this.getTokenStyle(type, modifiers);
if (classification) {
return this.getTokenStyle(classification);
}
} else if (typeof tokenStyleValue === 'object') { } else if (typeof tokenStyleValue === 'object') {
return tokenStyleValue; return tokenStyleValue;
} }
...@@ -243,11 +240,7 @@ export class ColorThemeData implements IColorTheme { ...@@ -243,11 +240,7 @@ export class ColorThemeData implements IColorTheme {
} }
public getTokenStyleMetadata(type: string, modifiers: string[], useDefault = true, definitions: TokenStyleDefinitions = {}): ITokenStyle | undefined { public getTokenStyleMetadata(type: string, modifiers: string[], useDefault = true, definitions: TokenStyleDefinitions = {}): ITokenStyle | undefined {
const classification = tokenClassificationRegistry.getTokenClassification(type, modifiers); const style = this.getTokenStyle(type, modifiers, useDefault, definitions);
if (!classification) {
return undefined;
}
const style = this.getTokenStyle(classification, useDefault, definitions);
if (!style) { if (!style) {
return undefined; return undefined;
} }
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
import { ColorThemeData } from 'vs/workbench/services/themes/common/colorThemeData'; import { ColorThemeData } from 'vs/workbench/services/themes/common/colorThemeData';
import * as assert from 'assert'; import * as assert from 'assert';
import { ITokenColorCustomizations } from 'vs/workbench/services/themes/common/workbenchThemeService'; import { ITokenColorCustomizations } from 'vs/workbench/services/themes/common/workbenchThemeService';
import { TokenStyle, getTokenClassificationRegistry } from 'vs/platform/theme/common/tokenClassificationRegistry'; import { TokenStyle } from 'vs/platform/theme/common/tokenClassificationRegistry';
import { Color } from 'vs/base/common/color'; import { Color } from 'vs/base/common/color';
import { isString } from 'vs/base/common/types'; import { isString } from 'vs/base/common/types';
import { FileService } from 'vs/platform/files/common/fileService'; import { FileService } from 'vs/platform/files/common/fileService';
...@@ -18,8 +18,6 @@ import { getPathFromAmdModule } from 'vs/base/common/amd'; ...@@ -18,8 +18,6 @@ import { getPathFromAmdModule } from 'vs/base/common/amd';
import { ExtensionResourceLoaderService } from 'vs/workbench/services/extensionResourceLoader/electron-browser/extensionResourceLoaderService'; import { ExtensionResourceLoaderService } from 'vs/workbench/services/extensionResourceLoader/electron-browser/extensionResourceLoaderService';
import { ITokenStyle } from 'vs/platform/theme/common/themeService'; import { ITokenStyle } from 'vs/platform/theme/common/themeService';
let tokenClassificationRegistry = getTokenClassificationRegistry();
const undefinedStyle = { bold: undefined, underline: undefined, italic: undefined }; const undefinedStyle = { bold: undefined, underline: undefined, italic: undefined };
const unsetStyle = { bold: false, underline: false, italic: false }; const unsetStyle = { bold: false, underline: false, italic: false };
...@@ -73,10 +71,7 @@ function assertTokenStyles(themeData: ColorThemeData, expected: { [qualifiedClas ...@@ -73,10 +71,7 @@ function assertTokenStyles(themeData: ColorThemeData, expected: { [qualifiedClas
for (let qualifiedClassifier in expected) { for (let qualifiedClassifier in expected) {
const [type, ...modifiers] = qualifiedClassifier.split('.'); const [type, ...modifiers] = qualifiedClassifier.split('.');
const classification = tokenClassificationRegistry.getTokenClassification(type, modifiers); const tokenStyle = themeData.getTokenStyle(type, modifiers);
assert.ok(classification, 'Classification not found');
const tokenStyle = themeData.getTokenStyle(classification!);
const expectedTokenStyle = expected[qualifiedClassifier]; const expectedTokenStyle = expected[qualifiedClassifier];
assertTokenStyle(tokenStyle, expectedTokenStyle, qualifiedClassifier); assertTokenStyle(tokenStyle, expectedTokenStyle, qualifiedClassifier);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册