| 1 |
(function () { |
| 2 |
var Pos = CodeMirror.Pos; |
| 3 |
|
| 4 |
function forEach(arr, f) { |
| 5 |
for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]); |
| 6 |
} |
| 7 |
|
| 8 |
function arrayContains(arr, item) { |
| 9 |
if (!Array.prototype.indexOf) { |
| 10 |
var i = arr.length; |
| 11 |
while (i--) { |
| 12 |
if (arr[i] === item) { |
| 13 |
return true; |
| 14 |
} |
| 15 |
} |
| 16 |
return false; |
| 17 |
} |
| 18 |
return arr.indexOf(item) != -1; |
| 19 |
} |
| 20 |
|
| 21 |
function scriptHint(editor, keywords, getToken, options) { |
| 22 |
// Find the token at the cursor |
| 23 |
var cur = editor.getCursor(), token = getToken(editor, cur), tprop = token; |
| 24 |
token.state = CodeMirror.innerMode(editor.getMode(), token.state).state; |
| 25 |
|
| 26 |
// If it's not a 'word-style' token, ignore the token. |
| 27 |
if (!/^[\w$_]*$/.test(token.string)) { |
| 28 |
token = tprop = {start: cur.ch, end: cur.ch, string: "", state: token.state, |
| 29 |
type: token.string == "." ? "property" : null}; |
| 30 |
} |
| 31 |
// If it is a property, find out what it is a property of. |
| 32 |
while (tprop.type == "property") { |
| 33 |
tprop = getToken(editor, Pos(cur.line, tprop.start)); |
| 34 |
if (tprop.string != ".") return; |
| 35 |
tprop = getToken(editor, Pos(cur.line, tprop.start)); |
| 36 |
if (tprop.string == ')') { |
| 37 |
var level = 1; |
| 38 |
do { |
| 39 |
tprop = getToken(editor, Pos(cur.line, tprop.start)); |
| 40 |
switch (tprop.string) { |
| 41 |
case ')': level++; break; |
| 42 |
case '(': level--; break; |
| 43 |
default: break; |
| 44 |
} |
| 45 |
} while (level > 0); |
| 46 |
tprop = getToken(editor, Pos(cur.line, tprop.start)); |
| 47 |
if (tprop.type.indexOf("variable") === 0) |
| 48 |
tprop.type = "function"; |
| 49 |
else return; // no clue |
| 50 |
} |
| 51 |
if (!context) var context = []; |
| 52 |
context.push(tprop); |
| 53 |
} |
| 54 |
return {list: getCompletions(token, context, keywords, options), |
| 55 |
from: Pos(cur.line, token.start), |
| 56 |
to: Pos(cur.line, token.end)}; |
| 57 |
} |
| 58 |
|
| 59 |
function javascriptHint(editor, options) { |
| 60 |
return scriptHint(editor, javascriptKeywords, |
| 61 |
function (e, cur) {return e.getTokenAt(cur);}, |
| 62 |
options); |
| 63 |
}; |
| 64 |
CodeMirror.javascriptHint = javascriptHint; // deprecated |
| 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.coffeescriptHint = coffeescriptHint; // deprecated |
| 89 |
CodeMirror.registerHelper("hint", "coffeescript", coffeescriptHint); |
| 90 |
|
| 91 |
var stringProps = ("charAt charCodeAt indexOf lastIndexOf substring substr slice trim trimLeft trimRight " + |
| 92 |
"toUpperCase toLowerCase split concat match replace search").split(" "); |
| 93 |
var arrayProps = ("length concat join splice push pop shift unshift slice reverse sort indexOf " + |
| 94 |
"lastIndexOf every some filter forEach map reduce reduceRight ").split(" "); |
| 95 |
var funcProps = "prototype apply call bind".split(" "); |
| 96 |
var javascriptKeywords = ("break case catch continue debugger default delete do else false finally for function " + |
| 97 |
"if in instanceof new null return switch throw true try typeof var void while with").split(" "); |
| 98 |
var coffeescriptKeywords = ("and break catch class continue delete do else extends false finally for " + |
| 99 |
"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(" "); |
| 100 |
|
| 101 |
function getCompletions(token, context, keywords, options) { |
| 102 |
var found = [], start = token.string; |
| 103 |
function maybeAdd(str) { |
| 104 |
if (str.indexOf(start) == 0 && !arrayContains(found, str)) found.push(str); |
| 105 |
} |
| 106 |
function gatherCompletions(obj) { |
| 107 |
if (typeof obj == "string") forEach(stringProps, maybeAdd); |
| 108 |
else if (obj instanceof Array) forEach(arrayProps, maybeAdd); |
| 109 |
else if (obj instanceof Function) forEach(funcProps, maybeAdd); |
| 110 |
for (var name in obj) maybeAdd(name); |
| 111 |
} |
| 112 |
|
| 113 |
if (context) { |
| 114 |
// If this is a property, see if it belongs to some object we can |
| 115 |
// find in the current environment. |
| 116 |
var obj = context.pop(), base; |
| 117 |
if (obj.type.indexOf("variable") === 0) { |
| 118 |
if (options && options.additionalContext) |
| 119 |
base = options.additionalContext[obj.string]; |
| 120 |
base = base || window[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 (window.jQuery != null && (obj.string == '$' || obj.string == 'jQuery') && |
| 127 |
(typeof window.jQuery == 'function')) |
| 128 |
base = window.jQuery(); |
| 129 |
else if (window._ != null && (obj.string == '_') && (typeof window._ == 'function')) |
| 130 |
base = window._(); |
| 131 |
} |
| 132 |
while (base != null && context.length) |
| 133 |
base = base[context.pop().string]; |
| 134 |
if (base != null) gatherCompletions(base); |
| 135 |
} |
| 136 |
else { |
| 137 |
// If not, just look in the window object and any local scope |
| 138 |
// (reading into JS mode internals to get at the local and global variables) |
| 139 |
for (var v = token.state.localVars; v; v = v.next) maybeAdd(v.name); |
| 140 |
for (var v = token.state.globalVars; v; v = v.next) maybeAdd(v.name); |
| 141 |
gatherCompletions(window); |
| 142 |
forEach(keywords, maybeAdd); |
| 143 |
} |
| 144 |
return found; |
| 145 |
} |
| 146 |
})(); |
| 147 |
|