Skip to content

Commit

Permalink
fix(registry): await load grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
gregermendle committed Dec 15, 2023
1 parent 39e8e1e commit 28a4834
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions packages/shiki/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,10 @@ export class Registry extends TextMateRegistry {
}

public async loadLanguage(lang: ILanguageRegistration) {
const embeddedLanguages = lang.embeddedLangs?.reduce(async (acc, l, idx) => {
if (!this.getLoadedLanguages().includes(l) && this._resolver.getLangRegistration(l)) {
await this._resolver.loadGrammar(this._resolver.getLangRegistration(l).scopeName)
acc[this._resolver.getLangRegistration(l).scopeName] = idx + 2
return acc
}
}, {})
const embeddedLanguages =
lang.embeddedLangs && lang.embeddedLangs.length > 0
? Object.fromEntries(await this.loadGrammars(lang.embeddedLangs))
: {}

const grammarConfig: IGrammarConfiguration = {
embeddedLanguages,
Expand Down Expand Up @@ -98,6 +95,18 @@ export class Registry extends TextMateRegistry {
return Object.keys(this._resolvedGrammars) as Lang[]
}

private async loadGrammars(embeddedLangs: Lang[]) {
const languageGrammarPromises = embeddedLangs
?.filter(l => !this.getLoadedLanguages().includes(l) && this._resolver.getLangRegistration(l))
.map((l, idx) =>
this._resolver
.loadGrammar(this._resolver.getLangRegistration(l).scopeName)
.then(() => [this._resolver.getLangRegistration(l).scopeName, idx + 2] as const)
)

return Promise.all(languageGrammarPromises)
}

private resolveEmbeddedLanguages(lang: ILanguageRegistration) {
if (!this._langGraph.has(lang.id)) {
this._langGraph.set(lang.id, lang)
Expand Down

0 comments on commit 28a4834

Please sign in to comment.