1 import * as fs from 'fs';
2 import * as jsonc from 'jsonc-parser';
3 import * as path from 'path';
4 import * as vscode from 'vscode';
6 export interface TextMateRule {
7 scope: string | string[];
8 settings: TextMateRuleSettings;
11 export interface TextMateRuleSettings {
17 // Current theme colors
18 const rules = new Map<string, TextMateRuleSettings>();
20 export function find(scope: string): TextMateRuleSettings | undefined {
21 return rules.get(scope);
24 // Load all textmate scopes in the currently active theme
25 export function load() {
26 // Remove any previous theme
28 // Find out current color theme
29 const themeName = vscode.workspace
30 .getConfiguration('workbench')
33 if (typeof themeName !== 'string') {
34 // console.warn('workbench.colorTheme is', themeName)
37 // Try to load colors from that theme
39 loadThemeNamed(themeName);
41 // console.warn('failed to load theme', themeName, e)
45 function filterThemeExtensions(extension: vscode.Extension<any>): boolean {
47 extension.extensionKind === vscode.ExtensionKind.UI &&
48 extension.packageJSON.contributes &&
49 extension.packageJSON.contributes.themes
53 // Find current theme on disk
54 function loadThemeNamed(themeName: string) {
55 const themePaths = vscode.extensions.all
56 .filter(filterThemeExtensions)
57 .reduce((list, extension) => {
58 return extension.packageJSON.contributes.themes
61 (element.id || element.label) === themeName,
63 .map((element: any) =>
64 path.join(extension.extensionPath, element.path),
69 themePaths.forEach(loadThemeFile);
71 const tokenColorCustomizations: [any] = [
73 .getConfiguration('editor')
74 .get('tokenColorCustomizations'),
77 tokenColorCustomizations
78 .filter(custom => custom && custom.textMateRules)
79 .map(custom => custom.textMateRules)
83 function loadThemeFile(themePath: string) {
84 const themeContent = [themePath]
85 .filter(it => fs.statSync(it).isFile())
86 .map(it => fs.readFileSync(it, 'utf8'))
87 .map(it => jsonc.parse(it))
88 .filter(theme => theme);
91 .filter(theme => theme.tokenColors)
92 .map(theme => theme.tokenColors)
96 .filter(theme => theme.include)
97 .map(theme => path.join(path.dirname(themePath), theme.include))
98 .forEach(loadThemeFile);
101 function mergeRuleSettings(
102 defaultSetting: TextMateRuleSettings | undefined,
103 override: TextMateRuleSettings,
104 ): TextMateRuleSettings {
105 if (defaultSetting === undefined) {
108 const mergedRule = defaultSetting;
110 mergedRule.background = override.background || defaultSetting.background;
111 mergedRule.foreground = override.foreground || defaultSetting.foreground;
112 mergedRule.fontStyle = override.fontStyle || defaultSetting.foreground;
117 function updateRules(
119 updatedSettings: TextMateRuleSettings,
122 .map(settings => mergeRuleSettings(settings, updatedSettings))
123 .forEach(settings => rules.set(scope, settings));
126 function loadColors(textMateRules: TextMateRule[]): void {
127 textMateRules.forEach(rule => {
128 if (typeof rule.scope === 'string') {
129 updateRules(rule.scope, rule.settings);
130 } else if (rule.scope instanceof Array) {
131 rule.scope.forEach(scope => updateRules(scope, rule.settings));