| 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 {IRawGrammar, IRawTheme} from 'vscode-textmate'; |
| b69ab31 | | | 9 | import type {Grammar, TextMateGrammar} from './types'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | import {createOnigScanner, createOnigString} from 'vscode-oniguruma'; |
| b69ab31 | | | 12 | import {Registry, parseRawGrammar} from 'vscode-textmate'; |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | /** |
| b69ab31 | | | 15 | * Caller is responsible for ensuring `loadWASM()` from `vscode-oniguruma` has |
| b69ab31 | | | 16 | * been called (and resolved successfully) before calling this function. |
| b69ab31 | | | 17 | */ |
| b69ab31 | | | 18 | export default function createTextMateRegistry( |
| b69ab31 | | | 19 | theme: IRawTheme, |
| b69ab31 | | | 20 | grammars: {[scopeName: string]: Grammar}, |
| b69ab31 | | | 21 | fetchGrammar: ( |
| b69ab31 | | | 22 | fileName: string, |
| b69ab31 | | | 23 | fileFormat: 'json' | 'plist', |
| b69ab31 | | | 24 | base: string, |
| b69ab31 | | | 25 | ) => Promise<TextMateGrammar>, |
| b69ab31 | | | 26 | base: string, |
| b69ab31 | | | 27 | ): Registry { |
| b69ab31 | | | 28 | return new Registry({ |
| b69ab31 | | | 29 | theme, |
| b69ab31 | | | 30 | onigLib: Promise.resolve({ |
| b69ab31 | | | 31 | createOnigScanner, |
| b69ab31 | | | 32 | createOnigString, |
| b69ab31 | | | 33 | }), |
| b69ab31 | | | 34 | |
| b69ab31 | | | 35 | async loadGrammar(scopeName: string): Promise<IRawGrammar | undefined | null> { |
| b69ab31 | | | 36 | const config = grammars[scopeName]; |
| b69ab31 | | | 37 | if (config != null) { |
| b69ab31 | | | 38 | const {type, grammar} = await fetchGrammar(config.fileName, config.fileFormat, base); |
| b69ab31 | | | 39 | // If this is a JSON grammar, filePath must be specified with a `.json` |
| b69ab31 | | | 40 | // file extension or else parseRawGrammar() will assume it is a PLIST |
| b69ab31 | | | 41 | // grammar. |
| b69ab31 | | | 42 | const filePath = `example.${type}`; |
| b69ab31 | | | 43 | return parseRawGrammar(grammar, filePath); |
| b69ab31 | | | 44 | } else { |
| b69ab31 | | | 45 | // text.html.markdown supports a ton of embedded languages, but we do |
| b69ab31 | | | 46 | // not bundle all of them, so we can expect to get requests for |
| b69ab31 | | | 47 | // languages we cannot satisfy. Because this this expected, we return |
| b69ab31 | | | 48 | // null rather than throw an error. |
| b69ab31 | | | 49 | return Promise.resolve(null); |
| b69ab31 | | | 50 | } |
| b69ab31 | | | 51 | }, |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | /** |
| b69ab31 | | | 54 | * For the given scope, returns a list of additional grammars that should be |
| b69ab31 | | | 55 | * "injected into" it (i.e., a list of grammars that want to extend the |
| b69ab31 | | | 56 | * specified `scopeName`). The most common example is other grammars that |
| b69ab31 | | | 57 | * want to "inject themselves" into the `text.html.markdown` scope so they |
| b69ab31 | | | 58 | * can be used with fenced code blocks. |
| b69ab31 | | | 59 | * |
| b69ab31 | | | 60 | * In the manifest of a VS Code extension, a grammar signals that it wants |
| b69ab31 | | | 61 | * to do this via the "injectTo" property: |
| b69ab31 | | | 62 | * https://code.visualstudio.com/api/language-extensions/syntax-highlight-guide#injection-grammars |
| b69ab31 | | | 63 | */ |
| b69ab31 | | | 64 | getInjections(scopeName: string): Array<string> | undefined { |
| b69ab31 | | | 65 | const grammar = grammars[scopeName]; |
| b69ab31 | | | 66 | return grammar?.injections ?? undefined; |
| b69ab31 | | | 67 | }, |
| b69ab31 | | | 68 | }); |
| b69ab31 | | | 69 | } |