Skip to content

Commit fb7cc04

Browse files
committed
more
1 parent 8db09a4 commit fb7cc04

3 files changed

Lines changed: 80 additions & 39 deletions

File tree

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

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { IJSONSchema, IJSONSchemaMap } from 'vs/base/common/jsonSchema';
88
import { Color } from 'vs/base/common/color';
99
import { ITheme } from 'vs/platform/theme/common/themeService';
1010
import { Event, Emitter } from 'vs/base/common/event';
11+
import * as nls from 'vs/nls';
1112

1213
import { Extensions as JSONExtensions, IJSONContributionRegistry } from 'vs/platform/jsonschemas/common/jsonContributionRegistry';
1314
import { RunOnceScheduler } from 'vs/base/common/async';
@@ -69,21 +70,21 @@ export namespace TokenStyle {
6970
}
7071
}
7172

72-
73+
export type ProbeScope = string[] | string;
7374

7475
export interface TokenStyleFunction {
7576
(theme: ITheme): TokenStyle | undefined;
7677
}
7778

7879
export interface TokenStyleDefaults {
79-
scopesToProbe?: string[];
80-
light: TokenStyle | null;
81-
dark: TokenStyle | null;
82-
hc: TokenStyle | null;
80+
scopesToProbe?: ProbeScope[];
81+
light: TokenStyleValue | null;
82+
dark: TokenStyleValue | null;
83+
hc: TokenStyleValue | null;
8384
}
8485

8586
/**
86-
* A TokenStyle Value is either a tokestyle literal, a reference to other color or a derived color
87+
* A TokenStyle Value is either a token style literal, a reference to other token style or a derived token style
8788
*/
8889
export type TokenStyleValue = TokenStyle | string | TokenStyleIdentifier | TokenStyleFunction;
8990

