openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

javascript-hint.js (6437B)


      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 forAllProps(obj, callback) {
    101     if (!Object.getOwnPropertyNames || !Object.getPrototypeOf) {
    102       for (var name in obj) callback(name)
    103     } else {
    104       for (var o = obj; o; o = Object.getPrototypeOf(o))
    105         Object.getOwnPropertyNames(o).forEach(callback)
    106     }
    107   }
    108 
    109   function getCompletions(token, context, keywords, options) {
    110     var found = [], start = token.string, global = options && options.globalScope || window;
    111     function maybeAdd(str) {
    112       if (str.lastIndexOf(start, 0) == 0 && !arrayContains(found, str)) found.push(str);
    113     }
    114     function gatherCompletions(obj) {
    115       if (typeof obj == "string") forEach(stringProps, maybeAdd);
    116       else if (obj instanceof Array) forEach(arrayProps, maybeAdd);
    117       else if (obj instanceof Function) forEach(funcProps, maybeAdd);
    118       forAllProps(obj, maybeAdd)
    119     }
    120 
    121     if (context && context.length) {
    122       // If this is a property, see if it belongs to some object we can
    123       // find in the current environment.
    124       var obj = context.pop(), base;
    125       if (obj.type && obj.type.indexOf("variable") === 0) {
    126         if (options && options.additionalContext)
    127           base = options.additionalContext[obj.string];
    128         if (!options || options.useGlobalScope !== false)
    129           base = base || global[obj.string];
    130       } else if (obj.type == "string") {
    131         base = "";
    132       } else if (obj.type == "atom") {
    133         base = 1;
    134       } else if (obj.type == "function") {
    135         if (global.jQuery != null && (obj.string == '$' || obj.string == 'jQuery') &&
    136             (typeof global.jQuery == 'function'))
    137           base = global.jQuery();
    138         else if (global._ != null && (obj.string == '_') && (typeof global._ == 'function'))
    139           base = global._();
    140       }
    141       while (base != null && context.length)
    142         base = base[context.pop().string];
    143       if (base != null) gatherCompletions(base);
    144     } else {
    145       // If not, just look in the global object and any local scope
    146       // (reading into JS mode internals to get at the local and global variables)
    147       for (var v = token.state.localVars; v; v = v.next) maybeAdd(v.name);
    148       for (var v = token.state.globalVars; v; v = v.next) maybeAdd(v.name);
    149       if (!options || options.useGlobalScope !== false)
    150         gatherCompletions(global);
    151       forEach(keywords, maybeAdd);
    152     }
    153     return found;
    154   }
    155 });