简体   繁体   English

摩纳哥编辑:令牌识别的动作

[英]Monaco editor: action on token recognition

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. 我已经看过文档,以下功能似乎是我想要做的事情的入口: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. 我试图设置一个alert(“ something”)而不是“ custom-error”,但是它只运行一次,而不是针对每次出现的令牌运行。 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. 我已经看过文档,并且似乎应该使用IMonarchLanguageRule和IMonarchLanguageAction,但仍不清楚如何使用它。

I am sure an example could solve this problem. 我相信一个例子可以解决这个问题。 Thanks a lot. 非常感谢。

Neither IMonarchLanguage nor IMonarchLanguageRule seems to give solutions to the problem. IMonarchLanguage和IMonarchLanguageRule似乎都没有提供解决方案。 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. 这将动态完成,因此如果用户键入命令,我将检查每个字符的正则表达式,直到满意为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM