5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror"], mod);
11
})(function(CodeMirror) {
12
var Pos = CodeMirror.Pos;
14
function forEach(arr, f) {
15
for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]);
18
function arrayContains(arr, item) {
19
if (!Array.prototype.indexOf) {
22
if (arr[i] === item) {
28
return arr.indexOf(item) != -1;
31
function scriptHint(editor, keywords, getToken, options) {
33
var cur = editor.getCursor(), token = getToken(editor, cur);
34
if (/\b(?:string|comment)\b/.test(token.type)) return;
35
var innerMode = CodeMirror.innerMode(editor.getMode(), token.state);
36
if (innerMode.mode.helperType === "json") return;
37
token.state = innerMode.state;
40
if (!/^[\w$_]*$/.test(token.string)) {
41
token = {start: cur.ch, end: cur.ch, string: "", state: token.state,
42
type: token.string == "." ? "property" : null};
43
} else if (token.end > cur.ch) {
45
token.string = token.string.slice(0, cur.ch - token.start);
50
while (tprop.type == "property") {
51
tprop = getToken(editor, Pos(cur.line, tprop.start));
52
if (tprop.string != ".") return;
53
tprop = getToken(editor, Pos(cur.line, tprop.start));
54
if (!context) var context = [];
57
return {list: getCompletions(token, context, keywords, options),
58
from: Pos(cur.line, token.start),
59
to: Pos(cur.line, token.end)};
62
function javascriptHint(editor, options) {
63
return scriptHint(editor, javascriptKeywords,
64
function (e, cur) {return e.getTokenAt(cur);},
67
CodeMirror.registerHelper("hint", "javascript", javascriptHint);
69
function getCoffeeScriptToken(editor, cur) {
73
var token = editor.getTokenAt(cur);
74
if (cur.ch == token.start + 1 && token.string.charAt(0) == '.') {
75
token.end = token.start;
77
token.type = "property";
79
else if (/^\.[\w$_]*$/.test(token.string)) {
80
token.type = "property";
82
token.string = token.string.replace(/\./, '');
87
function coffeescriptHint(editor, options) {
88
return scriptHint(editor, coffeescriptKeywords, getCoffeeScriptToken, options);
90
CodeMirror.registerHelper("hint", "coffeescript", coffeescriptHint);
92
var stringProps = ("charAt charCodeAt indexOf lastIndexOf substring substr slice trim trimLeft trimRight " +
93
"toUpperCase toLowerCase split concat match replace search").split(" ");
94
var arrayProps = ("length concat join splice push pop shift unshift slice reverse sort indexOf " +
95
"lastIndexOf every some filter forEach map reduce reduceRight ").split(" ");
96
var funcProps = "prototype apply call bind".split(" ");
97
var javascriptKeywords = ("break case catch class const continue debugger default delete do else export extends false finally for function " +
98
"if in import instanceof new null return super switch this throw true try typeof var void while with yield").split(" ");
99
var coffeescriptKeywords = ("and break catch class continue delete do else extends false finally for " +
100
"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(" ");
102
function forAllProps(obj, callback) {
103
if (!Object.getOwnPropertyNames || !Object.getPrototypeOf) {
104
for (var name in obj) callback(name)
106
for (var o = obj; o; o = Object.getPrototypeOf(o))
107
Object.getOwnPropertyNames(o).forEach(callback)
111
function getCompletions(token, context, keywords, options) {
112
var found = [], start = token.string, global = options && options.globalScope || window;
113
function maybeAdd(str) {
114
if (str.lastIndexOf(start, 0) == 0 && !arrayContains(found, str)) found.push(str);
116
function gatherCompletions(obj) {
117
if (typeof obj == "string") forEach(stringProps, maybeAdd);
118
else if (obj instanceof Array) forEach(arrayProps, maybeAdd);
119
else if (obj instanceof Function) forEach(funcProps, maybeAdd);
120
forAllProps(obj, maybeAdd)
123
if (context && context.length) {
126
var obj = context.pop(), base;
127
if (obj.type && obj.type.indexOf("variable") === 0) {
128
if (options && options.additionalContext)
129
base = options.additionalContext[obj.string];
130
if (!options || options.useGlobalScope !== false)
131
base = base || global[obj.string];
132
} else if (obj.type == "string") {
134
} else if (obj.type == "atom") {
136
} else if (obj.type == "function") {
137
if (global.jQuery != null && (obj.string == '$' || obj.string == 'jQuery') &&
138
(typeof global.jQuery == 'function'))
139
base = global.jQuery();
140
else if (global._ != null && (obj.string == '_') && (typeof global._ == 'function'))
143
while (base != null && context.length)
144
base = base[context.pop().string];
145
if (base != null) gatherCompletions(base);
149
for (var v = token.state.localVars; v; v = v.next) maybeAdd(v.name);
150
for (var c = token.state.context; c; c = c.prev)
151
for (var v = c.vars; v; v = v.next) maybeAdd(v.name)
152
for (var v = token.state.globalVars; v; v = v.next) maybeAdd(v.name);
153
if (options && options.additionalContext != null)
154
for (var key in options.additionalContext)
156
if (!options || options.useGlobalScope !== false)
157
gatherCompletions(global);
158
forEach(keywords, maybeAdd);