|
1 | | -import * as monaco from 'monaco-editor' |
2 | | -import { defineVSCodeTheme } from './registry' |
3 | | -import './semanticHighlight' |
4 | | -import { IVSCodeTheme } from './tools' |
| 1 | +import { setThemes, setDefaultThemes, IThemeExtensionPoint } from 'vscode/service-override/theme' |
| 2 | +import { Disposable } from 'vscode' |
5 | 3 | import defaultThemes from '../languages/extensions/themes.json' |
6 | 4 | import themeLoader from '../languages/extensions/themeLoader' |
7 | 5 |
|
8 | | -const DEFAULT_VSCODE_THEMES: Partial<Record<string, monaco.editor.BuiltinTheme>> = { |
9 | | - 'Default Light+': 'vs', |
10 | | - 'Default Dark+': 'vs-dark', |
11 | | - 'Default High Contrast': 'hc-black' |
12 | | - // 'Default High Contrast Light': 'hc-white' // FIXME: Uncomment me with monaco 0.34 which support hc-light theme |
| 6 | +interface DefaultTheme extends IThemeExtensionPoint { |
| 7 | + extension: string |
13 | 8 | } |
14 | | -// FIXME: Remove me with monaco 0.34 which support hc-light theme |
15 | | -const VSCODE_THEME_BLACKLIST = new Set(['Default High Contrast Light']) |
16 | 9 |
|
17 | | -function generateMonacoThemeId (vsCodeThemeId: string) { |
18 | | - return vsCodeThemeId.replace(/ /g, '-').toLowerCase().replace(/[^a-z0-9-]/g, '') |
| 10 | +interface VSCodeTheme extends IThemeExtensionPoint { |
| 11 | + load: () => Promise<string> |
19 | 12 | } |
20 | 13 |
|
21 | | -interface VSCodeTheme extends Omit<monaco.extra.IThemeExtensionPoint, '_watch'> { |
22 | | - extension: string |
| 14 | +setDefaultThemes((defaultThemes as DefaultTheme[]).map(themeExtensionPoint => ({ |
| 15 | + ...themeExtensionPoint, |
| 16 | + load: async () => JSON.stringify(await themeLoader[`${themeExtensionPoint.extension}:${themeExtensionPoint.path.slice(1)}`]!()) |
| 17 | +})), async (themeExtensionPoint) => themeExtensionPoint.load()) |
| 18 | + |
| 19 | +let themes: VSCodeTheme[] = [] |
| 20 | +function updateThemes () { |
| 21 | + setThemes(themes, async (themeExtensionPoint) => themeExtensionPoint.load()) |
23 | 22 | } |
| 23 | +updateThemes() |
24 | 24 |
|
25 | | -for (const themeExtensionPoint of (defaultThemes as VSCodeTheme[])) { |
26 | | - if (VSCODE_THEME_BLACKLIST.has(themeExtensionPoint.id)) { |
27 | | - continue |
| 25 | +function defineVSCodeTheme (id: string, load: () => Promise<string>): Disposable { |
| 26 | + const theme: VSCodeTheme = { |
| 27 | + id, |
| 28 | + path: `/${id}.json`, |
| 29 | + load, |
| 30 | + _watch: false |
28 | 31 | } |
29 | | - const loader = async (uri?: monaco.Uri): Promise<IVSCodeTheme> => { |
30 | | - return themeLoader[`${themeExtensionPoint.extension}:${uri?.path ?? themeExtensionPoint.path.slice(1)}`]!() |
| 32 | + themes = [ |
| 33 | + ...themes, |
| 34 | + theme |
| 35 | + ] |
| 36 | + updateThemes() |
| 37 | + return { |
| 38 | + dispose () { |
| 39 | + themes = themes.filter(_theme => _theme !== theme) |
| 40 | + updateThemes() |
| 41 | + } |
31 | 42 | } |
32 | | - |
33 | | - const monacoThemeId = DEFAULT_VSCODE_THEMES[themeExtensionPoint.id] ?? generateMonacoThemeId(themeExtensionPoint.id) |
34 | | - defineVSCodeTheme(monacoThemeId, loader, themeExtensionPoint, { |
35 | | - extensionName: themeExtensionPoint.extension, |
36 | | - extensionIsBuiltin: true |
37 | | - }).catch((error: Error) => { |
38 | | - monaco.errorHandler.onUnexpectedError(new Error(`Unable to define "${themeExtensionPoint.id}" vscode theme`, { |
39 | | - cause: error |
40 | | - })) |
41 | | - }) |
42 | 43 | } |
43 | 44 |
|
44 | 45 | export { |
|
0 commit comments