Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50665 views
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
24
if (!/^[\w$_]*$/.test(token.string)) {
25
token = tprop = {start: cur.ch, end: cur.ch, string: "", state: token.state,
26
className: token.string == ":" ? "python-type" : null};
27
}
28
29
if (!context) var context = [];
30
context.push(tprop);
31
32
var completionList = getCompletions(token, context);
33
completionList = completionList.sort();
34
35
return {list: completionList,
36
from: CodeMirror.Pos(cur.line, token.start),
37
to: CodeMirror.Pos(cur.line, token.end)};
38
}
39
40
function pythonHint(editor) {
41
return scriptHint(editor, pythonKeywordsU, function (e, cur) {return e.getTokenAt(cur);});
42
}
43
CodeMirror.pythonHint = pythonHint; // deprecated
44
CodeMirror.registerHelper("hint", "python", pythonHint);
45
46
var pythonKeywords = "and del from not while as elif global or with assert else if pass yield"
47
+ "break except import print class exec in raise continue finally is return def for lambda try";
48
var pythonKeywordsL = pythonKeywords.split(" ");
49
var pythonKeywordsU = pythonKeywords.toUpperCase().split(" ");
50
51
var pythonBuiltins = "abs divmod input open staticmethod all enumerate int ord str "
52
+ "any eval isinstance pow sum basestring execfile issubclass print super"
53
+ "bin file iter property tuple bool filter len range type"
54
+ "bytearray float list raw_input unichr callable format locals reduce unicode"
55
+ "chr frozenset long reload vars classmethod getattr map repr xrange"
56
+ "cmp globals max reversed zip compile hasattr memoryview round __import__"
57
+ "complex hash min set apply delattr help next setattr buffer"
58
+ "dict hex object slice coerce dir id oct sorted intern ";
59
var pythonBuiltinsL = pythonBuiltins.split(" ").join("() ").split(" ");
60
var pythonBuiltinsU = pythonBuiltins.toUpperCase().split(" ").join("() ").split(" ");
61
62
function getCompletions(token, context) {
63
var found = [], start = token.string;
64
function maybeAdd(str) {
65
if (str.lastIndexOf(start, 0) == 0 && !arrayContains(found, str)) found.push(str);
66
}
67
68
function gatherCompletions(_obj) {
69
forEach(pythonBuiltinsL, maybeAdd);
70
forEach(pythonBuiltinsU, maybeAdd);
71
forEach(pythonKeywordsL, maybeAdd);
72
forEach(pythonKeywordsU, maybeAdd);
73
}
74
75
if (context) {
76
// If this is a property, see if it belongs to some object we can
77
// find in the current environment.
78
var obj = context.pop(), base;
79
80
if (obj.type == "variable")
81
base = obj.string;
82
else if(obj.type == "variable-3")
83
base = ":" + obj.string;
84
85
while (base != null && context.length)
86
base = base[context.pop().string];
87
if (base != null) gatherCompletions(base);
88
}
89
return found;
90
}
91
})();
92
93