Search code examples
javascriptcodemirror

Codemirror Autocomplete Remove Global Suggestions


Weave: http://kodeweave.sourceforge.net/editor/#d956c96bdee0cdd1ce9193aee78353ac

Does anyone know of an effective way to remove some of the global variables out of Codemirror's Autocomplete?

For example StyleFix, PrefixFree, Html2Jade, etc: should not be visible.

enter image description here


Solution

  • Here is its summary: (from https://codemirror.net/doc/manual.html#addon_javascript-hint)

    This will simply use the JavaScript environment that the editor runs in as a source of information about objects and their properties.

    And related source code:

    var found = [], start = token.string, global = options && options.globalScope || window;
    

    and

    function gatherCompletions(obj) {
      if (typeof obj == "string") forEach(stringProps, maybeAdd);
      else if (obj instanceof Array) forEach(arrayProps, maybeAdd);
      else if (obj instanceof Function) forEach(funcProps, maybeAdd);
      for (var name in obj) maybeAdd(name);//important
    }
    

    (from https://mikethedj4.github.io/kodeWeave/editor/libraries/codemirror/addon/hint/javascript-hint.js)

    In which obj is global.

    So if you want to remove some of the global variables , just modify the globalScope param.

    Change this line:

    CodeMirror.commands.autocomplete(cm,null, {completeSingle: false});
    

    to

    var scope={};
    var preventList=['StyleFix', 'PrefixFree', 'Html2Jade','alert'];// map is better
    for(var i in window){
      if(preventList.indexOf(i)===-1){
        scope[i]=window[i]
      }
    }
    CodeMirror.commands.autocomplete(cm,null, {completeSingle: false,globalScope:scope});
    

    Live demo here: https://mikethedj4.github.io/kodeWeave/editor/#cf4c4aa884b6ddb30c4ac79dd8bf3997