Adding better debugging for testing themes missing tags and which scopes didn't map.

Since this file is no longer being pushed upstream, double down on monads.
This commit is contained in:
Seivan Heidari 2019-10-31 10:36:54 +01:00
parent 8edda0e7b1
commit 1f822c8e51
3 changed files with 58 additions and 45 deletions

View File

@ -73,7 +73,10 @@ export class Highlighter {
return [tag, decor]; return [tag, decor];
} }
else { else {
console.log('Missing theme for: ' + tag); console.log(' ');
console.log('Missing theme for: <"' + tag + '"> for following mapped scopes:')
console.log(scopesMapper.find(tag))
console.log(' ');
const color = new vscode.ThemeColor('ralsp.' + tag); const color = new vscode.ThemeColor('ralsp.' + tag);
const decor = vscode.window.createTextEditorDecorationType({ const decor = vscode.window.createTextEditorDecorationType({
color, color,

View File

@ -41,61 +41,72 @@ export function load() {
} }
} }
function filterThemeExtensions(extension: vscode.Extension<any>): boolean {
return extension.extensionKind === vscode.ExtensionKind.UI &&
extension.packageJSON.contributes &&
extension.packageJSON.contributes.themes
}
// Find current theme on disk // Find current theme on disk
function loadThemeNamed(themeName: string) { function loadThemeNamed(themeName: string) {
const themePaths = vscode.extensions.all const themePaths = vscode.extensions.all
.filter(extension => extension.extensionKind === vscode.ExtensionKind.UI) .filter(filterThemeExtensions)
.filter(extension => extension.packageJSON.contributes)
.filter(extension => extension.packageJSON.contributes.themes)
.reduce((list, extension) => { .reduce((list, extension) => {
const paths = extension.packageJSON.contributes.themes const paths = extension.packageJSON.contributes.themes
.filter((element: any) => (element.id || element.label) === themeName) .filter((element: any) => (element.id || element.label) === themeName)
.map((element: any) => path.join(extension.extensionPath, element.path)) .map((element: any) => path.join(extension.extensionPath, element.path))
return list.concat(paths) return list.concat(paths)
}, Array<string>()); }, Array<string>())
themePaths.forEach(loadThemeFile); themePaths.forEach(loadThemeFile)
const tokenColorCustomizations: [any] = [vscode.workspace.getConfiguration('editor').get('tokenColorCustomizations')]
tokenColorCustomizations
.filter(custom => custom && custom.textMateRules)
.map(custom => custom.textMateRules)
.forEach(loadColors)
const customization: any = vscode.workspace.getConfiguration('editor').get('tokenColorCustomizations');
if (customization && customization.textMateRules) {
loadColors(customization.textMateRules)
}
} }
function loadThemeFile(themePath: string) { function loadThemeFile(themePath: string) {
const themeContent = [themePath]
.filter(isFile)
.map(readFileText)
.map(parseJSON)
.filter(theme => theme)
if (checkFileExists(themePath)) { themeContent
const themeContentText: string = readFileText(themePath) .filter(theme => theme.tokenColors)
.map(theme => theme.tokenColors)
.forEach(loadColors)
const themeContent: any = JSON.parse(themeContentText) themeContent
.filter(theme => theme.include)
if (themeContent && themeContent.tokenColors) { .map(theme => path.join(path.dirname(themePath), theme.include))
loadColors(themeContent.tokenColors) .forEach(loadThemeFile)
if (themeContent.include) {
// parse included theme file
const includedThemePath: string = path.join(path.dirname(themePath), themeContent.include)
loadThemeFile(includedThemePath)
}
}
}
} }
function mergeRuleSettings(defaultSetting: TextMateRuleSettings, override: TextMateRuleSettings): TextMateRuleSettings { function mergeRuleSettings(defaultSetting: TextMateRuleSettings, override: TextMateRuleSettings): TextMateRuleSettings {
const mergedRule = defaultSetting; const mergedRule = defaultSetting
mergedRule.background = override.background || defaultSetting.background mergedRule.background = override.background || defaultSetting.background
mergedRule.foreground = override.foreground || defaultSetting.foreground mergedRule.foreground = override.foreground || defaultSetting.foreground
mergedRule.fontStyle = override.fontStyle || defaultSetting.foreground; mergedRule.fontStyle = override.fontStyle || defaultSetting.foreground
return mergedRule; return mergedRule
} }
function loadColors(textMateRules: TextMateRule[]): void { function loadColors(textMateRules: TextMateRule[]): void {
for (const rule of textMateRules) { for (const rule of textMateRules) {
if (typeof rule.scope === 'string') { if (typeof rule.scope === 'string') {
const existingRule = rules.get(rule.scope); const existingRule = rules.get(rule.scope)
if (existingRule) { if (existingRule) {
rules.set(rule.scope, mergeRuleSettings(existingRule, rule.settings)) rules.set(rule.scope, mergeRuleSettings(existingRule, rule.settings))
} }
@ -104,7 +115,7 @@ function loadColors(textMateRules: TextMateRule[]): void {
} }
} else if (rule.scope instanceof Array) { } else if (rule.scope instanceof Array) {
for (const scope of rule.scope) { for (const scope of rule.scope) {
const existingRule = rules.get(scope); const existingRule = rules.get(scope)
if (existingRule) { if (existingRule) {
rules.set(scope, mergeRuleSettings(existingRule, rule.settings)) rules.set(scope, mergeRuleSettings(existingRule, rule.settings))
} }
@ -116,19 +127,15 @@ function loadColors(textMateRules: TextMateRule[]): void {
} }
} }
function checkFileExists(filePath: string): boolean { function isFile(filePath: string): boolean {
return [filePath].map(fs.statSync).every(stat => stat.isFile())
const stats = fs.statSync(filePath);
if (stats && stats.isFile()) {
return true;
} else {
// console.warn('no such file', filePath)
return false;
}
} }
function readFileText(filePath: string, encoding: string = 'utf8'): string { function readFileText(filePath: string): string {
return fs.readFileSync(filePath, encoding); return fs.readFileSync(filePath, 'utf8')
}
// Might need to replace with JSONC if a theme contains comments.
function parseJSON(content: string): any {
return JSON.parse(content)
} }

View File

@ -28,21 +28,24 @@ const defaultMapping = new Map<string, string[]>([
['module', ['entity.name.section', 'entity.other']] ['module', ['entity.name.section', 'entity.other']]
] ]
) )
function find(scope: string): string[] {
// Temporary exported for debugging for now.
export function find(scope: string): string[] {
return mappings.get(scope) || [] return mappings.get(scope) || []
} }
export function toRule(scope: string, intoRule: (scope: string) => TextMateRuleSettings | undefined): TextMateRuleSettings | undefined { export function toRule(scope: string, intoRule: (scope: string) => TextMateRuleSettings | undefined): TextMateRuleSettings | undefined {
return find(scope).map(intoRule).filter(rule => rule !== undefined)[0]; return find(scope).map(intoRule).filter(rule => rule !== undefined)[0]
} }
export function load() { export function load() {
const configuration = vscode.workspace const configuration = vscode.workspace
.getConfiguration('rust-analyzer') .getConfiguration('rust-analyzer')
.get('scopeMappings') as Map<string, string[]> | undefined || new Map() .get('scopeMappings') as Map<string, string[]> | undefined
|| new Map()
mappings = new Map([...Array.from(defaultMapping.entries()), ...Array.from(configuration.entries())]); mappings = new Map([...Array.from(defaultMapping.entries()), ...Array.from(configuration.entries())])
} }