在多个编辑器中使用CodeActionProvider实例

时间:2019-03-14 16:25:32

标签: monaco-editor

使用摩纳哥,我正在尝试为我的语言实现自定义CodeActionProvider。

在下面的示例中,我可以为单个编辑器工作,但是这种方法似乎很奇怪。

已为一种语言注册了CodeActionProvider,但是必须触发的命令(至少是其ID)特定于编辑器实例。

这将在我的语言提供者和编辑器的特定实例之间建立依赖关系。

实际上,在我的场景中,我需要支持同一语言的多个编辑器实例。但是从架构的角度来看,我希望能够将我的语言提供程序与任何特定的编辑器实例分开。

对于CodeActionProvider,有什么方法可以实现这一目标吗?


来自github的示例:Quora question

var editor = monaco.editor.create(document.getElementById("container"), {
    value: "{\n\t\"dependencies\": {\n\t\t\n\t}\n}\n",
    language: "json"
});

var commandId = editor.addCommand(0, function() {
    // services available in `ctx`
    alert('my command is executing!');
}, '');

monaco.languages.registerCodeActionProvider('json', {
    provideCodeActions: function(model, range, context, token) {
                var command = {
                    id: commandId, // multiple editors will have a different commandId!
                    title: "Some command!"
                };
                return [{
                    command: command,
                    title: "Some command action!"
                }];
            }
});

0 个答案:

没有答案
相关问题