forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry.ts
More file actions
102 lines (87 loc) · 2.99 KB
/
registry.ts
File metadata and controls
102 lines (87 loc) · 2.99 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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
'use strict';
import { createGrammar, Grammar, collectIncludedScopes, IGrammarRepository, IScopeNameSet } from './grammar';
import { IRawGrammar } from './types';
import { Thenable } from './main';
import { IGrammar, IEmbeddedLanguagesMap, ITokenTypeMap } from './main';
import { Theme, ThemeTrieElementRule } from './theme';
import { IOnigLib } from './types';
import { getOnigasm } from './onigLibs';
export class SyncRegistry implements IGrammarRepository {
private readonly _grammars: { [scopeName: string]: Grammar; };
private readonly _rawGrammars: { [scopeName: string]: IRawGrammar; };
private readonly _injectionGrammars: { [scopeName: string]: string[]; };
private _theme: Theme;
private _onigLibPromise: Thenable<IOnigLib>;
constructor(theme: Theme, onigLibPromise: Thenable<IOnigLib>) {
this._theme = theme;
this._grammars = {};
this._rawGrammars = {};
this._injectionGrammars = {};
this._onigLibPromise = onigLibPromise || getOnigasm();
}
public setTheme(theme: Theme): void {
this._theme = theme;
Object.keys(this._grammars).forEach((scopeName) => {
let grammar = this._grammars[scopeName];
grammar.onDidChangeTheme();
});
}
public getColorMap(): string[] {
return this._theme.getColorMap();
}
/**
* Add `grammar` to registry and return a list of referenced scope names
*/
public addGrammar(grammar: IRawGrammar, injectionScopeNames?: string[]): string[] {
this._rawGrammars[grammar.scopeName] = grammar;
let includedScopes: IScopeNameSet = {};
collectIncludedScopes(includedScopes, grammar);
if (injectionScopeNames) {
this._injectionGrammars[grammar.scopeName] = injectionScopeNames;
injectionScopeNames.forEach(scopeName => {
includedScopes[scopeName] = true;
});
}
return Object.keys(includedScopes);
}
/**
* Lookup a raw grammar.
*/
public lookup(scopeName: string): IRawGrammar {
return this._rawGrammars[scopeName];
}
/**
* Returns the injections for the given grammar
*/
public injections(targetScope: string): string[] {
return this._injectionGrammars[targetScope];
}
/**
* Get the default theme settings
*/
public getDefaults(): ThemeTrieElementRule {
return this._theme.getDefaults();
}
/**
* Match a scope in the theme.
*/
public themeMatch(scopeName: string): ThemeTrieElementRule[] {
return this._theme.match(scopeName);
}
/**
* Lookup a grammar.
*/
public async grammarForScopeName(scopeName: string, initialLanguage: number, embeddedLanguages: IEmbeddedLanguagesMap, tokenTypes: ITokenTypeMap): Promise<IGrammar> {
if (!this._grammars[scopeName]) {
let rawGrammar = this._rawGrammars[scopeName];
if (!rawGrammar) {
return null;
}
this._grammars[scopeName] = createGrammar(rawGrammar, initialLanguage, embeddedLanguages, tokenTypes, this, await this._onigLibPromise);
}
return this._grammars[scopeName];
}
}