-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver.ts
70 lines (57 loc) · 1.87 KB
/
resolver.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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import { IOnigLib, IRawGrammar, RegistryOptions } from "./deps.ts";
import { languages } from "./languages.ts";
import { fetchGrammar, resolvePath } from "./loader.ts";
import { ILanguageRegistration } from "./types.ts";
const LANGUAGES_PATH = resolvePath("languages/");
export class Resolver implements RegistryOptions {
public languagesPath = LANGUAGES_PATH;
private readonly languageMap: {
[langIdOrAlias: string]: ILanguageRegistration;
} = {};
private readonly scopeToLangMap: {
[scope: string]: ILanguageRegistration;
} = {};
private readonly _onigLibPromise: Promise<IOnigLib>;
private readonly _onigLibName: string;
constructor(onigLibPromise: Promise<IOnigLib>, onigLibName: string) {
this._onigLibPromise = onigLibPromise;
this._onigLibName = onigLibName;
}
public get onigLib(): Promise<IOnigLib> {
return this._onigLibPromise;
}
public getOnigLibName(): string {
return this._onigLibName;
}
public getLangRegistration(langIdOrAlias: string): ILanguageRegistration {
return this.languageMap[langIdOrAlias];
}
public async loadGrammar(scopeName: string): Promise<IRawGrammar | null> {
const lang = this.scopeToLangMap[scopeName];
if (!lang) {
return null;
}
if (lang.grammar) {
return lang.grammar;
}
const g = await fetchGrammar(
languages.includes(lang)
? `${this.languagesPath}/${lang.path}`
: lang.path!,
);
lang.grammar = g;
return g;
}
public addLanguage(l: ILanguageRegistration) {
this.languageMap[l.id] = l;
if (l.aliases) {
l.aliases.forEach((a) => {
this.languageMap[a] = l;
});
}
this.scopeToLangMap[l.scopeName] = l;
}
}