github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/app/public/codemirror/addon/hint/javascript-hint.js (about)

     1  // CodeMirror, copyright (c) by Marijn Haverbeke and others
     2  // Distributed under an MIT license: http://codemirror.net/LICENSE
     3  
     4  (function(mod) {
     5    if (typeof exports == "object" && typeof module == "object") // CommonJS
     6      mod(require("../../lib/codemirror"));
     7    else if (typeof define == "function" && define.amd) // AMD
     8      define(["../../lib/codemirror"], mod);
     9    else // Plain browser env
    10      mod(CodeMirror);
    11  })(function(CodeMirror) {
    12    var Pos = CodeMirror.Pos;
    13  
    14    function forEach(arr, f) {
    15      for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]);
    16    }
    17  
    18    function arrayContains(arr, item) {
    19      if (!Array.prototype.indexOf) {
    20        var i = arr.length;
    21        while (i--) {
    22          if (arr[i] === item) {
    23            return true;
    24          }
    25        }
    26        return false;
    27      }
    28      return arr.indexOf(item) != -1;
    29    }
    30  
    31    function scriptHint(editor, keywords, getToken, options) {
    32      // Find the token at the cursor
    33      var cur = editor.getCursor(), token = getToken(editor, cur);
    34      if (/\b(?:string|comment)\b/.test(token.type)) return;
    35      token.state = CodeMirror.innerMode(editor.getMode(), token.state).state;
    36  
    37      // If it's not a 'word-style' token, ignore the token.
    38      if (!/^[\w$_]*$/.test(token.string)) {
    39        token = {start: cur.ch, end: cur.ch, string: "", state: token.state,
    40                 type: token.string == "." ? "property" : null};
    41      } else if (token.end > cur.ch) {
    42        token.end = cur.ch;
    43        token.string = token.string.slice(0, cur.ch - token.start);
    44      }
    45  
    46      var tprop = token;
    47      // If it is a property, find out what it is a property of.
    48      while (tprop.type == "property") {
    49        tprop = getToken(editor, Pos(cur.line, tprop.start));
    50        if (tprop.string != ".") return;
    51        tprop = getToken(editor, Pos(cur.line, tprop.start));
    52        if (!context) var context = [];
    53        context.push(tprop);
    54      }
    55      return {list: getCompletions(token, context, keywords, options),
    56              from: Pos(cur.line, token.start),
    57              to: Pos(cur.line, token.end)};
    58    }
    59  
    60    function javascriptHint(editor, options) {
    61      return scriptHint(editor, javascriptKeywords,
    62                        function (e, cur) {return e.getTokenAt(cur);},
    63                        options);
    64    };
    65    CodeMirror.registerHelper("hint", "javascript", javascriptHint);
    66  
    67    function getCoffeeScriptToken(editor, cur) {
    68    // This getToken, it is for coffeescript, imitates the behavior of
    69    // getTokenAt method in javascript.js, that is, returning "property"
    70    // type and treat "." as indepenent token.
    71      var token = editor.getTokenAt(cur);
    72      if (cur.ch == token.start + 1 && token.string.charAt(0) == '.') {
    73        token.end = token.start;
    74        token.string = '.';
    75        token.type = "property";
    76      }
    77      else if (/^\.[\w$_]*$/.test(token.string)) {
    78        token.type = "property";
    79        token.start++;
    80        token.string = token.string.replace(/\./, '');
    81      }
    82      return token;
    83    }
    84  
    85    function coffeescriptHint(editor, options) {
    86      return scriptHint(editor, coffeescriptKeywords, getCoffeeScriptToken, options);
    87    }
    88    CodeMirror.registerHelper("hint", "coffeescript", coffeescriptHint);
    89  
    90    var stringProps = ("charAt charCodeAt indexOf lastIndexOf substring substr slice trim trimLeft trimRight " +
    91                       "toUpperCase toLowerCase split concat match replace search").split(" ");
    92    var arrayProps = ("length concat join splice push pop shift unshift slice reverse sort indexOf " +
    93                      "lastIndexOf every some filter forEach map reduce reduceRight ").split(" ");
    94    var funcProps = "prototype apply call bind".split(" ");
    95    var javascriptKeywords = ("break case catch continue debugger default delete do else false finally for function " +
    96                    "if in instanceof new null return switch throw true try typeof var void while with").split(" ");
    97    var coffeescriptKeywords = ("and break catch class continue delete do else extends false finally for " +
    98                    "if in instanceof isnt new no not null of off on or return switch then throw true try typeof until void while with yes").split(" ");
    99  
   100    function getCompletions(token, context, keywords, options) {
   101      var found = [], start = token.string, global = options && options.globalScope || window;
   102      function maybeAdd(str) {
   103        if (str.lastIndexOf(start, 0) == 0 && !arrayContains(found, str)) found.push(str);
   104      }
   105      function gatherCompletions(obj) {
   106        if (typeof obj == "string") forEach(stringProps, maybeAdd);
   107        else if (obj instanceof Array) forEach(arrayProps, maybeAdd);
   108        else if (obj instanceof Function) forEach(funcProps, maybeAdd);
   109        for (var name in obj) maybeAdd(name);
   110      }
   111  
   112      if (context && context.length) {
   113        // If this is a property, see if it belongs to some object we can
   114        // find in the current environment.
   115        var obj = context.pop(), base;
   116        if (obj.type && obj.type.indexOf("variable") === 0) {
   117          if (options && options.additionalContext)
   118            base = options.additionalContext[obj.string];
   119          if (!options || options.useGlobalScope !== false)
   120            base = base || global[obj.string];
   121        } else if (obj.type == "string") {
   122          base = "";
   123        } else if (obj.type == "atom") {
   124          base = 1;
   125        } else if (obj.type == "function") {
   126          if (global.jQuery != null && (obj.string == '$' || obj.string == 'jQuery') &&
   127              (typeof global.jQuery == 'function'))
   128            base = global.jQuery();
   129          else if (global._ != null && (obj.string == '_') && (typeof global._ == 'function'))
   130            base = global._();
   131        }
   132        while (base != null && context.length)
   133          base = base[context.pop().string];
   134        if (base != null) gatherCompletions(base);
   135      } else {
   136        // If not, just look in the global object and any local scope
   137        // (reading into JS mode internals to get at the local and global variables)
   138        for (var v = token.state.localVars; v; v = v.next) maybeAdd(v.name);
   139        for (var v = token.state.globalVars; v; v = v.next) maybeAdd(v.name);
   140        if (!options || options.useGlobalScope !== false)
   141          gatherCompletions(global);
   142        forEach(keywords, maybeAdd);
   143      }
   144      return found;
   145    }
   146  });