Monaco editor: action on token recognition

1k Views Asked by At

I would like to run a function each time a token is recognized. I have seen the documentation and the following function seems to be the entry point to what I would like to do: setMonarchTokensProvider.

Here we have a provided example:

monaco.languages.setMonarchTokensProvider('mySpecialLanguage', {
tokenizer: {
    root: [
        [/\[error.*/, "custom-error"],
        [/\[notice.*/, "custom-notice"],
        [/\[info.*/, "custom-info"],
        [/\[[a-zA-Z 0-9:]+\]/, "custom-date"],
    ]
}});

monaco.editor.defineTheme('myCoolTheme', {
base: 'vs',
inherit: false,
rules: [
    { token: 'custom-info', foreground: '808080' },
    { token: 'custom-error', foreground: 'ff0000', fontStyle: 'bold' },
    { token: 'custom-notice', foreground: 'FFA500' },
    { token: 'custom-date', foreground: '008800' },
]});

I tried to set an alert("something") instead of the "custom-error" but it runs only once and not for each occurrence of the token. The provided code sample works well: it colours each token satisfy the regex.

I have seen the documentation and seems I should use IMonarchLanguageRule and so the IMonarchLanguageAction but still isn't clear to me how to use it.

I am sure an example could solve this problem. Thanks a lot.

1

There are 1 best solutions below

1
On

Neither IMonarchLanguage nor IMonarchLanguageRule seems to give solutions to the problem. That's why I am going to use a personal solution without to use the Monaco-editor matcher. This means I am going to realize twice the work Monaco-editor is already doing but I can't find a solution using Monaco.

I am going to check through regex what match a command, if yes I will execute the action I want. This will be done dynamically, so if the user will type a command I will check the regex for each character until it will be satisfied.