Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/addon/hint/javascript-hint.js
2074 views
(function () {1var Pos = CodeMirror.Pos;23function forEach(arr, f) {4for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]);5}67function arrayContains(arr, item) {8if (!Array.prototype.indexOf) {9var i = arr.length;10while (i--) {11if (arr[i] === item) {12return true;13}14}15return false;16}17return arr.indexOf(item) != -1;18}1920function scriptHint(editor, keywords, getToken, options) {21// Find the token at the cursor22var cur = editor.getCursor(), token = getToken(editor, cur), tprop = token;23if (/\b(?:string|comment)\b/.test(token.type)) return;24token.state = CodeMirror.innerMode(editor.getMode(), token.state).state;2526// If it's not a 'word-style' token, ignore the token.27if (!/^[\w$_]*$/.test(token.string)) {28token = tprop = {start: cur.ch, end: cur.ch, string: "", state: token.state,29type: token.string == "." ? "property" : null};30}31// If it is a property, find out what it is a property of.32while (tprop.type == "property") {33tprop = getToken(editor, Pos(cur.line, tprop.start));34if (tprop.string != ".") return;35tprop = getToken(editor, Pos(cur.line, tprop.start));36if (!context) var context = [];37context.push(tprop);38}39return {list: getCompletions(token, context, keywords, options),40from: Pos(cur.line, token.start),41to: Pos(cur.line, token.end)};42}4344function javascriptHint(editor, options) {45return scriptHint(editor, javascriptKeywords,46function (e, cur) {return e.getTokenAt(cur);},47options);48};49CodeMirror.javascriptHint = javascriptHint; // deprecated50CodeMirror.registerHelper("hint", "javascript", javascriptHint);5152function getCoffeeScriptToken(editor, cur) {53// This getToken, it is for coffeescript, imitates the behavior of54// getTokenAt method in javascript.js, that is, returning "property"55// type and treat "." as indepenent token.56var token = editor.getTokenAt(cur);57if (cur.ch == token.start + 1 && token.string.charAt(0) == '.') {58token.end = token.start;59token.string = '.';60token.type = "property";61}62else if (/^\.[\w$_]*$/.test(token.string)) {63token.type = "property";64token.start++;65token.string = token.string.replace(/\./, '');66}67return token;68}6970function coffeescriptHint(editor, options) {71return scriptHint(editor, coffeescriptKeywords, getCoffeeScriptToken, options);72}73CodeMirror.coffeescriptHint = coffeescriptHint; // deprecated74CodeMirror.registerHelper("hint", "coffeescript", coffeescriptHint);7576var stringProps = ("charAt charCodeAt indexOf lastIndexOf substring substr slice trim trimLeft trimRight " +77"toUpperCase toLowerCase split concat match replace search").split(" ");78var arrayProps = ("length concat join splice push pop shift unshift slice reverse sort indexOf " +79"lastIndexOf every some filter forEach map reduce reduceRight ").split(" ");80var funcProps = "prototype apply call bind".split(" ");81var javascriptKeywords = ("break case catch continue debugger default delete do else false finally for function " +82"if in instanceof new null return switch throw true try typeof var void while with").split(" ");83var coffeescriptKeywords = ("and break catch class continue delete do else extends false finally for " +84"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(" ");8586function getCompletions(token, context, keywords, options) {87var found = [], start = token.string;88function maybeAdd(str) {89if (str.lastIndexOf(start, 0) == 0 && !arrayContains(found, str)) found.push(str);90}91function gatherCompletions(obj) {92if (typeof obj == "string") forEach(stringProps, maybeAdd);93else if (obj instanceof Array) forEach(arrayProps, maybeAdd);94else if (obj instanceof Function) forEach(funcProps, maybeAdd);95for (var name in obj) maybeAdd(name);96}9798if (context && context.length) {99// If this is a property, see if it belongs to some object we can100// find in the current environment.101var obj = context.pop(), base;102if (obj.type && obj.type.indexOf("variable") === 0) {103if (options && options.additionalContext)104base = options.additionalContext[obj.string];105base = base || window[obj.string];106} else if (obj.type == "string") {107base = "";108} else if (obj.type == "atom") {109base = 1;110} else if (obj.type == "function") {111if (window.jQuery != null && (obj.string == '$' || obj.string == 'jQuery') &&112(typeof window.jQuery == 'function'))113base = window.jQuery();114else if (window._ != null && (obj.string == '_') && (typeof window._ == 'function'))115base = window._();116}117while (base != null && context.length)118base = base[context.pop().string];119if (base != null) gatherCompletions(base);120} else {121// If not, just look in the window object and any local scope122// (reading into JS mode internals to get at the local and global variables)123for (var v = token.state.localVars; v; v = v.next) maybeAdd(v.name);124for (var v = token.state.globalVars; v; v = v.next) maybeAdd(v.name);125gatherCompletions(window);126forEach(keywords, maybeAdd);127}128return found;129}130})();131132133