diff options
author | Seivan Heidari <[email protected]> | 2019-10-31 09:36:54 +0000 |
---|---|---|
committer | Seivan Heidari <[email protected]> | 2019-10-31 09:36:54 +0000 |
commit | 1f822c8e518132b01d7eda665f6bf7bc254780a7 (patch) | |
tree | fd36b1b6d88548ae630562febca9ad1426dbf42e /editors/code/src | |
parent | 8edda0e7b164009d6c03bb3d4be603fb38ad2e2a (diff) |
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.
Diffstat (limited to 'editors/code/src')
-rw-r--r-- | editors/code/src/highlighting.ts | 5 | ||||
-rw-r--r-- | editors/code/src/scopes.ts | 87 | ||||
-rw-r--r-- | editors/code/src/scopes_mapper.ts | 11 |
3 files changed, 58 insertions, 45 deletions
diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts index b7dffaff5..dad99254e 100644 --- a/editors/code/src/highlighting.ts +++ b/editors/code/src/highlighting.ts | |||
@@ -73,7 +73,10 @@ export class Highlighter { | |||
73 | return [tag, decor]; | 73 | return [tag, decor]; |
74 | } | 74 | } |
75 | else { | 75 | else { |
76 | console.log('Missing theme for: ' + tag); | 76 | console.log(' '); |
77 | console.log('Missing theme for: <"' + tag + '"> for following mapped scopes:') | ||
78 | console.log(scopesMapper.find(tag)) | ||
79 | console.log(' '); | ||
77 | const color = new vscode.ThemeColor('ralsp.' + tag); | 80 | const color = new vscode.ThemeColor('ralsp.' + tag); |
78 | const decor = vscode.window.createTextEditorDecorationType({ | 81 | const decor = vscode.window.createTextEditorDecorationType({ |
79 | color, | 82 | color, |
diff --git a/editors/code/src/scopes.ts b/editors/code/src/scopes.ts index 470ee716f..76cbbd84e 100644 --- a/editors/code/src/scopes.ts +++ b/editors/code/src/scopes.ts | |||
@@ -41,61 +41,72 @@ export function load() { | |||
41 | } | 41 | } |
42 | } | 42 | } |
43 | 43 | ||
44 | function filterThemeExtensions(extension: vscode.Extension<any>): boolean { | ||
45 | return extension.extensionKind === vscode.ExtensionKind.UI && | ||
46 | extension.packageJSON.contributes && | ||
47 | extension.packageJSON.contributes.themes | ||
48 | } | ||
49 | |||
50 | |||
51 | |||
44 | // Find current theme on disk | 52 | // Find current theme on disk |
45 | function loadThemeNamed(themeName: string) { | 53 | function loadThemeNamed(themeName: string) { |
46 | 54 | ||
47 | const themePaths = vscode.extensions.all | 55 | const themePaths = vscode.extensions.all |
48 | .filter(extension => extension.extensionKind === vscode.ExtensionKind.UI) | 56 | .filter(filterThemeExtensions) |
49 | .filter(extension => extension.packageJSON.contributes) | ||
50 | .filter(extension => extension.packageJSON.contributes.themes) | ||
51 | .reduce((list, extension) => { | 57 | .reduce((list, extension) => { |
52 | const paths = extension.packageJSON.contributes.themes | 58 | const paths = extension.packageJSON.contributes.themes |
53 | .filter((element: any) => (element.id || element.label) === themeName) | 59 | .filter((element: any) => (element.id || element.label) === themeName) |
54 | .map((element: any) => path.join(extension.extensionPath, element.path)) | 60 | .map((element: any) => path.join(extension.extensionPath, element.path)) |
55 | return list.concat(paths) | 61 | return list.concat(paths) |
56 | }, Array<string>()); | 62 | }, Array<string>()) |
57 | 63 | ||
58 | 64 | ||
59 | themePaths.forEach(loadThemeFile); | 65 | themePaths.forEach(loadThemeFile) |
60 | 66 | ||
61 | const customization: any = vscode.workspace.getConfiguration('editor').get('tokenColorCustomizations'); | 67 | const tokenColorCustomizations: [any] = [vscode.workspace.getConfiguration('editor').get('tokenColorCustomizations')] |
62 | if (customization && customization.textMateRules) { | ||
63 | loadColors(customization.textMateRules) | ||
64 | } | ||
65 | } | ||
66 | 68 | ||
67 | function loadThemeFile(themePath: string) { | 69 | tokenColorCustomizations |
70 | .filter(custom => custom && custom.textMateRules) | ||
71 | .map(custom => custom.textMateRules) | ||
72 | .forEach(loadColors) | ||
68 | 73 | ||
69 | if (checkFileExists(themePath)) { | 74 | } |
70 | const themeContentText: string = readFileText(themePath) | ||
71 | 75 | ||
72 | const themeContent: any = JSON.parse(themeContentText) | ||
73 | 76 | ||
74 | if (themeContent && themeContent.tokenColors) { | 77 | function loadThemeFile(themePath: string) { |
75 | loadColors(themeContent.tokenColors) | 78 | const themeContent = [themePath] |
76 | if (themeContent.include) { | 79 | .filter(isFile) |
77 | // parse included theme file | 80 | .map(readFileText) |
78 | const includedThemePath: string = path.join(path.dirname(themePath), themeContent.include) | 81 | .map(parseJSON) |
79 | loadThemeFile(includedThemePath) | 82 | .filter(theme => theme) |
80 | } | 83 | |
81 | } | 84 | themeContent |
82 | } | 85 | .filter(theme => theme.tokenColors) |
86 | .map(theme => theme.tokenColors) | ||
87 | .forEach(loadColors) | ||
88 | |||
89 | themeContent | ||
90 | .filter(theme => theme.include) | ||
91 | .map(theme => path.join(path.dirname(themePath), theme.include)) | ||
92 | .forEach(loadThemeFile) | ||
83 | } | 93 | } |
94 | |||
84 | function mergeRuleSettings(defaultSetting: TextMateRuleSettings, override: TextMateRuleSettings): TextMateRuleSettings { | 95 | function mergeRuleSettings(defaultSetting: TextMateRuleSettings, override: TextMateRuleSettings): TextMateRuleSettings { |
85 | const mergedRule = defaultSetting; | 96 | const mergedRule = defaultSetting |
86 | 97 | ||
87 | mergedRule.background = override.background || defaultSetting.background | 98 | mergedRule.background = override.background || defaultSetting.background |
88 | mergedRule.foreground = override.foreground || defaultSetting.foreground | 99 | mergedRule.foreground = override.foreground || defaultSetting.foreground |
89 | mergedRule.fontStyle = override.fontStyle || defaultSetting.foreground; | 100 | mergedRule.fontStyle = override.fontStyle || defaultSetting.foreground |
90 | 101 | ||
91 | return mergedRule; | 102 | return mergedRule |
92 | } | 103 | } |
93 | 104 | ||
94 | function loadColors(textMateRules: TextMateRule[]): void { | 105 | function loadColors(textMateRules: TextMateRule[]): void { |
95 | for (const rule of textMateRules) { | 106 | for (const rule of textMateRules) { |
96 | 107 | ||
97 | if (typeof rule.scope === 'string') { | 108 | if (typeof rule.scope === 'string') { |
98 | const existingRule = rules.get(rule.scope); | 109 | const existingRule = rules.get(rule.scope) |
99 | if (existingRule) { | 110 | if (existingRule) { |
100 | rules.set(rule.scope, mergeRuleSettings(existingRule, rule.settings)) | 111 | rules.set(rule.scope, mergeRuleSettings(existingRule, rule.settings)) |
101 | } | 112 | } |
@@ -104,7 +115,7 @@ function loadColors(textMateRules: TextMateRule[]): void { | |||
104 | } | 115 | } |
105 | } else if (rule.scope instanceof Array) { | 116 | } else if (rule.scope instanceof Array) { |
106 | for (const scope of rule.scope) { | 117 | for (const scope of rule.scope) { |
107 | const existingRule = rules.get(scope); | 118 | const existingRule = rules.get(scope) |
108 | if (existingRule) { | 119 | if (existingRule) { |
109 | rules.set(scope, mergeRuleSettings(existingRule, rule.settings)) | 120 | rules.set(scope, mergeRuleSettings(existingRule, rule.settings)) |
110 | } | 121 | } |
@@ -116,19 +127,15 @@ function loadColors(textMateRules: TextMateRule[]): void { | |||
116 | } | 127 | } |
117 | } | 128 | } |
118 | 129 | ||
119 | function checkFileExists(filePath: string): boolean { | 130 | function isFile(filePath: string): boolean { |
120 | 131 | return [filePath].map(fs.statSync).every(stat => stat.isFile()) | |
121 | const stats = fs.statSync(filePath); | 132 | } |
122 | if (stats && stats.isFile()) { | ||
123 | return true; | ||
124 | } else { | ||
125 | // console.warn('no such file', filePath) | ||
126 | return false; | ||
127 | } | ||
128 | |||
129 | 133 | ||
134 | function readFileText(filePath: string): string { | ||
135 | return fs.readFileSync(filePath, 'utf8') | ||
130 | } | 136 | } |
131 | 137 | ||
132 | function readFileText(filePath: string, encoding: string = 'utf8'): string { | 138 | // Might need to replace with JSONC if a theme contains comments. |
133 | return fs.readFileSync(filePath, encoding); | 139 | function parseJSON(content: string): any { |
140 | return JSON.parse(content) | ||
134 | } \ No newline at end of file | 141 | } \ No newline at end of file |
diff --git a/editors/code/src/scopes_mapper.ts b/editors/code/src/scopes_mapper.ts index 4534d8a32..19a4213d4 100644 --- a/editors/code/src/scopes_mapper.ts +++ b/editors/code/src/scopes_mapper.ts | |||
@@ -28,21 +28,24 @@ const defaultMapping = new Map<string, string[]>([ | |||
28 | ['module', ['entity.name.section', 'entity.other']] | 28 | ['module', ['entity.name.section', 'entity.other']] |
29 | ] | 29 | ] |
30 | ) | 30 | ) |
31 | function find(scope: string): string[] { | 31 | |
32 | // Temporary exported for debugging for now. | ||
33 | export function find(scope: string): string[] { | ||
32 | return mappings.get(scope) || [] | 34 | return mappings.get(scope) || [] |
33 | } | 35 | } |
34 | 36 | ||
35 | export function toRule(scope: string, intoRule: (scope: string) => TextMateRuleSettings | undefined): TextMateRuleSettings | undefined { | 37 | export function toRule(scope: string, intoRule: (scope: string) => TextMateRuleSettings | undefined): TextMateRuleSettings | undefined { |
36 | return find(scope).map(intoRule).filter(rule => rule !== undefined)[0]; | 38 | return find(scope).map(intoRule).filter(rule => rule !== undefined)[0] |
37 | } | 39 | } |
38 | 40 | ||
39 | 41 | ||
40 | export function load() { | 42 | export function load() { |
41 | const configuration = vscode.workspace | 43 | const configuration = vscode.workspace |
42 | .getConfiguration('rust-analyzer') | 44 | .getConfiguration('rust-analyzer') |
43 | .get('scopeMappings') as Map<string, string[]> | undefined || new Map() | 45 | .get('scopeMappings') as Map<string, string[]> | undefined |
46 | || new Map() | ||
44 | 47 | ||
45 | mappings = new Map([...Array.from(defaultMapping.entries()), ...Array.from(configuration.entries())]); | 48 | mappings = new Map([...Array.from(defaultMapping.entries()), ...Array.from(configuration.entries())]) |
46 | 49 | ||
47 | 50 | ||
48 | } \ No newline at end of file | 51 | } \ No newline at end of file |