addons/shared/textmate-lib/createTextMateRegistry.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import type {IRawGrammar, IRawTheme} from 'vscode-textmate';
b69ab319import type {Grammar, TextMateGrammar} from './types';
b69ab3110
b69ab3111import {createOnigScanner, createOnigString} from 'vscode-oniguruma';
b69ab3112import {Registry, parseRawGrammar} from 'vscode-textmate';
b69ab3113
b69ab3114/**
b69ab3115 * Caller is responsible for ensuring `loadWASM()` from `vscode-oniguruma` has
b69ab3116 * been called (and resolved successfully) before calling this function.
b69ab3117 */
b69ab3118export default function createTextMateRegistry(
b69ab3119 theme: IRawTheme,
b69ab3120 grammars: {[scopeName: string]: Grammar},
b69ab3121 fetchGrammar: (
b69ab3122 fileName: string,
b69ab3123 fileFormat: 'json' | 'plist',
b69ab3124 base: string,
b69ab3125 ) => Promise<TextMateGrammar>,
b69ab3126 base: string,
b69ab3127): Registry {
b69ab3128 return new Registry({
b69ab3129 theme,
b69ab3130 onigLib: Promise.resolve({
b69ab3131 createOnigScanner,
b69ab3132 createOnigString,
b69ab3133 }),
b69ab3134
b69ab3135 async loadGrammar(scopeName: string): Promise<IRawGrammar | undefined | null> {
b69ab3136 const config = grammars[scopeName];
b69ab3137 if (config != null) {
b69ab3138 const {type, grammar} = await fetchGrammar(config.fileName, config.fileFormat, base);
b69ab3139 // If this is a JSON grammar, filePath must be specified with a `.json`
b69ab3140 // file extension or else parseRawGrammar() will assume it is a PLIST
b69ab3141 // grammar.
b69ab3142 const filePath = `example.${type}`;
b69ab3143 return parseRawGrammar(grammar, filePath);
b69ab3144 } else {
b69ab3145 // text.html.markdown supports a ton of embedded languages, but we do
b69ab3146 // not bundle all of them, so we can expect to get requests for
b69ab3147 // languages we cannot satisfy. Because this this expected, we return
b69ab3148 // null rather than throw an error.
b69ab3149 return Promise.resolve(null);
b69ab3150 }
b69ab3151 },
b69ab3152
b69ab3153 /**
b69ab3154 * For the given scope, returns a list of additional grammars that should be
b69ab3155 * "injected into" it (i.e., a list of grammars that want to extend the
b69ab3156 * specified `scopeName`). The most common example is other grammars that
b69ab3157 * want to "inject themselves" into the `text.html.markdown` scope so they
b69ab3158 * can be used with fenced code blocks.
b69ab3159 *
b69ab3160 * In the manifest of a VS Code extension, a grammar signals that it wants
b69ab3161 * to do this via the "injectTo" property:
b69ab3162 * https://code.visualstudio.com/api/language-extensions/syntax-highlight-guide#injection-grammars
b69ab3163 */
b69ab3164 getInjections(scopeName: string): Array<string> | undefined {
b69ab3165 const grammar = grammars[scopeName];
b69ab3166 return grammar?.injections ?? undefined;
b69ab3167 },
b69ab3168 });
b69ab3169}