From 8db09a42ff4670fdb600d503c687d0ea488b03b1 Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Fri, 13 Sep 2019 12:15:50 +0200 Subject: [PATCH 01/15] introducing tokenScopes --- .../browser/standaloneThemeServiceImpl.ts | 6 + .../test/browser/standaloneLanguages.test.ts | 5 +- src/vs/platform/theme/common/themeService.ts | 3 + .../theme/common/tokenStyleRegistry.ts | 268 ++++++++++++++++++ .../theme/test/common/testThemeService.ts | 5 + .../terminalColorRegistry.test.ts | 6 +- .../services/themes/common/colorThemeData.ts | 153 +++++++++- .../themes/common/textMateScopeMatcher.ts | 111 ++++++++ .../tokenStyleResolving.test.ts | 89 ++++++ 9 files changed, 640 insertions(+), 6 deletions(-) create mode 100644 src/vs/platform/theme/common/tokenStyleRegistry.ts create mode 100644 src/vs/workbench/services/themes/common/textMateScopeMatcher.ts create mode 100644 src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts diff --git a/src/vs/editor/standalone/browser/standaloneThemeServiceImpl.ts b/src/vs/editor/standalone/browser/standaloneThemeServiceImpl.ts index 613ec87b2de..b26e44bf76c 100644 --- a/src/vs/editor/standalone/browser/standaloneThemeServiceImpl.ts +++ b/src/vs/editor/standalone/browser/standaloneThemeServiceImpl.ts @@ -14,6 +14,7 @@ import { IEnvironmentService } from 'vs/platform/environment/common/environment' import { Registry } from 'vs/platform/registry/common/platform'; import { ColorIdentifier, Extensions, IColorRegistry } from 'vs/platform/theme/common/colorRegistry'; import { Extensions as ThemingExtensions, ICssStyleCollector, IIconTheme, IThemingRegistry } from 'vs/platform/theme/common/themeService'; +import { TokenStyle, TokenStyleIdentifier } from 'vs/platform/theme/common/tokenStyleRegistry'; const VS_THEME_NAME = 'vs'; const VS_DARK_THEME_NAME = 'vs-dark'; @@ -23,6 +24,7 @@ const colorRegistry = Registry.as(Extensions.ColorContribution); const themingRegistry = Registry.as(ThemingExtensions.ThemingContribution); class StandaloneTheme implements IStandaloneTheme { + public readonly id: string; public readonly themeName: string; @@ -128,6 +130,10 @@ class StandaloneTheme implements IStandaloneTheme { } return this._tokenTheme; } + + getTokenStyle(tokenStyle: TokenStyleIdentifier, useDefault?: boolean | undefined): TokenStyle | undefined { + return undefined; + } } function isBuiltinTheme(themeName: string): themeName is BuiltinTheme { diff --git a/src/vs/editor/standalone/test/browser/standaloneLanguages.test.ts b/src/vs/editor/standalone/test/browser/standaloneLanguages.test.ts index b35c23cc03a..5dfa7d92149 100644 --- a/src/vs/editor/standalone/test/browser/standaloneLanguages.test.ts +++ b/src/vs/editor/standalone/test/browser/standaloneLanguages.test.ts @@ -13,6 +13,7 @@ import { ILineTokens, IToken, TokenizationSupport2Adapter, TokensProvider } from import { IStandaloneTheme, IStandaloneThemeData, IStandaloneThemeService } from 'vs/editor/standalone/common/standaloneThemeService'; import { ColorIdentifier } from 'vs/platform/theme/common/colorRegistry'; import { IIconTheme, ITheme, LIGHT } from 'vs/platform/theme/common/themeService'; +import { TokenStyleIdentifier } from 'vs/platform/theme/common/tokenStyleRegistry'; suite('TokenizationSupport2Adapter', () => { @@ -54,7 +55,9 @@ suite('TokenizationSupport2Adapter', () => { defines: (color: ColorIdentifier): boolean => { throw new Error('Not implemented'); - } + }, + + getTokenStyle: (tokenStyleId: TokenStyleIdentifier) => undefined }; } public getIconTheme(): IIconTheme { diff --git a/src/vs/platform/theme/common/themeService.ts b/src/vs/platform/theme/common/themeService.ts index 42e93300a85..7ac9788d1a7 100644 --- a/src/vs/platform/theme/common/themeService.ts +++ b/src/vs/platform/theme/common/themeService.ts @@ -10,6 +10,7 @@ import * as platform from 'vs/platform/registry/common/platform'; import { ColorIdentifier } from 'vs/platform/theme/common/colorRegistry'; import { Event, Emitter } from 'vs/base/common/event'; import { IEnvironmentService } from 'vs/platform/environment/common/environment'; +import { TokenStyleIdentifier, TokenStyle } from 'vs/platform/theme/common/tokenStyleRegistry'; export const IThemeService = createDecorator('themeService'); @@ -59,6 +60,8 @@ export interface ITheme { * default color will be used. */ defines(color: ColorIdentifier): boolean; + + getTokenStyle(color: TokenStyleIdentifier, useDefault?: boolean): TokenStyle | undefined; } export interface IIconTheme { diff --git a/src/vs/platform/theme/common/tokenStyleRegistry.ts b/src/vs/platform/theme/common/tokenStyleRegistry.ts new file mode 100644 index 00000000000..5f593d41370 --- /dev/null +++ b/src/vs/platform/theme/common/tokenStyleRegistry.ts @@ -0,0 +1,268 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import * as platform from 'vs/platform/registry/common/platform'; +import { IJSONSchema, IJSONSchemaMap } from 'vs/base/common/jsonSchema'; +import { Color } from 'vs/base/common/color'; +import { ITheme } from 'vs/platform/theme/common/themeService'; +import { Event, Emitter } from 'vs/base/common/event'; + +import { Extensions as JSONExtensions, IJSONContributionRegistry } from 'vs/platform/jsonschemas/common/jsonContributionRegistry'; +import { RunOnceScheduler } from 'vs/base/common/async'; + +// ------ API types + +export type TokenStyleIdentifier = string; + +export interface TokenStyleContribution { + readonly id: TokenStyleIdentifier; + readonly description: string; + readonly defaults: TokenStyleDefaults | null; + readonly deprecationMessage: string | undefined; +} + +export const enum TokenStyleBits { + BOLD = 0x01, + UNDERLINE = 0x02, + ITALIC = 0x04 +} + +export class TokenStyle { + constructor( + public readonly foreground?: Color, + public readonly background?: Color, + public readonly styles?: number + ) { + + } + + hasStyle(style: number): boolean { + return !!this.styles && ((this.styles & style) === style); + } +} + +export namespace TokenStyle { + export function fromString(s: string) { + const parts = s.split('-'); + let part = parts.shift(); + if (part) { + const foreground = Color.fromHex(part); + let background = undefined; + let style = undefined; + part = parts.shift(); + if (part && part[0] === '#') { + background = Color.fromHex(part); + part = parts.shift(); + } + if (part) { + try { + style = parseInt(part); + } catch (e) { + // ignore + } + } + return new TokenStyle(foreground, background, style); + } + return new TokenStyle(Color.red); + } +} + + + +export interface TokenStyleFunction { + (theme: ITheme): TokenStyle | undefined; +} + +export interface TokenStyleDefaults { + scopesToProbe?: string[]; + light: TokenStyle | null; + dark: TokenStyle | null; + hc: TokenStyle | null; +} + +/** + * A TokenStyle Value is either a tokestyle literal, a reference to other color or a derived color + */ +export type TokenStyleValue = TokenStyle | string | TokenStyleIdentifier | TokenStyleFunction; + +// TokenStyle registry +export const Extensions = { + TokenStyleContribution: 'base.contributions.tokenStyles' +}; + +export interface ITokenStyleRegistry { + + readonly onDidChangeSchema: Event; + + /** + * Register a TokenStyle to the registry. + * @param id The TokenStyle id as used in theme description files + * @param defaults The default values + * @description the description + */ + registerTokenStyle(id: string, defaults: TokenStyleDefaults, description: string): TokenStyleIdentifier; + + /** + * Register a TokenStyle to the registry. + */ + deregisterTokenStyle(id: string): void; + + /** + * Get all TokenStyle contributions + */ + getTokenStyles(): TokenStyleContribution[]; + + /** + * Gets the default TokenStyle of the given id + */ + resolveDefaultTokenStyle(id: TokenStyleIdentifier, theme: ITheme, findTokenStyleForScope: (scope: string) => TokenStyle | undefined): TokenStyle | undefined; + + /** + * JSON schema for an object to assign TokenStyle values to one of the TokenStyle contributions. + */ + getTokenStyleSchema(): IJSONSchema; + + /** + * JSON schema to for a reference to a TokenStyle contribution. + */ + getTokenStyleReferenceSchema(): IJSONSchema; + +} + + + +class TokenStyleRegistry implements ITokenStyleRegistry { + + private readonly _onDidChangeSchema = new Emitter(); + readonly onDidChangeSchema: Event = this._onDidChangeSchema.event; + + private tokenStyleById: { [key: string]: TokenStyleContribution }; + private tokenStyleSchema: IJSONSchema & { properties: IJSONSchemaMap } = { type: 'object', properties: {} }; + private tokenStyleReferenceSchema: IJSONSchema & { enum: string[], enumDescriptions: string[] } = { type: 'string', enum: [], enumDescriptions: [] }; + + constructor() { + this.tokenStyleById = {}; + } + + public registerTokenStyle(id: string, defaults: TokenStyleDefaults | null, description: string, deprecationMessage?: string): TokenStyleIdentifier { + let colorContribution: TokenStyleContribution = { id, description, defaults, deprecationMessage }; + this.tokenStyleById[id] = colorContribution; + let propertySchema: IJSONSchema = { type: 'string', description, format: 'color-hex', default: '#ff0000' }; + if (deprecationMessage) { + propertySchema.deprecationMessage = deprecationMessage; + } + this.tokenStyleSchema.properties[id] = propertySchema; + this.tokenStyleReferenceSchema.enum.push(id); + this.tokenStyleReferenceSchema.enumDescriptions.push(description); + + this._onDidChangeSchema.fire(); + return id; + } + + + public deregisterTokenStyle(id: string): void { + delete this.tokenStyleById[id]; + delete this.tokenStyleSchema.properties[id]; + const index = this.tokenStyleReferenceSchema.enum.indexOf(id); + if (index !== -1) { + this.tokenStyleReferenceSchema.enum.splice(index, 1); + this.tokenStyleReferenceSchema.enumDescriptions.splice(index, 1); + } + this._onDidChangeSchema.fire(); + } + + public getTokenStyles(): TokenStyleContribution[] { + return Object.keys(this.tokenStyleById).map(id => this.tokenStyleById[id]); + } + + public resolveDefaultTokenStyle(id: TokenStyleIdentifier, theme: ITheme, findTokenStyleForScope: (scope: string) => TokenStyle | undefined): TokenStyle | undefined { + const tokenStyleDesc = this.tokenStyleById[id]; + if (tokenStyleDesc && tokenStyleDesc.defaults) { + const scopesToProbe = tokenStyleDesc.defaults.scopesToProbe; + if (scopesToProbe) { + for (let scope of scopesToProbe) { + const style = findTokenStyleForScope(scope); + if (style) { + return style; + } + } + } + const tokenStyleValue = tokenStyleDesc.defaults[theme.type]; + return resolveTokenStyleValue(tokenStyleValue, theme); + } + return undefined; + } + + public getTokenStyleSchema(): IJSONSchema { + return this.tokenStyleSchema; + } + + public getTokenStyleReferenceSchema(): IJSONSchema { + return this.tokenStyleReferenceSchema; + } + + public toString() { + let sorter = (a: string, b: string) => { + let cat1 = a.indexOf('.') === -1 ? 0 : 1; + let cat2 = b.indexOf('.') === -1 ? 0 : 1; + if (cat1 !== cat2) { + return cat1 - cat2; + } + return a.localeCompare(b); + }; + + return Object.keys(this.tokenStyleById).sort(sorter).map(k => `- \`${k}\`: ${this.tokenStyleById[k].description}`).join('\n'); + } + +} + +const tokenStyleRegistry = new TokenStyleRegistry(); +platform.Registry.add(Extensions.TokenStyleContribution, tokenStyleRegistry); + +export function registerTokenStyle(id: string, defaults: TokenStyleDefaults | null, description: string, deprecationMessage?: string): TokenStyleIdentifier { + return tokenStyleRegistry.registerTokenStyle(id, defaults, description, deprecationMessage); +} + +export function getTokenStyleRegistry(): ITokenStyleRegistry { + return tokenStyleRegistry; +} + +// ----- implementation + +/** + * @param colorValue Resolve a color value in the context of a theme + */ +function resolveTokenStyleValue(tokenStyleValue: TokenStyleValue | null, theme: ITheme): TokenStyle | undefined { + if (tokenStyleValue === null) { + return undefined; + } else if (typeof tokenStyleValue === 'string') { + if (tokenStyleValue[0] === '#') { + return TokenStyle.fromString(tokenStyleValue); + } + return theme.getTokenStyle(tokenStyleValue); + } else if (typeof tokenStyleValue === 'object') { + return tokenStyleValue; + } else if (typeof tokenStyleValue === 'function') { + return tokenStyleValue(theme); + } + return undefined; +} + +export const tokenStyleColorsSchemaId = 'vscode://schemas/workbench-tokenstyles'; + +let schemaRegistry = platform.Registry.as(JSONExtensions.JSONContribution); +schemaRegistry.registerSchema(tokenStyleColorsSchemaId, tokenStyleRegistry.getTokenStyleSchema()); + +const delayer = new RunOnceScheduler(() => schemaRegistry.notifySchemaChanged(tokenStyleColorsSchemaId), 200); +tokenStyleRegistry.onDidChangeSchema(() => { + if (!delayer.isScheduled()) { + delayer.schedule(); + } +}); + +// setTimeout(_ => console.log(colorRegistry.toString()), 5000); + + + diff --git a/src/vs/platform/theme/test/common/testThemeService.ts b/src/vs/platform/theme/test/common/testThemeService.ts index fd49d393a34..e384b457389 100644 --- a/src/vs/platform/theme/test/common/testThemeService.ts +++ b/src/vs/platform/theme/test/common/testThemeService.ts @@ -6,6 +6,7 @@ import { Event, Emitter } from 'vs/base/common/event'; import { IThemeService, ITheme, DARK, IIconTheme } from 'vs/platform/theme/common/themeService'; import { Color } from 'vs/base/common/color'; +import { TokenStyle, TokenStyleIdentifier } from 'vs/platform/theme/common/tokenStyleRegistry'; export class TestTheme implements ITheme { @@ -23,6 +24,10 @@ export class TestTheme implements ITheme { defines(color: string): boolean { throw new Error('Method not implemented.'); } + + getTokenStyle(tokenStyleIdentifier: TokenStyleIdentifier, useDefault?: boolean | undefined): TokenStyle | undefined { + return undefined; + } } export class TestIconTheme implements IIconTheme { diff --git a/src/vs/workbench/contrib/terminal/test/electron-browser/terminalColorRegistry.test.ts b/src/vs/workbench/contrib/terminal/test/electron-browser/terminalColorRegistry.test.ts index bf413e442d4..d2d64fa1a24 100644 --- a/src/vs/workbench/contrib/terminal/test/electron-browser/terminalColorRegistry.test.ts +++ b/src/vs/workbench/contrib/terminal/test/electron-browser/terminalColorRegistry.test.ts @@ -9,6 +9,7 @@ import { Registry } from 'vs/platform/registry/common/platform'; import { ansiColorIdentifiers, registerColors } from 'vs/workbench/contrib/terminal/common/terminalColorRegistry'; import { ITheme, ThemeType } from 'vs/platform/theme/common/themeService'; import { Color } from 'vs/base/common/color'; +import { TokenStyleIdentifier } from 'vs/platform/theme/common/tokenStyleRegistry'; registerColors(); @@ -19,7 +20,8 @@ function getMockTheme(type: ThemeType): ITheme { label: '', type: type, getColor: (colorId: ColorIdentifier): Color | undefined => themingRegistry.resolveDefaultColor(colorId, theme), - defines: () => true + defines: () => true, + getTokenStyle: (tokenStyleId: TokenStyleIdentifier) => undefined }; return theme; } @@ -99,4 +101,4 @@ suite('Workbench - TerminalColorRegistry', () => { '#e5e5e5' ], 'The dark terminal colors should be used when a dark theme is active'); }); -}); \ No newline at end of file +}); diff --git a/src/vs/workbench/services/themes/common/colorThemeData.ts b/src/vs/workbench/services/themes/common/colorThemeData.ts index 6d39ebd157e..fcb5bd87bd5 100644 --- a/src/vs/workbench/services/themes/common/colorThemeData.ts +++ b/src/vs/workbench/services/themes/common/colorThemeData.ts @@ -12,7 +12,7 @@ import * as nls from 'vs/nls'; import * as types from 'vs/base/common/types'; import * as objects from 'vs/base/common/objects'; import * as resources from 'vs/base/common/resources'; -import { Extensions, IColorRegistry, ColorIdentifier, editorBackground, editorForeground } from 'vs/platform/theme/common/colorRegistry'; +import { Extensions as ColorRegistryExtensions, IColorRegistry, ColorIdentifier, editorBackground, editorForeground } from 'vs/platform/theme/common/colorRegistry'; import { ThemeType } from 'vs/platform/theme/common/themeService'; import { Registry } from 'vs/platform/registry/common/platform'; import { getParseErrorMessage } from 'vs/base/common/jsonErrorMessages'; @@ -20,8 +20,12 @@ import { URI } from 'vs/base/common/uri'; import { IFileService } from 'vs/platform/files/common/files'; import { parse as parsePList } from 'vs/workbench/services/themes/common/plistParser'; import { startsWith } from 'vs/base/common/strings'; +import { Extensions as TokenStyleRegistryExtensions, TokenStyle, TokenStyleIdentifier, ITokenStyleRegistry, TokenStyleBits } from 'vs/platform/theme/common/tokenStyleRegistry'; +import { MatcherWithPriority, Matcher, createMatchers } from 'vs/workbench/services/themes/common/textMateScopeMatcher'; -let colorRegistry = Registry.as(Extensions.ColorContribution); +let colorRegistry = Registry.as(ColorRegistryExtensions.ColorContribution); + +let tokenStyleRegistry = Registry.as(TokenStyleRegistryExtensions.TokenStyleContribution); const tokenGroupToScopesMap = { comments: ['comment'], @@ -33,6 +37,10 @@ const tokenGroupToScopesMap = { variables: ['variable', 'entity.name.variable'] }; +interface ITokenStyleMap { + [id: string]: TokenStyle; +} + export class ColorThemeData implements IColorTheme { id: string; @@ -49,6 +57,11 @@ export class ColorThemeData implements IColorTheme { private colorMap: IColorMap = {}; private customColorMap: IColorMap = {}; + private tokenStyleMap: ITokenStyleMap | undefined; + + private themeTokenScopeMatchers: Matcher[] | undefined; + private customTokenScopeMatchers: Matcher[] | undefined; + private constructor(id: string, label: string, settingsId: string) { this.id = id; this.label = label; @@ -103,10 +116,81 @@ export class ColorThemeData implements IColorTheme { return color; } + public getTokenStyle(tokenStyleId: TokenStyleIdentifier): TokenStyle | undefined { + if (!this.tokenStyleMap) { + this.tokenStyleMap = this.initTokenStyleMap(); + } + let style: TokenStyle | undefined = this.tokenStyleMap[tokenStyleId]; + if (style) { + return style; + } + if (types.isUndefined(style)) { + style = this.getDefaultTokenStyle(style); + } + return style; + } + + private initTokenStyleMap(): ITokenStyleMap { + return {}; + } + public getDefault(colorId: ColorIdentifier): Color | undefined { return colorRegistry.resolveDefaultColor(colorId, this); } + public getDefaultTokenStyle(tokenStyleId: TokenStyleIdentifier): TokenStyle | undefined { + return tokenStyleRegistry.resolveDefaultTokenStyle(tokenStyleId, this, this.findTokenStyleForScope.bind(this)); + } + + /** Public for testing reasons */ + public findTokenStyleForScope(scope: string): TokenStyle | undefined { + + function findTokenStyleForScopeInScopes(scopeMatchers: Matcher[], tokenColors: ITokenColorizationRule[]) { + for (let i = scopeMatchers.length - 1; i >= 0; i--) { + let matcher = scopeMatchers[i]; + if (!matcher) { + scopeMatchers[i] = matcher = getScopeMatcher(tokenColors[i]); + } + if (matcher(scope)) { + const settings = tokenColors[i].settings; + if (settings) { + if (foreground === null && settings.foreground) { + foreground = settings.foreground; + } + if (fontStyle === null && types.isString(settings.fontStyle)) { + fontStyle = settings.fontStyle; + } + if (foreground !== null && fontStyle !== null) { + break; + } + } + } + } + } + + let foreground: string | null = null; + let fontStyle: string | null = null; + + if (!this.customTokenScopeMatchers) { + this.customTokenScopeMatchers = new Array(this.customTokenColors.length); + } + findTokenStyleForScopeInScopes(this.customTokenScopeMatchers, this.customTokenColors); + + if (foreground === null || fontStyle === null) { + if (!this.themeTokenScopeMatchers) { + this.themeTokenScopeMatchers = new Array(this.themeTokenColors.length); + } + findTokenStyleForScopeInScopes(this.themeTokenScopeMatchers, this.themeTokenColors); + } + + if (foreground !== null || fontStyle !== null) { + return getTokenStyle(foreground, fontStyle); + } + return undefined; + } + + + public defines(colorId: ColorIdentifier): boolean { return this.customColorMap.hasOwnProperty(colorId) || this.colorMap.hasOwnProperty(colorId); } @@ -132,6 +216,7 @@ export class ColorThemeData implements IColorTheme { public setCustomTokenColors(customTokenColors: ITokenColorCustomizations) { this.customTokenColors = []; + this.customTokenScopeMatchers = undefined; // first add the non-theme specific settings this.addCustomTokenColors(customTokenColors); @@ -180,6 +265,7 @@ export class ColorThemeData implements IColorTheme { return Promise.resolve(undefined); } this.themeTokenColors = []; + this.themeTokenScopeMatchers = undefined; this.colorMap = {}; return _loadColorTheme(fileService, this.location, this.themeTokenColors, this.colorMap).then(_ => { this.isLoaded = true; @@ -381,4 +467,65 @@ let defaultThemeColors: { [baseTheme: string]: ITokenColorizationRule[] } = { { scope: 'token.error-token', settings: { foreground: '#FF0000' } }, { scope: 'token.debug-token', settings: { foreground: '#b267e6' } } ], -}; \ No newline at end of file +}; + +const noMatch = (_scope: string) => false; + +export function nameMatcher(identifers: string[], scope: string) { + for (const identifier of identifers) { + if (scopesAreMatching(scope, identifier)) { + return true; + } + } + return false; +} + +function scopesAreMatching(thisScopeName: string, scopeName: string): boolean { + if (!thisScopeName) { + return false; + } + if (thisScopeName === scopeName) { + return true; + } + const len = scopeName.length; + return thisScopeName.length > len && thisScopeName.substr(0, len) === scopeName && thisScopeName[len] === '.'; +} + +function getScopeMatcher(rule: ITokenColorizationRule): Matcher { + const scope = rule.scope; + if (!scope || !rule.settings) { + return noMatch; + } + const matchers: MatcherWithPriority[] = []; + if (Array.isArray(scope)) { + for (let s of scope) { + matchers.push(...createMatchers(s, nameMatcher)); + } + } else { + matchers.push(...createMatchers(scope, nameMatcher)); + } + return (scope: string) => matchers.some(m => m.matcher(scope)); +} + +function getTokenStyle(foreground: string | null, fontStyle: string | null): TokenStyle | undefined { + let styles: number | undefined; + let foregroundColor = undefined; + if (foreground !== null) { + foregroundColor = Color.fromHex(foreground); + } + + if (fontStyle !== null) { + styles = 0; + if (fontStyle.indexOf('underline') !== -1) { + styles |= TokenStyleBits.UNDERLINE; + } + if (fontStyle.indexOf('italic') !== -1) { + styles |= TokenStyleBits.ITALIC; + } + if (fontStyle.indexOf('bold') !== -1) { + styles |= TokenStyleBits.BOLD; + } + } + return new TokenStyle(foregroundColor, undefined, styles); + +} diff --git a/src/vs/workbench/services/themes/common/textMateScopeMatcher.ts b/src/vs/workbench/services/themes/common/textMateScopeMatcher.ts new file mode 100644 index 00000000000..16263c9ac89 --- /dev/null +++ b/src/vs/workbench/services/themes/common/textMateScopeMatcher.ts @@ -0,0 +1,111 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +'use strict'; + +export interface MatcherWithPriority { + matcher: Matcher; + priority: -1 | 0 | 1; +} + +export interface Matcher { + (matcherInput: T): boolean; +} + +export function createMatchers(selector: string, matchesName: (names: string[], matcherInput: T) => boolean): MatcherWithPriority[] { + const results = []>[]; + const tokenizer = newTokenizer(selector); + let token = tokenizer.next(); + while (token !== null) { + let priority: -1 | 0 | 1 = 0; + if (token.length === 2 && token.charAt(1) === ':') { + switch (token.charAt(0)) { + case 'R': priority = 1; break; + case 'L': priority = -1; break; + default: + console.log(`Unknown priority ${token} in scope selector`); + } + token = tokenizer.next(); + } + let matcher = parseConjunction(); + if (matcher) { + results.push({ matcher, priority }); + } + if (token !== ',') { + break; + } + token = tokenizer.next(); + } + return results; + + function parseOperand(): Matcher | null { + if (token === '-') { + token = tokenizer.next(); + const expressionToNegate = parseOperand(); + return matcherInput => !!expressionToNegate && !expressionToNegate(matcherInput); + } + if (token === '(') { + token = tokenizer.next(); + const expressionInParents = parseInnerExpression(); + if (token === ')') { + token = tokenizer.next(); + } + return expressionInParents; + } + if (isIdentifier(token)) { + const identifiers: string[] = []; + do { + identifiers.push(token); + token = tokenizer.next(); + } while (isIdentifier(token)); + return matcherInput => matchesName(identifiers, matcherInput); + } + return null; + } + function parseConjunction(): Matcher { + const matchers: Matcher[] = []; + let matcher = parseOperand(); + while (matcher) { + matchers.push(matcher); + matcher = parseOperand(); + } + return matcherInput => matchers.every(matcher => matcher(matcherInput)); // and + } + function parseInnerExpression(): Matcher { + const matchers: Matcher[] = []; + let matcher = parseConjunction(); + while (matcher) { + matchers.push(matcher); + if (token === '|' || token === ',') { + do { + token = tokenizer.next(); + } while (token === '|' || token === ','); // ignore subsequent commas + } else { + break; + } + matcher = parseConjunction(); + } + return matcherInput => matchers.some(matcher => matcher(matcherInput)); // or + } +} + +function isIdentifier(token: string | null): token is string { + return !!token && !!token.match(/[\w\.:]+/); +} + +function newTokenizer(input: string): { next: () => string | null } { + let regex = /([LR]:|[\w\.:][\w\.:\-]*|[\,\|\-\(\)])/g; + let match = regex.exec(input); + return { + next: () => { + if (!match) { + return null; + } + const res = match[0]; + match = regex.exec(input); + return res; + } + }; +} diff --git a/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts b/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts new file mode 100644 index 00000000000..27979e7eb9c --- /dev/null +++ b/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts @@ -0,0 +1,89 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { ColorThemeData } from 'vs/workbench/services/themes/common/colorThemeData'; +import * as assert from 'assert'; +import { ITokenColorCustomizations } from 'vs/workbench/services/themes/common/workbenchThemeService'; +import { TokenStyle, TokenStyleBits } from 'vs/platform/theme/common/tokenStyleRegistry'; +import { Color } from 'vs/base/common/color'; +import { isString } from 'vs/base/common/types'; + +function ts(foreground: string | undefined, styleFlags: number | undefined): TokenStyle { + const foregroundColor = isString(foreground) ? Color.fromHex(foreground) : undefined; + return new TokenStyle(foregroundColor, undefined, styleFlags); +} + +function tokenStyleAsString(ts: TokenStyle | undefined | null) { + return ts ? `${ts.foreground ? ts.foreground.toString() : 'no-foreground'}-${ts.styles ? ts.styles : 'no-styles'}` : 'tokenstyle-undefined'; +} + +function assertTokenStyle(expected: TokenStyle | undefined | null, actual: TokenStyle | undefined | null, message?: string) { + assert.equal(tokenStyleAsString(expected), tokenStyleAsString(actual), message); +} + + +suite('Themes - TokenStyleResolving', () => { + + // const fileService = new FileService(new NullLogService()); + // const diskFileSystemProvider = new DiskFileSystemProvider(new NullLogService()); + // fileService.registerProvider(Schemas.file, diskFileSystemProvider); + + + + + test('resolve resource', async () => { + const themeData = ColorThemeData.createLoadedEmptyTheme('test', 'test'); + + const customTokenColors: ITokenColorCustomizations = { + textMateRules: [ + { + scope: 'variable', + settings: { + fontStyle: '', + foreground: '#F8F8F2' + } + }, + { + scope: 'keyword', + settings: { + fontStyle: 'italic bold underline', + foreground: '#F92672' + } + }, + { + scope: 'storage', + settings: { + fontStyle: 'italic', + foreground: '#F92672' + } + }, + { + scope: 'storage.type', + settings: { + foreground: '#66D9EF' + } + } + ] + }; + + themeData.setCustomTokenColors(customTokenColors); + + let tokenStyle; + + tokenStyle = themeData.findTokenStyleForScope('variable'); + assertTokenStyle(tokenStyle, ts('#F8F8F2', 0), 'variable'); + + tokenStyle = themeData.findTokenStyleForScope('keyword'); + assertTokenStyle(tokenStyle, ts('#F92672', TokenStyleBits.ITALIC | TokenStyleBits.BOLD | TokenStyleBits.UNDERLINE), 'keyword'); + + tokenStyle = themeData.findTokenStyleForScope('storage'); + assertTokenStyle(tokenStyle, ts('#F92672', TokenStyleBits.ITALIC), 'storage'); + + tokenStyle = themeData.findTokenStyleForScope('storage.type'); + assertTokenStyle(tokenStyle, ts('#66D9EF', TokenStyleBits.ITALIC), 'storage.type'); + + + }); +}); From fb7cc04c0763af9daf042bc6d34ddb3790a45229 Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Fri, 13 Sep 2019 17:17:25 +0200 Subject: [PATCH 02/15] more --- .../theme/common/tokenStyleRegistry.ts | 43 ++++++++++----- .../services/themes/common/colorThemeData.ts | 52 +++++++++++-------- .../tokenStyleResolving.test.ts | 24 ++++++--- 3 files changed, 80 insertions(+), 39 deletions(-) diff --git a/src/vs/platform/theme/common/tokenStyleRegistry.ts b/src/vs/platform/theme/common/tokenStyleRegistry.ts index 5f593d41370..5be9d6a650d 100644 --- a/src/vs/platform/theme/common/tokenStyleRegistry.ts +++ b/src/vs/platform/theme/common/tokenStyleRegistry.ts @@ -8,6 +8,7 @@ import { IJSONSchema, IJSONSchemaMap } from 'vs/base/common/jsonSchema'; import { Color } from 'vs/base/common/color'; import { ITheme } from 'vs/platform/theme/common/themeService'; import { Event, Emitter } from 'vs/base/common/event'; +import * as nls from 'vs/nls'; import { Extensions as JSONExtensions, IJSONContributionRegistry } from 'vs/platform/jsonschemas/common/jsonContributionRegistry'; import { RunOnceScheduler } from 'vs/base/common/async'; @@ -69,21 +70,21 @@ export namespace TokenStyle { } } - +export type ProbeScope = string[] | string; export interface TokenStyleFunction { (theme: ITheme): TokenStyle | undefined; } export interface TokenStyleDefaults { - scopesToProbe?: string[]; - light: TokenStyle | null; - dark: TokenStyle | null; - hc: TokenStyle | null; + scopesToProbe?: ProbeScope[]; + light: TokenStyleValue | null; + dark: TokenStyleValue | null; + hc: TokenStyleValue | null; } /** - * A TokenStyle Value is either a tokestyle literal, a reference to other color or a derived color + * A TokenStyle Value is either a token style literal, a reference to other token style or a derived token style */ export type TokenStyleValue = TokenStyle | string | TokenStyleIdentifier | TokenStyleFunction; @@ -117,7 +118,7 @@ export interface ITokenStyleRegistry { /** * Gets the default TokenStyle of the given id */ - resolveDefaultTokenStyle(id: TokenStyleIdentifier, theme: ITheme, findTokenStyleForScope: (scope: string) => TokenStyle | undefined): TokenStyle | undefined; + resolveDefaultTokenStyle(id: TokenStyleIdentifier, theme: ITheme, findTokenStyleForScope: (scope: ProbeScope) => TokenStyle | undefined): TokenStyle | undefined; /** * JSON schema for an object to assign TokenStyle values to one of the TokenStyle contributions. @@ -147,9 +148,18 @@ class TokenStyleRegistry implements ITokenStyleRegistry { } public registerTokenStyle(id: string, defaults: TokenStyleDefaults | null, description: string, deprecationMessage?: string): TokenStyleIdentifier { - let colorContribution: TokenStyleContribution = { id, description, defaults, deprecationMessage }; - this.tokenStyleById[id] = colorContribution; - let propertySchema: IJSONSchema = { type: 'string', description, format: 'color-hex', default: '#ff0000' }; + let tokenStyleContribution: TokenStyleContribution = { id, description, defaults, deprecationMessage }; + this.tokenStyleById[id] = tokenStyleContribution; + let propertySchema: IJSONSchema = { + type: 'object', + description, + properties: { + 'foreground': { type: 'string', format: 'color-hex', default: '#ff0000' }, + 'italic': { type: 'boolean' }, + 'bold': { type: 'boolean' }, + 'underline': { type: 'boolean' } + } + }; if (deprecationMessage) { propertySchema.deprecationMessage = deprecationMessage; } @@ -177,7 +187,7 @@ class TokenStyleRegistry implements ITokenStyleRegistry { return Object.keys(this.tokenStyleById).map(id => this.tokenStyleById[id]); } - public resolveDefaultTokenStyle(id: TokenStyleIdentifier, theme: ITheme, findTokenStyleForScope: (scope: string) => TokenStyle | undefined): TokenStyle | undefined { + public resolveDefaultTokenStyle(id: TokenStyleIdentifier, theme: ITheme, findTokenStyleForScope: (scope: ProbeScope) => TokenStyle | undefined): TokenStyle | undefined { const tokenStyleDesc = this.tokenStyleById[id]; if (tokenStyleDesc && tokenStyleDesc.defaults) { const scopesToProbe = tokenStyleDesc.defaults.scopesToProbe; @@ -229,7 +239,16 @@ export function getTokenStyleRegistry(): ITokenStyleRegistry { return tokenStyleRegistry; } -// ----- implementation +// colors + + +export const comments = registerTokenStyle('comments', { scopesToProbe: ['comment'], dark: null, light: null, hc: null }, nls.localize('comments', "Token style for comments.")); +export const strings = registerTokenStyle('strings', { scopesToProbe: ['strings'], dark: null, light: null, hc: null }, nls.localize('strings', "Token style for strings.")); +export const keywords = registerTokenStyle('keywords', { scopesToProbe: ['keyword.control', 'storage', 'storage.type'], dark: null, light: null, hc: null }, nls.localize('keywords', "Token style for keywords.")); +export const numbers = registerTokenStyle('numbers', { scopesToProbe: ['constant.numeric'], dark: null, light: null, hc: null }, nls.localize('numbers', "Token style for numbers.")); +export const types = registerTokenStyle('types', { scopesToProbe: ['entity.name.type', 'entity.name.class', 'support.type', 'support.class'], dark: null, light: null, hc: null }, nls.localize('types', "Token style for types.")); +export const functions = registerTokenStyle('functions', { scopesToProbe: ['entity.name.function', 'support.function'], dark: null, light: null, hc: null }, nls.localize('functions', "Token style for functions.")); +export const variables = registerTokenStyle('variables', { scopesToProbe: ['variable', 'entity.name.variable'], dark: null, light: null, hc: null }, nls.localize('variables', "Token style for variables.")); /** * @param colorValue Resolve a color value in the context of a theme diff --git a/src/vs/workbench/services/themes/common/colorThemeData.ts b/src/vs/workbench/services/themes/common/colorThemeData.ts index fcb5bd87bd5..68281aaa572 100644 --- a/src/vs/workbench/services/themes/common/colorThemeData.ts +++ b/src/vs/workbench/services/themes/common/colorThemeData.ts @@ -20,7 +20,7 @@ import { URI } from 'vs/base/common/uri'; import { IFileService } from 'vs/platform/files/common/files'; import { parse as parsePList } from 'vs/workbench/services/themes/common/plistParser'; import { startsWith } from 'vs/base/common/strings'; -import { Extensions as TokenStyleRegistryExtensions, TokenStyle, TokenStyleIdentifier, ITokenStyleRegistry, TokenStyleBits } from 'vs/platform/theme/common/tokenStyleRegistry'; +import { Extensions as TokenStyleRegistryExtensions, TokenStyle, TokenStyleIdentifier, ITokenStyleRegistry, TokenStyleBits, ProbeScope } from 'vs/platform/theme/common/tokenStyleRegistry'; import { MatcherWithPriority, Matcher, createMatchers } from 'vs/workbench/services/themes/common/textMateScopeMatcher'; let colorRegistry = Registry.as(ColorRegistryExtensions.ColorContribution); @@ -59,8 +59,8 @@ export class ColorThemeData implements IColorTheme { private tokenStyleMap: ITokenStyleMap | undefined; - private themeTokenScopeMatchers: Matcher[] | undefined; - private customTokenScopeMatchers: Matcher[] | undefined; + private themeTokenScopeMatchers: Matcher[] | undefined; + private customTokenScopeMatchers: Matcher[] | undefined; private constructor(id: string, label: string, settingsId: string) { this.id = id; @@ -143,9 +143,9 @@ export class ColorThemeData implements IColorTheme { } /** Public for testing reasons */ - public findTokenStyleForScope(scope: string): TokenStyle | undefined { + public findTokenStyleForScope(scope: ProbeScope): TokenStyle | undefined { - function findTokenStyleForScopeInScopes(scopeMatchers: Matcher[], tokenColors: ITokenColorizationRule[]) { + function findTokenStyleForScopeInScopes(scopeMatchers: Matcher[], tokenColors: ITokenColorizationRule[]) { for (let i = scopeMatchers.length - 1; i >= 0; i--) { let matcher = scopeMatchers[i]; if (!matcher) { @@ -469,15 +469,25 @@ let defaultThemeColors: { [baseTheme: string]: ITokenColorizationRule[] } = { ], }; -const noMatch = (_scope: string) => false; +const noMatch = (_scope: ProbeScope) => false; -export function nameMatcher(identifers: string[], scope: string) { - for (const identifier of identifers) { - if (scopesAreMatching(scope, identifier)) { - return true; - } +function nameMatcher(identifers: string[], scope: ProbeScope) { + if (!Array.isArray(scope)) { + scope = [scope]; } - return false; + if (scope.length < identifers.length) { + return false; + } + let lastIndex = 0; + return identifers.every(identifier => { + for (let i = lastIndex; i < scope.length; i++) { + if (scopesAreMatching(scope[i], identifier)) { + lastIndex = i + 1; + return true; + } + } + return false; + }); } function scopesAreMatching(thisScopeName: string, scopeName: string): boolean { @@ -491,20 +501,20 @@ function scopesAreMatching(thisScopeName: string, scopeName: string): boolean { return thisScopeName.length > len && thisScopeName.substr(0, len) === scopeName && thisScopeName[len] === '.'; } -function getScopeMatcher(rule: ITokenColorizationRule): Matcher { - const scope = rule.scope; - if (!scope || !rule.settings) { +function getScopeMatcher(rule: ITokenColorizationRule): Matcher { + const ruleScope = rule.scope; + if (!ruleScope || !rule.settings) { return noMatch; } - const matchers: MatcherWithPriority[] = []; - if (Array.isArray(scope)) { - for (let s of scope) { - matchers.push(...createMatchers(s, nameMatcher)); + const matchers: MatcherWithPriority[] = []; + if (Array.isArray(ruleScope)) { + for (let rs of ruleScope) { + matchers.push(...createMatchers(rs, nameMatcher)); } } else { - matchers.push(...createMatchers(scope, nameMatcher)); + matchers.push(...createMatchers(ruleScope, nameMatcher)); } - return (scope: string) => matchers.some(m => m.matcher(scope)); + return (scope: ProbeScope) => matchers.some(m => m.matcher(scope)); } function getTokenStyle(foreground: string | null, fontStyle: string | null): TokenStyle | undefined { diff --git a/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts b/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts index 27979e7eb9c..c8c3ef86ccf 100644 --- a/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts +++ b/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts @@ -60,11 +60,18 @@ suite('Themes - TokenStyleResolving', () => { } }, { - scope: 'storage.type', + scope: ['storage.type', 'meta.structure.dictionary.json string.quoted.double.json'], settings: { foreground: '#66D9EF' } - } + }, + { + scope: 'entity.name.type, entity.name.class, entity.name.namespace, entity.name.scope-resolution', + settings: { + fontStyle: 'underline', + foreground: '#A6E22E' + } + }, ] }; @@ -72,18 +79,23 @@ suite('Themes - TokenStyleResolving', () => { let tokenStyle; - tokenStyle = themeData.findTokenStyleForScope('variable'); + tokenStyle = themeData.findTokenStyleForScope(['variable']); assertTokenStyle(tokenStyle, ts('#F8F8F2', 0), 'variable'); - tokenStyle = themeData.findTokenStyleForScope('keyword'); + tokenStyle = themeData.findTokenStyleForScope(['keyword']); assertTokenStyle(tokenStyle, ts('#F92672', TokenStyleBits.ITALIC | TokenStyleBits.BOLD | TokenStyleBits.UNDERLINE), 'keyword'); - tokenStyle = themeData.findTokenStyleForScope('storage'); + tokenStyle = themeData.findTokenStyleForScope(['storage']); assertTokenStyle(tokenStyle, ts('#F92672', TokenStyleBits.ITALIC), 'storage'); - tokenStyle = themeData.findTokenStyleForScope('storage.type'); + tokenStyle = themeData.findTokenStyleForScope(['storage.type']); assertTokenStyle(tokenStyle, ts('#66D9EF', TokenStyleBits.ITALIC), 'storage.type'); + tokenStyle = themeData.findTokenStyleForScope(['entity.name.class']); + assertTokenStyle(tokenStyle, ts('#A6E22E', TokenStyleBits.UNDERLINE), 'entity.name.class'); + + tokenStyle = themeData.findTokenStyleForScope(['meta.structure.dictionary.json', 'string.quoted.double.json']); + assertTokenStyle(tokenStyle, ts('#66D9EF', undefined), 'json property'); }); }); From f337499eb451e84d57a60a9ee9470c09e88d0f2d Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Wed, 18 Sep 2019 13:46:56 +0200 Subject: [PATCH 03/15] monokai test --- .../theme/common/tokenStyleRegistry.ts | 2 +- .../services/themes/common/colorThemeData.ts | 15 ++---- .../tokenStyleResolving.test.ts | 47 +++++++++++++++++-- 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/src/vs/platform/theme/common/tokenStyleRegistry.ts b/src/vs/platform/theme/common/tokenStyleRegistry.ts index 5be9d6a650d..1dbd13ff778 100644 --- a/src/vs/platform/theme/common/tokenStyleRegistry.ts +++ b/src/vs/platform/theme/common/tokenStyleRegistry.ts @@ -243,7 +243,7 @@ export function getTokenStyleRegistry(): ITokenStyleRegistry { export const comments = registerTokenStyle('comments', { scopesToProbe: ['comment'], dark: null, light: null, hc: null }, nls.localize('comments', "Token style for comments.")); -export const strings = registerTokenStyle('strings', { scopesToProbe: ['strings'], dark: null, light: null, hc: null }, nls.localize('strings', "Token style for strings.")); +export const strings = registerTokenStyle('strings', { scopesToProbe: ['string'], dark: null, light: null, hc: null }, nls.localize('strings', "Token style for strings.")); export const keywords = registerTokenStyle('keywords', { scopesToProbe: ['keyword.control', 'storage', 'storage.type'], dark: null, light: null, hc: null }, nls.localize('keywords', "Token style for keywords.")); export const numbers = registerTokenStyle('numbers', { scopesToProbe: ['constant.numeric'], dark: null, light: null, hc: null }, nls.localize('numbers', "Token style for numbers.")); export const types = registerTokenStyle('types', { scopesToProbe: ['entity.name.type', 'entity.name.class', 'support.type', 'support.class'], dark: null, light: null, hc: null }, nls.localize('types', "Token style for types.")); diff --git a/src/vs/workbench/services/themes/common/colorThemeData.ts b/src/vs/workbench/services/themes/common/colorThemeData.ts index 68281aaa572..9d2f319a343 100644 --- a/src/vs/workbench/services/themes/common/colorThemeData.ts +++ b/src/vs/workbench/services/themes/common/colorThemeData.ts @@ -57,7 +57,7 @@ export class ColorThemeData implements IColorTheme { private colorMap: IColorMap = {}; private customColorMap: IColorMap = {}; - private tokenStyleMap: ITokenStyleMap | undefined; + private tokenStyleMap: ITokenStyleMap = {}; private themeTokenScopeMatchers: Matcher[] | undefined; private customTokenScopeMatchers: Matcher[] | undefined; @@ -116,24 +116,17 @@ export class ColorThemeData implements IColorTheme { return color; } - public getTokenStyle(tokenStyleId: TokenStyleIdentifier): TokenStyle | undefined { - if (!this.tokenStyleMap) { - this.tokenStyleMap = this.initTokenStyleMap(); - } + public getTokenStyle(tokenStyleId: TokenStyleIdentifier, useDefault?: boolean): TokenStyle | undefined { let style: TokenStyle | undefined = this.tokenStyleMap[tokenStyleId]; if (style) { return style; } - if (types.isUndefined(style)) { - style = this.getDefaultTokenStyle(style); + if (useDefault !== false && types.isUndefined(style)) { + style = this.getDefaultTokenStyle(tokenStyleId); } return style; } - private initTokenStyleMap(): ITokenStyleMap { - return {}; - } - public getDefault(colorId: ColorIdentifier): Color | undefined { return colorRegistry.resolveDefaultColor(colorId, this); } diff --git a/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts b/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts index c8c3ef86ccf..adcb8dcd749 100644 --- a/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts +++ b/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts @@ -6,9 +6,15 @@ import { ColorThemeData } from 'vs/workbench/services/themes/common/colorThemeData'; import * as assert from 'assert'; import { ITokenColorCustomizations } from 'vs/workbench/services/themes/common/workbenchThemeService'; -import { TokenStyle, TokenStyleBits } from 'vs/platform/theme/common/tokenStyleRegistry'; +import { TokenStyle, TokenStyleBits, comments, variables, types, functions, keywords, numbers, strings } from 'vs/platform/theme/common/tokenStyleRegistry'; import { Color } from 'vs/base/common/color'; import { isString } from 'vs/base/common/types'; +import { FileService } from 'vs/platform/files/common/fileService'; +import { NullLogService } from 'vs/platform/log/common/log'; +import { DiskFileSystemProvider } from 'vs/platform/files/node/diskFileSystemProvider'; +import { Schemas } from 'vs/base/common/network'; +import { URI } from 'vs/base/common/uri'; +import { getPathFromAmdModule } from 'vs/base/common/amd'; function ts(foreground: string | undefined, styleFlags: number | undefined): TokenStyle { const foregroundColor = isString(foreground) ? Color.fromHex(foreground) : undefined; @@ -24,14 +30,45 @@ function assertTokenStyle(expected: TokenStyle | undefined | null, actual: Token } + suite('Themes - TokenStyleResolving', () => { - - // const fileService = new FileService(new NullLogService()); - // const diskFileSystemProvider = new DiskFileSystemProvider(new NullLogService()); - // fileService.registerProvider(Schemas.file, diskFileSystemProvider); + const fileService = new FileService(new NullLogService()); + const diskFileSystemProvider = new DiskFileSystemProvider(new NullLogService()); + fileService.registerProvider(Schemas.file, diskFileSystemProvider); + test('color defaults - monokai', async () => { + const themeData = ColorThemeData.createUnloadedTheme('foo'); + const themeLocation = getPathFromAmdModule(require, '../../../../../../../extensions/theme-monokai/themes/monokai-color-theme.json'); + themeData.location = URI.file(themeLocation); + await themeData.ensureLoaded(fileService); + assert.equal(themeData.isLoaded, true); + + let tokenStyle; + + tokenStyle = themeData.getTokenStyle(comments); + assertTokenStyle(tokenStyle, ts('#75715E', 0)); + + tokenStyle = themeData.getTokenStyle(variables); + assertTokenStyle(tokenStyle, ts('#F8F8F2', 0)); + + tokenStyle = themeData.getTokenStyle(types); + assertTokenStyle(tokenStyle, ts('#A6E22E', TokenStyleBits.UNDERLINE)); + + tokenStyle = themeData.getTokenStyle(functions); + assertTokenStyle(tokenStyle, ts('#A6E22E', 0)); + + tokenStyle = themeData.getTokenStyle(strings); + assertTokenStyle(tokenStyle, ts('#E6DB74', 0)); + + tokenStyle = themeData.getTokenStyle(numbers); + assertTokenStyle(tokenStyle, ts('#AE81FF', 0)); + + tokenStyle = themeData.getTokenStyle(keywords); + assertTokenStyle(tokenStyle, ts('#F92672', 0)); + + }); test('resolve resource', async () => { const themeData = ColorThemeData.createLoadedEmptyTheme('test', 'test'); From fa99aa5c34816ac20fa4c71652cf2b0fc477e99c Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Wed, 18 Sep 2019 23:16:52 +0200 Subject: [PATCH 04/15] more tests --- .../theme/common/tokenStyleRegistry.ts | 4 + .../tokenStyleResolving.test.ts | 141 +++++++++++++++--- 2 files changed, 126 insertions(+), 19 deletions(-) diff --git a/src/vs/platform/theme/common/tokenStyleRegistry.ts b/src/vs/platform/theme/common/tokenStyleRegistry.ts index 1dbd13ff778..e212367d873 100644 --- a/src/vs/platform/theme/common/tokenStyleRegistry.ts +++ b/src/vs/platform/theme/common/tokenStyleRegistry.ts @@ -12,6 +12,7 @@ import * as nls from 'vs/nls'; import { Extensions as JSONExtensions, IJSONContributionRegistry } from 'vs/platform/jsonschemas/common/jsonContributionRegistry'; import { RunOnceScheduler } from 'vs/base/common/async'; +import { editorForeground } from 'vs/platform/theme/common/colorRegistry'; // ------ API types @@ -200,6 +201,9 @@ class TokenStyleRegistry implements ITokenStyleRegistry { } } const tokenStyleValue = tokenStyleDesc.defaults[theme.type]; + if (tokenStyleValue === null) { + return new TokenStyle(theme.getColor(editorForeground)); + } return resolveTokenStyleValue(tokenStyleValue, theme); } return undefined; diff --git a/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts b/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts index adcb8dcd749..ac70d635999 100644 --- a/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts +++ b/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts @@ -25,8 +25,15 @@ function tokenStyleAsString(ts: TokenStyle | undefined | null) { return ts ? `${ts.foreground ? ts.foreground.toString() : 'no-foreground'}-${ts.styles ? ts.styles : 'no-styles'}` : 'tokenstyle-undefined'; } -function assertTokenStyle(expected: TokenStyle | undefined | null, actual: TokenStyle | undefined | null, message?: string) { - assert.equal(tokenStyleAsString(expected), tokenStyleAsString(actual), message); +function assertTokenStyle(actual: TokenStyle | undefined | null, expected: TokenStyle | undefined | null, message?: string) { + assert.equal(tokenStyleAsString(actual), tokenStyleAsString(expected), message); +} + +function assertTokenStyles(themeData: ColorThemeData, expected: { [tokenStyleId: string]: TokenStyle }) { + for (let tokenStyleId in expected) { + const tokenStyle = themeData.getTokenStyle(tokenStyleId); + assertTokenStyle(tokenStyle, expected[tokenStyleId], tokenStyleId); + } } @@ -45,28 +52,115 @@ suite('Themes - TokenStyleResolving', () => { assert.equal(themeData.isLoaded, true); - let tokenStyle; + assertTokenStyles(themeData, { + [comments]: ts('#75715E', 0), + [variables]: ts('#F8F8F2', 0), + [types]: ts('#A6E22E', TokenStyleBits.UNDERLINE), + [functions]: ts('#A6E22E', 0), + [strings]: ts('#E6DB74', 0), + [numbers]: ts('#AE81FF', 0), + [keywords]: ts('#F92672', 0) + }); - tokenStyle = themeData.getTokenStyle(comments); - assertTokenStyle(tokenStyle, ts('#75715E', 0)); + }); - tokenStyle = themeData.getTokenStyle(variables); - assertTokenStyle(tokenStyle, ts('#F8F8F2', 0)); + test('color defaults - dark+', async () => { + const themeData = ColorThemeData.createUnloadedTheme('foo'); + const themeLocation = getPathFromAmdModule(require, '../../../../../../../extensions/theme-defaults/themes/dark_plus.json'); + themeData.location = URI.file(themeLocation); + await themeData.ensureLoaded(fileService); - tokenStyle = themeData.getTokenStyle(types); - assertTokenStyle(tokenStyle, ts('#A6E22E', TokenStyleBits.UNDERLINE)); + assert.equal(themeData.isLoaded, true); - tokenStyle = themeData.getTokenStyle(functions); - assertTokenStyle(tokenStyle, ts('#A6E22E', 0)); + assertTokenStyles(themeData, { + [comments]: ts('#6A9955', 0), + [variables]: ts('#9CDCFE', 0), + [types]: ts('#4EC9B0', 0), + [functions]: ts('#DCDCAA', 0), + [strings]: ts('#CE9178', 0), + [numbers]: ts('#B5CEA8', 0), + [keywords]: ts('#C586C0', 0) + }); - tokenStyle = themeData.getTokenStyle(strings); - assertTokenStyle(tokenStyle, ts('#E6DB74', 0)); + }); - tokenStyle = themeData.getTokenStyle(numbers); - assertTokenStyle(tokenStyle, ts('#AE81FF', 0)); + test('color defaults - light vs', async () => { + const themeData = ColorThemeData.createUnloadedTheme('foo'); + const themeLocation = getPathFromAmdModule(require, '../../../../../../../extensions/theme-defaults/themes/light_vs.json'); + themeData.location = URI.file(themeLocation); + await themeData.ensureLoaded(fileService); - tokenStyle = themeData.getTokenStyle(keywords); - assertTokenStyle(tokenStyle, ts('#F92672', 0)); + assert.equal(themeData.isLoaded, true); + + assertTokenStyles(themeData, { + [comments]: ts('#008000', 0), + [variables]: ts('#000000', 0), + [types]: ts('#000000', 0), + [functions]: ts('#000000', 0), + [strings]: ts('#a31515', 0), + [numbers]: ts('#09885a', 0), + [keywords]: ts('#0000ff', 0) + }); + + }); + + test('color defaults - hc', async () => { + const themeData = ColorThemeData.createUnloadedTheme('foo'); + const themeLocation = getPathFromAmdModule(require, '../../../../../../../extensions/theme-defaults/themes/hc_black.json'); + themeData.location = URI.file(themeLocation); + await themeData.ensureLoaded(fileService); + + assert.equal(themeData.isLoaded, true); + + assertTokenStyles(themeData, { + [comments]: ts('#7ca668', 0), + [variables]: ts('#9CDCFE', 0), + [types]: ts('#4EC9B0', 0), + [functions]: ts('#DCDCAA', 0), + [strings]: ts('#ce9178', 0), + [numbers]: ts('#b5cea8', 0), + [keywords]: ts('#C586C0', 0) + }); + + }); + + test('color defaults - kimbie dark', async () => { + const themeData = ColorThemeData.createUnloadedTheme('foo'); + const themeLocation = getPathFromAmdModule(require, '../../../../../../../extensions/theme-kimbie-dark/themes/kimbie-dark-color-theme.json'); + themeData.location = URI.file(themeLocation); + await themeData.ensureLoaded(fileService); + + assert.equal(themeData.isLoaded, true); + + assertTokenStyles(themeData, { + [comments]: ts('#a57a4c', 0), + [variables]: ts('#dc3958', 0), + [types]: ts('#f06431', 0), + [functions]: ts('#8ab1b0', 0), + [strings]: ts('#889b4a', 0), + [numbers]: ts('#f79a32', 0), + [keywords]: ts('#98676a', 0) + }); + + }); + + test('color defaults - abyss', async () => { + const themeData = ColorThemeData.createUnloadedTheme('foo'); + const themeLocation = getPathFromAmdModule(require, '../../../../../../../extensions/theme-abyss/themes/abyss-color-theme.json'); + themeData.location = URI.file(themeLocation); + await themeData.ensureLoaded(fileService); + + assert.equal(themeData.isLoaded, true); + + assertTokenStyles(themeData, { + [comments]: ts('#384887', 0), + [variables]: ts('#9966b8', 0), + [types]: ts('#f06431', 0), + [functions]: ts('#8ab1b0', 0), + [strings]: ts('#22aa44', 0), + [numbers]: ts('#f280d0', 0), + [keywords]: ts('#98676a', 0) + }); }); @@ -83,7 +177,7 @@ suite('Themes - TokenStyleResolving', () => { } }, { - scope: 'keyword', + scope: 'keyword.operator', settings: { fontStyle: 'italic bold underline', foreground: '#F92672' @@ -119,9 +213,18 @@ suite('Themes - TokenStyleResolving', () => { tokenStyle = themeData.findTokenStyleForScope(['variable']); assertTokenStyle(tokenStyle, ts('#F8F8F2', 0), 'variable'); - tokenStyle = themeData.findTokenStyleForScope(['keyword']); + tokenStyle = themeData.findTokenStyleForScope(['keyword.operator']); assertTokenStyle(tokenStyle, ts('#F92672', TokenStyleBits.ITALIC | TokenStyleBits.BOLD | TokenStyleBits.UNDERLINE), 'keyword'); + tokenStyle = themeData.findTokenStyleForScope(['keyword']); + assertTokenStyle(tokenStyle, undefined, 'keyword'); + + tokenStyle = themeData.findTokenStyleForScope(['keyword.operator']); + assertTokenStyle(tokenStyle, ts('#F92672', TokenStyleBits.ITALIC | TokenStyleBits.BOLD | TokenStyleBits.UNDERLINE), 'keyword.operator'); + + tokenStyle = themeData.findTokenStyleForScope(['keyword.operators']); + assertTokenStyle(tokenStyle, undefined, 'keyword.operators'); + tokenStyle = themeData.findTokenStyleForScope(['storage']); assertTokenStyle(tokenStyle, ts('#F92672', TokenStyleBits.ITALIC), 'storage'); From 4a1614f80b78c92cf446d98e942846dbeacdf1de Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Thu, 19 Sep 2019 09:13:12 +0200 Subject: [PATCH 05/15] match score --- .../services/themes/common/colorThemeData.ts | 99 +++++++++++-------- .../themes/common/textMateScopeMatcher.ts | 43 ++++++-- 2 files changed, 93 insertions(+), 49 deletions(-) diff --git a/src/vs/workbench/services/themes/common/colorThemeData.ts b/src/vs/workbench/services/themes/common/colorThemeData.ts index 9d2f319a343..12079e669db 100644 --- a/src/vs/workbench/services/themes/common/colorThemeData.ts +++ b/src/vs/workbench/services/themes/common/colorThemeData.ts @@ -138,44 +138,41 @@ export class ColorThemeData implements IColorTheme { /** Public for testing reasons */ public findTokenStyleForScope(scope: ProbeScope): TokenStyle | undefined { + let foreground: string | null = null; + let fontStyle: string | null = null; + let foregroundScore = -1; + let fontStyleScore = -1; + function findTokenStyleForScopeInScopes(scopeMatchers: Matcher[], tokenColors: ITokenColorizationRule[]) { - for (let i = scopeMatchers.length - 1; i >= 0; i--) { + for (let i = 0; i < scopeMatchers.length; i++) { + const settings = tokenColors[i].settings; + if (!settings) { + continue; + } let matcher = scopeMatchers[i]; if (!matcher) { scopeMatchers[i] = matcher = getScopeMatcher(tokenColors[i]); } - if (matcher(scope)) { - const settings = tokenColors[i].settings; - if (settings) { - if (foreground === null && settings.foreground) { - foreground = settings.foreground; - } - if (fontStyle === null && types.isString(settings.fontStyle)) { - fontStyle = settings.fontStyle; - } - if (foreground !== null && fontStyle !== null) { - break; - } - } + const score = matcher(scope); + if (score > foregroundScore && settings.foreground) { + foreground = settings.foreground; + } + if (score > fontStyleScore && types.isString(settings.fontStyle)) { + fontStyle = settings.fontStyle; } } } - let foreground: string | null = null; - let fontStyle: string | null = null; + if (!this.themeTokenScopeMatchers) { + this.themeTokenScopeMatchers = new Array(this.themeTokenColors.length); + } + findTokenStyleForScopeInScopes(this.themeTokenScopeMatchers, this.themeTokenColors); if (!this.customTokenScopeMatchers) { this.customTokenScopeMatchers = new Array(this.customTokenColors.length); } findTokenStyleForScopeInScopes(this.customTokenScopeMatchers, this.customTokenColors); - if (foreground === null || fontStyle === null) { - if (!this.themeTokenScopeMatchers) { - this.themeTokenScopeMatchers = new Array(this.themeTokenColors.length); - } - findTokenStyleForScopeInScopes(this.themeTokenScopeMatchers, this.themeTokenColors); - } - if (foreground !== null || fontStyle !== null) { return getTokenStyle(foreground, fontStyle); } @@ -462,27 +459,43 @@ let defaultThemeColors: { [baseTheme: string]: ITokenColorizationRule[] } = { ], }; -const noMatch = (_scope: ProbeScope) => false; +const noMatch = (_scope: ProbeScope) => -1; -function nameMatcher(identifers: string[], scope: ProbeScope) { - if (!Array.isArray(scope)) { - scope = [scope]; - } - if (scope.length < identifers.length) { - return false; - } - let lastIndex = 0; - return identifers.every(identifier => { - for (let i = lastIndex; i < scope.length; i++) { - if (scopesAreMatching(scope[i], identifier)) { - lastIndex = i + 1; - return true; +function nameMatcher(identifers: string[], scope: ProbeScope): number { + function findInIdents(s: string, lastIndent: number): number { + for (let i = lastIndent - 1; i >= 0; i--) { + if (scopesAreMatching(identifers[i], s)) { + return i; } } - return false; - }); + return -1; + } + if (!Array.isArray(scope)) { + const idx = findInIdents(scope, identifers.length); + if (idx >= 0) { + return idx * 0x10000 + scope.length; + } + return -1; + } + if (scope.length < identifers.length) { + return -1; + } + let lastScopeIndex = scope.length - 1; + let lastIdentifierIndex = findInIdents(scope[lastScopeIndex--], identifers.length); + if (lastIdentifierIndex >= 0) { + const score = lastIdentifierIndex * 0x10000 + scope.length; + while (lastScopeIndex >= 0) { + lastIdentifierIndex = findInIdents(scope[lastScopeIndex--], lastIdentifierIndex); + if (lastIdentifierIndex === -1) { + return -1; + } + } + return score; + } + return -1; } + function scopesAreMatching(thisScopeName: string, scopeName: string): boolean { if (!thisScopeName) { return false; @@ -507,7 +520,13 @@ function getScopeMatcher(rule: ITokenColorizationRule): Matcher { } else { matchers.push(...createMatchers(ruleScope, nameMatcher)); } - return (scope: ProbeScope) => matchers.some(m => m.matcher(scope)); + return (scope: ProbeScope) => { + let max = 0; + for (const m of matchers) { + max = Math.max(max, m.matcher(scope)); + } + return max; + }; } function getTokenStyle(foreground: string | null, fontStyle: string | null): TokenStyle | undefined { diff --git a/src/vs/workbench/services/themes/common/textMateScopeMatcher.ts b/src/vs/workbench/services/themes/common/textMateScopeMatcher.ts index 16263c9ac89..701081bca4c 100644 --- a/src/vs/workbench/services/themes/common/textMateScopeMatcher.ts +++ b/src/vs/workbench/services/themes/common/textMateScopeMatcher.ts @@ -11,10 +11,10 @@ export interface MatcherWithPriority { } export interface Matcher { - (matcherInput: T): boolean; + (matcherInput: T): number; } -export function createMatchers(selector: string, matchesName: (names: string[], matcherInput: T) => boolean): MatcherWithPriority[] { +export function createMatchers(selector: string, matchesName: (names: string[], matcherInput: T) => number): MatcherWithPriority[] { const results = []>[]; const tokenizer = newTokenizer(selector); let token = tokenizer.next(); @@ -44,7 +44,13 @@ export function createMatchers(selector: string, matchesName: (names: string[ if (token === '-') { token = tokenizer.next(); const expressionToNegate = parseOperand(); - return matcherInput => !!expressionToNegate && !expressionToNegate(matcherInput); + if (!expressionToNegate) { + return null; + } + return matcherInput => { + const score = expressionToNegate(matcherInput); + return score < 0 ? 0 : -1; + }; } if (token === '(') { token = tokenizer.next(); @@ -64,18 +70,31 @@ export function createMatchers(selector: string, matchesName: (names: string[ } return null; } - function parseConjunction(): Matcher { - const matchers: Matcher[] = []; + function parseConjunction(): Matcher | null { let matcher = parseOperand(); + if (!matcher) { + return null; + } + + const matchers: Matcher[] = []; while (matcher) { matchers.push(matcher); matcher = parseOperand(); } - return matcherInput => matchers.every(matcher => matcher(matcherInput)); // and + return matcherInput => { // and + let min = matchers[0](matcherInput); + for (let i = 1; min >= 0 && i < matchers.length; i++) { + min = Math.min(min, matchers[i](matcherInput)); + } + return min; + }; } - function parseInnerExpression(): Matcher { - const matchers: Matcher[] = []; + function parseInnerExpression(): Matcher | null { let matcher = parseConjunction(); + if (!matcher) { + return null; + } + const matchers: Matcher[] = []; while (matcher) { matchers.push(matcher); if (token === '|' || token === ',') { @@ -87,7 +106,13 @@ export function createMatchers(selector: string, matchesName: (names: string[ } matcher = parseConjunction(); } - return matcherInput => matchers.some(matcher => matcher(matcherInput)); // or + return matcherInput => { // or + let max = matchers[0](matcherInput); + for (let i = 1; i < matchers.length; i++) { + max = Math.max(max, matchers[i](matcherInput)); + } + return max; + }; } } From 6dce52093c8a2fd178f86e4de8d4844a89c33297 Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Thu, 19 Sep 2019 16:11:43 +0200 Subject: [PATCH 06/15] fix score based rule matching --- .../theme-abyss/themes/abyss-color-theme.json | 11 ++--- .../theme/common/tokenStyleRegistry.ts | 16 +++---- .../services/themes/common/colorThemeData.ts | 42 +++++++++---------- .../tokenStyleResolving.test.ts | 14 ++++--- 4 files changed, 39 insertions(+), 44 deletions(-) diff --git a/extensions/theme-abyss/themes/abyss-color-theme.json b/extensions/theme-abyss/themes/abyss-color-theme.json index 18c8235f79c..775dc8614d0 100644 --- a/extensions/theme-abyss/themes/abyss-color-theme.json +++ b/extensions/theme-abyss/themes/abyss-color-theme.json @@ -1,12 +1,6 @@ { "name": "Abyss", "tokenColors": [ - { - "settings": { - "background": "#000c18", - "foreground": "#6688cc" - } - }, { "scope": ["meta.embedded", "source.groovy.embedded"], "settings": { @@ -260,6 +254,9 @@ ], "colors": { + "editor.background": "#000c18", + "editor.foreground": "#6688cc", + // Base // "foreground": "", "focusBorder": "#596F99", @@ -303,8 +300,6 @@ "scrollbarSlider.hoverBackground": "#3B3F5188", // Editor - "editor.background": "#000c18", - // "editor.foreground": "#6688cc", "editorWidget.background": "#262641", "editorCursor.foreground": "#ddbb88", "editorWhitespace.foreground": "#103050", diff --git a/src/vs/platform/theme/common/tokenStyleRegistry.ts b/src/vs/platform/theme/common/tokenStyleRegistry.ts index e212367d873..27f1a7ebc29 100644 --- a/src/vs/platform/theme/common/tokenStyleRegistry.ts +++ b/src/vs/platform/theme/common/tokenStyleRegistry.ts @@ -71,7 +71,7 @@ export namespace TokenStyle { } } -export type ProbeScope = string[] | string; +export type ProbeScope = string[]; export interface TokenStyleFunction { (theme: ITheme): TokenStyle | undefined; @@ -246,13 +246,13 @@ export function getTokenStyleRegistry(): ITokenStyleRegistry { // colors -export const comments = registerTokenStyle('comments', { scopesToProbe: ['comment'], dark: null, light: null, hc: null }, nls.localize('comments', "Token style for comments.")); -export const strings = registerTokenStyle('strings', { scopesToProbe: ['string'], dark: null, light: null, hc: null }, nls.localize('strings', "Token style for strings.")); -export const keywords = registerTokenStyle('keywords', { scopesToProbe: ['keyword.control', 'storage', 'storage.type'], dark: null, light: null, hc: null }, nls.localize('keywords', "Token style for keywords.")); -export const numbers = registerTokenStyle('numbers', { scopesToProbe: ['constant.numeric'], dark: null, light: null, hc: null }, nls.localize('numbers', "Token style for numbers.")); -export const types = registerTokenStyle('types', { scopesToProbe: ['entity.name.type', 'entity.name.class', 'support.type', 'support.class'], dark: null, light: null, hc: null }, nls.localize('types', "Token style for types.")); -export const functions = registerTokenStyle('functions', { scopesToProbe: ['entity.name.function', 'support.function'], dark: null, light: null, hc: null }, nls.localize('functions', "Token style for functions.")); -export const variables = registerTokenStyle('variables', { scopesToProbe: ['variable', 'entity.name.variable'], dark: null, light: null, hc: null }, nls.localize('variables', "Token style for variables.")); +export const comments = registerTokenStyle('comments', { scopesToProbe: [['comment']], dark: null, light: null, hc: null }, nls.localize('comments', "Token style for comments.")); +export const strings = registerTokenStyle('strings', { scopesToProbe: [['string']], dark: null, light: null, hc: null }, nls.localize('strings', "Token style for strings.")); +export const keywords = registerTokenStyle('keywords', { scopesToProbe: [['keyword.control'], ['storage'], ['storage.type']], dark: null, light: null, hc: null }, nls.localize('keywords', "Token style for keywords.")); +export const numbers = registerTokenStyle('numbers', { scopesToProbe: [['constant.numeric']], dark: null, light: null, hc: null }, nls.localize('numbers', "Token style for numbers.")); +export const types = registerTokenStyle('types', { scopesToProbe: [['entity.name.type'], ['entity.name.class'], ['support.type'], ['support.class']], dark: null, light: null, hc: null }, nls.localize('types', "Token style for types.")); +export const functions = registerTokenStyle('functions', { scopesToProbe: [['entity.name.function'], ['support.function']], dark: null, light: null, hc: null }, nls.localize('functions', "Token style for functions.")); +export const variables = registerTokenStyle('variables', { scopesToProbe: [['variable'], ['entity.name.variable']], dark: null, light: null, hc: null }, nls.localize('variables', "Token style for variables.")); /** * @param colorValue Resolve a color value in the context of a theme diff --git a/src/vs/workbench/services/themes/common/colorThemeData.ts b/src/vs/workbench/services/themes/common/colorThemeData.ts index 12079e669db..4dbdfcc2eff 100644 --- a/src/vs/workbench/services/themes/common/colorThemeData.ts +++ b/src/vs/workbench/services/themes/common/colorThemeData.ts @@ -138,7 +138,9 @@ export class ColorThemeData implements IColorTheme { /** Public for testing reasons */ public findTokenStyleForScope(scope: ProbeScope): TokenStyle | undefined { - let foreground: string | null = null; + let foregroundColor = this.getColor(editorForeground); + + let foreground = foregroundColor ? foregroundColor.toString() : null; let fontStyle: string | null = null; let foregroundScore = -1; let fontStyleScore = -1; @@ -154,12 +156,15 @@ export class ColorThemeData implements IColorTheme { scopeMatchers[i] = matcher = getScopeMatcher(tokenColors[i]); } const score = matcher(scope); - if (score > foregroundScore && settings.foreground) { - foreground = settings.foreground; - } - if (score > fontStyleScore && types.isString(settings.fontStyle)) { - fontStyle = settings.fontStyle; + if (score >= 0) { + if (score >= foregroundScore && settings.foreground) { + foreground = settings.foreground; + } + if (score >= fontStyleScore && types.isString(settings.fontStyle)) { + fontStyle = settings.fontStyle; + } } + } } @@ -173,10 +178,7 @@ export class ColorThemeData implements IColorTheme { } findTokenStyleForScopeInScopes(this.customTokenScopeMatchers, this.customTokenColors); - if (foreground !== null || fontStyle !== null) { - return getTokenStyle(foreground, fontStyle); - } - return undefined; + return getTokenStyle(foreground, fontStyle); } @@ -464,26 +466,19 @@ const noMatch = (_scope: ProbeScope) => -1; function nameMatcher(identifers: string[], scope: ProbeScope): number { function findInIdents(s: string, lastIndent: number): number { for (let i = lastIndent - 1; i >= 0; i--) { - if (scopesAreMatching(identifers[i], s)) { + if (scopesAreMatching(s, identifers[i])) { return i; } } return -1; } - if (!Array.isArray(scope)) { - const idx = findInIdents(scope, identifers.length); - if (idx >= 0) { - return idx * 0x10000 + scope.length; - } - return -1; - } if (scope.length < identifers.length) { return -1; } let lastScopeIndex = scope.length - 1; let lastIdentifierIndex = findInIdents(scope[lastScopeIndex--], identifers.length); if (lastIdentifierIndex >= 0) { - const score = lastIdentifierIndex * 0x10000 + scope.length; + const score = (lastIdentifierIndex + 1) * 0x10000 + scope.length; while (lastScopeIndex >= 0) { lastIdentifierIndex = findInIdents(scope[lastScopeIndex--], lastIdentifierIndex); if (lastIdentifierIndex === -1) { @@ -520,10 +515,13 @@ function getScopeMatcher(rule: ITokenColorizationRule): Matcher { } else { matchers.push(...createMatchers(ruleScope, nameMatcher)); } + if (matchers.length === 0) { + return noMatch; + } return (scope: ProbeScope) => { - let max = 0; - for (const m of matchers) { - max = Math.max(max, m.matcher(scope)); + let max = matchers[0].matcher(scope); + for (let i = 1; i < matchers.length; i++) { + max = Math.max(max, matchers[i].matcher(scope)); } return max; }; diff --git a/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts b/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts index ac70d635999..12b9e9102da 100644 --- a/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts +++ b/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts @@ -15,6 +15,7 @@ import { DiskFileSystemProvider } from 'vs/platform/files/node/diskFileSystemPro import { Schemas } from 'vs/base/common/network'; import { URI } from 'vs/base/common/uri'; import { getPathFromAmdModule } from 'vs/base/common/amd'; +import { editorForeground } from 'vs/platform/theme/common/colorRegistry'; function ts(foreground: string | undefined, styleFlags: number | undefined): TokenStyle { const foregroundColor = isString(foreground) ? Color.fromHex(foreground) : undefined; @@ -154,12 +155,12 @@ suite('Themes - TokenStyleResolving', () => { assertTokenStyles(themeData, { [comments]: ts('#384887', 0), - [variables]: ts('#9966b8', 0), - [types]: ts('#f06431', 0), - [functions]: ts('#8ab1b0', 0), + [variables]: ts('#6688cc', 0), + [types]: ts('#ffeebb', TokenStyleBits.UNDERLINE), + [functions]: ts('#ddbb88', 0), [strings]: ts('#22aa44', 0), [numbers]: ts('#f280d0', 0), - [keywords]: ts('#98676a', 0) + [keywords]: ts('#225588', 0) }); }); @@ -209,6 +210,7 @@ suite('Themes - TokenStyleResolving', () => { themeData.setCustomTokenColors(customTokenColors); let tokenStyle; + let defaultTokenStyle = new TokenStyle(themeData.getColor(editorForeground)); tokenStyle = themeData.findTokenStyleForScope(['variable']); assertTokenStyle(tokenStyle, ts('#F8F8F2', 0), 'variable'); @@ -217,13 +219,13 @@ suite('Themes - TokenStyleResolving', () => { assertTokenStyle(tokenStyle, ts('#F92672', TokenStyleBits.ITALIC | TokenStyleBits.BOLD | TokenStyleBits.UNDERLINE), 'keyword'); tokenStyle = themeData.findTokenStyleForScope(['keyword']); - assertTokenStyle(tokenStyle, undefined, 'keyword'); + assertTokenStyle(tokenStyle, defaultTokenStyle, 'keyword'); tokenStyle = themeData.findTokenStyleForScope(['keyword.operator']); assertTokenStyle(tokenStyle, ts('#F92672', TokenStyleBits.ITALIC | TokenStyleBits.BOLD | TokenStyleBits.UNDERLINE), 'keyword.operator'); tokenStyle = themeData.findTokenStyleForScope(['keyword.operators']); - assertTokenStyle(tokenStyle, undefined, 'keyword.operators'); + assertTokenStyle(tokenStyle, defaultTokenStyle, 'keyword.operators'); tokenStyle = themeData.findTokenStyleForScope(['storage']); assertTokenStyle(tokenStyle, ts('#F92672', TokenStyleBits.ITALIC), 'storage'); From 3bc026d9b05346bdc126db6aa67cefce3158f735 Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Thu, 19 Sep 2019 17:16:30 +0200 Subject: [PATCH 07/15] classifictions --- .../theme/common/tokenStyleRegistry.ts | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/vs/platform/theme/common/tokenStyleRegistry.ts b/src/vs/platform/theme/common/tokenStyleRegistry.ts index 27f1a7ebc29..45f548b3520 100644 --- a/src/vs/platform/theme/common/tokenStyleRegistry.ts +++ b/src/vs/platform/theme/common/tokenStyleRegistry.ts @@ -253,7 +253,43 @@ export const numbers = registerTokenStyle('numbers', { scopesToProbe: [['constan export const types = registerTokenStyle('types', { scopesToProbe: [['entity.name.type'], ['entity.name.class'], ['support.type'], ['support.class']], dark: null, light: null, hc: null }, nls.localize('types', "Token style for types.")); export const functions = registerTokenStyle('functions', { scopesToProbe: [['entity.name.function'], ['support.function']], dark: null, light: null, hc: null }, nls.localize('functions', "Token style for functions.")); export const variables = registerTokenStyle('variables', { scopesToProbe: [['variable'], ['entity.name.variable']], dark: null, light: null, hc: null }, nls.localize('variables', "Token style for variables.")); +/* +Tags: local, param, reference, write, async, documentation, overloaded +Struct + +Type +Class : Type +Interface : Type + +Field : Property +Method : Property +Property + +Variable +Parameter +Const + +Function + +Macro +Operator + +Namespace +Label +Event + +(Emum) + +Operator +Keyword + +literal +string : literal +number : literal +regex: literal + +*/ /** * @param colorValue Resolve a color value in the context of a theme */ From b351673fac97975912bb1386eff213ebdc32d9e9 Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Fri, 27 Sep 2019 16:54:45 +0200 Subject: [PATCH 08/15] TokenTypes & TokenModifiers --- .../browser/standaloneThemeServiceImpl.ts | 8 +- .../test/browser/standaloneLanguages.test.ts | 5 +- src/vs/platform/theme/common/themeService.ts | 6 +- .../common/tokenClassificationRegistry.ts | 345 ++++++++++++++++++ .../theme/common/tokenStyleRegistry.ts | 327 ----------------- .../theme/test/common/testThemeService.ts | 10 +- .../terminalColorRegistry.test.ts | 4 +- .../services/themes/common/colorThemeData.ts | 121 +++--- .../tokenStyleResolving.test.ts | 74 ++-- 9 files changed, 465 insertions(+), 435 deletions(-) create mode 100644 src/vs/platform/theme/common/tokenClassificationRegistry.ts delete mode 100644 src/vs/platform/theme/common/tokenStyleRegistry.ts diff --git a/src/vs/editor/standalone/browser/standaloneThemeServiceImpl.ts b/src/vs/editor/standalone/browser/standaloneThemeServiceImpl.ts index b26e44bf76c..61cf4274a0d 100644 --- a/src/vs/editor/standalone/browser/standaloneThemeServiceImpl.ts +++ b/src/vs/editor/standalone/browser/standaloneThemeServiceImpl.ts @@ -14,7 +14,7 @@ import { IEnvironmentService } from 'vs/platform/environment/common/environment' import { Registry } from 'vs/platform/registry/common/platform'; import { ColorIdentifier, Extensions, IColorRegistry } from 'vs/platform/theme/common/colorRegistry'; import { Extensions as ThemingExtensions, ICssStyleCollector, IIconTheme, IThemingRegistry } from 'vs/platform/theme/common/themeService'; -import { TokenStyle, TokenStyleIdentifier } from 'vs/platform/theme/common/tokenStyleRegistry'; +import { TokenStyle, TokenClassification, ProbeScope } from 'vs/platform/theme/common/tokenClassificationRegistry'; const VS_THEME_NAME = 'vs'; const VS_DARK_THEME_NAME = 'vs-dark'; @@ -131,7 +131,11 @@ class StandaloneTheme implements IStandaloneTheme { return this._tokenTheme; } - getTokenStyle(tokenStyle: TokenStyleIdentifier, useDefault?: boolean | undefined): TokenStyle | undefined { + getTokenStyle(classification: TokenClassification, useDefault?: boolean | undefined): TokenStyle | undefined { + return undefined; + } + + resolveScopes(scopes: ProbeScope[]): TokenStyle | undefined { return undefined; } } diff --git a/src/vs/editor/standalone/test/browser/standaloneLanguages.test.ts b/src/vs/editor/standalone/test/browser/standaloneLanguages.test.ts index 5dfa7d92149..4be6f232107 100644 --- a/src/vs/editor/standalone/test/browser/standaloneLanguages.test.ts +++ b/src/vs/editor/standalone/test/browser/standaloneLanguages.test.ts @@ -13,7 +13,6 @@ import { ILineTokens, IToken, TokenizationSupport2Adapter, TokensProvider } from import { IStandaloneTheme, IStandaloneThemeData, IStandaloneThemeService } from 'vs/editor/standalone/common/standaloneThemeService'; import { ColorIdentifier } from 'vs/platform/theme/common/colorRegistry'; import { IIconTheme, ITheme, LIGHT } from 'vs/platform/theme/common/themeService'; -import { TokenStyleIdentifier } from 'vs/platform/theme/common/tokenStyleRegistry'; suite('TokenizationSupport2Adapter', () => { @@ -57,7 +56,9 @@ suite('TokenizationSupport2Adapter', () => { throw new Error('Not implemented'); }, - getTokenStyle: (tokenStyleId: TokenStyleIdentifier) => undefined + getTokenStyle: () => undefined, + + }; } public getIconTheme(): IIconTheme { diff --git a/src/vs/platform/theme/common/themeService.ts b/src/vs/platform/theme/common/themeService.ts index 7ac9788d1a7..8117fd486a3 100644 --- a/src/vs/platform/theme/common/themeService.ts +++ b/src/vs/platform/theme/common/themeService.ts @@ -10,7 +10,7 @@ import * as platform from 'vs/platform/registry/common/platform'; import { ColorIdentifier } from 'vs/platform/theme/common/colorRegistry'; import { Event, Emitter } from 'vs/base/common/event'; import { IEnvironmentService } from 'vs/platform/environment/common/environment'; -import { TokenStyleIdentifier, TokenStyle } from 'vs/platform/theme/common/tokenStyleRegistry'; +import { TokenStyle, TokenClassification, ProbeScope } from 'vs/platform/theme/common/tokenClassificationRegistry'; export const IThemeService = createDecorator('themeService'); @@ -61,7 +61,9 @@ export interface ITheme { */ defines(color: ColorIdentifier): boolean; - getTokenStyle(color: TokenStyleIdentifier, useDefault?: boolean): TokenStyle | undefined; + getTokenStyle(classification: TokenClassification, useDefault?: boolean): TokenStyle | undefined; + + resolveScopes(scopes: ProbeScope[]): TokenStyle | undefined; } export interface IIconTheme { diff --git a/src/vs/platform/theme/common/tokenClassificationRegistry.ts b/src/vs/platform/theme/common/tokenClassificationRegistry.ts new file mode 100644 index 00000000000..d1778608f8f --- /dev/null +++ b/src/vs/platform/theme/common/tokenClassificationRegistry.ts @@ -0,0 +1,345 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import * as platform from 'vs/platform/registry/common/platform'; +import { Color } from 'vs/base/common/color'; +import { ITheme } from 'vs/platform/theme/common/themeService'; +import * as nls from 'vs/nls'; + +import { editorForeground } from 'vs/platform/theme/common/colorRegistry'; + +// ------ API types + +export const TOKEN_TYPE_WILDCARD = '*'; +export const TOKEN_TYPE_WILDCARD_NUM = -1; + +// qualified string [type|*](.modifier)* +export type TokenClassificationString = string; + +export interface TokenClassification { + type: number; + modifiers: number; +} + +export interface TokenTypeOrModifierContribution { + readonly num: number; + readonly id: string; + readonly description: string; + readonly deprecationMessage: string | undefined; +} + + +export interface TokenStyleData { + foreground?: Color; + bold?: boolean; + underline?: boolean; + italic?: boolean; +} + +export class TokenStyle implements Readonly { + constructor( + public readonly foreground?: Color, + public readonly bold?: boolean, + public readonly underline?: boolean, + public readonly italic?: boolean, + ) { + } +} + +export namespace TokenStyle { + export function fromData(data: { foreground?: Color, bold?: boolean, underline?: boolean, italic?: boolean }) { + return new TokenStyle(data.foreground, data.bold, data.underline, data.italic); + } +} + +export type ProbeScope = string[]; + +export interface TokenStyleFunction { + (theme: ITheme): TokenStyle | undefined; +} + +export interface TokenStyleDefaults { + scopesToProbe: ProbeScope[]; + light: TokenStyleValue | null; + dark: TokenStyleValue | null; + hc: TokenStyleValue | null; +} + +export interface TokenStylingDefaultRule { + classification: TokenClassification; + matchScore: number; + defaults: TokenStyleDefaults; +} + +export interface TokenStylingRule { + classification: TokenClassification; + matchScore: number; + value: TokenStyle; +} + +/** + * A TokenStyle Value is either a token style literal, or a TokenClassificationString + */ +export type TokenStyleValue = TokenStyle | TokenClassificationString; + +// TokenStyle registry +export const Extensions = { + TokenClassificationContribution: 'base.contributions.tokenClassification' +}; + +export interface ITokenClassificationRegistry { + + /** + * Register a token type to the registry. + * @param id The TokenType id as used in theme description files + * @description the description + */ + registerTokenType(id: string, description: string): void; + + /** + * Register a token modifier to the registry. + * @param id The TokenModifier id as used in theme description files + * @description the description + */ + registerTokenModifier(id: string, description: string): void; + + getTokenClassificationFromString(str: TokenClassificationString): TokenClassification | undefined; + getTokenClassification(type: string, modifiers: string[]): TokenClassification | undefined; + + /** + * Register a TokenStyle default to the registry. + * @param selector The rule selector + * @param defaults The default values + */ + registerTokenStyleDefault(selector: TokenClassification, defaults: TokenStyleDefaults): void; + + /** + * Deregister a TokenType from the registry. + */ + deregisterTokenType(id: string): void; + + /** + * Deregister a TokenModifier from the registry. + */ + deregisterTokenModifier(id: string): void; + + /** + * Get all TokenType contributions + */ + getTokenTypes(): TokenTypeOrModifierContribution[]; + + /** + * Get all TokenModifier contributions + */ + getTokenModifiers(): TokenTypeOrModifierContribution[]; + + /** + * Resolves a token classification against the given rules and default rules from the registry. + */ + resolveTokenStyle(classification: TokenClassification, themingRules: TokenStylingRule[], useDefault: boolean, theme: ITheme): TokenStyle | undefined; +} + + + +class TokenClassificationRegistry implements ITokenClassificationRegistry { + + private currentTypeNumber = 0; + private currentModifierBit = 1; + + private tokenTypeById: { [key: string]: TokenTypeOrModifierContribution }; + private tokenModifierById: { [key: string]: TokenTypeOrModifierContribution }; + + private tokenStylingDefaultRules: TokenStylingDefaultRule[] = []; + + constructor() { + this.tokenTypeById = {}; + 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 { + const num = this.currentTypeNumber++; + let tokenStyleContribution: TokenTypeOrModifierContribution = { num, id, description, deprecationMessage }; + this.tokenTypeById[id] = tokenStyleContribution; + } + + public registerTokenModifier(id: string, description: string, deprecationMessage?: string): void { + const num = this.currentModifierBit; + this.currentModifierBit = this.currentModifierBit * 2; + let tokenStyleContribution: TokenTypeOrModifierContribution = { num, id, description, deprecationMessage }; + this.tokenModifierById[id] = tokenStyleContribution; + } + + 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 getTokenClassificationFromString(str: TokenClassificationString): TokenClassification | undefined { + const parts = str.split('.'); + const type = parts.shift(); + if (type) { + return this.getTokenClassification(type, parts); + } + return undefined; + } + + public registerTokenStyleDefault(classification: TokenClassification, defaults: TokenStyleDefaults): void { + const matchScore = bitCount(classification.modifiers) + ((classification.type !== TOKEN_TYPE_WILDCARD_NUM) ? 1 : 0); + this.tokenStylingDefaultRules.push({ classification, matchScore, defaults }); + } + + public deregisterTokenType(id: string): void { + delete this.tokenTypeById[id]; + } + + public deregisterTokenModifier(id: string): void { + delete this.tokenModifierById[id]; + } + + public getTokenTypes(): TokenTypeOrModifierContribution[] { + return Object.keys(this.tokenTypeById).map(id => this.tokenTypeById[id]); + } + + public getTokenModifiers(): TokenTypeOrModifierContribution[] { + return Object.keys(this.tokenModifierById).map(id => this.tokenModifierById[id]); + } + + public resolveTokenStyle(classification: TokenClassification, themingRules: TokenStylingRule[], useDefault: boolean, theme: ITheme): TokenStyle | undefined { + let result: any = { + foreground: theme.getColor(editorForeground), + bold: false, + underline: false, + italic: false + }; + let score = { + foreground: -1, + bold: -1, + underline: -1, + italic: -1 + }; + + function _processStyle(matchScore: number, style: TokenStyle) { + for (let p in result) { + const property = p as keyof TokenStyle; + const info = style[property]; + if (info !== undefined && score[property] <= matchScore) { + score[property] = matchScore; + result[property] = info; + } + } + } + themingRules.forEach(rule => { + const matchScore = match(rule, classification); + if (matchScore >= 0) { + _processStyle(matchScore, rule.value); + } + }); + if (useDefault) { + this.tokenStylingDefaultRules.forEach(rule => { + const matchScore = match(rule, classification); + if (matchScore >= 0) { + let style = theme.resolveScopes(rule.defaults.scopesToProbe); + if (!style) { + style = this.resolveTokenStyleValue(rule.defaults[theme.type], theme); + } + if (style) { + _processStyle(matchScore, style); + } + } + }); + } + return TokenStyle.fromData(result); + } + + /** + * @param tokenStyleValue Resolve a tokenStyleValue in the context of a theme + */ + private resolveTokenStyleValue(tokenStyleValue: TokenStyleValue | null, theme: ITheme): TokenStyle | undefined { + if (tokenStyleValue === null) { + return undefined; + } else if (typeof tokenStyleValue === 'string') { + const classification = this.getTokenClassificationFromString(tokenStyleValue); + if (classification) { + return theme.getTokenStyle(classification); + } + } else if (typeof tokenStyleValue === 'object') { + return tokenStyleValue; + } + return undefined; + } + + + public toString() { + let sorter = (a: string, b: string) => { + let cat1 = a.indexOf('.') === -1 ? 0 : 1; + let cat2 = b.indexOf('.') === -1 ? 0 : 1; + if (cat1 !== cat2) { + return cat1 - cat2; + } + return a.localeCompare(b); + }; + + return Object.keys(this.tokenTypeById).sort(sorter).map(k => `- \`${k}\`: ${this.tokenTypeById[k].description}`).join('\n'); + } + +} + +function match(themeSelector: TokenStylingRule | TokenStylingDefaultRule, classification: TokenClassification): number { + const selectorType = themeSelector.classification.type; + if (selectorType !== TOKEN_TYPE_WILDCARD_NUM && selectorType === classification.type) { + return -1; + } + const selectorModifier = themeSelector.classification.modifiers; + if ((classification.modifiers & selectorModifier) !== selectorModifier) { + return -1; + } + return themeSelector.matchScore; +} + + +const tokenClassificationRegistry = new TokenClassificationRegistry(); +platform.Registry.add(Extensions.TokenClassificationContribution, tokenClassificationRegistry); + +export function registerTokenType(id: string, defaults: TokenStyleDefaults | null, description: string, deprecationMessage?: string): string { + tokenClassificationRegistry.registerTokenType(id, description, deprecationMessage); + + if (defaults) { + const classification = tokenClassificationRegistry.getTokenClassification(id, []); + tokenClassificationRegistry.registerTokenStyleDefault(classification!, defaults); + } + return id; +} + +export function getTokenClassificationRegistry(): ITokenClassificationRegistry { + return tokenClassificationRegistry; +} + +export const comments = registerTokenType('comments', { scopesToProbe: [['comment']], dark: null, light: null, hc: null }, nls.localize('comments', "Token style for comments.")); +export const strings = registerTokenType('strings', { scopesToProbe: [['string']], dark: null, light: null, hc: null }, nls.localize('strings', "Token style for strings.")); +export const keywords = registerTokenType('keywords', { scopesToProbe: [['keyword.control'], ['storage'], ['storage.type']], dark: null, light: null, hc: null }, nls.localize('keywords', "Token style for keywords.")); +export const numbers = registerTokenType('numbers', { scopesToProbe: [['constant.numeric']], dark: null, light: null, hc: null }, nls.localize('numbers', "Token style for numbers.")); +export const types = registerTokenType('types', { scopesToProbe: [['entity.name.type'], ['entity.name.class'], ['support.type'], ['support.class']], dark: null, light: null, hc: null }, nls.localize('types', "Token style for types.")); +export const functions = registerTokenType('functions', { scopesToProbe: [['entity.name.function'], ['support.function']], dark: null, light: null, hc: null }, nls.localize('functions', "Token style for functions.")); +export const variables = registerTokenType('variables', { scopesToProbe: [['variable'], ['entity.name.variable']], dark: null, light: null, hc: null }, nls.localize('variables', "Token style for variables.")); + + + +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; +} diff --git a/src/vs/platform/theme/common/tokenStyleRegistry.ts b/src/vs/platform/theme/common/tokenStyleRegistry.ts deleted file mode 100644 index 45f548b3520..00000000000 --- a/src/vs/platform/theme/common/tokenStyleRegistry.ts +++ /dev/null @@ -1,327 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - *--------------------------------------------------------------------------------------------*/ - -import * as platform from 'vs/platform/registry/common/platform'; -import { IJSONSchema, IJSONSchemaMap } from 'vs/base/common/jsonSchema'; -import { Color } from 'vs/base/common/color'; -import { ITheme } from 'vs/platform/theme/common/themeService'; -import { Event, Emitter } from 'vs/base/common/event'; -import * as nls from 'vs/nls'; - -import { Extensions as JSONExtensions, IJSONContributionRegistry } from 'vs/platform/jsonschemas/common/jsonContributionRegistry'; -import { RunOnceScheduler } from 'vs/base/common/async'; -import { editorForeground } from 'vs/platform/theme/common/colorRegistry'; - -// ------ API types - -export type TokenStyleIdentifier = string; - -export interface TokenStyleContribution { - readonly id: TokenStyleIdentifier; - readonly description: string; - readonly defaults: TokenStyleDefaults | null; - readonly deprecationMessage: string | undefined; -} - -export const enum TokenStyleBits { - BOLD = 0x01, - UNDERLINE = 0x02, - ITALIC = 0x04 -} - -export class TokenStyle { - constructor( - public readonly foreground?: Color, - public readonly background?: Color, - public readonly styles?: number - ) { - - } - - hasStyle(style: number): boolean { - return !!this.styles && ((this.styles & style) === style); - } -} - -export namespace TokenStyle { - export function fromString(s: string) { - const parts = s.split('-'); - let part = parts.shift(); - if (part) { - const foreground = Color.fromHex(part); - let background = undefined; - let style = undefined; - part = parts.shift(); - if (part && part[0] === '#') { - background = Color.fromHex(part); - part = parts.shift(); - } - if (part) { - try { - style = parseInt(part); - } catch (e) { - // ignore - } - } - return new TokenStyle(foreground, background, style); - } - return new TokenStyle(Color.red); - } -} - -export type ProbeScope = string[]; - -export interface TokenStyleFunction { - (theme: ITheme): TokenStyle | undefined; -} - -export interface TokenStyleDefaults { - scopesToProbe?: ProbeScope[]; - light: TokenStyleValue | null; - dark: TokenStyleValue | null; - hc: TokenStyleValue | null; -} - -/** - * A TokenStyle Value is either a token style literal, a reference to other token style or a derived token style - */ -export type TokenStyleValue = TokenStyle | string | TokenStyleIdentifier | TokenStyleFunction; - -// TokenStyle registry -export const Extensions = { - TokenStyleContribution: 'base.contributions.tokenStyles' -}; - -export interface ITokenStyleRegistry { - - readonly onDidChangeSchema: Event; - - /** - * Register a TokenStyle to the registry. - * @param id The TokenStyle id as used in theme description files - * @param defaults The default values - * @description the description - */ - registerTokenStyle(id: string, defaults: TokenStyleDefaults, description: string): TokenStyleIdentifier; - - /** - * Register a TokenStyle to the registry. - */ - deregisterTokenStyle(id: string): void; - - /** - * Get all TokenStyle contributions - */ - getTokenStyles(): TokenStyleContribution[]; - - /** - * Gets the default TokenStyle of the given id - */ - resolveDefaultTokenStyle(id: TokenStyleIdentifier, theme: ITheme, findTokenStyleForScope: (scope: ProbeScope) => TokenStyle | undefined): TokenStyle | undefined; - - /** - * JSON schema for an object to assign TokenStyle values to one of the TokenStyle contributions. - */ - getTokenStyleSchema(): IJSONSchema; - - /** - * JSON schema to for a reference to a TokenStyle contribution. - */ - getTokenStyleReferenceSchema(): IJSONSchema; - -} - - - -class TokenStyleRegistry implements ITokenStyleRegistry { - - private readonly _onDidChangeSchema = new Emitter(); - readonly onDidChangeSchema: Event = this._onDidChangeSchema.event; - - private tokenStyleById: { [key: string]: TokenStyleContribution }; - private tokenStyleSchema: IJSONSchema & { properties: IJSONSchemaMap } = { type: 'object', properties: {} }; - private tokenStyleReferenceSchema: IJSONSchema & { enum: string[], enumDescriptions: string[] } = { type: 'string', enum: [], enumDescriptions: [] }; - - constructor() { - this.tokenStyleById = {}; - } - - public registerTokenStyle(id: string, defaults: TokenStyleDefaults | null, description: string, deprecationMessage?: string): TokenStyleIdentifier { - let tokenStyleContribution: TokenStyleContribution = { id, description, defaults, deprecationMessage }; - this.tokenStyleById[id] = tokenStyleContribution; - let propertySchema: IJSONSchema = { - type: 'object', - description, - properties: { - 'foreground': { type: 'string', format: 'color-hex', default: '#ff0000' }, - 'italic': { type: 'boolean' }, - 'bold': { type: 'boolean' }, - 'underline': { type: 'boolean' } - } - }; - if (deprecationMessage) { - propertySchema.deprecationMessage = deprecationMessage; - } - this.tokenStyleSchema.properties[id] = propertySchema; - this.tokenStyleReferenceSchema.enum.push(id); - this.tokenStyleReferenceSchema.enumDescriptions.push(description); - - this._onDidChangeSchema.fire(); - return id; - } - - - public deregisterTokenStyle(id: string): void { - delete this.tokenStyleById[id]; - delete this.tokenStyleSchema.properties[id]; - const index = this.tokenStyleReferenceSchema.enum.indexOf(id); - if (index !== -1) { - this.tokenStyleReferenceSchema.enum.splice(index, 1); - this.tokenStyleReferenceSchema.enumDescriptions.splice(index, 1); - } - this._onDidChangeSchema.fire(); - } - - public getTokenStyles(): TokenStyleContribution[] { - return Object.keys(this.tokenStyleById).map(id => this.tokenStyleById[id]); - } - - public resolveDefaultTokenStyle(id: TokenStyleIdentifier, theme: ITheme, findTokenStyleForScope: (scope: ProbeScope) => TokenStyle | undefined): TokenStyle | undefined { - const tokenStyleDesc = this.tokenStyleById[id]; - if (tokenStyleDesc && tokenStyleDesc.defaults) { - const scopesToProbe = tokenStyleDesc.defaults.scopesToProbe; - if (scopesToProbe) { - for (let scope of scopesToProbe) { - const style = findTokenStyleForScope(scope); - if (style) { - return style; - } - } - } - const tokenStyleValue = tokenStyleDesc.defaults[theme.type]; - if (tokenStyleValue === null) { - return new TokenStyle(theme.getColor(editorForeground)); - } - return resolveTokenStyleValue(tokenStyleValue, theme); - } - return undefined; - } - - public getTokenStyleSchema(): IJSONSchema { - return this.tokenStyleSchema; - } - - public getTokenStyleReferenceSchema(): IJSONSchema { - return this.tokenStyleReferenceSchema; - } - - public toString() { - let sorter = (a: string, b: string) => { - let cat1 = a.indexOf('.') === -1 ? 0 : 1; - let cat2 = b.indexOf('.') === -1 ? 0 : 1; - if (cat1 !== cat2) { - return cat1 - cat2; - } - return a.localeCompare(b); - }; - - return Object.keys(this.tokenStyleById).sort(sorter).map(k => `- \`${k}\`: ${this.tokenStyleById[k].description}`).join('\n'); - } - -} - -const tokenStyleRegistry = new TokenStyleRegistry(); -platform.Registry.add(Extensions.TokenStyleContribution, tokenStyleRegistry); - -export function registerTokenStyle(id: string, defaults: TokenStyleDefaults | null, description: string, deprecationMessage?: string): TokenStyleIdentifier { - return tokenStyleRegistry.registerTokenStyle(id, defaults, description, deprecationMessage); -} - -export function getTokenStyleRegistry(): ITokenStyleRegistry { - return tokenStyleRegistry; -} - -// colors - - -export const comments = registerTokenStyle('comments', { scopesToProbe: [['comment']], dark: null, light: null, hc: null }, nls.localize('comments', "Token style for comments.")); -export const strings = registerTokenStyle('strings', { scopesToProbe: [['string']], dark: null, light: null, hc: null }, nls.localize('strings', "Token style for strings.")); -export const keywords = registerTokenStyle('keywords', { scopesToProbe: [['keyword.control'], ['storage'], ['storage.type']], dark: null, light: null, hc: null }, nls.localize('keywords', "Token style for keywords.")); -export const numbers = registerTokenStyle('numbers', { scopesToProbe: [['constant.numeric']], dark: null, light: null, hc: null }, nls.localize('numbers', "Token style for numbers.")); -export const types = registerTokenStyle('types', { scopesToProbe: [['entity.name.type'], ['entity.name.class'], ['support.type'], ['support.class']], dark: null, light: null, hc: null }, nls.localize('types', "Token style for types.")); -export const functions = registerTokenStyle('functions', { scopesToProbe: [['entity.name.function'], ['support.function']], dark: null, light: null, hc: null }, nls.localize('functions', "Token style for functions.")); -export const variables = registerTokenStyle('variables', { scopesToProbe: [['variable'], ['entity.name.variable']], dark: null, light: null, hc: null }, nls.localize('variables', "Token style for variables.")); -/* -Tags: local, param, reference, write, async, documentation, overloaded - -Struct - -Type -Class : Type -Interface : Type - -Field : Property -Method : Property -Property - -Variable -Parameter -Const - -Function - -Macro -Operator - -Namespace -Label -Event - -(Emum) - -Operator -Keyword - -literal -string : literal -number : literal -regex: literal - -*/ -/** - * @param colorValue Resolve a color value in the context of a theme - */ -function resolveTokenStyleValue(tokenStyleValue: TokenStyleValue | null, theme: ITheme): TokenStyle | undefined { - if (tokenStyleValue === null) { - return undefined; - } else if (typeof tokenStyleValue === 'string') { - if (tokenStyleValue[0] === '#') { - return TokenStyle.fromString(tokenStyleValue); - } - return theme.getTokenStyle(tokenStyleValue); - } else if (typeof tokenStyleValue === 'object') { - return tokenStyleValue; - } else if (typeof tokenStyleValue === 'function') { - return tokenStyleValue(theme); - } - return undefined; -} - -export const tokenStyleColorsSchemaId = 'vscode://schemas/workbench-tokenstyles'; - -let schemaRegistry = platform.Registry.as(JSONExtensions.JSONContribution); -schemaRegistry.registerSchema(tokenStyleColorsSchemaId, tokenStyleRegistry.getTokenStyleSchema()); - -const delayer = new RunOnceScheduler(() => schemaRegistry.notifySchemaChanged(tokenStyleColorsSchemaId), 200); -tokenStyleRegistry.onDidChangeSchema(() => { - if (!delayer.isScheduled()) { - delayer.schedule(); - } -}); - -// setTimeout(_ => console.log(colorRegistry.toString()), 5000); - - - diff --git a/src/vs/platform/theme/test/common/testThemeService.ts b/src/vs/platform/theme/test/common/testThemeService.ts index e384b457389..b9279dc39ee 100644 --- a/src/vs/platform/theme/test/common/testThemeService.ts +++ b/src/vs/platform/theme/test/common/testThemeService.ts @@ -6,7 +6,7 @@ import { Event, Emitter } from 'vs/base/common/event'; import { IThemeService, ITheme, DARK, IIconTheme } from 'vs/platform/theme/common/themeService'; import { Color } from 'vs/base/common/color'; -import { TokenStyle, TokenStyleIdentifier } from 'vs/platform/theme/common/tokenStyleRegistry'; +import { TokenStyle, TokenClassification, ProbeScope } from 'vs/platform/theme/common/tokenClassificationRegistry'; export class TestTheme implements ITheme { @@ -25,8 +25,12 @@ export class TestTheme implements ITheme { throw new Error('Method not implemented.'); } - getTokenStyle(tokenStyleIdentifier: TokenStyleIdentifier, useDefault?: boolean | undefined): TokenStyle | undefined { - return undefined; + getTokenStyle(classification: TokenClassification, useDefault?: boolean | undefined): TokenStyle | undefined { + throw new Error('Method not implemented.'); + } + + resolveScopes(scopes: ProbeScope[]): TokenStyle | undefined { + throw new Error('Method not implemented.'); } } diff --git a/src/vs/workbench/contrib/terminal/test/electron-browser/terminalColorRegistry.test.ts b/src/vs/workbench/contrib/terminal/test/electron-browser/terminalColorRegistry.test.ts index d2d64fa1a24..1026d3a91a1 100644 --- a/src/vs/workbench/contrib/terminal/test/electron-browser/terminalColorRegistry.test.ts +++ b/src/vs/workbench/contrib/terminal/test/electron-browser/terminalColorRegistry.test.ts @@ -9,7 +9,7 @@ import { Registry } from 'vs/platform/registry/common/platform'; import { ansiColorIdentifiers, registerColors } from 'vs/workbench/contrib/terminal/common/terminalColorRegistry'; import { ITheme, ThemeType } from 'vs/platform/theme/common/themeService'; import { Color } from 'vs/base/common/color'; -import { TokenStyleIdentifier } from 'vs/platform/theme/common/tokenStyleRegistry'; +import { TokenClassification } from 'vs/platform/theme/common/tokenClassificationRegistry'; registerColors(); @@ -21,7 +21,7 @@ function getMockTheme(type: ThemeType): ITheme { type: type, getColor: (colorId: ColorIdentifier): Color | undefined => themingRegistry.resolveDefaultColor(colorId, theme), defines: () => true, - getTokenStyle: (tokenStyleId: TokenStyleIdentifier) => undefined + getTokenStyle: (tokenStyleId: TokenClassification) => undefined }; return theme; } diff --git a/src/vs/workbench/services/themes/common/colorThemeData.ts b/src/vs/workbench/services/themes/common/colorThemeData.ts index 4dbdfcc2eff..53fa06ef149 100644 --- a/src/vs/workbench/services/themes/common/colorThemeData.ts +++ b/src/vs/workbench/services/themes/common/colorThemeData.ts @@ -20,12 +20,12 @@ import { URI } from 'vs/base/common/uri'; import { IFileService } from 'vs/platform/files/common/files'; import { parse as parsePList } from 'vs/workbench/services/themes/common/plistParser'; import { startsWith } from 'vs/base/common/strings'; -import { Extensions as TokenStyleRegistryExtensions, TokenStyle, TokenStyleIdentifier, ITokenStyleRegistry, TokenStyleBits, ProbeScope } from 'vs/platform/theme/common/tokenStyleRegistry'; +import { Extensions as TokenStyleRegistryExtensions, TokenStyle, TokenClassification, ITokenClassificationRegistry, ProbeScope, TokenStylingRule } from 'vs/platform/theme/common/tokenClassificationRegistry'; import { MatcherWithPriority, Matcher, createMatchers } from 'vs/workbench/services/themes/common/textMateScopeMatcher'; let colorRegistry = Registry.as(ColorRegistryExtensions.ColorContribution); -let tokenStyleRegistry = Registry.as(TokenStyleRegistryExtensions.TokenStyleContribution); +let tokenClassificationRegistry = Registry.as(TokenStyleRegistryExtensions.TokenClassificationContribution); const tokenGroupToScopesMap = { comments: ['comment'], @@ -37,9 +37,6 @@ const tokenGroupToScopesMap = { variables: ['variable', 'entity.name.variable'] }; -interface ITokenStyleMap { - [id: string]: TokenStyle; -} export class ColorThemeData implements IColorTheme { @@ -57,7 +54,7 @@ export class ColorThemeData implements IColorTheme { private colorMap: IColorMap = {}; private customColorMap: IColorMap = {}; - private tokenStyleMap: ITokenStyleMap = {}; + private tokenStylingRules: TokenStylingRule[] = []; private themeTokenScopeMatchers: Matcher[] | undefined; private customTokenScopeMatchers: Matcher[] | undefined; @@ -116,73 +113,57 @@ export class ColorThemeData implements IColorTheme { return color; } - public getTokenStyle(tokenStyleId: TokenStyleIdentifier, useDefault?: boolean): TokenStyle | undefined { - let style: TokenStyle | undefined = this.tokenStyleMap[tokenStyleId]; - if (style) { - return style; - } - if (useDefault !== false && types.isUndefined(style)) { - style = this.getDefaultTokenStyle(tokenStyleId); - } - return style; + public getTokenStyle(tokenClassification: TokenClassification, useDefault?: boolean): TokenStyle | undefined { + // todo: cache results + return tokenClassificationRegistry.resolveTokenStyle(tokenClassification, this.tokenStylingRules, !!useDefault, this); } public getDefault(colorId: ColorIdentifier): Color | undefined { return colorRegistry.resolveDefaultColor(colorId, this); } - public getDefaultTokenStyle(tokenStyleId: TokenStyleIdentifier): TokenStyle | undefined { - return tokenStyleRegistry.resolveDefaultTokenStyle(tokenStyleId, this, this.findTokenStyleForScope.bind(this)); + public getDefaultTokenStyle(tokenClassification: TokenClassification): TokenStyle | undefined { + return tokenClassificationRegistry.resolveTokenStyle(tokenClassification, [], true, this); } - /** Public for testing reasons */ - public findTokenStyleForScope(scope: ProbeScope): TokenStyle | undefined { - - let foregroundColor = this.getColor(editorForeground); - - let foreground = foregroundColor ? foregroundColor.toString() : null; - let fontStyle: string | null = null; - let foregroundScore = -1; - let fontStyleScore = -1; - - function findTokenStyleForScopeInScopes(scopeMatchers: Matcher[], tokenColors: ITokenColorizationRule[]) { - for (let i = 0; i < scopeMatchers.length; i++) { - const settings = tokenColors[i].settings; - if (!settings) { - continue; - } - let matcher = scopeMatchers[i]; - if (!matcher) { - scopeMatchers[i] = matcher = getScopeMatcher(tokenColors[i]); - } - const score = matcher(scope); - if (score >= 0) { - if (score >= foregroundScore && settings.foreground) { - foreground = settings.foreground; - } - if (score >= fontStyleScore && types.isString(settings.fontStyle)) { - fontStyle = settings.fontStyle; - } - } - - } - } + public resolveScopes(scopes: ProbeScope[]): TokenStyle | undefined { if (!this.themeTokenScopeMatchers) { - this.themeTokenScopeMatchers = new Array(this.themeTokenColors.length); + this.themeTokenScopeMatchers = this.themeTokenColors.map(getScopeMatcher); } - findTokenStyleForScopeInScopes(this.themeTokenScopeMatchers, this.themeTokenColors); - if (!this.customTokenScopeMatchers) { - this.customTokenScopeMatchers = new Array(this.customTokenColors.length); + this.customTokenScopeMatchers = this.customTokenColors.map(getScopeMatcher); } - findTokenStyleForScopeInScopes(this.customTokenScopeMatchers, this.customTokenColors); - return getTokenStyle(foreground, fontStyle); + for (let scope of scopes) { + let foreground: string | null = null; + let fontStyle: string | null = null; + let foregroundScore = -1; + let fontStyleScore = -1; + + function findTokenStyleForScopeInScopes(scopeMatchers: Matcher[], tokenColors: ITokenColorizationRule[]) { + for (let i = 0; i < scopeMatchers.length; i++) { + const score = scopeMatchers[i](scope); + if (score >= 0) { + const settings = tokenColors[i].settings; + if (score >= foregroundScore && settings.foreground) { + foreground = settings.foreground; + } + if (score >= fontStyleScore && types.isString(settings.fontStyle)) { + fontStyle = settings.fontStyle; + } + } + } + } + findTokenStyleForScopeInScopes(this.themeTokenScopeMatchers, this.themeTokenColors); + findTokenStyleForScopeInScopes(this.customTokenScopeMatchers, this.customTokenColors); + if (foreground !== null || fontStyle !== null) { + return getTokenStyle(foreground, fontStyle); + } + } + return undefined; } - - public defines(colorId: ColorIdentifier): boolean { return this.customColorMap.hasOwnProperty(colorId) || this.colorMap.hasOwnProperty(colorId); } @@ -508,12 +489,8 @@ function getScopeMatcher(rule: ITokenColorizationRule): Matcher { return noMatch; } const matchers: MatcherWithPriority[] = []; - if (Array.isArray(ruleScope)) { - for (let rs of ruleScope) { - matchers.push(...createMatchers(rs, nameMatcher)); - } - } else { - matchers.push(...createMatchers(ruleScope, nameMatcher)); + for (let rs of ruleScope) { + matchers.push(...createMatchers(rs, nameMatcher)); } if (matchers.length === 0) { return noMatch; @@ -528,24 +505,16 @@ function getScopeMatcher(rule: ITokenColorizationRule): Matcher { } function getTokenStyle(foreground: string | null, fontStyle: string | null): TokenStyle | undefined { - let styles: number | undefined; let foregroundColor = undefined; if (foreground !== null) { foregroundColor = Color.fromHex(foreground); } - + let bold, underline, italic; if (fontStyle !== null) { - styles = 0; - if (fontStyle.indexOf('underline') !== -1) { - styles |= TokenStyleBits.UNDERLINE; - } - if (fontStyle.indexOf('italic') !== -1) { - styles |= TokenStyleBits.ITALIC; - } - if (fontStyle.indexOf('bold') !== -1) { - styles |= TokenStyleBits.BOLD; - } + bold = fontStyle.indexOf('bold') !== -1; + underline = fontStyle.indexOf('underline') !== -1; + italic = fontStyle.indexOf('italic') !== -1; } - return new TokenStyle(foregroundColor, undefined, styles); + return new TokenStyle(foregroundColor, bold, underline, italic); } diff --git a/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts b/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts index 12b9e9102da..324a9664a3c 100644 --- a/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts +++ b/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts @@ -6,7 +6,7 @@ import { ColorThemeData } from 'vs/workbench/services/themes/common/colorThemeData'; import * as assert from 'assert'; import { ITokenColorCustomizations } from 'vs/workbench/services/themes/common/workbenchThemeService'; -import { TokenStyle, TokenStyleBits, comments, variables, types, functions, keywords, numbers, strings } from 'vs/platform/theme/common/tokenStyleRegistry'; +import { Extensions as TokenStyleRegistryExtensions, ITokenClassificationRegistry, TokenStyle, comments, variables, types, functions, keywords, numbers, strings } from 'vs/platform/theme/common/tokenClassificationRegistry'; import { Color } from 'vs/base/common/color'; import { isString } from 'vs/base/common/types'; import { FileService } from 'vs/platform/files/common/fileService'; @@ -15,30 +15,59 @@ import { DiskFileSystemProvider } from 'vs/platform/files/node/diskFileSystemPro import { Schemas } from 'vs/base/common/network'; import { URI } from 'vs/base/common/uri'; import { getPathFromAmdModule } from 'vs/base/common/amd'; -import { editorForeground } from 'vs/platform/theme/common/colorRegistry'; +import { Registry } from 'vs/platform/registry/common/platform'; + +let tokenClassificationRegistry = Registry.as(TokenStyleRegistryExtensions.TokenClassificationContribution); + +const enum TokenStyleBits { + BOLD = 0x01, + UNDERLINE = 0x02, + ITALIC = 0x04 +} + function ts(foreground: string | undefined, styleFlags: number | undefined): TokenStyle { const foregroundColor = isString(foreground) ? Color.fromHex(foreground) : undefined; - return new TokenStyle(foregroundColor, undefined, styleFlags); + let bold, underline, italic; + if (styleFlags !== undefined) { + bold = (styleFlags & TokenStyleBits.BOLD) !== 0; + underline = (styleFlags & TokenStyleBits.UNDERLINE) !== 0; + italic = (styleFlags & TokenStyleBits.ITALIC) !== 0; + } + return new TokenStyle(foregroundColor, bold, underline, italic); } function tokenStyleAsString(ts: TokenStyle | undefined | null) { - return ts ? `${ts.foreground ? ts.foreground.toString() : 'no-foreground'}-${ts.styles ? ts.styles : 'no-styles'}` : 'tokenstyle-undefined'; + if (!ts) { + return 'tokenstyle-undefined'; + } + let str = ts.foreground ? ts.foreground.toString() : 'no-foreground'; + if (ts.bold !== undefined) { + str = ts.bold ? '+B' : '-B'; + } + if (ts.underline !== undefined) { + str = ts.underline ? '+U' : '-U'; + } + if (ts.italic !== undefined) { + str = ts.italic ? '+I' : '-I'; + } + return str; } function assertTokenStyle(actual: TokenStyle | undefined | null, expected: TokenStyle | undefined | null, message?: string) { assert.equal(tokenStyleAsString(actual), tokenStyleAsString(expected), message); } -function assertTokenStyles(themeData: ColorThemeData, expected: { [tokenStyleId: string]: TokenStyle }) { - for (let tokenStyleId in expected) { - const tokenStyle = themeData.getTokenStyle(tokenStyleId); - assertTokenStyle(tokenStyle, expected[tokenStyleId], tokenStyleId); +function assertTokenStyles(themeData: ColorThemeData, expected: { [qualifiedClassifier: string]: TokenStyle }) { + for (let qualifiedClassifier in expected) { + const classification = tokenClassificationRegistry.getTokenClassificationFromString(qualifiedClassifier); + assert.ok(classification, 'Classification not found'); + + const tokenStyle = themeData.getTokenStyle(classification!); + assertTokenStyle(tokenStyle, expected[qualifiedClassifier], qualifiedClassifier); } } - - suite('Themes - TokenStyleResolving', () => { const fileService = new FileService(new NullLogService()); const diskFileSystemProvider = new DiskFileSystemProvider(new NullLogService()); @@ -165,7 +194,7 @@ suite('Themes - TokenStyleResolving', () => { }); - test('resolve resource', async () => { + test('resolveScopes', async () => { const themeData = ColorThemeData.createLoadedEmptyTheme('test', 'test'); const customTokenColors: ITokenColorCustomizations = { @@ -210,34 +239,37 @@ suite('Themes - TokenStyleResolving', () => { themeData.setCustomTokenColors(customTokenColors); let tokenStyle; - let defaultTokenStyle = new TokenStyle(themeData.getColor(editorForeground)); + let defaultTokenStyle = undefined; - tokenStyle = themeData.findTokenStyleForScope(['variable']); + tokenStyle = themeData.resolveScopes([['variable']]); assertTokenStyle(tokenStyle, ts('#F8F8F2', 0), 'variable'); - tokenStyle = themeData.findTokenStyleForScope(['keyword.operator']); + tokenStyle = themeData.resolveScopes([['keyword.operator']]); assertTokenStyle(tokenStyle, ts('#F92672', TokenStyleBits.ITALIC | TokenStyleBits.BOLD | TokenStyleBits.UNDERLINE), 'keyword'); - tokenStyle = themeData.findTokenStyleForScope(['keyword']); + tokenStyle = themeData.resolveScopes([['keyword']]); assertTokenStyle(tokenStyle, defaultTokenStyle, 'keyword'); - tokenStyle = themeData.findTokenStyleForScope(['keyword.operator']); + tokenStyle = themeData.resolveScopes([['keyword.operator']]); assertTokenStyle(tokenStyle, ts('#F92672', TokenStyleBits.ITALIC | TokenStyleBits.BOLD | TokenStyleBits.UNDERLINE), 'keyword.operator'); - tokenStyle = themeData.findTokenStyleForScope(['keyword.operators']); + tokenStyle = themeData.resolveScopes([['keyword.operators']]); assertTokenStyle(tokenStyle, defaultTokenStyle, 'keyword.operators'); - tokenStyle = themeData.findTokenStyleForScope(['storage']); + tokenStyle = themeData.resolveScopes([['storage']]); assertTokenStyle(tokenStyle, ts('#F92672', TokenStyleBits.ITALIC), 'storage'); - tokenStyle = themeData.findTokenStyleForScope(['storage.type']); + tokenStyle = themeData.resolveScopes([['storage.type']]); assertTokenStyle(tokenStyle, ts('#66D9EF', TokenStyleBits.ITALIC), 'storage.type'); - tokenStyle = themeData.findTokenStyleForScope(['entity.name.class']); + tokenStyle = themeData.resolveScopes([['entity.name.class']]); assertTokenStyle(tokenStyle, ts('#A6E22E', TokenStyleBits.UNDERLINE), 'entity.name.class'); - tokenStyle = themeData.findTokenStyleForScope(['meta.structure.dictionary.json', 'string.quoted.double.json']); + tokenStyle = themeData.resolveScopes([['meta.structure.dictionary.json', 'string.quoted.double.json']]); assertTokenStyle(tokenStyle, ts('#66D9EF', undefined), 'json property'); + tokenStyle = themeData.resolveScopes([['keyword'], ['storage.type'], ['entity.name.class']]); + assertTokenStyle(tokenStyle, ts('#66D9EF', TokenStyleBits.ITALIC), 'storage.type'); + }); }); From 3ec1475b14e277d5750ae145dc9ad0720c91e649 Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Fri, 27 Sep 2019 17:22:30 +0200 Subject: [PATCH 09/15] fix missing resolveScopes --- .../standalone/test/browser/standaloneLanguages.test.ts | 2 +- .../test/electron-browser/terminalColorRegistry.test.ts | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/vs/editor/standalone/test/browser/standaloneLanguages.test.ts b/src/vs/editor/standalone/test/browser/standaloneLanguages.test.ts index 4be6f232107..3cb4e474b1d 100644 --- a/src/vs/editor/standalone/test/browser/standaloneLanguages.test.ts +++ b/src/vs/editor/standalone/test/browser/standaloneLanguages.test.ts @@ -57,7 +57,7 @@ suite('TokenizationSupport2Adapter', () => { }, getTokenStyle: () => undefined, - + resolveScopes: () => undefined }; } diff --git a/src/vs/workbench/contrib/terminal/test/electron-browser/terminalColorRegistry.test.ts b/src/vs/workbench/contrib/terminal/test/electron-browser/terminalColorRegistry.test.ts index 1026d3a91a1..10cad170f83 100644 --- a/src/vs/workbench/contrib/terminal/test/electron-browser/terminalColorRegistry.test.ts +++ b/src/vs/workbench/contrib/terminal/test/electron-browser/terminalColorRegistry.test.ts @@ -9,7 +9,6 @@ import { Registry } from 'vs/platform/registry/common/platform'; import { ansiColorIdentifiers, registerColors } from 'vs/workbench/contrib/terminal/common/terminalColorRegistry'; import { ITheme, ThemeType } from 'vs/platform/theme/common/themeService'; import { Color } from 'vs/base/common/color'; -import { TokenClassification } from 'vs/platform/theme/common/tokenClassificationRegistry'; registerColors(); @@ -21,7 +20,9 @@ function getMockTheme(type: ThemeType): ITheme { type: type, getColor: (colorId: ColorIdentifier): Color | undefined => themingRegistry.resolveDefaultColor(colorId, theme), defines: () => true, - getTokenStyle: (tokenStyleId: TokenClassification) => undefined + getTokenStyle: () => undefined, + resolveScopes: () => undefined + }; return theme; } From 74c44d63a2595c31a6e95f88bf88cdbf0f98a622 Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Mon, 7 Oct 2019 16:57:52 +0200 Subject: [PATCH 10/15] all types & modifiers --- .../common/tokenClassificationRegistry.ts | 50 +++++++++++++++---- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/src/vs/platform/theme/common/tokenClassificationRegistry.ts b/src/vs/platform/theme/common/tokenClassificationRegistry.ts index d1778608f8f..9b46ae5cbe1 100644 --- a/src/vs/platform/theme/common/tokenClassificationRegistry.ts +++ b/src/vs/platform/theme/common/tokenClassificationRegistry.ts @@ -314,29 +314,59 @@ function match(themeSelector: TokenStylingRule | TokenStylingDefaultRule, classi const tokenClassificationRegistry = new TokenClassificationRegistry(); platform.Registry.add(Extensions.TokenClassificationContribution, tokenClassificationRegistry); -export function registerTokenType(id: string, defaults: TokenStyleDefaults | null, description: string, deprecationMessage?: string): string { +export function registerTokenType(id: string, description: string, scopesToProbe: ProbeScope[] = [], extendsTC: string | null = null, deprecationMessage?: string): string { tokenClassificationRegistry.registerTokenType(id, description, deprecationMessage); - if (defaults) { + if (scopesToProbe || extendsTC) { const classification = tokenClassificationRegistry.getTokenClassification(id, []); - tokenClassificationRegistry.registerTokenStyleDefault(classification!, defaults); + tokenClassificationRegistry.registerTokenStyleDefault(classification!, { scopesToProbe, light: extendsTC, dark: extendsTC, hc: extendsTC }); } return id; } +export function registerTokenModifier(id: string, description: string, deprecationMessage?: string): string { + tokenClassificationRegistry.registerTokenModifier(id, description, deprecationMessage); + return id; +} + export function getTokenClassificationRegistry(): ITokenClassificationRegistry { return tokenClassificationRegistry; } -export const comments = registerTokenType('comments', { scopesToProbe: [['comment']], dark: null, light: null, hc: null }, nls.localize('comments', "Token style for comments.")); -export const strings = registerTokenType('strings', { scopesToProbe: [['string']], dark: null, light: null, hc: null }, nls.localize('strings', "Token style for strings.")); -export const keywords = registerTokenType('keywords', { scopesToProbe: [['keyword.control'], ['storage'], ['storage.type']], dark: null, light: null, hc: null }, nls.localize('keywords', "Token style for keywords.")); -export const numbers = registerTokenType('numbers', { scopesToProbe: [['constant.numeric']], dark: null, light: null, hc: null }, nls.localize('numbers', "Token style for numbers.")); -export const types = registerTokenType('types', { scopesToProbe: [['entity.name.type'], ['entity.name.class'], ['support.type'], ['support.class']], dark: null, light: null, hc: null }, nls.localize('types', "Token style for types.")); -export const functions = registerTokenType('functions', { scopesToProbe: [['entity.name.function'], ['support.function']], dark: null, light: null, hc: null }, nls.localize('functions', "Token style for functions.")); -export const variables = registerTokenType('variables', { scopesToProbe: [['variable'], ['entity.name.variable']], dark: null, light: null, hc: null }, nls.localize('variables', "Token style for variables.")); +export const comments = registerTokenType('comments', nls.localize('comments', "Token style for comments."), [['comment']]); +export const strings = registerTokenType('strings', nls.localize('strings', "Token style for strings."), [['string']]); +export const keywords = registerTokenType('keywords', nls.localize('keywords', "Token style for keywords."), [['keyword.control']]); +export const numbers = registerTokenType('numbers', nls.localize('numbers', "Token style for numbers."), [['constant.numeric']]); +export const regexp = registerTokenType('regexp', nls.localize('regexp', "Token style for regular expressions."), [['constant.regexp']]); +export const operators = registerTokenType('operators', nls.localize('operator', "Token style for operators."), [['keyword.operator']]); +export const namespaces = registerTokenType('namespaces', nls.localize('namespace', "Token style for namespaces."), [['entity.name.namespace']]); +export const types = registerTokenType('types', nls.localize('types', "Token style for types."), [['entity.name.type'], ['entity.name.class'], ['support.type'], ['support.class']]); +export const structs = registerTokenType('structs', nls.localize('struct', "Token style for struct."), [['storage.type.struct']], types); +export const classes = registerTokenType('classes', nls.localize('class', "Token style for classes."), [['ntity.name.class']], types); +export const interfaces = registerTokenType('interfaces', nls.localize('interface', "Token style for interfaces."), undefined, types); +export const enums = registerTokenType('enums', nls.localize('enum', "Token style for enums."), undefined, types); +export const parameterTypes = registerTokenType('parameterTypes', nls.localize('parameterType', "Token style for parameterTypes."), undefined, types); + +export const functions = registerTokenType('functions', nls.localize('functions', "Token style for functions."), [['entity.name.function'], ['support.function']]); +export const macros = registerTokenType('macros', nls.localize('macro', "Token style for macros."), undefined, functions); + +export const variables = registerTokenType('variables', nls.localize('variables', "Token style for variables."), [['variable'], ['entity.name.variable']]); +export const constants = registerTokenType('constants', nls.localize('constants', "Token style for constants."), undefined, variables); +export const parameters = registerTokenType('parameters', nls.localize('parameters', "Token style for parameters."), undefined, variables); +export const property = registerTokenType('properties', nls.localize('properties', "Token style for properties."), undefined, variables); + +export const labels = registerTokenType('labels', nls.localize('labels', "Token style for labels."), undefined); + +export const m_declaration = registerTokenModifier('declaration', nls.localize('declaration', "Token modifier for declarations."), undefined); +export const m_documentation = registerTokenModifier('documentation', nls.localize('documentation', "Token modifier for documentation."), undefined); +export const m_member = registerTokenModifier('member', nls.localize('member', "Token modifier for member."), undefined); +export const m_static = registerTokenModifier('static', nls.localize('static', "Token modifier for statics."), undefined); +export const m_abstract = registerTokenModifier('abstract', nls.localize('abstract', "Token modifier for abstracts."), undefined); +export const m_deprecated = registerTokenModifier('deprecated', nls.localize('deprecated', "Token modifier for deprecated."), undefined); +export const m_modification = registerTokenModifier('modification', nls.localize('modification', "Token modifier for modification."), undefined); +export const m_async = registerTokenModifier('async', nls.localize('async', "Token modifier for async."), undefined); function bitCount(u: number) { // https://blogs.msdn.microsoft.com/jeuge/2005/06/08/bit-fiddling-3/ From 65bcaa88af7c244c3dd01572ac19c8d82ca7d99d Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Mon, 7 Oct 2019 23:05:20 +0200 Subject: [PATCH 11/15] more tests --- .../common/tokenClassificationRegistry.ts | 37 ++++- .../services/themes/common/colorThemeData.ts | 19 ++- .../themes/common/textMateScopeMatcher.ts | 4 +- .../tokenStyleResolving.test.ts | 153 ++++++++++-------- 4 files changed, 130 insertions(+), 83 deletions(-) diff --git a/src/vs/platform/theme/common/tokenClassificationRegistry.ts b/src/vs/platform/theme/common/tokenClassificationRegistry.ts index 9b46ae5cbe1..205f8ba33f2 100644 --- a/src/vs/platform/theme/common/tokenClassificationRegistry.ts +++ b/src/vs/platform/theme/common/tokenClassificationRegistry.ts @@ -108,6 +108,8 @@ export interface ITokenClassificationRegistry { getTokenClassificationFromString(str: TokenClassificationString): TokenClassification | undefined; getTokenClassification(type: string, modifiers: string[]): TokenClassification | undefined; + getTokenStylingRule(classification: TokenClassification | string | undefined, value: TokenStyle): TokenStylingRule | undefined; + /** * Register a TokenStyle default to the registry. * @param selector The rule selector @@ -197,9 +199,18 @@ class TokenClassificationRegistry implements ITokenClassificationRegistry { return undefined; } + public getTokenStylingRule(classification: TokenClassification | string | undefined, value: TokenStyle): TokenStylingRule | undefined { + if (typeof classification === 'string') { + classification = this.getTokenClassificationFromString(classification); + } + if (classification) { + return { classification, matchScore: getTokenStylingScore(classification), value }; + } + return undefined; + } + public registerTokenStyleDefault(classification: TokenClassification, defaults: TokenStyleDefaults): void { - const matchScore = bitCount(classification.modifiers) + ((classification.type !== TOKEN_TYPE_WILDCARD_NUM) ? 1 : 0); - this.tokenStylingDefaultRules.push({ classification, matchScore, defaults }); + this.tokenStylingDefaultRules.push({ classification, matchScore: getTokenStylingScore(classification), defaults }); } public deregisterTokenType(id: string): void { @@ -233,12 +244,20 @@ class TokenClassificationRegistry implements ITokenClassificationRegistry { }; function _processStyle(matchScore: number, style: TokenStyle) { - for (let p in result) { + if (style.foreground && score.foreground <= matchScore) { + score.foreground = matchScore; + result.foreground = style.foreground; + } + for (let p of ['bold', 'underline', 'italic']) { const property = p as keyof TokenStyle; const info = style[property]; - if (info !== undefined && score[property] <= matchScore) { - score[property] = matchScore; - result[property] = info; + if (info !== undefined) { + if (score[property] < matchScore) { + score[property] = matchScore; + result[property] = info; + } else if (score[property] === matchScore) { + result[property] = result[property] || info; + } } } } @@ -300,7 +319,7 @@ class TokenClassificationRegistry implements ITokenClassificationRegistry { function match(themeSelector: TokenStylingRule | TokenStylingDefaultRule, classification: TokenClassification): number { const selectorType = themeSelector.classification.type; - if (selectorType !== TOKEN_TYPE_WILDCARD_NUM && selectorType === classification.type) { + if (selectorType !== TOKEN_TYPE_WILDCARD_NUM && selectorType !== classification.type) { return -1; } const selectorModifier = themeSelector.classification.modifiers; @@ -373,3 +392,7 @@ function bitCount(u: number) { 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); +} diff --git a/src/vs/workbench/services/themes/common/colorThemeData.ts b/src/vs/workbench/services/themes/common/colorThemeData.ts index 53fa06ef149..7daee343887 100644 --- a/src/vs/workbench/services/themes/common/colorThemeData.ts +++ b/src/vs/workbench/services/themes/common/colorThemeData.ts @@ -20,12 +20,12 @@ import { URI } from 'vs/base/common/uri'; import { IFileService } from 'vs/platform/files/common/files'; import { parse as parsePList } from 'vs/workbench/services/themes/common/plistParser'; import { startsWith } from 'vs/base/common/strings'; -import { Extensions as TokenStyleRegistryExtensions, TokenStyle, TokenClassification, ITokenClassificationRegistry, ProbeScope, TokenStylingRule } from 'vs/platform/theme/common/tokenClassificationRegistry'; +import { TokenStyle, TokenClassification, ProbeScope, TokenStylingRule, getTokenClassificationRegistry } from 'vs/platform/theme/common/tokenClassificationRegistry'; import { MatcherWithPriority, Matcher, createMatchers } from 'vs/workbench/services/themes/common/textMateScopeMatcher'; let colorRegistry = Registry.as(ColorRegistryExtensions.ColorContribution); -let tokenClassificationRegistry = Registry.as(TokenStyleRegistryExtensions.TokenClassificationContribution); +let tokenClassificationRegistry = getTokenClassificationRegistry(); const tokenGroupToScopesMap = { comments: ['comment'], @@ -115,7 +115,7 @@ export class ColorThemeData implements IColorTheme { public getTokenStyle(tokenClassification: TokenClassification, useDefault?: boolean): TokenStyle | undefined { // todo: cache results - return tokenClassificationRegistry.resolveTokenStyle(tokenClassification, this.tokenStylingRules, !!useDefault, this); + return tokenClassificationRegistry.resolveTokenStyle(tokenClassification, this.tokenStylingRules, useDefault !== false, this); } public getDefault(colorId: ColorIdentifier): Color | undefined { @@ -200,6 +200,10 @@ export class ColorThemeData implements IColorTheme { } } + public setTokenStyleRules(tokenStylingRules: TokenStylingRule[]) { + this.tokenStylingRules = tokenStylingRules; + } + private addCustomTokenColors(customTokenColors: ITokenColorCustomizations) { // Put the general customizations such as comments, strings, etc. first so that // they can be overridden by specific customizations like "string.interpolated" @@ -489,9 +493,14 @@ function getScopeMatcher(rule: ITokenColorizationRule): Matcher { return noMatch; } const matchers: MatcherWithPriority[] = []; - for (let rs of ruleScope) { - matchers.push(...createMatchers(rs, nameMatcher)); + if (Array.isArray(ruleScope)) { + for (let rs of ruleScope) { + createMatchers(rs, nameMatcher, matchers); + } + } else { + createMatchers(ruleScope, nameMatcher, matchers); } + if (matchers.length === 0) { return noMatch; } diff --git a/src/vs/workbench/services/themes/common/textMateScopeMatcher.ts b/src/vs/workbench/services/themes/common/textMateScopeMatcher.ts index 701081bca4c..4d1c66adf6f 100644 --- a/src/vs/workbench/services/themes/common/textMateScopeMatcher.ts +++ b/src/vs/workbench/services/themes/common/textMateScopeMatcher.ts @@ -14,8 +14,7 @@ export interface Matcher { (matcherInput: T): number; } -export function createMatchers(selector: string, matchesName: (names: string[], matcherInput: T) => number): MatcherWithPriority[] { - const results = []>[]; +export function createMatchers(selector: string, matchesName: (names: string[], matcherInput: T) => number, results: MatcherWithPriority[]): void { const tokenizer = newTokenizer(selector); let token = tokenizer.next(); while (token !== null) { @@ -38,7 +37,6 @@ export function createMatchers(selector: string, matchesName: (names: string[ } token = tokenizer.next(); } - return results; function parseOperand(): Matcher | null { if (token === '-') { diff --git a/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts b/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts index 324a9664a3c..b8956caab0a 100644 --- a/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts +++ b/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts @@ -6,7 +6,7 @@ import { ColorThemeData } from 'vs/workbench/services/themes/common/colorThemeData'; import * as assert from 'assert'; import { ITokenColorCustomizations } from 'vs/workbench/services/themes/common/workbenchThemeService'; -import { Extensions as TokenStyleRegistryExtensions, ITokenClassificationRegistry, TokenStyle, comments, variables, types, functions, keywords, numbers, strings } from 'vs/platform/theme/common/tokenClassificationRegistry'; +import { TokenStyle, comments, variables, types, functions, keywords, numbers, strings, getTokenClassificationRegistry, TokenStylingRule } from 'vs/platform/theme/common/tokenClassificationRegistry'; import { Color } from 'vs/base/common/color'; import { isString } from 'vs/base/common/types'; import { FileService } from 'vs/platform/files/common/fileService'; @@ -15,26 +15,15 @@ import { DiskFileSystemProvider } from 'vs/platform/files/node/diskFileSystemPro import { Schemas } from 'vs/base/common/network'; import { URI } from 'vs/base/common/uri'; import { getPathFromAmdModule } from 'vs/base/common/amd'; -import { Registry } from 'vs/platform/registry/common/platform'; -let tokenClassificationRegistry = Registry.as(TokenStyleRegistryExtensions.TokenClassificationContribution); +let tokenClassificationRegistry = getTokenClassificationRegistry(); -const enum TokenStyleBits { - BOLD = 0x01, - UNDERLINE = 0x02, - ITALIC = 0x04 -} +const unsetStyle = { bold: false, underline: false, italic: false }; -function ts(foreground: string | undefined, styleFlags: number | undefined): TokenStyle { +function ts(foreground: string | undefined, styleFlags: { bold?: boolean; underline?: boolean; italic?: boolean } | undefined): TokenStyle { const foregroundColor = isString(foreground) ? Color.fromHex(foreground) : undefined; - let bold, underline, italic; - if (styleFlags !== undefined) { - bold = (styleFlags & TokenStyleBits.BOLD) !== 0; - underline = (styleFlags & TokenStyleBits.UNDERLINE) !== 0; - italic = (styleFlags & TokenStyleBits.ITALIC) !== 0; - } - return new TokenStyle(foregroundColor, bold, underline, italic); + return new TokenStyle(foregroundColor, styleFlags && styleFlags.bold, styleFlags && styleFlags.underline, styleFlags && styleFlags.italic); } function tokenStyleAsString(ts: TokenStyle | undefined | null) { @@ -43,17 +32,25 @@ function tokenStyleAsString(ts: TokenStyle | undefined | null) { } let str = ts.foreground ? ts.foreground.toString() : 'no-foreground'; if (ts.bold !== undefined) { - str = ts.bold ? '+B' : '-B'; + str += ts.bold ? '+B' : '-B'; } if (ts.underline !== undefined) { - str = ts.underline ? '+U' : '-U'; + str += ts.underline ? '+U' : '-U'; } if (ts.italic !== undefined) { - str = ts.italic ? '+I' : '-I'; + str += ts.italic ? '+I' : '-I'; } return str; } +function getTokenStyleRules(rules: [string, TokenStyle][]): TokenStylingRule[] { + return rules.map(e => { + const rule = tokenClassificationRegistry.getTokenStylingRule(e[0], e[1]); + assert.ok(rule); + return rule!; + }); +} + function assertTokenStyle(actual: TokenStyle | undefined | null, expected: TokenStyle | undefined | null, message?: string) { assert.equal(tokenStyleAsString(actual), tokenStyleAsString(expected), message); } @@ -69,6 +66,8 @@ function assertTokenStyles(themeData: ColorThemeData, expected: { [qualifiedClas } suite('Themes - TokenStyleResolving', () => { + + const fileService = new FileService(new NullLogService()); const diskFileSystemProvider = new DiskFileSystemProvider(new NullLogService()); fileService.registerProvider(Schemas.file, diskFileSystemProvider); @@ -83,13 +82,13 @@ suite('Themes - TokenStyleResolving', () => { assert.equal(themeData.isLoaded, true); assertTokenStyles(themeData, { - [comments]: ts('#75715E', 0), - [variables]: ts('#F8F8F2', 0), - [types]: ts('#A6E22E', TokenStyleBits.UNDERLINE), - [functions]: ts('#A6E22E', 0), - [strings]: ts('#E6DB74', 0), - [numbers]: ts('#AE81FF', 0), - [keywords]: ts('#F92672', 0) + [comments]: ts('#75715E', unsetStyle), + [variables]: ts('#F8F8F2', unsetStyle), + [types]: ts('#A6E22E', { underline: true, bold: false, italic: false }), + [functions]: ts('#A6E22E', unsetStyle), + [strings]: ts('#E6DB74', unsetStyle), + [numbers]: ts('#AE81FF', unsetStyle), + [keywords]: ts('#F92672', unsetStyle) }); }); @@ -103,13 +102,13 @@ suite('Themes - TokenStyleResolving', () => { assert.equal(themeData.isLoaded, true); assertTokenStyles(themeData, { - [comments]: ts('#6A9955', 0), - [variables]: ts('#9CDCFE', 0), - [types]: ts('#4EC9B0', 0), - [functions]: ts('#DCDCAA', 0), - [strings]: ts('#CE9178', 0), - [numbers]: ts('#B5CEA8', 0), - [keywords]: ts('#C586C0', 0) + [comments]: ts('#6A9955', unsetStyle), + [variables]: ts('#9CDCFE', unsetStyle), + [types]: ts('#4EC9B0', unsetStyle), + [functions]: ts('#DCDCAA', unsetStyle), + [strings]: ts('#CE9178', unsetStyle), + [numbers]: ts('#B5CEA8', unsetStyle), + [keywords]: ts('#C586C0', unsetStyle) }); }); @@ -123,13 +122,13 @@ suite('Themes - TokenStyleResolving', () => { assert.equal(themeData.isLoaded, true); assertTokenStyles(themeData, { - [comments]: ts('#008000', 0), - [variables]: ts('#000000', 0), - [types]: ts('#000000', 0), - [functions]: ts('#000000', 0), - [strings]: ts('#a31515', 0), - [numbers]: ts('#09885a', 0), - [keywords]: ts('#0000ff', 0) + [comments]: ts('#008000', unsetStyle), + [variables]: ts('#000000', unsetStyle), + [types]: ts('#000000', unsetStyle), + [functions]: ts('#000000', unsetStyle), + [strings]: ts('#a31515', unsetStyle), + [numbers]: ts('#09885a', unsetStyle), + [keywords]: ts('#0000ff', unsetStyle) }); }); @@ -143,13 +142,13 @@ suite('Themes - TokenStyleResolving', () => { assert.equal(themeData.isLoaded, true); assertTokenStyles(themeData, { - [comments]: ts('#7ca668', 0), - [variables]: ts('#9CDCFE', 0), - [types]: ts('#4EC9B0', 0), - [functions]: ts('#DCDCAA', 0), - [strings]: ts('#ce9178', 0), - [numbers]: ts('#b5cea8', 0), - [keywords]: ts('#C586C0', 0) + [comments]: ts('#7ca668', unsetStyle), + [variables]: ts('#9CDCFE', unsetStyle), + [types]: ts('#4EC9B0', unsetStyle), + [functions]: ts('#DCDCAA', unsetStyle), + [strings]: ts('#ce9178', unsetStyle), + [numbers]: ts('#b5cea8', unsetStyle), + [keywords]: ts('#C586C0', unsetStyle) }); }); @@ -163,13 +162,13 @@ suite('Themes - TokenStyleResolving', () => { assert.equal(themeData.isLoaded, true); assertTokenStyles(themeData, { - [comments]: ts('#a57a4c', 0), - [variables]: ts('#dc3958', 0), - [types]: ts('#f06431', 0), - [functions]: ts('#8ab1b0', 0), - [strings]: ts('#889b4a', 0), - [numbers]: ts('#f79a32', 0), - [keywords]: ts('#98676a', 0) + [comments]: ts('#a57a4c', unsetStyle), + [variables]: ts('#dc3958', unsetStyle), + [types]: ts('#f06431', unsetStyle), + [functions]: ts('#8ab1b0', unsetStyle), + [strings]: ts('#889b4a', unsetStyle), + [numbers]: ts('#f79a32', unsetStyle), + [keywords]: ts('#98676a', unsetStyle) }); }); @@ -183,13 +182,13 @@ suite('Themes - TokenStyleResolving', () => { assert.equal(themeData.isLoaded, true); assertTokenStyles(themeData, { - [comments]: ts('#384887', 0), - [variables]: ts('#6688cc', 0), - [types]: ts('#ffeebb', TokenStyleBits.UNDERLINE), - [functions]: ts('#ddbb88', 0), - [strings]: ts('#22aa44', 0), - [numbers]: ts('#f280d0', 0), - [keywords]: ts('#225588', 0) + [comments]: ts('#384887', unsetStyle), + [variables]: ts('#6688cc', unsetStyle), + [types]: ts('#ffeebb', { underline: true, bold: false, italic: false }), + [functions]: ts('#ddbb88', unsetStyle), + [strings]: ts('#22aa44', unsetStyle), + [numbers]: ts('#f280d0', unsetStyle), + [keywords]: ts('#225588', unsetStyle) }); }); @@ -242,34 +241,52 @@ suite('Themes - TokenStyleResolving', () => { let defaultTokenStyle = undefined; tokenStyle = themeData.resolveScopes([['variable']]); - assertTokenStyle(tokenStyle, ts('#F8F8F2', 0), 'variable'); + assertTokenStyle(tokenStyle, ts('#F8F8F2', unsetStyle), 'variable'); tokenStyle = themeData.resolveScopes([['keyword.operator']]); - assertTokenStyle(tokenStyle, ts('#F92672', TokenStyleBits.ITALIC | TokenStyleBits.BOLD | TokenStyleBits.UNDERLINE), 'keyword'); + assertTokenStyle(tokenStyle, ts('#F92672', { italic: true, bold: true, underline: true }), 'keyword'); tokenStyle = themeData.resolveScopes([['keyword']]); assertTokenStyle(tokenStyle, defaultTokenStyle, 'keyword'); tokenStyle = themeData.resolveScopes([['keyword.operator']]); - assertTokenStyle(tokenStyle, ts('#F92672', TokenStyleBits.ITALIC | TokenStyleBits.BOLD | TokenStyleBits.UNDERLINE), 'keyword.operator'); + assertTokenStyle(tokenStyle, ts('#F92672', { italic: true, bold: true, underline: true }), 'keyword.operator'); tokenStyle = themeData.resolveScopes([['keyword.operators']]); assertTokenStyle(tokenStyle, defaultTokenStyle, 'keyword.operators'); tokenStyle = themeData.resolveScopes([['storage']]); - assertTokenStyle(tokenStyle, ts('#F92672', TokenStyleBits.ITALIC), 'storage'); + assertTokenStyle(tokenStyle, ts('#F92672', { italic: true, underline: false, bold: false }), 'storage'); tokenStyle = themeData.resolveScopes([['storage.type']]); - assertTokenStyle(tokenStyle, ts('#66D9EF', TokenStyleBits.ITALIC), 'storage.type'); + assertTokenStyle(tokenStyle, ts('#66D9EF', { italic: true, underline: false, bold: false }), 'storage.type'); tokenStyle = themeData.resolveScopes([['entity.name.class']]); - assertTokenStyle(tokenStyle, ts('#A6E22E', TokenStyleBits.UNDERLINE), 'entity.name.class'); + assertTokenStyle(tokenStyle, ts('#A6E22E', { underline: true, italic: false, bold: false }), 'entity.name.class'); tokenStyle = themeData.resolveScopes([['meta.structure.dictionary.json', 'string.quoted.double.json']]); assertTokenStyle(tokenStyle, ts('#66D9EF', undefined), 'json property'); tokenStyle = themeData.resolveScopes([['keyword'], ['storage.type'], ['entity.name.class']]); - assertTokenStyle(tokenStyle, ts('#66D9EF', TokenStyleBits.ITALIC), 'storage.type'); + assertTokenStyle(tokenStyle, ts('#66D9EF', { italic: true, underline: false, bold: false }), 'storage.type'); + + }); + + test('rule matching', async () => { + const themeData = ColorThemeData.createLoadedEmptyTheme('test', 'test'); + themeData.setCustomColors({ 'editor.foreground': '#000000' }); + themeData.setTokenStyleRules(getTokenStyleRules([ + ['types', ts('#ff0000', undefined)], + ['classes', ts('#0000ff', undefined)], + ['*.static', ts(undefined, { bold: true })], + ['*.declaration', ts(undefined, { italic: true })] + ])); + + assertTokenStyles(themeData, { + 'types': ts('#ff0000', unsetStyle), + 'types.static': ts('#ff0000', { bold: true, italic: false, underline: false }), + 'types.static.declaration': ts('#ff0000', { bold: true, italic: true, underline: false }) + }); }); }); From 5b6f03e8c69e04c8e3cf823eba563f73f1f65107 Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Wed, 6 Nov 2019 11:56:05 +0100 Subject: [PATCH 12/15] adopt merge --- .../extensionResourceLoaderService.ts | 2 +- .../tokenStyleResolving.test.ts | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/vs/workbench/services/extensionResourceLoader/electron-browser/extensionResourceLoaderService.ts b/src/vs/workbench/services/extensionResourceLoader/electron-browser/extensionResourceLoaderService.ts index 2acf10e0b53..744f2860788 100644 --- a/src/vs/workbench/services/extensionResourceLoader/electron-browser/extensionResourceLoaderService.ts +++ b/src/vs/workbench/services/extensionResourceLoader/electron-browser/extensionResourceLoaderService.ts @@ -8,7 +8,7 @@ import { registerSingleton } from 'vs/platform/instantiation/common/extensions'; import { IFileService } from 'vs/platform/files/common/files'; import { IExtensionResourceLoaderService } from 'vs/workbench/services/extensionResourceLoader/common/extensionResourceLoader'; -class ExtensionResourceLoaderService implements IExtensionResourceLoaderService { +export class ExtensionResourceLoaderService implements IExtensionResourceLoaderService { _serviceBrand: undefined; diff --git a/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts b/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts index b8956caab0a..6c0d9af598c 100644 --- a/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts +++ b/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts @@ -15,6 +15,7 @@ import { DiskFileSystemProvider } from 'vs/platform/files/node/diskFileSystemPro import { Schemas } from 'vs/base/common/network'; import { URI } from 'vs/base/common/uri'; import { getPathFromAmdModule } from 'vs/base/common/amd'; +import { ExtensionResourceLoaderService } from 'vs/workbench/services/extensionResourceLoader/electron-browser/extensionResourceLoaderService'; let tokenClassificationRegistry = getTokenClassificationRegistry(); @@ -69,6 +70,8 @@ suite('Themes - TokenStyleResolving', () => { const fileService = new FileService(new NullLogService()); + const extensionResourceLoaderService = new ExtensionResourceLoaderService(fileService); + const diskFileSystemProvider = new DiskFileSystemProvider(new NullLogService()); fileService.registerProvider(Schemas.file, diskFileSystemProvider); @@ -77,12 +80,12 @@ suite('Themes - TokenStyleResolving', () => { const themeData = ColorThemeData.createUnloadedTheme('foo'); const themeLocation = getPathFromAmdModule(require, '../../../../../../../extensions/theme-monokai/themes/monokai-color-theme.json'); themeData.location = URI.file(themeLocation); - await themeData.ensureLoaded(fileService); + await themeData.ensureLoaded(extensionResourceLoaderService); assert.equal(themeData.isLoaded, true); assertTokenStyles(themeData, { - [comments]: ts('#75715E', unsetStyle), + [comments]: ts('#88846f', unsetStyle), [variables]: ts('#F8F8F2', unsetStyle), [types]: ts('#A6E22E', { underline: true, bold: false, italic: false }), [functions]: ts('#A6E22E', unsetStyle), @@ -97,7 +100,7 @@ suite('Themes - TokenStyleResolving', () => { const themeData = ColorThemeData.createUnloadedTheme('foo'); const themeLocation = getPathFromAmdModule(require, '../../../../../../../extensions/theme-defaults/themes/dark_plus.json'); themeData.location = URI.file(themeLocation); - await themeData.ensureLoaded(fileService); + await themeData.ensureLoaded(extensionResourceLoaderService); assert.equal(themeData.isLoaded, true); @@ -117,7 +120,7 @@ suite('Themes - TokenStyleResolving', () => { const themeData = ColorThemeData.createUnloadedTheme('foo'); const themeLocation = getPathFromAmdModule(require, '../../../../../../../extensions/theme-defaults/themes/light_vs.json'); themeData.location = URI.file(themeLocation); - await themeData.ensureLoaded(fileService); + await themeData.ensureLoaded(extensionResourceLoaderService); assert.equal(themeData.isLoaded, true); @@ -137,7 +140,7 @@ suite('Themes - TokenStyleResolving', () => { const themeData = ColorThemeData.createUnloadedTheme('foo'); const themeLocation = getPathFromAmdModule(require, '../../../../../../../extensions/theme-defaults/themes/hc_black.json'); themeData.location = URI.file(themeLocation); - await themeData.ensureLoaded(fileService); + await themeData.ensureLoaded(extensionResourceLoaderService); assert.equal(themeData.isLoaded, true); @@ -157,7 +160,7 @@ suite('Themes - TokenStyleResolving', () => { const themeData = ColorThemeData.createUnloadedTheme('foo'); const themeLocation = getPathFromAmdModule(require, '../../../../../../../extensions/theme-kimbie-dark/themes/kimbie-dark-color-theme.json'); themeData.location = URI.file(themeLocation); - await themeData.ensureLoaded(fileService); + await themeData.ensureLoaded(extensionResourceLoaderService); assert.equal(themeData.isLoaded, true); @@ -177,7 +180,7 @@ suite('Themes - TokenStyleResolving', () => { const themeData = ColorThemeData.createUnloadedTheme('foo'); const themeLocation = getPathFromAmdModule(require, '../../../../../../../extensions/theme-abyss/themes/abyss-color-theme.json'); themeData.location = URI.file(themeLocation); - await themeData.ensureLoaded(fileService); + await themeData.ensureLoaded(extensionResourceLoaderService); assert.equal(themeData.isLoaded, true); From a9de2f33c3c8b44c6b4dc74e18c5dcff81e16653 Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Thu, 7 Nov 2019 17:10:45 +0100 Subject: [PATCH 13/15] keep unset attributes as undefined --- .../common/tokenClassificationRegistry.ts | 30 +++--- .../tokenStyleResolving.test.ts | 93 ++++++++++--------- 2 files changed, 63 insertions(+), 60 deletions(-) diff --git a/src/vs/platform/theme/common/tokenClassificationRegistry.ts b/src/vs/platform/theme/common/tokenClassificationRegistry.ts index 205f8ba33f2..7817b7a5778 100644 --- a/src/vs/platform/theme/common/tokenClassificationRegistry.ts +++ b/src/vs/platform/theme/common/tokenClassificationRegistry.ts @@ -8,8 +8,6 @@ import { Color } from 'vs/base/common/color'; import { ITheme } from 'vs/platform/theme/common/themeService'; import * as nls from 'vs/nls'; -import { editorForeground } from 'vs/platform/theme/common/colorRegistry'; - // ------ API types export const TOKEN_TYPE_WILDCARD = '*'; @@ -231,10 +229,10 @@ class TokenClassificationRegistry implements ITokenClassificationRegistry { public resolveTokenStyle(classification: TokenClassification, themingRules: TokenStylingRule[], useDefault: boolean, theme: ITheme): TokenStyle | undefined { let result: any = { - foreground: theme.getColor(editorForeground), - bold: false, - underline: false, - italic: false + foreground: undefined, + bold: undefined, + underline: undefined, + italic: undefined }; let score = { foreground: -1, @@ -252,23 +250,15 @@ class TokenClassificationRegistry implements ITokenClassificationRegistry { const property = p as keyof TokenStyle; const info = style[property]; if (info !== undefined) { - if (score[property] < matchScore) { + if (score[property] <= matchScore) { score[property] = matchScore; result[property] = info; - } else if (score[property] === matchScore) { - result[property] = result[property] || info; } } } } - themingRules.forEach(rule => { - const matchScore = match(rule, classification); - if (matchScore >= 0) { - _processStyle(matchScore, rule.value); - } - }); if (useDefault) { - this.tokenStylingDefaultRules.forEach(rule => { + for (const rule of this.tokenStylingDefaultRules) { const matchScore = match(rule, classification); if (matchScore >= 0) { let style = theme.resolveScopes(rule.defaults.scopesToProbe); @@ -279,7 +269,13 @@ class TokenClassificationRegistry implements ITokenClassificationRegistry { _processStyle(matchScore, style); } } - }); + } + } + for (const rule of themingRules) { + const matchScore = match(rule, classification); + if (matchScore >= 0) { + _processStyle(matchScore, rule.value); + } } return TokenStyle.fromData(result); } diff --git a/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts b/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts index 6c0d9af598c..a0fe30b8683 100644 --- a/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts +++ b/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts @@ -19,9 +19,9 @@ import { ExtensionResourceLoaderService } from 'vs/workbench/services/extensionR let tokenClassificationRegistry = getTokenClassificationRegistry(); +const undefinedStyle = { bold: undefined, underline: undefined, italic: undefined }; const unsetStyle = { bold: false, underline: false, italic: false }; - function ts(foreground: string | undefined, styleFlags: { bold?: boolean; underline?: boolean; italic?: boolean } | undefined): TokenStyle { const foregroundColor = isString(foreground) ? Color.fromHex(foreground) : undefined; return new TokenStyle(foregroundColor, styleFlags && styleFlags.bold, styleFlags && styleFlags.underline, styleFlags && styleFlags.italic); @@ -85,13 +85,13 @@ suite('Themes - TokenStyleResolving', () => { assert.equal(themeData.isLoaded, true); assertTokenStyles(themeData, { - [comments]: ts('#88846f', unsetStyle), + [comments]: ts('#88846f', undefinedStyle), [variables]: ts('#F8F8F2', unsetStyle), [types]: ts('#A6E22E', { underline: true, bold: false, italic: false }), [functions]: ts('#A6E22E', unsetStyle), - [strings]: ts('#E6DB74', unsetStyle), - [numbers]: ts('#AE81FF', unsetStyle), - [keywords]: ts('#F92672', unsetStyle) + [strings]: ts('#E6DB74', undefinedStyle), + [numbers]: ts('#AE81FF', undefinedStyle), + [keywords]: ts('#F92672', undefinedStyle) }); }); @@ -105,13 +105,13 @@ suite('Themes - TokenStyleResolving', () => { assert.equal(themeData.isLoaded, true); assertTokenStyles(themeData, { - [comments]: ts('#6A9955', unsetStyle), - [variables]: ts('#9CDCFE', unsetStyle), - [types]: ts('#4EC9B0', unsetStyle), - [functions]: ts('#DCDCAA', unsetStyle), - [strings]: ts('#CE9178', unsetStyle), - [numbers]: ts('#B5CEA8', unsetStyle), - [keywords]: ts('#C586C0', unsetStyle) + [comments]: ts('#6A9955', undefinedStyle), + [variables]: ts('#9CDCFE', undefinedStyle), + [types]: ts('#4EC9B0', undefinedStyle), + [functions]: ts('#DCDCAA', undefinedStyle), + [strings]: ts('#CE9178', undefinedStyle), + [numbers]: ts('#B5CEA8', undefinedStyle), + [keywords]: ts('#C586C0', undefinedStyle) }); }); @@ -125,13 +125,13 @@ suite('Themes - TokenStyleResolving', () => { assert.equal(themeData.isLoaded, true); assertTokenStyles(themeData, { - [comments]: ts('#008000', unsetStyle), - [variables]: ts('#000000', unsetStyle), - [types]: ts('#000000', unsetStyle), - [functions]: ts('#000000', unsetStyle), - [strings]: ts('#a31515', unsetStyle), - [numbers]: ts('#09885a', unsetStyle), - [keywords]: ts('#0000ff', unsetStyle) + [comments]: ts('#008000', undefinedStyle), + [variables]: ts(undefined, undefinedStyle), + [types]: ts(undefined, undefinedStyle), + [functions]: ts(undefined, undefinedStyle), + [strings]: ts('#a31515', undefinedStyle), + [numbers]: ts('#09885a', undefinedStyle), + [keywords]: ts('#0000ff', undefinedStyle) }); }); @@ -145,13 +145,13 @@ suite('Themes - TokenStyleResolving', () => { assert.equal(themeData.isLoaded, true); assertTokenStyles(themeData, { - [comments]: ts('#7ca668', unsetStyle), - [variables]: ts('#9CDCFE', unsetStyle), - [types]: ts('#4EC9B0', unsetStyle), - [functions]: ts('#DCDCAA', unsetStyle), - [strings]: ts('#ce9178', unsetStyle), - [numbers]: ts('#b5cea8', unsetStyle), - [keywords]: ts('#C586C0', unsetStyle) + [comments]: ts('#7ca668', undefinedStyle), + [variables]: ts('#9CDCFE', undefinedStyle), + [types]: ts('#4EC9B0', undefinedStyle), + [functions]: ts('#DCDCAA', undefinedStyle), + [strings]: ts('#ce9178', undefinedStyle), + [numbers]: ts('#b5cea8', undefinedStyle), + [keywords]: ts('#C586C0', undefinedStyle) }); }); @@ -165,13 +165,13 @@ suite('Themes - TokenStyleResolving', () => { assert.equal(themeData.isLoaded, true); assertTokenStyles(themeData, { - [comments]: ts('#a57a4c', unsetStyle), - [variables]: ts('#dc3958', unsetStyle), - [types]: ts('#f06431', unsetStyle), - [functions]: ts('#8ab1b0', unsetStyle), - [strings]: ts('#889b4a', unsetStyle), - [numbers]: ts('#f79a32', unsetStyle), - [keywords]: ts('#98676a', unsetStyle) + [comments]: ts('#a57a4c', undefinedStyle), + [variables]: ts('#dc3958', undefinedStyle), + [types]: ts('#f06431', undefinedStyle), + [functions]: ts('#8ab1b0', undefinedStyle), + [strings]: ts('#889b4a', undefinedStyle), + [numbers]: ts('#f79a32', undefinedStyle), + [keywords]: ts('#98676a', undefinedStyle) }); }); @@ -185,13 +185,13 @@ suite('Themes - TokenStyleResolving', () => { assert.equal(themeData.isLoaded, true); assertTokenStyles(themeData, { - [comments]: ts('#384887', unsetStyle), - [variables]: ts('#6688cc', unsetStyle), + [comments]: ts('#384887', undefinedStyle), + [variables]: ts(undefined, unsetStyle), [types]: ts('#ffeebb', { underline: true, bold: false, italic: false }), [functions]: ts('#ddbb88', unsetStyle), - [strings]: ts('#22aa44', unsetStyle), - [numbers]: ts('#f280d0', unsetStyle), - [keywords]: ts('#225588', unsetStyle) + [strings]: ts('#22aa44', undefinedStyle), + [numbers]: ts('#f280d0', undefinedStyle), + [keywords]: ts('#225588', undefinedStyle) }); }); @@ -280,15 +280,22 @@ suite('Themes - TokenStyleResolving', () => { themeData.setCustomColors({ 'editor.foreground': '#000000' }); themeData.setTokenStyleRules(getTokenStyleRules([ ['types', ts('#ff0000', undefined)], - ['classes', ts('#0000ff', undefined)], + ['classes', ts('#0000ff', { italic: true })], ['*.static', ts(undefined, { bold: true })], - ['*.declaration', ts(undefined, { italic: true })] + ['*.declaration', ts(undefined, { italic: true })], + ['*.async.static', ts('#00ffff', { bold: false, underline: true })], + ['*.async', ts('#000fff', { italic: false, underline: true })] ])); assertTokenStyles(themeData, { - 'types': ts('#ff0000', unsetStyle), - 'types.static': ts('#ff0000', { bold: true, italic: false, underline: false }), - 'types.static.declaration': ts('#ff0000', { bold: true, italic: true, underline: false }) + 'types': ts('#ff0000', undefinedStyle), + 'types.static': ts('#ff0000', { bold: true, italic: undefined, underline: undefined }), + 'types.static.declaration': ts('#ff0000', { bold: true, italic: true, underline: undefined }), + 'classes': ts('#0000ff', { bold: undefined, italic: true, underline: undefined }), + 'classes.static.declaration': ts('#0000ff', { bold: true, italic: true, underline: undefined }), + 'classes.declaration': ts('#0000ff', { bold: undefined, italic: true, underline: undefined }), + 'classes.declaration.async': ts('#000fff', { bold: undefined, italic: false, underline: true }), + 'classes.declaration.async.static': ts('#00ffff', { bold: false, italic: false, underline: true }), }); }); From a5fe35b780b9ed7356f54c5fe14e447a0cf1833c Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Fri, 8 Nov 2019 11:27:51 +0100 Subject: [PATCH 14/15] No longer preselected color in colorCustomizations. Fixes #84152 --- src/vs/platform/theme/common/colorRegistry.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vs/platform/theme/common/colorRegistry.ts b/src/vs/platform/theme/common/colorRegistry.ts index 89c695d2dee..4c3093b823d 100644 --- a/src/vs/platform/theme/common/colorRegistry.ts +++ b/src/vs/platform/theme/common/colorRegistry.ts @@ -103,7 +103,7 @@ class ColorRegistry implements IColorRegistry { public registerColor(id: string, defaults: ColorDefaults | null, description: string, needsTransparency = false, deprecationMessage?: string): ColorIdentifier { let colorContribution: ColorContribution = { id, description, defaults, needsTransparency, deprecationMessage }; this.colorsById[id] = colorContribution; - let propertySchema: IJSONSchema = { type: 'string', description, format: 'color-hex', defaultSnippets: [{ body: '#ff0000' }] }; + let propertySchema: IJSONSchema = { type: 'string', description, format: 'color-hex', defaultSnippets: [{ body: '${1:#ff0000}' }] }; if (deprecationMessage) { propertySchema.deprecationMessage = deprecationMessage; } From 100318a63e2af38b5a442362d25742d732fc0d94 Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Mon, 11 Nov 2019 16:04:09 +0100 Subject: [PATCH 15/15] styling in settings and themes --- .../common/tokenClassificationRegistry.ts | 170 +++++++++++++----- .../browser/abstractTextMateService.ts | 4 +- .../themes/browser/workbenchThemeService.ts | 24 ++- .../services/themes/common/colorThemeData.ts | 128 +++++++++---- .../themes/common/themeCompatibility.ts | 8 +- .../themes/common/workbenchThemeService.ts | 17 +- .../tokenStyleResolving.test.ts | 52 +++--- 7 files changed, 281 insertions(+), 122 deletions(-) diff --git a/src/vs/platform/theme/common/tokenClassificationRegistry.ts b/src/vs/platform/theme/common/tokenClassificationRegistry.ts index 7817b7a5778..2ec489ae46f 100644 --- a/src/vs/platform/theme/common/tokenClassificationRegistry.ts +++ b/src/vs/platform/theme/common/tokenClassificationRegistry.ts @@ -7,6 +7,10 @@ import * as platform from 'vs/platform/registry/common/platform'; import { Color } from 'vs/base/common/color'; import { ITheme } from 'vs/platform/theme/common/themeService'; import * as nls from 'vs/nls'; +import { Extensions as JSONExtensions, IJSONContributionRegistry } from 'vs/platform/jsonschemas/common/jsonContributionRegistry'; +import { RunOnceScheduler } from 'vs/base/common/async'; +import { Event, Emitter } from 'vs/base/common/event'; +import { IJSONSchema, IJSONSchemaMap } from 'vs/base/common/jsonSchema'; // ------ API types @@ -89,6 +93,8 @@ export const Extensions = { export interface ITokenClassificationRegistry { + readonly onDidChangeSchema: Event; + /** * Register a token type to the registry. * @param id The TokenType id as used in theme description files @@ -106,7 +112,7 @@ export interface ITokenClassificationRegistry { getTokenClassificationFromString(str: TokenClassificationString): TokenClassification | undefined; getTokenClassification(type: string, modifiers: string[]): TokenClassification | undefined; - getTokenStylingRule(classification: TokenClassification | string | undefined, value: TokenStyle): TokenStylingRule | undefined; + getTokenStylingRule(classification: TokenClassification, value: TokenStyle): TokenStylingRule; /** * Register a TokenStyle default to the registry. @@ -138,13 +144,19 @@ export interface ITokenClassificationRegistry { /** * Resolves a token classification against the given rules and default rules from the registry. */ - resolveTokenStyle(classification: TokenClassification, themingRules: TokenStylingRule[], useDefault: boolean, theme: ITheme): TokenStyle | undefined; + resolveTokenStyle(classification: TokenClassification, themingRules: TokenStylingRule[] | undefined, customThemingRules: TokenStylingRule[], theme: ITheme): TokenStyle | undefined; + + /** + * JSON schema for an object to assign styling to token classifications + */ + getTokenStylingSchema(): IJSONSchema; } - - class TokenClassificationRegistry implements ITokenClassificationRegistry { + private readonly _onDidChangeSchema = new Emitter(); + readonly onDidChangeSchema: Event = this._onDidChangeSchema.event; + private currentTypeNumber = 0; private currentModifierBit = 1; @@ -153,6 +165,38 @@ class TokenClassificationRegistry implements ITokenClassificationRegistry { private tokenStylingDefaultRules: TokenStylingDefaultRule[] = []; + private tokenStylingSchema: IJSONSchema & { properties: IJSONSchemaMap } = { + type: 'object', + properties: {}, + definitions: { + style: { + type: 'object', + description: nls.localize('schema.token.settings', 'Colors and styles for the token.'), + properties: { + foreground: { + type: 'string', + description: nls.localize('schema.token.foreground', 'Foreground color for the token.'), + format: 'color-hex', + default: '#ff0000' + }, + background: { + type: 'string', + deprecationMessage: nls.localize('schema.token.background.warning', 'Token background colors are currently not supported.') + }, + fontStyle: { + type: 'string', + description: nls.localize('schema.token.fontStyle', 'Font style of the rule: \'italic\', \'bold\' or \'underline\', \'-italic\', \'-bold\' or \'-underline\'or a combination. The empty string unsets inherited settings.'), + pattern: '^(\\s*(-?italic|-?bold|-?underline))*\\s*$', + patternErrorMessage: nls.localize('schema.fontStyle.error', 'Font style must be \'italic\', \'bold\' or \'underline\' to set a style or \'-italic\', \'-bold\' or \'-underline\' to unset or a combination. The empty string unsets all styles.'), + defaultSnippets: [{ label: nls.localize('schema.token.fontStyle.none', 'None (clear inherited style)'), bodyText: '""' }, { body: 'italic' }, { body: 'bold' }, { body: 'underline' }, { body: '-italic' }, { body: '-bold' }, { body: '-underline' }, { body: 'italic bold' }, { body: 'italic underline' }, { body: 'bold underline' }, { body: 'italic bold underline' }] + } + }, + additionalProperties: false, + defaultSnippets: [{ body: { foreground: '${1:#FF0000}', fontStyle: '${2:bold}' } }] + } + } + }; + constructor() { this.tokenTypeById = {}; this.tokenModifierById = {}; @@ -164,6 +208,8 @@ class TokenClassificationRegistry implements ITokenClassificationRegistry { const num = this.currentTypeNumber++; let tokenStyleContribution: TokenTypeOrModifierContribution = { num, id, description, deprecationMessage }; this.tokenTypeById[id] = tokenStyleContribution; + + this.tokenStylingSchema.properties[id] = getStylingSchemeEntry(description, deprecationMessage); } public registerTokenModifier(id: string, description: string, deprecationMessage?: string): void { @@ -171,6 +217,8 @@ class TokenClassificationRegistry implements ITokenClassificationRegistry { this.currentModifierBit = this.currentModifierBit * 2; let tokenStyleContribution: TokenTypeOrModifierContribution = { num, id, description, deprecationMessage }; this.tokenModifierById[id] = tokenStyleContribution; + + this.tokenStylingSchema.properties[`*.${id}`] = getStylingSchemeEntry(description, deprecationMessage); } public getTokenClassification(type: string, modifiers: string[]): TokenClassification | undefined { @@ -197,14 +245,8 @@ class TokenClassificationRegistry implements ITokenClassificationRegistry { return undefined; } - public getTokenStylingRule(classification: TokenClassification | string | undefined, value: TokenStyle): TokenStylingRule | undefined { - if (typeof classification === 'string') { - classification = this.getTokenClassificationFromString(classification); - } - if (classification) { - return { classification, matchScore: getTokenStylingScore(classification), value }; - } - return undefined; + public getTokenStylingRule(classification: TokenClassification, value: TokenStyle): TokenStylingRule { + return { classification, matchScore: getTokenStylingScore(classification), value }; } public registerTokenStyleDefault(classification: TokenClassification, defaults: TokenStyleDefaults): void { @@ -213,10 +255,12 @@ class TokenClassificationRegistry implements ITokenClassificationRegistry { public deregisterTokenType(id: string): void { delete this.tokenTypeById[id]; + delete this.tokenStylingSchema.properties[id]; } public deregisterTokenModifier(id: string): void { delete this.tokenModifierById[id]; + delete this.tokenStylingSchema.properties[`*.${id}`]; } public getTokenTypes(): TokenTypeOrModifierContribution[] { @@ -227,7 +271,7 @@ class TokenClassificationRegistry implements ITokenClassificationRegistry { return Object.keys(this.tokenModifierById).map(id => this.tokenModifierById[id]); } - public resolveTokenStyle(classification: TokenClassification, themingRules: TokenStylingRule[], useDefault: boolean, theme: ITheme): TokenStyle | undefined { + public resolveTokenStyle(classification: TokenClassification, themingRules: TokenStylingRule[] | undefined, customThemingRules: TokenStylingRule[], theme: ITheme): TokenStyle | undefined { let result: any = { foreground: undefined, bold: undefined, @@ -257,7 +301,7 @@ class TokenClassificationRegistry implements ITokenClassificationRegistry { } } } - if (useDefault) { + if (themingRules === undefined) { for (const rule of this.tokenStylingDefaultRules) { const matchScore = match(rule, classification); if (matchScore >= 0) { @@ -270,8 +314,15 @@ class TokenClassificationRegistry implements ITokenClassificationRegistry { } } } + } else { + for (const rule of themingRules) { + const matchScore = match(rule, classification); + if (matchScore >= 0) { + _processStyle(matchScore, rule.value); + } + } } - for (const rule of themingRules) { + for (const rule of customThemingRules) { const matchScore = match(rule, classification); if (matchScore >= 0) { _processStyle(matchScore, rule.value); @@ -297,6 +348,10 @@ class TokenClassificationRegistry implements ITokenClassificationRegistry { return undefined; } + public getTokenStylingSchema(): IJSONSchema { + return this.tokenStylingSchema; + } + public toString() { let sorter = (a: string, b: string) => { @@ -348,40 +403,40 @@ export function getTokenClassificationRegistry(): ITokenClassificationRegistry { return tokenClassificationRegistry; } -export const comments = registerTokenType('comments', nls.localize('comments', "Token style for comments."), [['comment']]); -export const strings = registerTokenType('strings', nls.localize('strings', "Token style for strings."), [['string']]); -export const keywords = registerTokenType('keywords', nls.localize('keywords', "Token style for keywords."), [['keyword.control']]); -export const numbers = registerTokenType('numbers', nls.localize('numbers', "Token style for numbers."), [['constant.numeric']]); -export const regexp = registerTokenType('regexp', nls.localize('regexp', "Token style for regular expressions."), [['constant.regexp']]); -export const operators = registerTokenType('operators', nls.localize('operator', "Token style for operators."), [['keyword.operator']]); +export const comments = registerTokenType('comments', nls.localize('comments', "Style for comments."), [['comment']]); +export const strings = registerTokenType('strings', nls.localize('strings', "Style for strings."), [['string']]); +export const keywords = registerTokenType('keywords', nls.localize('keywords', "Style for keywords."), [['keyword.control']]); +export const numbers = registerTokenType('numbers', nls.localize('numbers', "Style for numbers."), [['constant.numeric']]); +export const regexp = registerTokenType('regexp', nls.localize('regexp', "Style for expressions."), [['constant.regexp']]); +export const operators = registerTokenType('operators', nls.localize('operator', "Style for operators."), [['keyword.operator']]); -export const namespaces = registerTokenType('namespaces', nls.localize('namespace', "Token style for namespaces."), [['entity.name.namespace']]); +export const namespaces = registerTokenType('namespaces', nls.localize('namespace', "Style for namespaces."), [['entity.name.namespace']]); -export const types = registerTokenType('types', nls.localize('types', "Token style for types."), [['entity.name.type'], ['entity.name.class'], ['support.type'], ['support.class']]); -export const structs = registerTokenType('structs', nls.localize('struct', "Token style for struct."), [['storage.type.struct']], types); -export const classes = registerTokenType('classes', nls.localize('class', "Token style for classes."), [['ntity.name.class']], types); -export const interfaces = registerTokenType('interfaces', nls.localize('interface', "Token style for interfaces."), undefined, types); -export const enums = registerTokenType('enums', nls.localize('enum', "Token style for enums."), undefined, types); -export const parameterTypes = registerTokenType('parameterTypes', nls.localize('parameterType', "Token style for parameterTypes."), undefined, types); +export const types = registerTokenType('types', nls.localize('types', "Style for types."), [['entity.name.type'], ['entity.name.class'], ['support.type'], ['support.class']]); +export const structs = registerTokenType('structs', nls.localize('struct', "Style for structs."), [['storage.type.struct']], types); +export const classes = registerTokenType('classes', nls.localize('class', "Style for classes."), [['entity.name.class']], types); +export const interfaces = registerTokenType('interfaces', nls.localize('interface', "Style for interfaces."), undefined, types); +export const enums = registerTokenType('enums', nls.localize('enum', "Style for enums."), undefined, types); +export const parameterTypes = registerTokenType('parameterTypes', nls.localize('parameterType', "Style for parameter types."), undefined, types); -export const functions = registerTokenType('functions', nls.localize('functions', "Token style for functions."), [['entity.name.function'], ['support.function']]); -export const macros = registerTokenType('macros', nls.localize('macro', "Token style for macros."), undefined, functions); +export const functions = registerTokenType('functions', nls.localize('functions', "Style for functions"), [['entity.name.function'], ['support.function']]); +export const macros = registerTokenType('macros', nls.localize('macro', "Style for macros."), undefined, functions); -export const variables = registerTokenType('variables', nls.localize('variables', "Token style for variables."), [['variable'], ['entity.name.variable']]); -export const constants = registerTokenType('constants', nls.localize('constants', "Token style for constants."), undefined, variables); -export const parameters = registerTokenType('parameters', nls.localize('parameters', "Token style for parameters."), undefined, variables); -export const property = registerTokenType('properties', nls.localize('properties', "Token style for properties."), undefined, variables); +export const variables = registerTokenType('variables', nls.localize('variables', "Style for variables."), [['variable'], ['entity.name.variable']]); +export const constants = registerTokenType('constants', nls.localize('constants', "Style for constants."), undefined, variables); +export const parameters = registerTokenType('parameters', nls.localize('parameters', "Style for parameters."), undefined, variables); +export const property = registerTokenType('properties', nls.localize('properties', "Style for properties."), undefined, variables); -export const labels = registerTokenType('labels', nls.localize('labels', "Token style for labels."), undefined); +export const labels = registerTokenType('labels', nls.localize('labels', "Style for labels. "), undefined); -export const m_declaration = registerTokenModifier('declaration', nls.localize('declaration', "Token modifier for declarations."), undefined); -export const m_documentation = registerTokenModifier('documentation', nls.localize('documentation', "Token modifier for documentation."), undefined); -export const m_member = registerTokenModifier('member', nls.localize('member', "Token modifier for member."), undefined); -export const m_static = registerTokenModifier('static', nls.localize('static', "Token modifier for statics."), undefined); -export const m_abstract = registerTokenModifier('abstract', nls.localize('abstract', "Token modifier for abstracts."), undefined); -export const m_deprecated = registerTokenModifier('deprecated', nls.localize('deprecated', "Token modifier for deprecated."), undefined); -export const m_modification = registerTokenModifier('modification', nls.localize('modification', "Token modifier for modification."), undefined); -export const m_async = registerTokenModifier('async', nls.localize('async', "Token modifier for async."), undefined); +export const m_declaration = registerTokenModifier('declaration', nls.localize('declaration', "Style for all symbol declarations."), undefined); +export const m_documentation = registerTokenModifier('documentation', nls.localize('documentation', "Style to use for references in documentation."), undefined); +export const m_member = registerTokenModifier('member', nls.localize('member', "Style to use for member functions, variables (fields) and types."), undefined); +export const m_static = registerTokenModifier('static', nls.localize('static', "Style to use for symbols that are static."), undefined); +export const m_abstract = registerTokenModifier('abstract', nls.localize('abstract', "Style to use for symbols that are abstract."), undefined); +export const m_deprecated = registerTokenModifier('deprecated', nls.localize('deprecated', "Style to use for symbols that are deprecated."), undefined); +export const m_modification = registerTokenModifier('modification', nls.localize('modification', "Style to use for write accesses."), undefined); +export const m_async = registerTokenModifier('async', nls.localize('async', "Style to use for symbols that are async."), undefined); function bitCount(u: number) { // https://blogs.msdn.microsoft.com/jeuge/2005/06/08/bit-fiddling-3/ @@ -392,3 +447,32 @@ function bitCount(u: number) { function getTokenStylingScore(classification: TokenClassification) { return bitCount(classification.modifiers) + ((classification.type !== TOKEN_TYPE_WILDCARD_NUM) ? 1 : 0); } + +function getStylingSchemeEntry(description: string, deprecationMessage?: string): IJSONSchema { + return { + description, + deprecationMessage, + defaultSnippets: [{ body: '${1:#ff0000}' }], + anyOf: [ + { + type: 'string', + format: 'color-hex' + }, + { + $ref: '#definitions/style' + } + ] + }; +} + +export const tokenStylingSchemaId = 'vscode://schemas/token-styling'; + +let schemaRegistry = platform.Registry.as(JSONExtensions.JSONContribution); +schemaRegistry.registerSchema(tokenStylingSchemaId, tokenClassificationRegistry.getTokenStylingSchema()); + +const delayer = new RunOnceScheduler(() => schemaRegistry.notifySchemaChanged(tokenStylingSchemaId), 200); +tokenClassificationRegistry.onDidChangeSchema(() => { + if (!delayer.isScheduled()) { + delayer.schedule(); + } +}); diff --git a/src/vs/workbench/services/textMate/browser/abstractTextMateService.ts b/src/vs/workbench/services/textMate/browser/abstractTextMateService.ts index cb553b6fcc8..dc9222e4b02 100644 --- a/src/vs/workbench/services/textMate/browser/abstractTextMateService.ts +++ b/src/vs/workbench/services/textMate/browser/abstractTextMateService.ts @@ -22,7 +22,7 @@ import { IStorageService, StorageScope } from 'vs/platform/storage/common/storag import { ExtensionMessageCollector } from 'vs/workbench/services/extensions/common/extensionsRegistry'; import { ITMSyntaxExtensionPoint, grammarsExtPoint } from 'vs/workbench/services/textMate/common/TMGrammars'; import { ITextMateService } from 'vs/workbench/services/textMate/common/textMateService'; -import { ITokenColorizationRule, IWorkbenchThemeService, IColorTheme } from 'vs/workbench/services/themes/common/workbenchThemeService'; +import { ITextMateThemingRule, IWorkbenchThemeService, IColorTheme } from 'vs/workbench/services/themes/common/workbenchThemeService'; import { IGrammar, StackElement, IOnigLib, IRawTheme } from 'vscode-textmate'; import { Disposable, IDisposable, dispose } from 'vs/base/common/lifecycle'; import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; @@ -257,7 +257,7 @@ export abstract class AbstractTextMateService extends Disposable implements ITex TokenizationRegistry.setColorMap(colorMap); } - private static equalsTokenRules(a: ITokenColorizationRule[] | null, b: ITokenColorizationRule[] | null): boolean { + private static equalsTokenRules(a: ITextMateThemingRule[] | null, b: ITextMateThemingRule[] | null): boolean { if (!b || !a || b.length !== a.length) { return false; } diff --git a/src/vs/workbench/services/themes/browser/workbenchThemeService.ts b/src/vs/workbench/services/themes/browser/workbenchThemeService.ts index 85b09406b2c..91e4c86fe53 100644 --- a/src/vs/workbench/services/themes/browser/workbenchThemeService.ts +++ b/src/vs/workbench/services/themes/browser/workbenchThemeService.ts @@ -6,7 +6,7 @@ import * as nls from 'vs/nls'; import * as types from 'vs/base/common/types'; import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions'; -import { IWorkbenchThemeService, IColorTheme, ITokenColorCustomizations, IFileIconTheme, ExtensionData, VS_LIGHT_THEME, VS_DARK_THEME, VS_HC_THEME, COLOR_THEME_SETTING, ICON_THEME_SETTING, CUSTOM_WORKBENCH_COLORS_SETTING, CUSTOM_EDITOR_COLORS_SETTING, DETECT_HC_SETTING, HC_THEME_ID, IColorCustomizations } from 'vs/workbench/services/themes/common/workbenchThemeService'; +import { IWorkbenchThemeService, IColorTheme, ITokenColorCustomizations, IFileIconTheme, ExtensionData, VS_LIGHT_THEME, VS_DARK_THEME, VS_HC_THEME, COLOR_THEME_SETTING, ICON_THEME_SETTING, CUSTOM_WORKBENCH_COLORS_SETTING, CUSTOM_EDITOR_COLORS_SETTING, DETECT_HC_SETTING, HC_THEME_ID, IColorCustomizations, CUSTOM_EDITOR_TOKENSTYLES_SETTING, IExperimentalTokenStyleCustomizations } from 'vs/workbench/services/themes/common/workbenchThemeService'; import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage'; import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry'; import { Registry } from 'vs/platform/registry/common/platform'; @@ -29,6 +29,7 @@ import * as resources from 'vs/base/common/resources'; import { IJSONSchema } from 'vs/base/common/jsonSchema'; import { textmateColorsSchemaId, registerColorThemeSchemas, textmateColorSettingsSchemaId } from 'vs/workbench/services/themes/common/colorThemeSchema'; import { workbenchColorsSchemaId } from 'vs/platform/theme/common/colorRegistry'; +import { tokenStylingSchemaId } from 'vs/platform/theme/common/tokenClassificationRegistry'; import { registerSingleton } from 'vs/platform/instantiation/common/extensions'; import { getRemoteAuthority } from 'vs/platform/remote/common/remoteHosts'; import { IWorkbenchLayoutService } from 'vs/workbench/services/layout/browser/layoutService'; @@ -92,6 +93,10 @@ export class WorkbenchThemeService implements IWorkbenchThemeService { return this.configurationService.getValue(CUSTOM_EDITOR_COLORS_SETTING) || {}; } + private get tokenStylesCustomizations(): IExperimentalTokenStyleCustomizations { + return this.configurationService.getValue(CUSTOM_EDITOR_TOKENSTYLES_SETTING) || {}; + } + constructor( @IExtensionService extensionService: IExtensionService, @IStorageService private readonly storageService: IStorageService, @@ -126,6 +131,7 @@ export class WorkbenchThemeService implements IWorkbenchThemeService { } themeData.setCustomColors(this.colorCustomizations); themeData.setCustomTokenColors(this.tokenColorCustomizations); + themeData.setCustomTokenStyleRules(this.tokenStylesCustomizations); this.updateDynamicCSSRules(themeData); this.applyTheme(themeData, undefined, true); @@ -154,18 +160,22 @@ export class WorkbenchThemeService implements IWorkbenchThemeService { const themeSpecificWorkbenchColors: IJSONSchema = { properties: {} }; const themeSpecificTokenColors: IJSONSchema = { properties: {} }; + const themeSpecificTokenStyling: IJSONSchema = { properties: {} }; const workbenchColors = { $ref: workbenchColorsSchemaId, additionalProperties: false }; const tokenColors = { properties: tokenColorSchema.properties, additionalProperties: false }; + const tokenStyling = { $ref: tokenStylingSchemaId, additionalProperties: false }; for (let t of event.themes) { // add theme specific color customization ("[Abyss]":{ ... }) const themeId = `[${t.settingsId}]`; themeSpecificWorkbenchColors.properties![themeId] = workbenchColors; themeSpecificTokenColors.properties![themeId] = tokenColors; + themeSpecificTokenStyling.properties![themeId] = tokenStyling; } colorCustomizationsSchema.allOf![1] = themeSpecificWorkbenchColors; tokenColorCustomizationSchema.allOf![1] = themeSpecificTokenColors; + experimentalTokenStylingCustomizationSchema.allOf![1] = themeSpecificTokenStyling; configurationRegistry.notifyConfigurationSchemaUpdated(themeSettingsConfiguration, tokenColorCustomizationConfiguration); @@ -308,6 +318,10 @@ export class WorkbenchThemeService implements IWorkbenchThemeService { this.currentColorTheme.setCustomTokenColors(this.tokenColorCustomizations); hasColorChanges = true; } + if (e.affectsConfiguration(CUSTOM_EDITOR_TOKENSTYLES_SETTING)) { + this.currentColorTheme.setCustomTokenStyleRules(this.tokenStylesCustomizations); + hasColorChanges = true; + } if (hasColorChanges) { this.updateDynamicCSSRules(this.currentColorTheme); this.onColorThemeChange.fire(this.currentColorTheme); @@ -698,12 +712,18 @@ const tokenColorCustomizationSchema: IConfigurationPropertySchema = { default: {}, allOf: [tokenColorSchema] }; +const experimentalTokenStylingCustomizationSchema: IConfigurationPropertySchema = { + description: nls.localize('editorColorsTokenStyles', "Overrides token color and styles from the currently selected color theme."), + default: {}, + allOf: [{ $ref: tokenStylingSchemaId }] +}; const tokenColorCustomizationConfiguration: IConfigurationNode = { id: 'editor', order: 7.2, type: 'object', properties: { - [CUSTOM_EDITOR_COLORS_SETTING]: tokenColorCustomizationSchema + [CUSTOM_EDITOR_COLORS_SETTING]: tokenColorCustomizationSchema, + [CUSTOM_EDITOR_TOKENSTYLES_SETTING]: experimentalTokenStylingCustomizationSchema } }; configurationRegistry.registerConfiguration(tokenColorCustomizationConfiguration); diff --git a/src/vs/workbench/services/themes/common/colorThemeData.ts b/src/vs/workbench/services/themes/common/colorThemeData.ts index e417676fc2e..4a1b760b4e2 100644 --- a/src/vs/workbench/services/themes/common/colorThemeData.ts +++ b/src/vs/workbench/services/themes/common/colorThemeData.ts @@ -6,7 +6,7 @@ import { basename } from 'vs/base/common/path'; import * as Json from 'vs/base/common/json'; import { Color } from 'vs/base/common/color'; -import { ExtensionData, ITokenColorCustomizations, ITokenColorizationRule, IColorTheme, IColorMap, IThemeExtensionPoint, VS_LIGHT_THEME, VS_HC_THEME, IColorCustomizations } from 'vs/workbench/services/themes/common/workbenchThemeService'; +import { ExtensionData, ITokenColorCustomizations, ITextMateThemingRule, IColorTheme, IColorMap, IThemeExtensionPoint, VS_LIGHT_THEME, VS_HC_THEME, IColorCustomizations, IExperimentalTokenStyleCustomizations, ITokenColorizationSetting } from 'vs/workbench/services/themes/common/workbenchThemeService'; import { convertSettings } from 'vs/workbench/services/themes/common/themeCompatibility'; import * as nls from 'vs/nls'; import * as types from 'vs/base/common/types'; @@ -49,12 +49,13 @@ export class ColorThemeData implements IColorTheme { watch?: boolean; extensionData?: ExtensionData; - private themeTokenColors: ITokenColorizationRule[] = []; - private customTokenColors: ITokenColorizationRule[] = []; + private themeTokenColors: ITextMateThemingRule[] = []; + private customTokenColors: ITextMateThemingRule[] = []; private colorMap: IColorMap = {}; private customColorMap: IColorMap = {}; - private tokenStylingRules: TokenStylingRule[] = []; + private tokenStylingRules: TokenStylingRule[] | undefined = undefined; + private customTokenStylingRules: TokenStylingRule[] = []; private themeTokenScopeMatchers: Matcher[] | undefined; private customTokenScopeMatchers: Matcher[] | undefined; @@ -66,8 +67,8 @@ export class ColorThemeData implements IColorTheme { this.isLoaded = false; } - get tokenColors(): ITokenColorizationRule[] { - const result: ITokenColorizationRule[] = []; + get tokenColors(): ITextMateThemingRule[] { + const result: ITextMateThemingRule[] = []; // the default rule (scope empty) is always the first rule. Ignore all other default rules. const foreground = this.getColor(editorForeground) || this.getDefault(editorForeground)!; @@ -81,7 +82,7 @@ export class ColorThemeData implements IColorTheme { let hasDefaultTokens = false; - function addRule(rule: ITokenColorizationRule) { + function addRule(rule: ITextMateThemingRule) { if (rule.scope && rule.settings) { if (rule.scope === 'token.info-token') { hasDefaultTokens = true; @@ -115,7 +116,7 @@ export class ColorThemeData implements IColorTheme { public getTokenStyle(tokenClassification: TokenClassification, useDefault?: boolean): TokenStyle | undefined { // todo: cache results - return tokenClassificationRegistry.resolveTokenStyle(tokenClassification, this.tokenStylingRules, useDefault !== false, this); + return tokenClassificationRegistry.resolveTokenStyle(tokenClassification, this.tokenStylingRules, this.customTokenStylingRules, this); } public getDefault(colorId: ColorIdentifier): Color | undefined { @@ -123,7 +124,7 @@ export class ColorThemeData implements IColorTheme { } public getDefaultTokenStyle(tokenClassification: TokenClassification): TokenStyle | undefined { - return tokenClassificationRegistry.resolveTokenStyle(tokenClassification, [], true, this); + return tokenClassificationRegistry.resolveTokenStyle(tokenClassification, undefined, [], this); } public resolveScopes(scopes: ProbeScope[]): TokenStyle | undefined { @@ -136,12 +137,12 @@ export class ColorThemeData implements IColorTheme { } for (let scope of scopes) { - let foreground: string | null = null; - let fontStyle: string | null = null; + let foreground: string | undefined = undefined; + let fontStyle: string | undefined = undefined; let foregroundScore = -1; let fontStyleScore = -1; - function findTokenStyleForScopeInScopes(scopeMatchers: Matcher[], tokenColors: ITokenColorizationRule[]) { + function findTokenStyleForScopeInScopes(scopeMatchers: Matcher[], tokenColors: ITextMateThemingRule[]) { for (let i = 0; i < scopeMatchers.length; i++) { const score = scopeMatchers[i](scope); if (score >= 0) { @@ -157,7 +158,7 @@ export class ColorThemeData implements IColorTheme { } findTokenStyleForScopeInScopes(this.themeTokenScopeMatchers, this.themeTokenColors); findTokenStyleForScopeInScopes(this.customTokenScopeMatchers, this.customTokenColors); - if (foreground !== null || fontStyle !== null) { + if (foreground !== undefined || fontStyle !== undefined) { return getTokenStyle(foreground, fontStyle); } } @@ -200,8 +201,14 @@ export class ColorThemeData implements IColorTheme { } } - public setTokenStyleRules(tokenStylingRules: TokenStylingRule[]) { - this.tokenStylingRules = tokenStylingRules; + public setCustomTokenStyleRules(tokenStylingRules: IExperimentalTokenStyleCustomizations) { + this.tokenStylingRules = []; + readCustomTokenStyleRules(tokenStylingRules, this.tokenStylingRules); + + const themeSpecificColors = tokenStylingRules[`[${this.settingsId}]`] as IExperimentalTokenStyleCustomizations; + if (types.isObject(themeSpecificColors)) { + readCustomTokenStyleRules(themeSpecificColors, this.tokenStylingRules); + } } private addCustomTokenColors(customTokenColors: ITokenColorCustomizations) { @@ -243,9 +250,17 @@ export class ColorThemeData implements IColorTheme { } this.themeTokenColors = []; this.themeTokenScopeMatchers = undefined; - this.colorMap = {}; - return _loadColorTheme(extensionResourceLoaderService, this.location, this.themeTokenColors, this.colorMap).then(_ => { + + const result = { + colors: {}, + textMateRules: [], + stylingRules: undefined + }; + return _loadColorTheme(extensionResourceLoaderService, this.location, result).then(_ => { this.isLoaded = true; + this.tokenStylingRules = result.stylingRules; + this.colorMap = result.colors; + this.themeTokenColors = result.textMateRules; }); } @@ -358,7 +373,7 @@ function toCSSSelector(extensionId: string, path: string) { return str; } -function _loadColorTheme(extensionResourceLoaderService: IExtensionResourceLoaderService, themeLocation: URI, resultRules: ITokenColorizationRule[], resultColors: IColorMap): Promise { +function _loadColorTheme(extensionResourceLoaderService: IExtensionResourceLoaderService, themeLocation: URI, result: { textMateRules: ITextMateThemingRule[], colors: IColorMap, stylingRules: TokenStylingRule[] | undefined }): Promise { if (resources.extname(themeLocation) === '.json') { return extensionResourceLoaderService.readExtensionResource(themeLocation).then(content => { let errors: Json.ParseError[] = []; @@ -370,11 +385,11 @@ function _loadColorTheme(extensionResourceLoaderService: IExtensionResourceLoade } let includeCompletes: Promise = Promise.resolve(null); if (contentValue.include) { - includeCompletes = _loadColorTheme(extensionResourceLoaderService, resources.joinPath(resources.dirname(themeLocation), contentValue.include), resultRules, resultColors); + includeCompletes = _loadColorTheme(extensionResourceLoaderService, resources.joinPath(resources.dirname(themeLocation), contentValue.include), result); } return includeCompletes.then(_ => { if (Array.isArray(contentValue.settings)) { - convertSettings(contentValue.settings, resultRules, resultColors); + convertSettings(contentValue.settings, result); return null; } let colors = contentValue.colors; @@ -386,38 +401,42 @@ function _loadColorTheme(extensionResourceLoaderService: IExtensionResourceLoade for (let colorId in colors) { let colorHex = colors[colorId]; if (typeof colorHex === 'string') { // ignore colors tht are null - resultColors[colorId] = Color.fromHex(colors[colorId]); + result.colors[colorId] = Color.fromHex(colors[colorId]); } } } let tokenColors = contentValue.tokenColors; if (tokenColors) { if (Array.isArray(tokenColors)) { - resultRules.push(...tokenColors); + result.textMateRules.push(...tokenColors); return null; } else if (typeof tokenColors === 'string') { - return _loadSyntaxTokens(extensionResourceLoaderService, resources.joinPath(resources.dirname(themeLocation), tokenColors), resultRules, {}); + return _loadSyntaxTokens(extensionResourceLoaderService, resources.joinPath(resources.dirname(themeLocation), tokenColors), result); } else { return Promise.reject(new Error(nls.localize({ key: 'error.invalidformat.tokenColors', comment: ['{0} will be replaced by a path. Values in quotes should not be translated.'] }, "Problem parsing color theme file: {0}. Property 'tokenColors' should be either an array specifying colors or a path to a TextMate theme file", themeLocation.toString()))); } } + let tokenStylingRules = contentValue.tokenStylingRules; + if (tokenStylingRules && typeof tokenStylingRules === 'object') { + result.stylingRules = readCustomTokenStyleRules(tokenStylingRules, result.stylingRules); + } return null; }); }); } else { - return _loadSyntaxTokens(extensionResourceLoaderService, themeLocation, resultRules, resultColors); + return _loadSyntaxTokens(extensionResourceLoaderService, themeLocation, result); } } -function _loadSyntaxTokens(extensionResourceLoaderService: IExtensionResourceLoaderService, themeLocation: URI, resultRules: ITokenColorizationRule[], resultColors: IColorMap): Promise { +function _loadSyntaxTokens(extensionResourceLoaderService: IExtensionResourceLoaderService, themeLocation: URI, result: { textMateRules: ITextMateThemingRule[], colors: IColorMap }): Promise { return extensionResourceLoaderService.readExtensionResource(themeLocation).then(content => { try { let contentValue = parsePList(content); - let settings: ITokenColorizationRule[] = contentValue.settings; + let settings: ITextMateThemingRule[] = contentValue.settings; if (!Array.isArray(settings)) { return Promise.reject(new Error(nls.localize('error.plist.invalidformat', "Problem parsing tmTheme file: {0}. 'settings' is not array."))); } - convertSettings(settings, resultRules, resultColors); + convertSettings(settings, result); return Promise.resolve(null); } catch (e) { return Promise.reject(new Error(nls.localize('error.cannotparse', "Problems parsing tmTheme file: {0}", e.message))); @@ -427,7 +446,7 @@ function _loadSyntaxTokens(extensionResourceLoaderService: IExtensionResourceLoa }); } -let defaultThemeColors: { [baseTheme: string]: ITokenColorizationRule[] } = { +let defaultThemeColors: { [baseTheme: string]: ITextMateThemingRule[] } = { 'light': [ { scope: 'token.info-token', settings: { foreground: '#316bcd' } }, { scope: 'token.warn-token', settings: { foreground: '#cd9731' } }, @@ -489,7 +508,7 @@ function scopesAreMatching(thisScopeName: string, scopeName: string): boolean { return thisScopeName.length > len && thisScopeName.substr(0, len) === scopeName && thisScopeName[len] === '.'; } -function getScopeMatcher(rule: ITokenColorizationRule): Matcher { +function getScopeMatcher(rule: ITextMateThemingRule): Matcher { const ruleScope = rule.scope; if (!ruleScope || !rule.settings) { return noMatch; @@ -515,17 +534,56 @@ function getScopeMatcher(rule: ITokenColorizationRule): Matcher { }; } -function getTokenStyle(foreground: string | null, fontStyle: string | null): TokenStyle | undefined { +function getTokenStyle(foreground: string | undefined, fontStyle: string | undefined): TokenStyle { let foregroundColor = undefined; - if (foreground !== null) { + if (foreground !== undefined) { foregroundColor = Color.fromHex(foreground); } let bold, underline, italic; - if (fontStyle !== null) { - bold = fontStyle.indexOf('bold') !== -1; - underline = fontStyle.indexOf('underline') !== -1; - italic = fontStyle.indexOf('italic') !== -1; + if (fontStyle !== undefined) { + fontStyle = fontStyle.trim(); + if (fontStyle.length === 0) { + bold = italic = underline = false; + } else { + const expression = /-?italic|-?bold|-?underline/g; + let match; + while ((match = expression.exec(fontStyle))) { + switch (match[0]) { + case 'bold': bold = true; break; + case '-bold': bold = false; break; + case 'italic': italic = true; break; + case '-italic': italic = false; break; + case 'underline': underline = true; break; + case '-underline': underline = false; break; + } + } + } } return new TokenStyle(foregroundColor, bold, underline, italic); } + +function readCustomTokenStyleRules(tokenStylingRuleSection: IExperimentalTokenStyleCustomizations, result: TokenStylingRule[] = []) { + for (let key in tokenStylingRuleSection) { + if (key[0] !== '[') { + const classification = tokenClassificationRegistry.getTokenClassificationFromString(key); + if (classification) { + const settings = tokenStylingRuleSection[key]; + let style: TokenStyle | undefined; + if (typeof settings === 'string') { + style = getTokenStyle(settings, undefined); + } else if (isTokenColorizationSetting(settings)) { + style = getTokenStyle(settings.foreground, settings.fontStyle); + } + if (style) { + result.push(tokenClassificationRegistry.getTokenStylingRule(classification, style)); + } + } + } + } + return result; +} + +function isTokenColorizationSetting(style: any): style is ITokenColorizationSetting { + return style && (style.foreground || style.fontStyle); +} diff --git a/src/vs/workbench/services/themes/common/themeCompatibility.ts b/src/vs/workbench/services/themes/common/themeCompatibility.ts index 6518ff4a4ac..7af65c955ad 100644 --- a/src/vs/workbench/services/themes/common/themeCompatibility.ts +++ b/src/vs/workbench/services/themes/common/themeCompatibility.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { ITokenColorizationRule, IColorMap } from 'vs/workbench/services/themes/common/workbenchThemeService'; +import { ITextMateThemingRule, IColorMap } from 'vs/workbench/services/themes/common/workbenchThemeService'; import { Color } from 'vs/base/common/color'; import * as colorRegistry from 'vs/platform/theme/common/colorRegistry'; @@ -18,9 +18,9 @@ function addSettingMapping(settingId: string, colorId: string) { colorIds.push(colorId); } -export function convertSettings(oldSettings: ITokenColorizationRule[], resultRules: ITokenColorizationRule[], resultColors: IColorMap): void { +export function convertSettings(oldSettings: ITextMateThemingRule[], result: { textMateRules: ITextMateThemingRule[], colors: IColorMap }): void { for (let rule of oldSettings) { - resultRules.push(rule); + result.textMateRules.push(rule); if (!rule.scope) { let settings = rule.settings; if (!settings) { @@ -34,7 +34,7 @@ export function convertSettings(oldSettings: ITokenColorizationRule[], resultRul if (typeof colorHex === 'string') { let color = Color.fromHex(colorHex); for (let colorId of mappings) { - resultColors[colorId] = color; + result.colors[colorId] = color; } } } diff --git a/src/vs/workbench/services/themes/common/workbenchThemeService.ts b/src/vs/workbench/services/themes/common/workbenchThemeService.ts index 8684b54a804..9937ca11be9 100644 --- a/src/vs/workbench/services/themes/common/workbenchThemeService.ts +++ b/src/vs/workbench/services/themes/common/workbenchThemeService.ts @@ -22,6 +22,7 @@ export const DETECT_HC_SETTING = 'window.autoDetectHighContrast'; export const ICON_THEME_SETTING = 'workbench.iconTheme'; export const CUSTOM_WORKBENCH_COLORS_SETTING = 'workbench.colorCustomizations'; export const CUSTOM_EDITOR_COLORS_SETTING = 'editor.tokenColorCustomizations'; +export const CUSTOM_EDITOR_TOKENSTYLES_SETTING = 'editor.tokenColorCustomizationsExperimental'; export interface IColorTheme extends ITheme { readonly id: string; @@ -30,7 +31,7 @@ export interface IColorTheme extends ITheme { readonly extensionData?: ExtensionData; readonly description?: string; readonly isLoaded: boolean; - readonly tokenColors: ITokenColorizationRule[]; + readonly tokenColors: ITextMateThemingRule[]; } export interface IColorMap { @@ -69,7 +70,7 @@ export interface IColorCustomizations { } export interface ITokenColorCustomizations { - [groupIdOrThemeSettingsId: string]: string | ITokenColorizationSetting | ITokenColorCustomizations | undefined | ITokenColorizationRule[]; + [groupIdOrThemeSettingsId: string]: string | ITokenColorizationSetting | ITokenColorCustomizations | undefined | ITextMateThemingRule[]; comments?: string | ITokenColorizationSetting; strings?: string | ITokenColorizationSetting; numbers?: string | ITokenColorizationSetting; @@ -77,10 +78,14 @@ export interface ITokenColorCustomizations { types?: string | ITokenColorizationSetting; functions?: string | ITokenColorizationSetting; variables?: string | ITokenColorizationSetting; - textMateRules?: ITokenColorizationRule[]; + textMateRules?: ITextMateThemingRule[]; } -export interface ITokenColorizationRule { +export interface IExperimentalTokenStyleCustomizations { + [styleRuleOrThemeSettingsId: string]: string | ITokenColorizationSetting | IExperimentalTokenStyleCustomizations | undefined; +} + +export interface ITextMateThemingRule { name?: string; scope?: string | string[]; settings: ITokenColorizationSetting; @@ -89,7 +94,7 @@ export interface ITokenColorizationRule { export interface ITokenColorizationSetting { foreground?: string; background?: string; - fontStyle?: string; // italic, underline, bold + fontStyle?: string; /* [italic|underline|bold] */ } export interface ExtensionData { @@ -106,4 +111,4 @@ export interface IThemeExtensionPoint { path: string; uiTheme?: typeof VS_LIGHT_THEME | typeof VS_DARK_THEME | typeof VS_HC_THEME; _watch: boolean; // unsupported options to watch location -} \ No newline at end of file +} diff --git a/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts b/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts index a0fe30b8683..ab41c917cb8 100644 --- a/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts +++ b/src/vs/workbench/services/themes/test/electron-browser/tokenStyleResolving.test.ts @@ -6,7 +6,7 @@ import { ColorThemeData } from 'vs/workbench/services/themes/common/colorThemeData'; import * as assert from 'assert'; import { ITokenColorCustomizations } from 'vs/workbench/services/themes/common/workbenchThemeService'; -import { TokenStyle, comments, variables, types, functions, keywords, numbers, strings, getTokenClassificationRegistry, TokenStylingRule } from 'vs/platform/theme/common/tokenClassificationRegistry'; +import { TokenStyle, comments, variables, types, functions, keywords, numbers, strings, getTokenClassificationRegistry } from 'vs/platform/theme/common/tokenClassificationRegistry'; import { Color } from 'vs/base/common/color'; import { isString } from 'vs/base/common/types'; import { FileService } from 'vs/platform/files/common/fileService'; @@ -44,14 +44,6 @@ function tokenStyleAsString(ts: TokenStyle | undefined | null) { return str; } -function getTokenStyleRules(rules: [string, TokenStyle][]): TokenStylingRule[] { - return rules.map(e => { - const rule = tokenClassificationRegistry.getTokenStylingRule(e[0], e[1]); - assert.ok(rule); - return rule!; - }); -} - function assertTokenStyle(actual: TokenStyle | undefined | null, expected: TokenStyle | undefined | null, message?: string) { assert.equal(tokenStyleAsString(actual), tokenStyleAsString(expected), message); } @@ -87,7 +79,7 @@ suite('Themes - TokenStyleResolving', () => { assertTokenStyles(themeData, { [comments]: ts('#88846f', undefinedStyle), [variables]: ts('#F8F8F2', unsetStyle), - [types]: ts('#A6E22E', { underline: true, bold: false, italic: false }), + [types]: ts('#A6E22E', { underline: true }), [functions]: ts('#A6E22E', unsetStyle), [strings]: ts('#E6DB74', undefinedStyle), [numbers]: ts('#AE81FF', undefinedStyle), @@ -187,7 +179,7 @@ suite('Themes - TokenStyleResolving', () => { assertTokenStyles(themeData, { [comments]: ts('#384887', undefinedStyle), [variables]: ts(undefined, unsetStyle), - [types]: ts('#ffeebb', { underline: true, bold: false, italic: false }), + [types]: ts('#ffeebb', { underline: true }), [functions]: ts('#ddbb88', unsetStyle), [strings]: ts('#22aa44', undefinedStyle), [numbers]: ts('#f280d0', undefinedStyle), @@ -259,43 +251,43 @@ suite('Themes - TokenStyleResolving', () => { assertTokenStyle(tokenStyle, defaultTokenStyle, 'keyword.operators'); tokenStyle = themeData.resolveScopes([['storage']]); - assertTokenStyle(tokenStyle, ts('#F92672', { italic: true, underline: false, bold: false }), 'storage'); + assertTokenStyle(tokenStyle, ts('#F92672', { italic: true }), 'storage'); tokenStyle = themeData.resolveScopes([['storage.type']]); - assertTokenStyle(tokenStyle, ts('#66D9EF', { italic: true, underline: false, bold: false }), 'storage.type'); + assertTokenStyle(tokenStyle, ts('#66D9EF', { italic: true }), 'storage.type'); tokenStyle = themeData.resolveScopes([['entity.name.class']]); - assertTokenStyle(tokenStyle, ts('#A6E22E', { underline: true, italic: false, bold: false }), 'entity.name.class'); + assertTokenStyle(tokenStyle, ts('#A6E22E', { underline: true }), 'entity.name.class'); tokenStyle = themeData.resolveScopes([['meta.structure.dictionary.json', 'string.quoted.double.json']]); assertTokenStyle(tokenStyle, ts('#66D9EF', undefined), 'json property'); tokenStyle = themeData.resolveScopes([['keyword'], ['storage.type'], ['entity.name.class']]); - assertTokenStyle(tokenStyle, ts('#66D9EF', { italic: true, underline: false, bold: false }), 'storage.type'); + assertTokenStyle(tokenStyle, ts('#66D9EF', { italic: true }), 'storage.type'); }); test('rule matching', async () => { const themeData = ColorThemeData.createLoadedEmptyTheme('test', 'test'); themeData.setCustomColors({ 'editor.foreground': '#000000' }); - themeData.setTokenStyleRules(getTokenStyleRules([ - ['types', ts('#ff0000', undefined)], - ['classes', ts('#0000ff', { italic: true })], - ['*.static', ts(undefined, { bold: true })], - ['*.declaration', ts(undefined, { italic: true })], - ['*.async.static', ts('#00ffff', { bold: false, underline: true })], - ['*.async', ts('#000fff', { italic: false, underline: true })] - ])); + themeData.setCustomTokenStyleRules({ + 'types': '#ff0000', + 'classes': { foreground: '#0000ff', fontStyle: 'italic' }, + '*.static': { fontStyle: 'bold' }, + '*.declaration': { fontStyle: 'italic' }, + '*.async.static': { fontStyle: 'italic underline' }, + '*.async': { foreground: '#000fff', fontStyle: '-italic underline' } + }); assertTokenStyles(themeData, { 'types': ts('#ff0000', undefinedStyle), - 'types.static': ts('#ff0000', { bold: true, italic: undefined, underline: undefined }), - 'types.static.declaration': ts('#ff0000', { bold: true, italic: true, underline: undefined }), - 'classes': ts('#0000ff', { bold: undefined, italic: true, underline: undefined }), - 'classes.static.declaration': ts('#0000ff', { bold: true, italic: true, underline: undefined }), - 'classes.declaration': ts('#0000ff', { bold: undefined, italic: true, underline: undefined }), - 'classes.declaration.async': ts('#000fff', { bold: undefined, italic: false, underline: true }), - 'classes.declaration.async.static': ts('#00ffff', { bold: false, italic: false, underline: true }), + 'types.static': ts('#ff0000', { bold: true }), + 'types.static.declaration': ts('#ff0000', { bold: true, italic: true }), + 'classes': ts('#0000ff', { italic: true }), + 'classes.static.declaration': ts('#0000ff', { bold: true, italic: true }), + 'classes.declaration': ts('#0000ff', { italic: true }), + 'classes.declaration.async': ts('#000fff', { underline: true, italic: false }), + 'classes.declaration.async.static': ts('#000fff', { italic: true, underline: true, bold: true }), }); });