aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/scopes.ts
blob: 73fabbf549adb2fa154a27d86d7eafaff70421f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import * as fs from 'fs';
import * as jsonc from 'jsonc-parser';
import * as path from 'path';
import * as vscode from 'vscode';

export interface TextMateRuleSettings {
    foreground?: string;
    background?: string;
    fontStyle?: string;
}

// Load all textmate scopes in the currently active theme
export function loadThemeColors(): Map<string, TextMateRuleSettings> {
    // Find out current color theme
    const themeName = vscode.workspace
        .getConfiguration('workbench')
        .get('colorTheme');

    if (typeof themeName !== 'string') {
        // console.warn('workbench.colorTheme is', themeName)
        return new Map();
    }
    return loadThemeNamed(themeName);
}

function loadThemeNamed(themeName: string): Map<string, TextMateRuleSettings> {
    function isTheme(extension: vscode.Extension<any>): boolean {
        return (
            extension.extensionKind === vscode.ExtensionKind.UI &&
            extension.packageJSON.contributes &&
            extension.packageJSON.contributes.themes
        );
    }

    let themePaths = vscode.extensions.all
        .filter(isTheme)
        .flatMap(ext => {
            return ext.packageJSON.contributes.themes
                .filter((it: any) => (it.id || it.label) === themeName)
                .map((it: any) => path.join(ext.extensionPath, it.path));
        })

    const res = new Map();
    for (const themePath of themePaths) {
        mergeInto(res, loadThemeFile(themePath))
    }

    const customizations: any = vscode.workspace.getConfiguration('editor').get('tokenColorCustomizations');
    mergeInto(res, loadColors(customizations?.textMateRules ?? []))

    return res;
}

function loadThemeFile(themePath: string): Map<string, TextMateRuleSettings> {
    let text;
    try {
        text = fs.readFileSync(themePath, 'utf8')
    } catch {
        return new Map();
    }
    const obj = jsonc.parse(text);
    const tokenColors = obj?.tokenColors ?? [];
    const res = loadColors(tokenColors);

    for (const include in obj?.include ?? []) {
        const includePath = path.join(path.dirname(themePath), include);
        const tmp = loadThemeFile(includePath);
        mergeInto(res, tmp);
    }

    return res;
}

interface TextMateRule {
    scope: string | string[];
    settings: TextMateRuleSettings;
}

function loadColors(textMateRules: TextMateRule[]): Map<string, TextMateRuleSettings> {
    const res = new Map();
    for (const rule of textMateRules) {
        const scopes = typeof rule.scope === 'string'
            ? [rule.scope]
            : rule.scope;
        for (const scope of scopes) {
            res.set(scope, rule.settings)
        }
    }
    return res
}

function mergeRuleSettings(
    defaultSetting: TextMateRuleSettings | undefined,
    override: TextMateRuleSettings,
): TextMateRuleSettings {
    return {
        foreground: defaultSetting?.foreground ?? override.foreground,
        background: defaultSetting?.background ?? override.background,
        fontStyle: defaultSetting?.fontStyle ?? override.fontStyle,
    }
}

function mergeInto(dst: Map<string, TextMateRuleSettings>, addition: Map<string, TextMateRuleSettings>) {
    addition.forEach((value, key) => {
        const merged = mergeRuleSettings(dst.get(key), value)
        dst.set(key, merged)
    })
}