如何使用show-hint插件订阅CodeMirror中的select个事件?

How to subscribe to select events in CodeMirror with show-hint plugin?

我想在用户浏览自动完成建议时显示一些附加信息。 documentaion 中有一些关于 select 事件的内容,但如果可能的话,我找不到如何订阅它。

有人可以建议如何订阅该活动吗?或任何其他方法来检测当前 selected 并检测它何时发生变化?

关于这个有几个问题,但我找不到任何解决方案。

这些事件不会在内部完成对象上触发,内部完成对象通常在提示提供程序之外不可用,因此不能直接订阅。

无论如何,如果你真的想订阅这些事件,你必须覆盖提示提供者。例如,

// Some other code defined CodeMirror.hint.foo

var fooHint = CodeMirror.hint.foo;
CodeMirror.hint.foo = function(cm, options) {
    var result = fooHint(cm, options);
    if (result) CodeMirror.on(result, "pick", function() { /* ... */ });
    return result;
};

参见https://github.com/codemirror/CodeMirror/issues/3092。感谢 marijnh 提供解决方案。

事件在完成数据对象上触发,可以像这样附加到它。

extraKeys: {
    "Ctrl-Space": function(cm) { 
        hintFunction(cm);
        var completion = cm.state.completionActive.data;
        CodeMirror.on(completion, 'select', function(completion, element) {
            console.log('huh');
        })
    }
}