Skip to content

Commit a3143da

Browse files
committed
Make ITheme.getTokenStyleMetadata return an object to be able to express "inherit" (microsoft#89600)
1 parent bf15553 commit a3143da

8 files changed

Lines changed: 71 additions & 56 deletions

File tree

src/vs/editor/common/services/modelServiceImpl.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { Range } from 'vs/editor/common/core/range';
1414
import { DefaultEndOfLine, EndOfLinePreference, EndOfLineSequence, IIdentifiedSingleEditOperation, ITextBuffer, ITextBufferFactory, ITextModel, ITextModelCreationOptions } from 'vs/editor/common/model';
1515
import { TextModel, createTextBuffer } from 'vs/editor/common/model/textModel';
1616
import { IModelLanguageChangedEvent, IModelContentChangedEvent } from 'vs/editor/common/model/textModelEvents';
17-
import { LanguageIdentifier, DocumentSemanticTokensProviderRegistry, DocumentSemanticTokensProvider, SemanticTokensLegend, SemanticTokens, SemanticTokensEdits, TokenMetadata } from 'vs/editor/common/modes';
17+
import { LanguageIdentifier, DocumentSemanticTokensProviderRegistry, DocumentSemanticTokensProvider, SemanticTokensLegend, SemanticTokens, SemanticTokensEdits, TokenMetadata, FontStyle, MetadataConsts } from 'vs/editor/common/modes';
1818
import { PLAINTEXT_LANGUAGE_IDENTIFIER } from 'vs/editor/common/modes/modesRegistry';
1919
import { ILanguageSelection } from 'vs/editor/common/services/modeService';
2020
import { IModelService } from 'vs/editor/common/services/modelService';
@@ -629,7 +629,7 @@ class SemanticColoringProviderStyling {
629629

630630
public getMetadata(tokenTypeIndex: number, tokenModifierSet: number): number {
631631
const entry = this._hashTable.get(tokenTypeIndex, tokenModifierSet);
632-
let metadata: number | undefined;
632+
let metadata: number;
633633
if (entry) {
634634
metadata = entry.metadata;
635635
} else {
@@ -643,9 +643,21 @@ class SemanticColoringProviderStyling {
643643
modifierSet = modifierSet >> 1;
644644
}
645645

646-
metadata = this._themeService.getTheme().getTokenStyleMetadata(tokenType, tokenModifiers);
647-
if (typeof metadata === 'undefined') {
646+
const tokenStyle = this._themeService.getTheme().getTokenStyleMetadata(tokenType, tokenModifiers);
647+
if (typeof tokenStyle === 'undefined') {
648648
metadata = Constants.NO_STYLING;
649+
} else {
650+
const fontStyle = (
651+
(tokenStyle.italic ? FontStyle.Italic : 0)
652+
| (tokenStyle.bold ? FontStyle.Bold : 0)
653+
| (tokenStyle.underline ? FontStyle.Underline : 0)
654+
);
655+
const foreground = tokenStyle.foreground || 0;
656+
657+
metadata = (
658+
foreground << MetadataConsts.FOREGROUND_OFFSET
659+
| fontStyle << MetadataConsts.FONT_STYLE_OFFSET
660+
) >>> 0;
649661
}
650662
this._hashTable.add(tokenTypeIndex, tokenModifierSet, metadata);
651663
}

src/vs/editor/standalone/browser/standaloneThemeServiceImpl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { hc_black, vs, vs_dark } from 'vs/editor/standalone/common/themes';
1313
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
1414
import { Registry } from 'vs/platform/registry/common/platform';
1515
import { ColorIdentifier, Extensions, IColorRegistry } from 'vs/platform/theme/common/colorRegistry';
16-
import { Extensions as ThemingExtensions, ICssStyleCollector, IIconTheme, IThemingRegistry } from 'vs/platform/theme/common/themeService';
16+
import { Extensions as ThemingExtensions, ICssStyleCollector, IIconTheme, IThemingRegistry, ITokenStyle } from 'vs/platform/theme/common/themeService';
1717
import { IDisposable, Disposable } from 'vs/base/common/lifecycle';
1818

1919
const VS_THEME_NAME = 'vs';
@@ -131,7 +131,7 @@ class StandaloneTheme implements IStandaloneTheme {
131131
return this._tokenTheme;
132132
}
133133

134-
public getTokenStyleMetadata(type: string, modifiers: string[]): number | undefined {
134+
public getTokenStyleMetadata(type: string, modifiers: string[]): ITokenStyle | undefined {
135135
return undefined;
136136
}
137137

src/vs/editor/standalone/test/browser/standaloneLanguages.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { TokenTheme } from 'vs/editor/common/modes/supports/tokenization';
1212
import { ILineTokens, IToken, TokenizationSupport2Adapter, TokensProvider } from 'vs/editor/standalone/browser/standaloneLanguages';
1313
import { IStandaloneTheme, IStandaloneThemeData, IStandaloneThemeService } from 'vs/editor/standalone/common/standaloneThemeService';
1414
import { ColorIdentifier } from 'vs/platform/theme/common/colorRegistry';
15-
import { IIconTheme, ITheme, LIGHT } from 'vs/platform/theme/common/themeService';
15+
import { IIconTheme, ITheme, LIGHT, ITokenStyle } from 'vs/platform/theme/common/themeService';
1616

1717
suite('TokenizationSupport2Adapter', () => {
1818

@@ -56,7 +56,7 @@ suite('TokenizationSupport2Adapter', () => {
5656
throw new Error('Not implemented');
5757
},
5858

59-
getTokenStyleMetadata: (type: string, modifiers: string[]): number | undefined => {
59+
getTokenStyleMetadata: (type: string, modifiers: string[]): ITokenStyle | undefined => {
6060
return undefined;
6161
},
6262

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ export function getThemeTypeSelector(type: ThemeType): string {
7979
}
8080
}
8181

82+
export interface ITokenStyle {
83+
readonly foreground?: number;
84+
readonly bold?: boolean;
85+
readonly underline?: boolean;
86+
readonly italic?: boolean;
87+
}
88+
8289
export interface ITheme {
8390
readonly type: ThemeType;
8491

@@ -99,7 +106,7 @@ export interface ITheme {
99106
/**
100107
* Returns the token style for a given classification. The result uses the <code>MetadataConsts</code> format
101108
*/
102-
getTokenStyleMetadata(type: string, modifiers: string[]): number | undefined;
109+
getTokenStyleMetadata(type: string, modifiers: string[]): ITokenStyle | undefined;
103110

104111
/**
105112
* List of all colors used with tokens. <code>getTokenStyleMetadata</code> references the colors by index into this list.

src/vs/platform/theme/test/common/testThemeService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { Event, Emitter } from 'vs/base/common/event';
7-
import { IThemeService, ITheme, DARK, IIconTheme } from 'vs/platform/theme/common/themeService';
7+
import { IThemeService, ITheme, DARK, IIconTheme, ITokenStyle } from 'vs/platform/theme/common/themeService';
88
import { Color } from 'vs/base/common/color';
99

1010
export class TestTheme implements ITheme {
@@ -24,7 +24,7 @@ export class TestTheme implements ITheme {
2424
throw new Error('Method not implemented.');
2525
}
2626

27-
getTokenStyleMetadata(type: string, modifiers: string[]): number | undefined {
27+
getTokenStyleMetadata(type: string, modifiers: string[]): ITokenStyle | undefined {
2828
return undefined;
2929
}
3030

src/vs/workbench/contrib/codeEditor/browser/inspectEditorTokens/inspectEditorTokens.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { Position } from 'vs/editor/common/core/position';
1717
import { Range } from 'vs/editor/common/core/range';
1818
import { IEditorContribution } from 'vs/editor/common/editorCommon';
1919
import { ITextModel } from 'vs/editor/common/model';
20-
import { FontStyle, LanguageIdentifier, StandardTokenType, TokenMetadata, DocumentSemanticTokensProviderRegistry, SemanticTokensLegend, SemanticTokens } from 'vs/editor/common/modes';
20+
import { FontStyle, LanguageIdentifier, StandardTokenType, TokenMetadata, DocumentSemanticTokensProviderRegistry, SemanticTokensLegend, SemanticTokens, LanguageId } from 'vs/editor/common/modes';
2121
import { IModeService } from 'vs/editor/common/services/modeService';
2222
import { INotificationService } from 'vs/platform/notification/common/notification';
2323
import { editorHoverBackground, editorHoverBorder } from 'vs/platform/theme/common/colorRegistry';
@@ -474,9 +474,30 @@ class InspectEditorTokensWidget extends Disposable implements IContentWidget {
474474
const modifiers = semanticTokens.legend.tokenModifiers.filter((_, k) => modSet & 1 << k);
475475
const range = new Range(line + 1, character + 1, line + 1, character + 1 + len);
476476
const definitions = {};
477+
const colorMap = this._themeService.getColorTheme().tokenColorMap;
477478
const theme = this._themeService.getTheme() as ColorThemeData;
478-
const m = theme.getTokenStyleMetadata(type, modifiers, true, definitions);
479-
const metadata = this._decodeMetadata(m || 0);
479+
const tokenStyle = theme.getTokenStyleMetadata(type, modifiers, true, definitions);
480+
481+
let fontStyle = FontStyle.None;
482+
let foreground: string | undefined = undefined;
483+
if (tokenStyle) {
484+
fontStyle = (
485+
(tokenStyle.italic ? FontStyle.Italic : 0)
486+
| (tokenStyle.bold ? FontStyle.Bold : 0)
487+
| (tokenStyle.underline ? FontStyle.Underline : 0)
488+
);
489+
if (tokenStyle.foreground) {
490+
foreground = colorMap[tokenStyle.foreground];
491+
}
492+
}
493+
494+
const metadata: IDecodedMetadata = {
495+
languageIdentifier: this._modeService.getLanguageIdentifier(LanguageId.Null)!,
496+
tokenType: StandardTokenType.Other,
497+
fontStyle: this._fontStyleToString(fontStyle),
498+
foreground: foreground,
499+
};
500+
480501
return { type, modifiers, range, metadata, definitions };
481502
}
482503
lastLine = line;

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

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import * as types from 'vs/base/common/types';
1313
import * as objects from 'vs/base/common/objects';
1414
import * as resources from 'vs/base/common/resources';
1515
import { Extensions as ColorRegistryExtensions, IColorRegistry, ColorIdentifier, editorBackground, editorForeground } from 'vs/platform/theme/common/colorRegistry';
16-
import { ThemeType } from 'vs/platform/theme/common/themeService';
16+
import { ThemeType, ITokenStyle } from 'vs/platform/theme/common/themeService';
1717
import { Registry } from 'vs/platform/registry/common/platform';
1818
import { getParseErrorMessage } from 'vs/base/common/jsonErrorMessages';
1919
import { URI } from 'vs/base/common/uri';
@@ -22,7 +22,6 @@ import { startsWith } from 'vs/base/common/strings';
2222
import { TokenStyle, TokenClassification, ProbeScope, TokenStylingRule, getTokenClassificationRegistry, TokenStyleValue, TokenStyleData } from 'vs/platform/theme/common/tokenClassificationRegistry';
2323
import { MatcherWithPriority, Matcher, createMatchers } from 'vs/workbench/services/themes/common/textMateScopeMatcher';
2424
import { IExtensionResourceLoaderService } from 'vs/workbench/services/extensionResourceLoader/common/extensionResourceLoader';
25-
import { FontStyle, ColorId, MetadataConsts } from 'vs/editor/common/modes';
2625
import { CharCode } from 'vs/base/common/charCode';
2726

2827
let colorRegistry = Registry.as<IColorRegistry>(ColorRegistryExtensions.ColorContribution);
@@ -243,27 +242,22 @@ export class ColorThemeData implements IColorTheme {
243242
return this.getTokenColorIndex().asArray();
244243
}
245244

246-
public getTokenStyleMetadata(type: string, modifiers: string[], useDefault = true, definitions: TokenStyleDefinitions = {}): number | undefined {
245+
public getTokenStyleMetadata(type: string, modifiers: string[], useDefault = true, definitions: TokenStyleDefinitions = {}): ITokenStyle | undefined {
247246
const classification = tokenClassificationRegistry.getTokenClassification(type, modifiers);
248247
if (!classification) {
249248
return undefined;
250249
}
251250
const style = this.getTokenStyle(classification, useDefault, definitions);
252-
let fontStyle = FontStyle.None;
253-
let foreground = 0;
254-
if (style) {
255-
if (style.bold) {
256-
fontStyle |= FontStyle.Bold;
257-
}
258-
if (style.underline) {
259-
fontStyle |= FontStyle.Underline;
260-
}
261-
if (style.italic) {
262-
fontStyle |= FontStyle.Italic;
263-
}
264-
foreground = this.getTokenColorIndex().get(style.foreground);
251+
if (!style) {
252+
return undefined;
265253
}
266-
return toMetadata(fontStyle, foreground, 0);
254+
255+
return {
256+
foreground: this.getTokenColorIndex().get(style.foreground),
257+
bold: style.bold,
258+
underline: style.underline,
259+
italic: style.italic
260+
};
267261
}
268262

269263
public getTokenStylingRuleScope(rule: TokenStylingRule): 'setting' | 'theme' | undefined {
@@ -819,19 +813,3 @@ function hexUpper(charCode: CharCode): number {
819813
}
820814
return 0;
821815
}
822-
823-
function toMetadata(fontStyle: FontStyle, foreground: ColorId | number, background: ColorId | number) {
824-
const fontStyleBits = fontStyle << MetadataConsts.FONT_STYLE_OFFSET;
825-
const foregroundBits = foreground << MetadataConsts.FOREGROUND_OFFSET;
826-
const backgroundBits = background << MetadataConsts.BACKGROUND_OFFSET;
827-
if ((fontStyleBits & MetadataConsts.FONT_STYLE_MASK) !== fontStyleBits) {
828-
console.log(`Can not express fontStyle ${fontStyle} in metadata`);
829-
}
830-
if ((backgroundBits & MetadataConsts.BACKGROUND_MASK) !== backgroundBits) {
831-
console.log(`Can not express background ${background} in metadata`);
832-
}
833-
if ((foregroundBits & MetadataConsts.FOREGROUND_MASK) !== foregroundBits) {
834-
console.log(`Can not express foreground ${foreground} in metadata`);
835-
}
836-
return (fontStyleBits | foregroundBits | backgroundBits) >>> 0;
837-
}

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { Schemas } from 'vs/base/common/network';
1616
import { URI } from 'vs/base/common/uri';
1717
import { getPathFromAmdModule } from 'vs/base/common/amd';
1818
import { ExtensionResourceLoaderService } from 'vs/workbench/services/extensionResourceLoader/electron-browser/extensionResourceLoaderService';
19-
import { TokenMetadata, FontStyle } from 'vs/editor/common/modes';
19+
import { ITokenStyle } from 'vs/platform/theme/common/themeService';
2020

2121
let tokenClassificationRegistry = getTokenClassificationRegistry();
2222

@@ -49,24 +49,21 @@ function assertTokenStyle(actual: TokenStyle | undefined | null, expected: Token
4949
assert.equal(tokenStyleAsString(actual), tokenStyleAsString(expected), message);
5050
}
5151

52-
function assertTokenStyleMetaData(colorIndex: string[], actual: number | undefined, expected: TokenStyle | undefined | null, message?: string) {
52+
function assertTokenStyleMetaData(colorIndex: string[], actual: ITokenStyle | undefined, expected: TokenStyle | undefined | null, message?: string) {
5353
if (expected === undefined || expected === null || actual === undefined) {
5454
assert.equal(actual, expected, message);
5555
return;
5656
}
57-
const actualFontStyle = TokenMetadata.getFontStyle(actual);
58-
assert.equal((actualFontStyle & FontStyle.Bold) === FontStyle.Bold, expected.bold === true, 'bold');
59-
assert.equal((actualFontStyle & FontStyle.Italic) === FontStyle.Italic, expected.italic === true, 'italic');
60-
assert.equal((actualFontStyle & FontStyle.Underline) === FontStyle.Underline, expected.underline === true, 'underline');
57+
assert.strictEqual(actual.bold, expected.bold, 'bold');
58+
assert.strictEqual(actual.italic, expected.italic, 'italic');
59+
assert.strictEqual(actual.underline, expected.underline, 'underline');
6160

62-
const actualForegroundIndex = TokenMetadata.getForeground(actual);
61+
const actualForegroundIndex = actual.foreground;
6362
if (expected.foreground) {
6463
assert.equal(actualForegroundIndex, colorIndex.indexOf(Color.Format.CSS.formatHexA(expected.foreground, true).toUpperCase()), 'foreground');
6564
} else {
6665
assert.equal(actualForegroundIndex, 0, 'foreground');
6766
}
68-
const actualBackgroundIndex = TokenMetadata.getBackground(actual);
69-
assert.equal(actualBackgroundIndex, 0, 'background');
7067
}
7168

7269

0 commit comments

Comments
 (0)