Skip to content

Commit 6dce520

Browse files
committed
fix score based rule matching
1 parent 4a1614f commit 6dce520

4 files changed

Lines changed: 39 additions & 44 deletions

File tree

extensions/theme-abyss/themes/abyss-color-theme.json

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
{
22
"name": "Abyss",
33
"tokenColors": [
4-
{
5-
"settings": {
6-
"background": "#000c18",
7-
"foreground": "#6688cc"
8-
}
9-
},
104
{
115
"scope": ["meta.embedded", "source.groovy.embedded"],
126
"settings": {
@@ -260,6 +254,9 @@
260254
],
261255
"colors": {
262256

257+
"editor.background": "#000c18",
258+
"editor.foreground": "#6688cc",
259+
263260
// Base
264261
// "foreground": "",
265262
"focusBorder": "#596F99",
@@ -303,8 +300,6 @@
303300
"scrollbarSlider.hoverBackground": "#3B3F5188",
304301

305302
// Editor
306-
"editor.background": "#000c18",
307-
// "editor.foreground": "#6688cc",
308303
"editorWidget.background": "#262641",
309304
"editorCursor.foreground": "#ddbb88",
310305
"editorWhitespace.foreground": "#103050",

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export namespace TokenStyle {
7171
}
7272
}
7373

74-
export type ProbeScope = string[] | string;
74+
export type ProbeScope = string[];
7575

7676
export interface TokenStyleFunction {
7777
(theme: ITheme): TokenStyle | undefined;
@@ -246,13 +246,13 @@ export function getTokenStyleRegistry(): ITokenStyleRegistry {
246246
// colors
247247

248248

249-
export const comments = registerTokenStyle('comments', { scopesToProbe: ['comment'], dark: null, light: null, hc: null }, nls.localize('comments', "Token style for comments."));
250-
export const strings = registerTokenStyle('strings', { scopesToProbe: ['string'], dark: null, light: null, hc: null }, nls.localize('strings', "Token style for strings."));
251-
export const keywords = registerTokenStyle('keywords', { scopesToProbe: ['keyword.control', 'storage', 'storage.type'], dark: null, light: null, hc: null }, nls.localize('keywords', "Token style for keywords."));
252-
export const numbers = registerTokenStyle('numbers', { scopesToProbe: ['constant.numeric'], dark: null, light: null, hc: null }, nls.localize('numbers', "Token style for numbers."));
253-
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."));
254-
export const functions = registerTokenStyle('functions', { scopesToProbe: ['entity.name.function', 'support.function'], dark: null, light: null, hc: null }, nls.localize('functions', "Token style for functions."));
255-
export const variables = registerTokenStyle('variables', { scopesToProbe: ['variable', 'entity.name.variable'], dark: null, light: null, hc: null }, nls.localize('variables', "Token style for variables."));
249+
export const comments = registerTokenStyle('comments', { scopesToProbe: [['comment']], dark: null, light: null, hc: null }, nls.localize('comments', "Token style for comments."));
250+
export const strings = registerTokenStyle('strings', { scopesToProbe: [['string']], dark: null, light: null, hc: null }, nls.localize('strings', "Token style for strings."));
251+
export const keywords = registerTokenStyle('keywords', { scopesToProbe: [['keyword.control'], ['storage'], ['storage.type']], dark: null, light: null, hc: null }, nls.localize('keywords', "Token style for keywords."));
252+
export const numbers = registerTokenStyle('numbers', { scopesToProbe: [['constant.numeric']], dark: null, light: null, hc: null }, nls.localize('numbers', "Token style for numbers."));
253+
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."));
254+
export const functions = registerTokenStyle('functions', { scopesToProbe: [['entity.name.function'], ['support.function']], dark: null, light: null, hc: null }, nls.localize('functions', "Token style for functions."));
255+
export const variables = registerTokenStyle('variables', { scopesToProbe: [['variable'], ['entity.name.variable']], dark: null, light: null, hc: null }, nls.localize('variables', "Token style for variables."));
256256

257257
/**
258258
* @param colorValue Resolve a color value in the context of a theme

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

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ export class ColorThemeData implements IColorTheme {
138138
/** Public for testing reasons */
139139
public findTokenStyleForScope(scope: ProbeScope): TokenStyle | undefined {
140140

141-
let foreground: string | null = null;
141+
let foregroundColor = this.getColor(editorForeground);
142+
143+
let foreground = foregroundColor ? foregroundColor.toString() : null;
142144
let fontStyle: string | null = null;
143145
let foregroundScore = -1;
144146
let fontStyleScore = -1;
@@ -154,12 +156,15 @@ export class ColorThemeData implements IColorTheme {
154156
scopeMatchers[i] = matcher = getScopeMatcher(tokenColors[i]);
155157
}
156158
const score = matcher(scope);
157-
if (score > foregroundScore && settings.foreground) {
158-
foreground = settings.foreground;
159-
}
160-
if (score > fontStyleScore && types.isString(settings.fontStyle)) {
161-
fontStyle = settings.fontStyle;
159+
if (score >= 0) {
160+
if (score >= foregroundScore && settings.foreground) {
161+
foreground = settings.foreground;
162+
}
163+
if (score >= fontStyleScore && types.isString(settings.fontStyle)) {
164+
fontStyle = settings.fontStyle;
165+
}
162166
}
167+
163168
}
164169
}
165170

@@ -173,10 +178,7 @@ export class ColorThemeData implements IColorTheme {
173178
}
174179
findTokenStyleForScopeInScopes(this.customTokenScopeMatchers, this.customTokenColors);
175180

176-
if (foreground !== null || fontStyle !== null) {
177-
return getTokenStyle(foreground, fontStyle);
178-
}
179-
return undefined;
181+
return getTokenStyle(foreground, fontStyle);
180182
}
181183

182184

@@ -464,26 +466,19 @@ const noMatch = (_scope: ProbeScope) => -1;
464466
function nameMatcher(identifers: string[], scope: ProbeScope): number {
465467
function findInIdents(s: string, lastIndent: number): number {
466468
for (let i = lastIndent - 1; i >= 0; i--) {
467-
if (scopesAreMatching(identifers[i], s)) {
469+
if (scopesAreMatching(s, identifers[i])) {
468470
return i;
469471
}
470472
}
471473
return -1;
472474
}
473-
if (!Array.isArray(scope)) {
474-
const idx = findInIdents(scope, identifers.length);
475-
if (idx >= 0) {
476-
return idx * 0x10000 + scope.length;
477-
}
478-
return -1;
479-
}
480475
if (scope.length < identifers.length) {
481476
return -1;
482477
}
483478
let lastScopeIndex = scope.length - 1;
484479
let lastIdentifierIndex = findInIdents(scope[lastScopeIndex--], identifers.length);
485480
if (lastIdentifierIndex >= 0) {
486-
const score = lastIdentifierIndex * 0x10000 + scope.length;
481+
const score = (lastIdentifierIndex + 1) * 0x10000 + scope.length;
487482
while (lastScopeIndex >= 0) {
488483
lastIdentifierIndex = findInIdents(scope[lastScopeIndex--], lastIdentifierIndex);
489484
if (lastIdentifierIndex === -1) {
@@ -520,10 +515,13 @@ function getScopeMatcher(rule: ITokenColorizationRule): Matcher<ProbeScope> {
520515
} else {
521516
matchers.push(...createMatchers(ruleScope, nameMatcher));
522517
}
518+
if (matchers.length === 0) {
519+
return noMatch;
520+
}
523521
return (scope: ProbeScope) => {
524-
let max = 0;
525-
for (const m of matchers) {
526-
max = Math.max(max, m.matcher(scope));
522+
let max = matchers[0].matcher(scope);
523+
for (let i = 1; i < matchers.length; i++) {
524+
max = Math.max(max, matchers[i].matcher(scope));
527525
}
528526
return max;
529527
};

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { DiskFileSystemProvider } from 'vs/platform/files/node/diskFileSystemPro
1515
import { Schemas } from 'vs/base/common/network';
1616
import { URI } from 'vs/base/common/uri';
1717
import { getPathFromAmdModule } from 'vs/base/common/amd';
18+
import { editorForeground } from 'vs/platform/theme/common/colorRegistry';
1819

1920
function ts(foreground: string | undefined, styleFlags: number | undefined): TokenStyle {
2021
const foregroundColor = isString(foreground) ? Color.fromHex(foreground) : undefined;
@@ -154,12 +155,12 @@ suite('Themes - TokenStyleResolving', () => {
154155

155156
assertTokenStyles(themeData, {
156157
[comments]: ts('#384887', 0),
157-
[variables]: ts('#9966b8', 0),
158-
[types]: ts('#f06431', 0),
159-
[functions]: ts('#8ab1b0', 0),
158+
[variables]: ts('#6688cc', 0),
159+
[types]: ts('#ffeebb', TokenStyleBits.UNDERLINE),
160+
[functions]: ts('#ddbb88', 0),
160161
[strings]: ts('#22aa44', 0),
161162
[numbers]: ts('#f280d0', 0),
162-
[keywords]: ts('#98676a', 0)
163+
[keywords]: ts('#225588', 0)
163164
});
164165

165166
});
@@ -209,6 +210,7 @@ suite('Themes - TokenStyleResolving', () => {
209210
themeData.setCustomTokenColors(customTokenColors);
210211

211212
let tokenStyle;
213+
let defaultTokenStyle = new TokenStyle(themeData.getColor(editorForeground));
212214

213215
tokenStyle = themeData.findTokenStyleForScope(['variable']);
214216
assertTokenStyle(tokenStyle, ts('#F8F8F2', 0), 'variable');
@@ -217,13 +219,13 @@ suite('Themes - TokenStyleResolving', () => {
217219
assertTokenStyle(tokenStyle, ts('#F92672', TokenStyleBits.ITALIC | TokenStyleBits.BOLD | TokenStyleBits.UNDERLINE), 'keyword');
218220

219221
tokenStyle = themeData.findTokenStyleForScope(['keyword']);
220-
assertTokenStyle(tokenStyle, undefined, 'keyword');
222+
assertTokenStyle(tokenStyle, defaultTokenStyle, 'keyword');
221223

222224
tokenStyle = themeData.findTokenStyleForScope(['keyword.operator']);
223225
assertTokenStyle(tokenStyle, ts('#F92672', TokenStyleBits.ITALIC | TokenStyleBits.BOLD | TokenStyleBits.UNDERLINE), 'keyword.operator');
224226

225227
tokenStyle = themeData.findTokenStyleForScope(['keyword.operators']);
226-
assertTokenStyle(tokenStyle, undefined, 'keyword.operators');
228+
assertTokenStyle(tokenStyle, defaultTokenStyle, 'keyword.operators');
227229

228230
tokenStyle = themeData.findTokenStyleForScope(['storage']);
229231
assertTokenStyle(tokenStyle, ts('#F92672', TokenStyleBits.ITALIC), 'storage');

0 commit comments

Comments
 (0)