@@ -117,7 +118,7 @@ export interface ITokenStyleRegistry {
117118
/**
118119
* Gets the default TokenStyle of the given id
119120
*/
120-
resolveDefaultTokenStyle(id: TokenStyleIdentifier, theme: ITheme, findTokenStyleForScope: (scope: string) => TokenStyle | undefined): TokenStyle | undefined;
121+
resolveDefaultTokenStyle(id: TokenStyleIdentifier, theme: ITheme, findTokenStyleForScope: (scope: ProbeScope) => TokenStyle | undefined): TokenStyle | undefined;
121122

122123
/**
123124
* JSON schema for an object to assign TokenStyle values to one of the TokenStyle contributions.
@@ -147,9 +148,18 @@ class TokenStyleRegistry implements ITokenStyleRegistry {
147148
}
148149

149150
public registerTokenStyle(id: string, defaults: TokenStyleDefaults | null, description: string, deprecationMessage?: string): TokenStyleIdentifier {
150-
let colorContribution: TokenStyleContribution = { id, description, defaults, deprecationMessage };
151-
this.tokenStyleById[id] = colorContribution;
152-
let propertySchema: IJSONSchema = { type: 'string', description, format: 'color-hex', default: '#ff0000' };
151+
let tokenStyleContribution: TokenStyleContribution = { id, description, defaults, deprecationMessage };
152+
this.tokenStyleById[id] = tokenStyleContribution;
153+
let propertySchema: IJSONSchema = {
154+
type: 'object',
155+
description,
156+
properties: {
157+
'foreground': { type: 'string', format: 'color-hex', default: '#ff0000' },
158+
'italic': { type: 'boolean' },
159+
'bold': { type: 'boolean' },
160+
'underline': { type: 'boolean' }
161+
}
162+
};
153163
if (deprecationMessage) {
154164
propertySchema.deprecationMessage = deprecationMessage;
155165
}
@@ -177,7 +187,7 @@ class TokenStyleRegistry implements ITokenStyleRegistry {
177187
return Object.keys(this.tokenStyleById).map(id => this.tokenStyleById[id]);
178188
}
179189

180-
public resolveDefaultTokenStyle(id: TokenStyleIdentifier, theme: ITheme, findTokenStyleForScope: (scope: string) => TokenStyle | undefined): TokenStyle | undefined {
190+
public resolveDefaultTokenStyle(id: TokenStyleIdentifier, theme: ITheme, findTokenStyleForScope: (scope: ProbeScope) => TokenStyle | undefined): TokenStyle | undefined {
181191
const tokenStyleDesc = this.tokenStyleById[id];
182192
if (tokenStyleDesc && tokenStyleDesc.defaults) {
183193
const scopesToProbe = tokenStyleDesc.defaults.scopesToProbe;
@@ -229,7 +239,16 @@ export function getTokenStyleRegistry(): ITokenStyleRegistry {
229239
return tokenStyleRegistry;
230240
}
231241

232-
// ----- implementation
242+
// colors
243+
244+
245+
export const comments = registerTokenStyle('comments', { scopesToProbe: ['comment'], dark: null, light: null, hc: null }, nls.localize('comments', "Token style for comments."));
246+
export const strings = registerTokenStyle('strings', { scopesToProbe: ['strings'], dark: null, light: null, hc: null }, nls.localize('strings', "Token style for strings."));
247+
export const keywords = registerTokenStyle('keywords', { scopesToProbe: ['keyword.control', 'storage', 'storage.type'], dark: null, light: null, hc: null }, nls.localize('keywords', "Token style for keywords."));
248+
export const numbers = registerTokenStyle('numbers', { scopesToProbe: ['constant.numeric'], dark: null, light: null, hc: null }, nls.localize('numbers', "Token style for numbers."));
249+
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."));
250+
export const functions = registerTokenStyle('functions', { scopesToProbe: ['entity.name.function', 'support.function'], dark: null, light: null, hc: null }, nls.localize('functions', "Token style for functions."));
251+
export const variables = registerTokenStyle('variables', { scopesToProbe: ['variable', 'entity.name.variable'], dark: null, light: null, hc: null }, nls.localize('variables', "Token style for variables."));
233252

234253
/**
235254
* @param colorValue Resolve a color value in the context of a theme

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

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { URI } from 'vs/base/common/uri';
2020
import { IFileService } from 'vs/platform/files/common/files';
2121
import { parse as parsePList } from 'vs/workbench/services/themes/common/plistParser';
2222
import { startsWith } from 'vs/base/common/strings';
23-
import { Extensions as TokenStyleRegistryExtensions, TokenStyle, TokenStyleIdentifier, ITokenStyleRegistry, TokenStyleBits } from 'vs/platform/theme/common/tokenStyleRegistry';
23+
import { Extensions as TokenStyleRegistryExtensions, TokenStyle, TokenStyleIdentifier, ITokenStyleRegistry, TokenStyleBits, ProbeScope } from 'vs/platform/theme/common/tokenStyleRegistry';
2424
import { MatcherWithPriority, Matcher, createMatchers } from 'vs/workbench/services/themes/common/textMateScopeMatcher';
2525

2626
let colorRegistry = Registry.as<IColorRegistry>(ColorRegistryExtensions.ColorContribution);
@@ -59,8 +59,8 @@ export class ColorThemeData implements IColorTheme {
5959

6060
private tokenStyleMap: ITokenStyleMap | undefined;
6161

62-
private themeTokenScopeMatchers: Matcher<String>[] | undefined;
63-
private customTokenScopeMatchers: Matcher<String>[] | undefined;
62+
private themeTokenScopeMatchers: Matcher<ProbeScope>[] | undefined;
63+
private customTokenScopeMatchers: Matcher<ProbeScope>[] | undefined;
6464

6565
private constructor(id: string, label: string, settingsId: string) {
6666
this.id = id;
@@ -143,9 +143,9 @@ export class ColorThemeData implements IColorTheme {
143143
}
144144

145145
/** Public for testing reasons */
146-
public findTokenStyleForScope(scope: string): TokenStyle | undefined {
146+
public findTokenStyleForScope(scope: ProbeScope): TokenStyle | undefined {
147147

148-
function findTokenStyleForScopeInScopes(scopeMatchers: Matcher<string>[], tokenColors: ITokenColorizationRule[]) {
148+
function findTokenStyleForScopeInScopes(scopeMatchers: Matcher<ProbeScope>[], tokenColors: ITokenColorizationRule[]) {
149149
for (let i = scopeMatchers.length - 1; i >= 0; i--) {
150150
let matcher = scopeMatchers[i];
151151
if (!matcher) {
@@ -469,15 +469,25 @@ let defaultThemeColors: { [baseTheme: string]: ITokenColorizationRule[] } = {
469469
],
470470
};
471471

472-
const noMatch = (_scope: string) => false;
472+
const noMatch = (_scope: ProbeScope) => false;
473473

474-
export function nameMatcher(identifers: string[], scope: string) {
475-
for (const identifier of identifers) {
476-
if (scopesAreMatching(scope, identifier)) {
477-
return true;
478-
}
474+
function nameMatcher(identifers: string[], scope: ProbeScope) {
475+
if (!Array.isArray(scope)) {
476+
scope = [scope];
477+
}
478+
if (scope.length < identifers.length) {
479+
return false;
479480
}
480-
return false;
481+
let lastIndex = 0;
482+
return identifers.every(identifier => {
483+
for (let i = lastIndex; i < scope.length; i++) {
484+
if (scopesAreMatching(scope[i], identifier)) {
485+
lastIndex = i + 1;
486+
return true;
487+
}
488+
}
489+
return false;
490+
});
481491
}
482492

483493
function scopesAreMatching(thisScopeName: string, scopeName: string): boolean {
@@ -491,20 +501,20 @@ function scopesAreMatching(thisScopeName: string, scopeName: string): boolean {
491501
return thisScopeName.length > len && thisScopeName.substr(0, len) === scopeName && thisScopeName[len] === '.';
492502
}
493503

494-
function getScopeMatcher(rule: ITokenColorizationRule): Matcher<string> {
495-
const scope = rule.scope;
496-
if (!scope || !rule.settings) {
504+
function getScopeMatcher(rule: ITokenColorizationRule): Matcher<ProbeScope> {
505+
const ruleScope = rule.scope;
506+
if (!ruleScope || !rule.settings) {
497507
return noMatch;
498508
}
499-
const matchers: MatcherWithPriority<string>[] = [];
500-
if (Array.isArray(scope)) {
501-
for (let s of scope) {
502-
matchers.push(...createMatchers(s, nameMatcher));
509+
const matchers: MatcherWithPriority<ProbeScope>[] = [];
510+
if (Array.isArray(ruleScope)) {
511+
for (let rs of ruleScope) {
512+
matchers.push(...createMatchers(rs, nameMatcher));
503513
}
504514
} else {
505-
matchers.push(...createMatchers(scope, nameMatcher));
515+
matchers.push(...createMatchers(ruleScope, nameMatcher));
506516
}
507-
return (scope: string) => matchers.some(m => m.matcher(scope));
517+
return (scope: ProbeScope) => matchers.some(m => m.matcher(scope));
508518
}
509519

510520
function getTokenStyle(foreground: string | null, fontStyle: string | null): TokenStyle | undefined {

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,42 @@ suite('Themes - TokenStyleResolving', () => {
6060
}
6161
},
6262
{
63-
scope: 'storage.type',
63+
scope: ['storage.type', 'meta.structure.dictionary.json string.quoted.double.json'],
6464
settings: {
6565
foreground: '#66D9EF'
6666
}
67-
}
67+
},
68+
{
69+
scope: 'entity.name.type, entity.name.class, entity.name.namespace, entity.name.scope-resolution',
70+
settings: {
71+
fontStyle: 'underline',
72+
foreground: '#A6E22E'
73+
}
74+
},
6875
]
6976
};
7077

7178
themeData.setCustomTokenColors(customTokenColors);
7279

7380
let tokenStyle;
7481

75-
tokenStyle = themeData.findTokenStyleForScope('variable');
82+
tokenStyle = themeData.findTokenStyleForScope(['variable']);
7683
assertTokenStyle(tokenStyle, ts('#F8F8F2', 0), 'variable');
7784

78-
tokenStyle = themeData.findTokenStyleForScope('keyword');
85+
tokenStyle = themeData.findTokenStyleForScope(['keyword']);
7986
assertTokenStyle(tokenStyle, ts('#F92672', TokenStyleBits.ITALIC | TokenStyleBits.BOLD | TokenStyleBits.UNDERLINE), 'keyword');
8087

81-
tokenStyle = themeData.findTokenStyleForScope('storage');
88+
tokenStyle = themeData.findTokenStyleForScope(['storage']);
8289
assertTokenStyle(tokenStyle, ts('#F92672', TokenStyleBits.ITALIC), 'storage');
8390

84-
tokenStyle = themeData.findTokenStyleForScope('storage.type');
91+
tokenStyle = themeData.findTokenStyleForScope(['storage.type']);
8592
assertTokenStyle(tokenStyle, ts('#66D9EF', TokenStyleBits.ITALIC), 'storage.type');
8693

94+
tokenStyle = themeData.findTokenStyleForScope(['entity.name.class']);
95+
assertTokenStyle(tokenStyle, ts('#A6E22E', TokenStyleBits.UNDERLINE), 'entity.name.class');
96+
97+
tokenStyle = themeData.findTokenStyleForScope(['meta.structure.dictionary.json', 'string.quoted.double.json']);
98+
assertTokenStyle(tokenStyle, ts('#66D9EF', undefined), 'json property');
8799

88100
});
89101
});

0 commit comments

Comments
 (0)