Skip to content

Commit

Permalink
refactor: cache getLoadedThemes and getLoadedLanguages (#648)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Apr 3, 2024
1 parent 8653f88 commit 1035c25
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
3 changes: 1 addition & 2 deletions packages/core/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ export async function getShikiInternal(options: HighlighterCoreOptions = {}): Pr
langs,
)

const _registry = new Registry(resolver, themes, langs)
Object.assign(_registry.alias, options.langAlias)
const _registry = new Registry(resolver, themes, langs, options.langAlias)
await _registry.init()

let _lastTheme: string | ThemeRegistrationAny
Expand Down
34 changes: 23 additions & 11 deletions packages/core/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ export class Registry extends TextMateRegistry {
private _resolvedGrammars: Record<string, IGrammar> = {}
private _langMap: Record<string, LanguageRegistration> = {}
private _langGraph: Map<string, LanguageRegistration> = new Map()

alias: Record<string, string> = {}
private _loadedThemesCache: string[] | null = null
private _loadedLanguagesCache: string[] | null = null

constructor(
private _resolver: Resolver,
public _themes: ThemeRegistrationResolved[],
public _langs: LanguageRegistration[],
private _themes: ThemeRegistrationResolved[],
private _langs: LanguageRegistration[],
private _alias: Record<string, string> = {},
) {
super(_resolver)

Expand All @@ -33,20 +34,25 @@ export class Registry extends TextMateRegistry {

public loadTheme(theme: ThemeRegistrationAny): ThemeRegistrationResolved {
const _theme = normalizeTheme(theme)
if (_theme.name)
if (_theme.name) {
this._resolvedThemes[_theme.name] = _theme
// Reset cache
this._loadedThemesCache = null
}
return _theme
}

public getLoadedThemes() {
return Object.keys(this._resolvedThemes) as string[]
if (!this._loadedThemesCache)
this._loadedThemesCache = Object.keys(this._resolvedThemes)
return this._loadedThemesCache
}

public getGrammar(name: string) {
if (this.alias[name]) {
if (this._alias[name]) {
const resolved = new Set<string>([name])
while (this.alias[name]) {
name = this.alias[name]
while (this._alias[name]) {
name = this._alias[name]
if (resolved.has(name))
throw new ShikiError(`Circular alias \`${Array.from(resolved).join(' -> ')} -> ${name}\``)
resolved.add(name)
Expand Down Expand Up @@ -74,14 +80,18 @@ export class Registry extends TextMateRegistry {
this._resolvedGrammars[lang.name] = g!
if (lang.aliases) {
lang.aliases.forEach((alias) => {
this.alias[alias] = lang.name
this._alias[alias] = lang.name
})
}
// Reset cache
this._loadedLanguagesCache = null

// If there is a language that embeds this language lazily, we need to reload it
if (embeddedLazilyBy.size) {
for (const e of embeddedLazilyBy) {
delete this._resolvedGrammars[e.name]
// Reset cache
this._loadedLanguagesCache = null
// @ts-expect-error clear cache
this._syncRegistry?._injectionGrammars?.delete(e.scopeName)
// @ts-expect-error clear cache
Expand Down Expand Up @@ -118,7 +128,9 @@ export class Registry extends TextMateRegistry {
}

public getLoadedLanguages() {
return Object.keys({ ...this._resolvedGrammars, ...this.alias }) as string[]
if (!this._loadedLanguagesCache)
this._loadedLanguagesCache = Object.keys({ ...this._resolvedGrammars, ...this._alias })
return this._loadedLanguagesCache
}

private resolveEmbeddedLanguages(lang: LanguageRegistration) {
Expand Down

0 comments on commit 1035c25

Please sign in to comment.