Search code examples
javascriptcodemirror

CodeMirror Use multiple hint sources for autocomplete


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?


Solution

  • 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.