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