forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththemeFinder.ts
More file actions
223 lines (195 loc) · 9.34 KB
/
themeFinder.ts
File metadata and controls
223 lines (195 loc) · 9.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import * as path from 'path';
import { EXTENSION_ROOT_DIR, PYTHON_LANGUAGE } from '../common/constants';
import { traceError } from '../common/logger';
import { IFileSystem } from '../common/platform/types';
import { ICurrentProcess, IExtensions } from '../common/types';
import { IThemeFinder } from './types';
// tslint:disable:no-any
interface IThemeData {
rootFile: string;
isDark: boolean;
}
@injectable()
export class ThemeFinder implements IThemeFinder {
private themeCache: { [key: string]: IThemeData | undefined } = {};
private languageCache: { [key: string]: string | undefined } = {};
constructor(
@inject(IExtensions) private extensions: IExtensions,
@inject(ICurrentProcess) private currentProcess: ICurrentProcess,
@inject(IFileSystem) private fs: IFileSystem
) {}
public async findThemeRootJson(themeName: string): Promise<string | undefined> {
// find our data
const themeData = await this.findThemeData(themeName);
// Use that data if it worked
if (themeData) {
return themeData.rootFile;
}
}
public async findTmLanguage(language: string): Promise<string | undefined> {
// See if already found it or not
if (!this.themeCache.hasOwnProperty(language)) {
try {
this.languageCache[language] = await this.findMatchingLanguage(language);
} catch (exc) {
traceError(exc);
}
}
return this.languageCache[language];
}
public async isThemeDark(themeName: string): Promise<boolean | undefined> {
// find our data
const themeData = await this.findThemeData(themeName);
// Use that data if it worked
if (themeData) {
return themeData.isDark;
}
}
private async findThemeData(themeName: string): Promise<IThemeData | undefined> {
// See if already found it or not
if (!this.themeCache.hasOwnProperty(themeName)) {
try {
this.themeCache[themeName] = await this.findMatchingTheme(themeName);
} catch (exc) {
traceError(exc);
}
}
return this.themeCache[themeName];
}
private async findMatchingLanguage(language: string): Promise<string | undefined> {
const currentExe = this.currentProcess.execPath;
let currentPath = path.dirname(currentExe);
// Should be somewhere under currentPath/resources/app/extensions inside of a json file
let extensionsPath = path.join(currentPath, 'resources', 'app', 'extensions');
if (!(await this.fs.directoryExists(extensionsPath))) {
// Might be on mac or linux. try a different path
currentPath = path.resolve(currentPath, '../../../..');
extensionsPath = path.join(currentPath, 'resources', 'app', 'extensions');
}
// Search through all of the files in this folder
let results = await this.findMatchingLanguages(language, extensionsPath);
// If that didn't work, see if it's our MagicPython predefined tmLanguage
if (!results && language === PYTHON_LANGUAGE) {
results = await this.fs.readFile(path.join(EXTENSION_ROOT_DIR, 'resources', 'MagicPython.tmLanguage.json'));
}
return results;
}
private async findMatchingLanguages(language: string, rootPath: string): Promise<string | undefined> {
// Environment variable to mimic missing json problem
if (process.env.VSC_PYTHON_MIMIC_REMOTE) {
return undefined;
}
// Search through all package.json files in the directory and below, looking
// for the themeName in them.
const foundPackages = await this.fs.search('**/package.json', rootPath);
if (foundPackages.length > 0) {
// For each one, open it up and look for the theme name.
for (const f of foundPackages) {
const fpath = path.join(rootPath, f);
const data = await this.findMatchingLanguageFromJson(fpath, language);
if (data) {
return data;
}
}
}
}
private async findMatchingTheme(themeName: string): Promise<IThemeData | undefined> {
// Environment variable to mimic missing json problem
if (process.env.VSC_PYTHON_MIMIC_REMOTE) {
return undefined;
}
// Look through all extensions to find the theme. This will search
// the default extensions folder and our installed extensions.
const extensions = this.extensions.all;
for (const e of extensions) {
const result = await this.findMatchingThemeFromJson(path.join(e.extensionPath, 'package.json'), themeName);
if (result) {
return result;
}
}
// If didn't find in the extensions folder, then try searching manually. This shouldn't happen, but
// this is our backup plan in case vscode changes stuff.
const currentExe = this.currentProcess.execPath;
let currentPath = path.dirname(currentExe);
// Should be somewhere under currentPath/resources/app/extensions inside of a json file
let extensionsPath = path.join(currentPath, 'resources', 'app', 'extensions');
if (!(await this.fs.directoryExists(extensionsPath))) {
// Might be on mac or linux. try a different path
currentPath = path.resolve(currentPath, '../../../..');
extensionsPath = path.join(currentPath, 'resources', 'app', 'extensions');
}
const other = await this.findMatchingThemes(extensionsPath, themeName);
if (other) {
return other;
}
}
private async findMatchingThemes(rootPath: string, themeName: string): Promise<IThemeData | undefined> {
// Search through all package.json files in the directory and below, looking
// for the themeName in them.
const foundPackages = await this.fs.search('**/package.json', rootPath);
if (foundPackages.length > 0) {
// For each one, open it up and look for the theme name.
for (const f of foundPackages) {
const fpath = path.join(rootPath, f);
const data = await this.findMatchingThemeFromJson(fpath, themeName);
if (data) {
return data;
}
}
}
}
private async findMatchingLanguageFromJson(packageJson: string, language: string): Promise<string | undefined> {
// Read the contents of the json file
const text = await this.fs.readFile(packageJson);
const json = JSON.parse(text);
// Should have a name entry and a contributes entry
if (json.hasOwnProperty('name') && json.hasOwnProperty('contributes')) {
// See if contributes has a grammars
const contributes = json.contributes;
if (contributes.hasOwnProperty('grammars')) {
const grammars = contributes.grammars as any[];
// Go through each theme, seeing if the label matches our theme name
for (const t of grammars) {
if (t.hasOwnProperty('language') && t.language === language) {
// Path is relative to the package.json file.
const rootFile = t.hasOwnProperty('path')
? path.join(path.dirname(packageJson), t.path.toString())
: '';
return this.fs.readFile(rootFile);
}
}
}
}
}
private async findMatchingThemeFromJson(packageJson: string, themeName: string): Promise<IThemeData | undefined> {
// Read the contents of the json file
const text = await this.fs.readFile(packageJson);
const json = JSON.parse(text);
// Should have a name entry and a contributes entry
if (json.hasOwnProperty('name') && json.hasOwnProperty('contributes')) {
// See if contributes has a theme
const contributes = json.contributes;
if (contributes.hasOwnProperty('themes')) {
const themes = contributes.themes as any[];
// Go through each theme, seeing if the label matches our theme name
for (const t of themes) {
if (
(t.hasOwnProperty('label') && t.label === themeName) ||
(t.hasOwnProperty('id') && t.id === themeName)
) {
const isDark = t.hasOwnProperty('uiTheme') && t.uiTheme === 'vs-dark';
// Path is relative to the package.json file.
const rootFile = t.hasOwnProperty('path')
? path.join(path.dirname(packageJson), t.path.toString())
: '';
return { isDark, rootFile };
}
}
}
}
}
}