2
function forEach(arr, f) {
3
for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]);
6
function arrayContains(arr, item) {
7
if (!Array.prototype.indexOf) {
10
if (arr[i] === item) {
16
return arr.indexOf(item) != -1;
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};
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 == ')') {
35
tprop = getToken(editor, {line: cur.line, ch: tprop.start});
36
switch (tprop.string) {
37
case ')': level++; break;
38
case '(': level--; break;
42
tprop = getToken(editor, {line: cur.line, ch: tprop.start});
43
if (tprop.className == 'variable')
44
tprop.className = 'function';
45
else return; // no clue
47
if (!context) var context = [];
50
return {list: getCompletions(token, context, keywords),
51
from: {line: cur.line, ch: token.start},
52
to: {line: cur.line, ch: token.end}};
55
CodeMirror.javascriptHint = function(editor) {
56
return scriptHint(editor, javascriptKeywords,
57
function (e, cur) {return e.getTokenAt(cur);});
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;
68
token.className = "property";
70
else if (/^\.[\w$_]*$/.test(token.string)) {
71
token.className = "property";
73
token.string = token.string.replace(/\./, '');
78
CodeMirror.coffeescriptHint = function(editor) {
79
return scriptHint(editor, coffeescriptKeywords, getCoffeeScriptToken);
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(" ");
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);
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);
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")
112
else if (obj.className == "atom")
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'))
121
while (base != null && context.length)
122
base = base[context.pop().string];
123
if (base != null) gatherCompletions(base);
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);