-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.ts
82 lines (71 loc) · 2.17 KB
/
registry.ts
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
import { IGrammar, Registry as TextMateRegistry } from "./deps.ts";
import {
ILanguageRegistration,
IShikiTheme,
IThemeRegistration,
} from "./types.ts";
import { fetchTheme, resolvePath, toShikiTheme } from "./loader.ts";
import { Theme } from "./themes.ts";
import { Resolver } from "./resolver.ts";
import { Lang } from "./languages.ts";
const THEMES_PATH = resolvePath("themes/");
export class Registry extends TextMateRegistry {
public themesPath = THEMES_PATH;
private _resolvedThemes: Record<string, IShikiTheme> = {};
private _resolvedGrammars: Record<string, IGrammar> = {};
constructor(private _resolver: Resolver) {
super(_resolver);
}
public getTheme(theme: Theme | IShikiTheme | string) {
if (typeof theme === "string") {
return this._resolvedThemes[theme];
} else {
return theme;
}
}
public async loadTheme(theme: Theme | IShikiTheme | string) {
if (typeof theme === "string") {
if (!this._resolvedThemes[theme]) {
this._resolvedThemes[theme] = await fetchTheme(
`${this.themesPath}/${theme}.json`,
);
}
return this._resolvedThemes[theme];
} else {
theme = toShikiTheme(theme);
if (theme.name) {
this._resolvedThemes[theme.name] = theme;
}
return theme;
}
}
public async loadThemes(themes: IThemeRegistration[]) {
return await Promise.all(themes.map((theme) => this.loadTheme(theme)));
}
public getLoadedThemes() {
return Object.keys(this._resolvedThemes) as Theme[];
}
public getGrammar(name: string) {
return this._resolvedGrammars[name];
}
public async loadLanguage(lang: ILanguageRegistration) {
const g = await this.loadGrammar(lang.scopeName);
this._resolvedGrammars[lang.id] = g!;
if (lang.aliases) {
lang.aliases.forEach((la) => {
this._resolvedGrammars[la] = g!;
});
}
}
public async loadLanguages(langs: ILanguageRegistration[]) {
for (const lang of langs) {
this._resolver.addLanguage(lang);
}
for (const lang of langs) {
await this.loadLanguage(lang);
}
}
public getLoadedLanguages() {
return Object.keys(this._resolvedGrammars) as Lang[];
}
}