| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {TextMateGrammar} from 'shared/textmate-lib/types'; |
| b69ab31 | | | 9 | import type {IGrammar, Registry} from 'vscode-textmate'; |
| b69ab31 | | | 10 | import type {ThemeColor} from '../../theme'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import createTextMateRegistry from 'shared/textmate-lib/createTextMateRegistry'; |
| b69ab31 | | | 13 | import {nullthrows} from 'shared/utils'; |
| b69ab31 | | | 14 | import {grammars} from '../../generated/textmate/TextMateGrammarManifest'; |
| b69ab31 | | | 15 | import VSCodeDarkPlusTheme from './VSCodeDarkPlusTheme'; |
| b69ab31 | | | 16 | import VSCodeLightPlusTheme from './VSCodeLightPlusTheme'; |
| b69ab31 | | | 17 | |
| b69ab31 | | | 18 | const grammarCache: Map<string, Promise<IGrammar | null>> = new Map(); |
| b69ab31 | | | 19 | export function getGrammar(store: Registry, scopeName: string): Promise<IGrammar | null> { |
| b69ab31 | | | 20 | if (grammarCache.has(scopeName)) { |
| b69ab31 | | | 21 | return nullthrows(grammarCache.get(scopeName)); |
| b69ab31 | | | 22 | } |
| b69ab31 | | | 23 | const grammarPromise = store.loadGrammar(scopeName); |
| b69ab31 | | | 24 | grammarCache.set(scopeName, grammarPromise); |
| b69ab31 | | | 25 | return grammarPromise; |
| b69ab31 | | | 26 | } |
| b69ab31 | | | 27 | |
| b69ab31 | | | 28 | async function fetchGrammar( |
| b69ab31 | | | 29 | moduleName: string, |
| b69ab31 | | | 30 | type: 'json' | 'plist', |
| b69ab31 | | | 31 | base: string, |
| b69ab31 | | | 32 | ): Promise<TextMateGrammar> { |
| b69ab31 | | | 33 | const uri = new URL(`./generated/textmate/${moduleName}.${type}`, base); |
| b69ab31 | | | 34 | const response = await fetch(uri); |
| b69ab31 | | | 35 | const grammar = await response.text(); |
| b69ab31 | | | 36 | return {type, grammar}; |
| b69ab31 | | | 37 | } |
| b69ab31 | | | 38 | |
| b69ab31 | | | 39 | let cachedGrammarStore: {value: Registry; theme: ThemeColor} | null = null; |
| b69ab31 | | | 40 | export function getGrammarStore( |
| b69ab31 | | | 41 | theme: ThemeColor, |
| b69ab31 | | | 42 | base: string, |
| b69ab31 | | | 43 | onNewColors?: (colorMap: string[]) => void, |
| b69ab31 | | | 44 | ) { |
| b69ab31 | | | 45 | const found = cachedGrammarStore; |
| b69ab31 | | | 46 | if (found != null && found.theme === theme) { |
| b69ab31 | | | 47 | return found.value; |
| b69ab31 | | | 48 | } |
| b69ab31 | | | 49 | |
| b69ab31 | | | 50 | // Grammars were cached according to the store, but the theme may have changed. Just bust the cache |
| b69ab31 | | | 51 | // to force grammars to reload. |
| b69ab31 | | | 52 | grammarCache.clear(); |
| b69ab31 | | | 53 | |
| b69ab31 | | | 54 | const themeValues = theme === 'light' ? VSCodeLightPlusTheme : VSCodeDarkPlusTheme; |
| b69ab31 | | | 55 | |
| b69ab31 | | | 56 | const registry = createTextMateRegistry(themeValues, grammars, fetchGrammar, base); |
| b69ab31 | | | 57 | |
| b69ab31 | | | 58 | onNewColors?.(registry.getColorMap()); |
| b69ab31 | | | 59 | |
| b69ab31 | | | 60 | cachedGrammarStore = {value: registry, theme}; |
| b69ab31 | | | 61 | return registry; |
| b69ab31 | | | 62 | } |