Skip to content

Commit c26ddea

Browse files
committed
fix: Do not throw an error when trying to load a grammar which doesn't exist
1 parent 6fdf5eb commit c26ddea

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/languages/textMate/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ function getOrCreateGrammarFactory (): CGTMGrammarFactory {
3333
}
3434

3535
const languageService = monaco.extra.StandaloneServices.get(monaco.languages.ILanguageService)
36-
async function createTextMateTokensProvider (languageId: string): Promise<monaco.languages.ITokenizationSupport> {
36+
async function createTextMateTokensProvider (languageId: string): Promise<monaco.languages.ITokenizationSupport | null> {
3737
const grammarFactory = getOrCreateGrammarFactory()
3838
const encodedLanguageId = languageService.languageIdCodec.encodeLanguageId(languageId)
3939
const { grammar, initialState, containsEmbeddedLanguages } = await grammarFactory.createGrammar(languageId, encodedLanguageId)
4040
if (grammar == null) {
41-
throw new Error(`No grammar found for language ${languageId}`)
41+
return null
4242
}
4343
const tokenization = new monaco.extra.TMTokenization(grammar, initialState, containsEmbeddedLanguages)
4444
tokenization.onDidEncounterLanguage((encodedLanguageId) => {
@@ -48,9 +48,9 @@ async function createTextMateTokensProvider (languageId: string): Promise<monaco
4848
return new CGTMTokenizationSupport(languageId, encodedLanguageId, tokenization, grammar)
4949
}
5050

51-
const tokenizationSupports = new Map<string, Promise<monaco.languages.ITokenizationSupport>>()
51+
const tokenizationSupports = new Map<string, Promise<monaco.languages.ITokenizationSupport | null>>()
5252

53-
export function getOrCreateTextMateTokensProvider (languageId: string): Promise<monaco.languages.ITokenizationSupport> {
53+
export function getOrCreateTextMateTokensProvider (languageId: string): Promise<monaco.languages.ITokenizationSupport | null> {
5454
let tokenizationSupportPromise = tokenizationSupports.get(languageId)
5555
if (tokenizationSupportPromise == null) {
5656
tokenizationSupportPromise = createTextMateTokensProvider(languageId)

0 commit comments

Comments
 (0)