profile
viewpoint

Ask questionsMonarch: How do you add "@keywords" key to "language object"? (undocumented)

Version: 0.16.2

I'm trying to reproduce the "mylang" example for Monarch on https://microsoft.github.io/monaco-editor/monarch.html. However, I can't figure out where the object literal goes when using the Monaco API.

In particular, it's clear where to put the tokenizer object:

monacoEditor.languages.setMonarchTokensProvider("substance", {
        tokenizer: {
...

But adding the keywords key to this object throws a type error. I want to have the keywords key, etc available to dynamically match against the list of keywords using @keywords syntax e,g, as seen in the example:

      [/[a-z_$][\w$]*/, { cases: { '@typeKeywords': 'keyword',
                                   '@keywords': 'keyword',
                                   '@default': 'identifier' } }],

So where should this key go? The text on the page says

The attribute keywords must be defined in the language object and consist of an array of strings.

But what is a "language object"? I can't find it anywhere in the docs. Is it the object passed into monaco.languages.register({...})? That throws a type error too.

microsoft/monaco-editor

Answer questions blacksteed232

@maxkrieger Hey, this is how I made it work

import * as monaco from 'monaco-editor';
interface MonarchLanguageConfiguration extends monaco.languages.IMonarchLanguage {
    keywords: string[];
}

monaco.languages.setMonarchTokensProvider('yourLanguageName', {
   keywords: ['class', 'extends', ...],
   tokenizer: { ... }
} as MonarchLanguageConfiguration)

Also, it's undocumented because they auto-generate their docs. I think you can close this thread

useful!

Related questions

Error: Cannot find module 'monaco-editor/esm/vs/editor/contrib/gotoSymbol/goToCommands' hot 2
Removing the tooltip on the read-only editor that says 'cannot edit in read-only editor' hot 1
Monaco paste event hot 1
Registering/Providing custom Commands/CodeActions hot 1
How to get the line count including line wraps hot 1
Error: Cannot find module 'monaco-editor/esm/vs/editor/contrib/goToDefinition/goToDefinitionCommands' hot 1
TypeScript declaration preview from extra lib results in error hot 1
Editor props/extends the actual width of the parent that is a flexbox item hot 1
Problem with insane.js while using monaco-editor@0.18.0 hot 1
Change the language of an existing editor hot 1
javascript/typescript typedefs lost when webworker times out hot 1
Can't scroll in firefox hot 1
How to add simple custom language support? hot 1
monaco-editor polute the global variables, making incompatible with other JS libraries, such as CodeMirror, RequireJS, SeaJS, etc. hot 1
Highlight merge conflicts in monaco like in VSCode hot 1
Github User Rank List