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