aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/scopes.ts
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code/src/scopes.ts')
-rw-r--r--editors/code/src/scopes.ts87
1 files changed, 47 insertions, 40 deletions
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
44function 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
45function loadThemeNamed(themeName: string) { 53function 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
67function 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) { 77function 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
84function mergeRuleSettings(defaultSetting: TextMateRuleSettings, override: TextMateRuleSettings): TextMateRuleSettings { 95function 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
94function loadColors(textMateRules: TextMateRule[]): void { 105function 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
119function checkFileExists(filePath: string): boolean { 130function 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
134function readFileText(filePath: string): string {
135 return fs.readFileSync(filePath, 'utf8')
130} 136}
131 137
132function 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); 139function parseJSON(content: string): any {
140 return JSON.parse(content)
134} \ No newline at end of file 141} \ No newline at end of file