首页 文章

单击获取Monaco Editor CodeLens信息

提问于
浏览
1

使用这个CodeLens provider example作为起点,我一直试图弄清楚如何在单击链接时获取与CodeLens相关的范围信息 .

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

}, '');

monaco.languages.registerCodeLensProvider('json', {
    provideCodeLenses: function(model, token) {
        return [
            {
                range: {
                    startLineNumber: 1,
                    startColumn: 1,
                    endLineNumber: 2,
                    endColumn: 1
                },
                id: "First Line",
                command: {
                    id: commandId,
                    title: "First Line"
                }
            }
        ];
    },
    resolveCodeLens: function(model, codeLens, token) {
        return codeLens;
    }
});

我不确定 ctx 评论指的是什么 . 我已经尝试将此作为参数添加到 addCommand 中的匿名函数参数中,但我没有得到任何东西 . 是否有可能获得 provideCodeLenses 函数中指定的范围信息?

1 回答

  • 0

    为了解决这个问题,我在 provideCodeLenses 中将 arguments 属性添加到返回对象的 command 属性中:

    provideCodeLenses: function(model, token) {
        return [
            {
                range: {
                    startLineNumber: 1,
                    startColumn: 1,
                    endLineNumber: 2,
                    endColumn: 1
                },
                id: "First Line",
                command: {
                    id: commandId,
                    title: "First Line",
                    arguments: { from: 1, to: 2 },
                }
            }
        ];
    }
    

    然后可以在 addCommand 匿名函数中访问它:

    var commandId = editor.addCommand(0, function(ctx, args) {
        console.log(args); // { from: 1, to: 2 }
    }, '');
    

相关问题