From 6c1d92d6c56185f88581c73617e647397fc79dd2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 31 Dec 2019 09:41:16 +0100 Subject: Rename file --- editors/code/src/highlighting.ts | 2 +- editors/code/src/load_theme_colors.ts | 108 ++++++++++++++++++++++++++++++++++ editors/code/src/scopes.ts | 108 ---------------------------------- editors/code/src/scopes_mapper.ts | 2 +- 4 files changed, 110 insertions(+), 110 deletions(-) create mode 100644 editors/code/src/load_theme_colors.ts delete mode 100644 editors/code/src/scopes.ts (limited to 'editors') diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts index 5e9cbe0de..e97eb086a 100644 --- a/editors/code/src/highlighting.ts +++ b/editors/code/src/highlighting.ts @@ -3,7 +3,7 @@ import * as lc from 'vscode-languageclient'; import * as seedrandom_ from 'seedrandom'; const seedrandom = seedrandom_; // https://github.com/jvandemo/generator-angular2-library/issues/221#issuecomment-355945207 -import { loadThemeColors, TextMateRuleSettings } from './scopes'; +import { loadThemeColors, TextMateRuleSettings } from './load_theme_colors'; import * as scopesMapper from './scopes_mapper'; import { Ctx } from './ctx'; diff --git a/editors/code/src/load_theme_colors.ts b/editors/code/src/load_theme_colors.ts new file mode 100644 index 000000000..73fabbf54 --- /dev/null +++ b/editors/code/src/load_theme_colors.ts @@ -0,0 +1,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 { + // 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 { + function isTheme(extension: vscode.Extension): 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 { + 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 { + 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, addition: Map) { + addition.forEach((value, key) => { + const merged = mergeRuleSettings(dst.get(key), value) + dst.set(key, merged) + }) +} diff --git a/editors/code/src/scopes.ts b/editors/code/src/scopes.ts deleted file mode 100644 index 73fabbf54..000000000 --- a/editors/code/src/scopes.ts +++ /dev/null @@ -1,108 +0,0 @@ -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 { - // 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 { - function isTheme(extension: vscode.Extension): 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 { - 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 { - 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, addition: Map) { - addition.forEach((value, key) => { - const merged = mergeRuleSettings(dst.get(key), value) - dst.set(key, merged) - }) -} diff --git a/editors/code/src/scopes_mapper.ts b/editors/code/src/scopes_mapper.ts index e738fa239..1295ab476 100644 --- a/editors/code/src/scopes_mapper.ts +++ b/editors/code/src/scopes_mapper.ts @@ -1,5 +1,5 @@ import * as vscode from 'vscode'; -import { TextMateRuleSettings } from './scopes'; +import { TextMateRuleSettings } from './load_theme_colors'; let mappings = new Map(); -- cgit v1.2.3