CodeMirror Use multiple hint sources for autocomplete

家住魔仙堡 提交于 2019-12-06 14:27:31

问题


Is it possible to include multiple hint sources for autocomplete? I tried this:

CodeMirror.commands.autocomplete = function(cm) {
    CodeMirror.showHint(cm, CodeMirror.hint.xml);
    CodeMirror.showHint(cm, CodeMirror.hint.html);
    CodeMirror.showHint(cm, CodeMirror.hint.css);
    CodeMirror.showHint(cm, CodeMirror.hint.javascript);
};

but it seems to just include the last source file that is referenced and ignore the rest. Is there any easy way of doing this?


回答1:


I found the answer to my question in another question so please excuse me if that makes this question a little redundant. What I needed to do is find out what mode is currently active (i am using a mixed mode) at the time autocomplete is called. To do this first I needed the mode:

var mode = CodeMirror.innerMode(cm.getMode(), cm.getTokenAt(POS).state).mode.name;

which I found here. For my situation I wanted to do that whenever the autocomplete was called so my function looks like this:

CodeMirror.commands.autocomplete = function(cm) {
    var doc = cm.getDoc();
    var POS = doc.getCursor();
    var mode = CodeMirror.innerMode(cm.getMode(), cm.getTokenAt(POS).state).mode.name;

    if (mode == 'xml') { //html depends on xml
        CodeMirror.showHint(cm, CodeMirror.hint.html);
    } else if (mode == 'javascript') {
        CodeMirror.showHint(cm, CodeMirror.hint.javascript);
    } else if (mode == 'css') {
        CodeMirror.showHint(cm, CodeMirror.hint.css);
    }
};

Now whenever the autocomplete is called it checks what mode the editor is in at that specific point in the document.




回答2:


A feature that handles this is already present in (recent versions of) CodeMirror, where you can define language-specific helper functions with defineHelper, and the standard completer (show-hint.js) will, if you don't give it an explicit completion function, use the one defined for the language. The standard completer addons do register themselves as applicable to their languages, so it should work 'out of the box'.



来源:https://stackoverflow.com/questions/19520877/codemirror-use-multiple-hint-sources-for-autocomplete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!