aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/scopes.ts
diff options
context:
space:
mode:
authorSeivan Heidari <[email protected]>2019-11-09 16:23:30 +0000
committerSeivan Heidari <[email protected]>2019-11-09 16:23:30 +0000
commit83a33fbbeae9cbec8bec855e9338b7ccd08bd3a0 (patch)
tree7378bc3b85599264027eaab28ff017ed933fb984 /editors/code/src/scopes.ts
parent529b227d42951feabf64c8c964b00e726dd92d46 (diff)
Vscode wasn't running the linter automatically so ran `npm run fix` - wonder if it's related to `tslint` being deprecated.
Diffstat (limited to 'editors/code/src/scopes.ts')
-rw-r--r--editors/code/src/scopes.ts60
1 files changed, 36 insertions, 24 deletions
diff --git a/editors/code/src/scopes.ts b/editors/code/src/scopes.ts
index a6138fad0..98099872c 100644
--- a/editors/code/src/scopes.ts
+++ b/editors/code/src/scopes.ts
@@ -2,8 +2,6 @@ import * as fs from 'fs';
2import * as path from 'path'; 2import * as path from 'path';
3import * as vscode from 'vscode'; 3import * as vscode from 'vscode';
4 4
5
6
7export interface TextMateRule { 5export interface TextMateRule {
8 scope: string | string[]; 6 scope: string | string[];
9 settings: TextMateRuleSettings; 7 settings: TextMateRuleSettings;
@@ -27,7 +25,9 @@ export function load() {
27 // Remove any previous theme 25 // Remove any previous theme
28 rules.clear(); 26 rules.clear();
29 // Find out current color theme 27 // Find out current color theme
30 const themeName = vscode.workspace.getConfiguration('workbench').get('colorTheme'); 28 const themeName = vscode.workspace
29 .getConfiguration('workbench')
30 .get('colorTheme');
31 31
32 if (typeof themeName !== 'string') { 32 if (typeof themeName !== 'string') {
33 // console.warn('workbench.colorTheme is', themeName) 33 // console.warn('workbench.colorTheme is', themeName)
@@ -42,38 +42,43 @@ export function load() {
42} 42}
43 43
44function filterThemeExtensions(extension: vscode.Extension<any>): boolean { 44function filterThemeExtensions(extension: vscode.Extension<any>): boolean {
45 return extension.extensionKind === vscode.ExtensionKind.UI && 45 return (
46 extension.extensionKind === vscode.ExtensionKind.UI &&
46 extension.packageJSON.contributes && 47 extension.packageJSON.contributes &&
47 extension.packageJSON.contributes.themes; 48 extension.packageJSON.contributes.themes
49 );
48} 50}
49 51
50
51
52// Find current theme on disk 52// Find current theme on disk
53function loadThemeNamed(themeName: string) { 53function loadThemeNamed(themeName: string) {
54
55 const themePaths = vscode.extensions.all 54 const themePaths = vscode.extensions.all
56 .filter(filterThemeExtensions) 55 .filter(filterThemeExtensions)
57 .reduce((list, extension) => { 56 .reduce((list, extension) => {
58 return extension.packageJSON.contributes.themes 57 return extension.packageJSON.contributes.themes
59 .filter((element: any) => (element.id || element.label) === themeName) 58 .filter(
60 .map((element: any) => path.join(extension.extensionPath, element.path)) 59 (element: any) =>
61 .concat(list) 60 (element.id || element.label) === themeName
61 )
62 .map((element: any) =>
63 path.join(extension.extensionPath, element.path)
64 )
65 .concat(list);
62 }, Array<string>()); 66 }, Array<string>());
63 67
64
65 themePaths.forEach(loadThemeFile); 68 themePaths.forEach(loadThemeFile);
66 69
67 const tokenColorCustomizations: [any] = [vscode.workspace.getConfiguration('editor').get('tokenColorCustomizations')] 70 const tokenColorCustomizations: [any] = [
71 vscode.workspace
72 .getConfiguration('editor')
73 .get('tokenColorCustomizations')
74 ];
68 75
69 tokenColorCustomizations 76 tokenColorCustomizations
70 .filter(custom => custom && custom.textMateRules) 77 .filter(custom => custom && custom.textMateRules)
71 .map(custom => custom.textMateRules) 78 .map(custom => custom.textMateRules)
72 .forEach(loadColors); 79 .forEach(loadColors);
73
74} 80}
75 81
76
77function loadThemeFile(themePath: string) { 82function loadThemeFile(themePath: string) {
78 const themeContent = [themePath] 83 const themeContent = [themePath]
79 .filter(isFile) 84 .filter(isFile)
@@ -92,18 +97,26 @@ function loadThemeFile(themePath: string) {
92 .forEach(loadThemeFile); 97 .forEach(loadThemeFile);
93} 98}
94 99
95function mergeRuleSettings(defaultSetting: TextMateRuleSettings | undefined, override: TextMateRuleSettings): TextMateRuleSettings { 100function mergeRuleSettings(
96 if (defaultSetting === undefined) { return override; } 101 defaultSetting: TextMateRuleSettings | undefined,
102 override: TextMateRuleSettings
103): TextMateRuleSettings {
104 if (defaultSetting === undefined) {
105 return override;
106 }
97 const mergedRule = defaultSetting; 107 const mergedRule = defaultSetting;
98 108
99 mergedRule.background = override.background || defaultSetting.background; 109 mergedRule.background = override.background || defaultSetting.background;
100 mergedRule.foreground = override.foreground || defaultSetting.foreground; 110 mergedRule.foreground = override.foreground || defaultSetting.foreground;
101 mergedRule.fontStyle = override.fontStyle || defaultSetting.foreground; 111 mergedRule.fontStyle = override.fontStyle || defaultSetting.foreground;
102 112
103 return mergedRule 113 return mergedRule;
104} 114}
105 115
106function updateRules(scope: string, updatedSettings: TextMateRuleSettings): void { 116function updateRules(
117 scope: string,
118 updatedSettings: TextMateRuleSettings
119): void {
107 [rules.get(scope)] 120 [rules.get(scope)]
108 .map(settings => mergeRuleSettings(settings, updatedSettings)) 121 .map(settings => mergeRuleSettings(settings, updatedSettings))
109 .forEach(settings => rules.set(scope, settings)); 122 .forEach(settings => rules.set(scope, settings));
@@ -113,11 +126,10 @@ function loadColors(textMateRules: TextMateRule[]): void {
113 textMateRules.forEach(rule => { 126 textMateRules.forEach(rule => {
114 if (typeof rule.scope === 'string') { 127 if (typeof rule.scope === 'string') {
115 updateRules(rule.scope, rule.settings); 128 updateRules(rule.scope, rule.settings);
116 } 129 } else if (rule.scope instanceof Array) {
117 else if (rule.scope instanceof Array) {
118 rule.scope.forEach(scope => updateRules(scope, rule.settings)); 130 rule.scope.forEach(scope => updateRules(scope, rule.settings));
119 } 131 }
120 }) 132 });
121} 133}
122 134
123function isFile(filePath: string): boolean { 135function isFile(filePath: string): boolean {
@@ -128,7 +140,7 @@ function readFileText(filePath: string): string {
128 return fs.readFileSync(filePath, 'utf8'); 140 return fs.readFileSync(filePath, 'utf8');
129} 141}
130 142
131// Might need to replace with JSONC if a theme contains comments. 143// Might need to replace with JSONC if a theme contains comments.
132function parseJSON(content: string): any { 144function parseJSON(content: string): any {
133 return JSON.parse(content); 145 return JSON.parse(content);
134} \ No newline at end of file 146}