Question

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?

Was it helpful?

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top