Skip to content

Commit

Permalink
[themes] opt-in to semanticHighlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
aeschli committed Mar 13, 2020
1 parent bf8fb5f commit 41a4adb
Show file tree
Hide file tree
Showing 23 changed files with 96 additions and 39 deletions.
3 changes: 2 additions & 1 deletion extensions/theme-abyss/themes/abyss-color-theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -434,5 +434,6 @@
"terminal.ansiBrightMagenta": "#d778ff",
"terminal.ansiBrightCyan": "#78ffff",
"terminal.ansiBrightWhite": "#ffffff"
}
},
"semanticHighlighting": true
}
5 changes: 3 additions & 2 deletions extensions/theme-defaults/themes/dark_defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
"menu.foreground": "#CCCCCC",
"statusBarItem.remoteForeground": "#FFF",
"statusBarItem.remoteBackground": "#16825D"
}
}
},
"semanticHighlighting": true
}
5 changes: 3 additions & 2 deletions extensions/theme-defaults/themes/hc_black_defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -337,5 +337,6 @@
"foreground": "#569cd6"
}
}
]
}
],
"semanticHighlighting": true
}
3 changes: 2 additions & 1 deletion extensions/theme-defaults/themes/light_defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
"settings.numberInputBorder": "#CECECE",
"statusBarItem.remoteForeground": "#FFF",
"statusBarItem.remoteBackground": "#16825D"
}
},
"semanticHighlighting": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -394,5 +394,6 @@
"foreground": "#dc3958"
}
}
]
],
"semanticHighlighting": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -572,5 +572,6 @@
"foreground": "#c7444a"
}
}
]
],
"semanticHighlighting": true
}
3 changes: 2 additions & 1 deletion extensions/theme-monokai/themes/monokai-color-theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -476,5 +476,6 @@
"foreground": "#FD971F"
}
}
]
],
"semanticHighlighting": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -494,5 +494,6 @@
"walkThrough.embeddedEditorBackground": "#00000014",
"editorIndentGuide.background": "#aaaaaa60",
"editorIndentGuide.activeBackground": "#777777b0"
}
},
"semanticHighlighting": true
}
3 changes: 2 additions & 1 deletion extensions/theme-red/themes/Red-color-theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -385,5 +385,6 @@
"foreground": "#ec0d1e"
}
}
]
],
"semanticHighlighting": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -477,5 +477,6 @@
"terminal.ansiBrightMagenta": "#6c71c4",
"terminal.ansiBrightCyan": "#93a1a1",
"terminal.ansiBrightWhite": "#fdf6e3"
}
},
"semanticHighlighting": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -484,5 +484,6 @@

