1
1
import { Registry } from "vscode-textmate" ;
2
- import vscodeOniguruma from "vscode-oniguruma" ;
2
+ import {
3
+ loadWASM ,
4
+ createOnigScanner ,
5
+ createOnigString ,
6
+ } from "vscode-oniguruma" ;
7
+ // @ts -ignore
3
8
import onig from "vscode-oniguruma/release/onig.wasm" ;
4
9
import { loadTheme , getThemeColors } from "./themes" ;
5
10
import { tokenize } from "./tokenizer.js" ;
6
11
import { loadGrammarByScope } from "./grammars.js" ;
7
12
import { aliasToLangData } from "./language" ;
13
+ import { LanguageAlias } from "./language-data" ;
8
14
9
- let registry = null ;
15
+ let registry : Registry | null = null ;
10
16
11
- export async function highlight ( code , alias , themeOrThemeName = "dark-plus" ) {
17
+ export async function highlight (
18
+ code : string ,
19
+ alias : LanguageAlias ,
20
+ themeOrThemeName = "dark-plus"
21
+ ) {
12
22
// get the language object from the alias
13
23
const langData = aliasToLangData ( alias ) ;
14
24
@@ -19,9 +29,12 @@ export async function highlight(code, alias, themeOrThemeName = "dark-plus") {
19
29
20
30
// initialize the registry the first time
21
31
if ( ! registry ) {
22
- await vscodeOniguruma . loadWASM ( onig ) ;
32
+ const onigLibPromise = loadWASM ( onig ) . then ( ( ) => ( {
33
+ createOnigScanner,
34
+ createOnigString,
35
+ } ) ) ;
23
36
registry = new Registry ( {
24
- onigLib : vscodeOniguruma ,
37
+ onigLib : onigLibPromise ,
25
38
loadGrammar : ( scopeName ) => loadGrammarByScope ( scopeName ) ,
26
39
} ) ;
27
40
}
@@ -31,7 +44,7 @@ export async function highlight(code, alias, themeOrThemeName = "dark-plus") {
31
44
const theme = await loadTheme ( themeOrThemeName ) ;
32
45
registry . setTheme ( theme ) ;
33
46
34
- const grammar = await grammarsPromise ;
47
+ const grammar = ( await grammarsPromise ) ! ;
35
48
const colorMap = registry . getColorMap ( ) ;
36
49
37
50
return {
@@ -42,8 +55,8 @@ export async function highlight(code, alias, themeOrThemeName = "dark-plus") {
42
55
}
43
56
44
57
export class UnknownLanguageError extends Error {
45
- alias ;
46
- constructor ( alias ) {
58
+ alias : string ;
59
+ constructor ( alias : string ) {
47
60
super ( `Unknown language: ${ alias } ` ) ;
48
61
this . alias = alias ;
49
62
}
0 commit comments