最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - CodeMirror Use multiple hint sources for autocomplete - Stack Overflow

programmeradmin2浏览0评论

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

CodeMirrormands.autoplete = 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?

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

CodeMirror.mands.autoplete = 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?

Share Improve this question asked Oct 22, 2013 at 14:36 JeffJenkJeffJenk 2,6653 gold badges23 silver badges29 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

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 autoplete 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 autoplete was called so my function looks like this:

CodeMirror.mands.autoplete = 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 autoplete is called it checks what mode the editor is in at that specific point in the document.

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 pleter (show-hint.js) will, if you don't give it an explicit pletion function, use the one defined for the language. The standard pleter addons do register themselves as applicable to their languages, so it should work 'out of the box'.

发布评论

评论列表(0)

  1. 暂无评论