Skip to content

Commit

Permalink
fix: Do not throw an error when trying to load a grammar which doesn'…
Browse files Browse the repository at this point in the history
…t exist
  • Loading branch information
CGNonofr committed Apr 19, 2022
1 parent 6fdf5eb commit c26ddea
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/languages/textMate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ function getOrCreateGrammarFactory (): CGTMGrammarFactory {
}

const languageService = monaco.extra.StandaloneServices.get(monaco.languages.ILanguageService)
async function createTextMateTokensProvider (languageId: string): Promise<monaco.languages.ITokenizationSupport> {
async function createTextMateTokensProvider (languageId: string): Promise<monaco.languages.ITokenizationSupport | null> {
const grammarFactory = getOrCreateGrammarFactory()
const encodedLanguageId = languageService.languageIdCodec.encodeLanguageId(languageId)
const { grammar, initialState, containsEmbeddedLanguages } = await grammarFactory.createGrammar(languageId, encodedLanguageId)
if (grammar == null) {
throw new Error(`No grammar found for language ${languageId}`)
return null
}
const tokenization = new monaco.extra.TMTokenization(grammar, initialState, containsEmbeddedLanguages)
tokenization.onDidEncounterLanguage((encodedLanguageId) => {
Expand All @@ -48,9 +48,9 @@ async function createTextMateTokensProvider (languageId: string): Promise<monaco
return new CGTMTokenizationSupport(languageId, encodedLanguageId, tokenization, grammar)
}

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

export function getOrCreateTextMateTokensProvider (languageId: string): Promise<monaco.languages.ITokenizationSupport> {
export function getOrCreateTextMateTokensProvider (languageId: string): Promise<monaco.languages.ITokenizationSupport | null> {
let tokenizationSupportPromise = tokenizationSupports.get(languageId)
if (tokenizationSupportPromise == null) {
tokenizationSupportPromise = createTextMateTokensProvider(languageId)
Expand Down

0 comments on commit c26ddea

Please sign in to comment.