// Interactive Playground
"walkThrough.embeddedEditorBackground": "#00000014"
}
},
"semanticHighlighting": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,6 @@
"foreground": "#b267e6"
}
}
]
],
"semanticHighlighting": true
}
37 changes: 21 additions & 16 deletions src/vs/editor/common/services/modelServiceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,15 +466,16 @@ class SemanticColoringFeature extends Disposable {

private _watchers: Record<string, ModelSemanticColoring>;
private _semanticStyling: SemanticStyling;
private _configurationService: IConfigurationService;

constructor(modelService: IModelService, themeService: IThemeService, configurationService: IConfigurationService, logService: ILogService) {
super();
this._configurationService = configurationService;
this._watchers = Object.create(null);
this._semanticStyling = this._register(new SemanticStyling(themeService, logService));

const isSemanticColoringEnabled = (model: ITextModel) => {
if (!themeService.getColorTheme().semanticHighlighting) {
return false;
}
const options = configurationService.getValue<IEditorSemanticHighlightingOptions>(SemanticColoringFeature.SETTING_ID, { overrideIdentifier: model.getLanguageIdentifier().language, resource: model.uri });
return options && options.enabled;
};
Expand All @@ -485,6 +486,20 @@ class SemanticColoringFeature extends Disposable {
modelSemanticColoring.dispose();
delete this._watchers[model.uri.toString()];
};
const handleSettingOrThemeChange = () => {
for (let model of modelService.getModels()) {
const curr = this._watchers[model.uri.toString()];
if (isSemanticColoringEnabled(model)) {
if (!curr) {
register(model);
}
} else {
if (curr) {
deregister(model, curr);
}
}
}
};
this._register(modelService.onModelAdded((model) => {
if (isSemanticColoringEnabled(model)) {
register(model);
Expand All @@ -496,22 +511,12 @@ class SemanticColoringFeature extends Disposable {
deregister(model, curr);
}
}));
this._configurationService.onDidChangeConfiguration(e => {
this._register(configurationService.onDidChangeConfiguration(e => {
if (e.affectsConfiguration(SemanticColoringFeature.SETTING_ID)) {
for (let model of modelService.getModels()) {
const curr = this._watchers[model.uri.toString()];
if (isSemanticColoringEnabled(model)) {
if (!curr) {
register(model);
}
} else {
if (curr) {
deregister(model, curr);
}
}
}
handleSettingOrThemeChange();
}
});
}));
this._register(themeService.onDidColorThemeChange(handleSettingOrThemeChange));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ class StandaloneTheme implements IStandaloneTheme {
public get tokenColorMap(): string[] {
return [];
}

public readonly semanticHighlighting = false;
}

function isBuiltinTheme(themeName: string): themeName is BuiltinTheme {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ suite('TokenizationSupport2Adapter', () => {
return undefined;
},

semanticHighlighting: false,

tokenColorMap: []
};
}
Expand Down
5 changes: 5 additions & 0 deletions src/vs/platform/theme/common/themeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ export interface IColorTheme {
* List of all colors used with tokens. <code>getTokenStyleMetadata</code> references the colors by index into this list.
*/
readonly tokenColorMap: string[];

/**
* Defines whether semantic highlighting should be enabled for the theme.
*/
readonly semanticHighlighting: boolean;
}

export interface IFileIconTheme {
Expand Down
2 changes: 2 additions & 0 deletions src/vs/platform/theme/test/common/testThemeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export class TestColorTheme implements IColorTheme {
return undefined;
}

readonly semanticHighlighting = false;

get tokenColorMap(): string[] {
return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ class InspectEditorTokensWidget extends Disposable implements IContentWidget {
}

private _isSemanticColoringEnabled() {
if (!this._themeService.getColorTheme().semanticHighlighting) {
return false;
}
const options = this._configurationService.getValue<IEditorSemanticHighlightingOptions>('editor.semanticHighlighting', { overrideIdentifier: this._model.getLanguageIdentifier().language, resource: this._model.uri });
return options && options.enabled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ function getMockTheme(type: ThemeType): IColorTheme {
getColor: (colorId: ColorIdentifier): Color | undefined => themingRegistry.resolveDefaultColor(colorId, theme),
defines: () => true,
getTokenStyleMetadata: () => undefined,
tokenColorMap: []

tokenColorMap: [],
semanticHighlighting: false
};
return theme;
}
Expand Down
28 changes: 22 additions & 6 deletions src/vs/workbench/services/themes/common/colorThemeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ export class ColorThemeData implements IWorkbenchColorTheme {
watch?: boolean;
extensionData?: ExtensionData;

private themeSemanticHighlighting: boolean;
private customSemanticHighlightSupport: boolean | undefined;

private themeTokenColors: ITextMateThemingRule[] = [];
private customTokenColors: ITextMateThemingRule[] = [];
private colorMap: IColorMap = {};
Expand All @@ -78,6 +81,12 @@ export class ColorThemeData implements IWorkbenchColorTheme {
this.label = label;
this.settingsId = settingsId;
this.isLoaded = false;
this.themeSemanticHighlighting = false;
this.customSemanticHighlightSupport = false;
}

get semanticHighlighting(): boolean {
return this.customSemanticHighlightSupport !== undefined ? this.customSemanticHighlightSupport : this.themeSemanticHighlighting;
}

get tokenColors(): ITextMateThemingRule[] {
Expand Down Expand Up @@ -360,6 +369,7 @@ export class ColorThemeData implements IWorkbenchColorTheme {

public setCustomTokenColors(customTokenColors: ITokenColorCustomizations) {
this.customTokenColors = [];
this.customSemanticHighlightSupport = undefined;

// first add the non-theme specific settings
this.addCustomTokenColors(customTokenColors);
Expand Down Expand Up @@ -411,6 +421,9 @@ export class ColorThemeData implements IWorkbenchColorTheme {
}
}
}
if (customTokenColors.semanticHighlighting !== undefined) {
this.customSemanticHighlightSupport = customTokenColors.semanticHighlighting;
}
}

public ensureLoaded(extensionResourceLoaderService: IExtensionResourceLoaderService): Promise<void> {
Expand All @@ -431,13 +444,15 @@ export class ColorThemeData implements IWorkbenchColorTheme {
const result = {
colors: {},
textMateRules: [],
stylingRules: undefined
stylingRules: undefined,
semanticHighlighting: false
};
return _loadColorTheme(extensionResourceLoaderService, this.location, result).then(_ => {
this.isLoaded = true;
this.tokenStylingRules = result.stylingRules;
this.colorMap = result.colors;
this.themeTokenColors = result.textMateRules;
this.themeSemanticHighlighting = result.semanticHighlighting;
});
}

Expand Down Expand Up @@ -562,7 +577,7 @@ function toCSSSelector(extensionId: string, path: string) {
return str;
}

function _loadColorTheme(extensionResourceLoaderService: IExtensionResourceLoaderService, themeLocation: URI, result: { textMateRules: ITextMateThemingRule[], colors: IColorMap, stylingRules: TokenStylingRule[] | undefined }): Promise<any> {
function _loadColorTheme(extensionResourceLoaderService: IExtensionResourceLoaderService, themeLocation: URI, result: { textMateRules: ITextMateThemingRule[], colors: IColorMap, stylingRules: TokenStylingRule[] | undefined, semanticHighlighting: boolean }): Promise<any> {
if (resources.extname(themeLocation) === '.json') {
return extensionResourceLoaderService.readExtensionResource(themeLocation).then(content => {
let errors: Json.ParseError[] = [];
Expand All @@ -581,6 +596,7 @@ function _loadColorTheme(extensionResourceLoaderService: IExtensionResourceLoade
convertSettings(contentValue.settings, result);
return null;
}
result.semanticHighlighting = result.semanticHighlighting || contentValue.semanticHighlighting;
let colors = contentValue.colors;
if (colors) {
if (typeof colors !== 'object') {
Expand All @@ -605,10 +621,10 @@ function _loadColorTheme(extensionResourceLoaderService: IExtensionResourceLoade
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);
}
// let tokenStylingRules = contentValue.tokenStylingRules;
// if (tokenStylingRules && typeof tokenStylingRules === 'object') {
// result.stylingRules = readCustomTokenStyleRules(tokenStylingRules, result.stylingRules);
// }
return null;
});
});
Expand Down
4 changes: 4 additions & 0 deletions src/vs/workbench/services/themes/common/colorThemeSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ const colorThemeSchema: IJSONSchema = {
$ref: textmateColorsSchemaId
}
]
},
semanticHighlighting: {
type: 'boolean',
description: nls.localize('schema.supportsSemanticHighlighting', 'Whether semantic highlighting should be enabled for this theme.')
}
}
};
Expand Down
5 changes: 5 additions & 0 deletions src/vs/workbench/services/themes/common/themeConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ const tokenColorSchema: IJSONSchema = {
textMateRules: {
description: nls.localize('editorColors.textMateRules', 'Sets colors and styles using textmate theming rules (advanced).'),
$ref: textmateColorsSchemaId
},
semanticHighlighting: {
description: nls.localize('editorColors.semanticHighlighting', 'Whether semantic highlighting should be enabled for this theme.'),
type: 'boolean'
}
}
};
Expand All @@ -154,6 +158,7 @@ const tokenColorCustomizationConfiguration: IConfigurationNode = {
[ThemeSettings.TOKEN_COLOR_CUSTOMIZATIONS_EXPERIMENTAL]: experimentalTokenStylingCustomizationSchema
}
};

configurationRegistry.registerConfiguration(tokenColorCustomizationConfiguration);

export function updateColorThemeConfigurationSchemas(themes: IWorkbenchColorTheme[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export interface IColorCustomizations {
}

export interface ITokenColorCustomizations {
[groupIdOrThemeSettingsId: string]: string | ITokenColorizationSetting | ITokenColorCustomizations | undefined | ITextMateThemingRule[];
[groupIdOrThemeSettingsId: string]: string | ITokenColorizationSetting | ITokenColorCustomizations | undefined | ITextMateThemingRule[] | boolean;
comments?: string | ITokenColorizationSetting;
strings?: string | ITokenColorizationSetting;
numbers?: string | ITokenColorizationSetting;
Expand All @@ -106,6 +106,7 @@ export interface ITokenColorCustomizations {
functions?: string | ITokenColorizationSetting;
variables?: string | ITokenColorizationSetting;
textMateRules?: ITextMateThemingRule[];
semanticHighlighting?: boolean;
}

export interface IExperimentalTokenStyleCustomizations {
Expand Down

0 comments on commit 41a4adb

Please sign in to comment.