Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
1
// CodeMirror, copyright (c) by Marijn Haverbeke and others
2
// Distributed under an MIT license: http://codemirror.net/LICENSE
3
4
(function(mod) {
5
if (typeof exports == "object" && typeof module == "object") // CommonJS
6
mod(require("codemirror"));
7
else if (typeof define == "function" && define.amd) // AMD
8
define(["../../lib/codemirror"], mod);
9
else // Plain browser env
10
mod(CodeMirror);
11
})(function(CodeMirror) {
12
"use strict";
13
14
function forEach(arr, f) {
15
for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]);
16
}
17
18
function arrayContains(arr, item) {
19
if (!Array.prototype.indexOf) {
20
var i = arr.length;
21
while (i--) {
22
if (arr[i] === item) {
23
return true;
24
}
25
}
26
return false;
27
}
28
return arr.indexOf(item) != -1;
29
}
30
31
function scriptHint(editor, _keywords, getToken) {
32
// Find the token at the cursor
33
var cur = editor.getCursor(), token = getToken(editor, cur), tprop = token;
34
// If it's not a 'word-style' token, ignore the token.
35
36
if (!/^[\w$_]*$/.test(token.string)) {
37
token = tprop = {start: cur.ch, end: cur.ch, string: "", state: token.state,
38
className: token.string == ":" ? "python-type" : null};
39
}
40
41
if (!context) var context = [];
42
context.push(tprop);
43
44
var completionList = getCompletions(token, context);
45
completionList = completionList.sort();
46
47
return {list: completionList,
48
from: CodeMirror.Pos(cur.line, token.start),
49
to: CodeMirror.Pos(cur.line, token.end)};
50
}
51
52
function pythonHint(editor) {
53
return scriptHint(editor, pythonKeywordsU, function (e, cur) {return e.getTokenAt(cur);});
54
}
55
CodeMirror.registerHelper("hint", "python", pythonHint);
56
57
var pythonKeywords = "and del from not while as elif global or with assert else if pass yield"
58
+ "break except import print class exec in raise continue finally is return def for lambda try";
59
var pythonKeywordsL = pythonKeywords.split(" ");
60
var pythonKeywordsU = pythonKeywords.toUpperCase().split(" ");
61
62
var pythonBuiltins = "abs divmod input open staticmethod all enumerate int ord str "
63
+ "any eval isinstance pow sum basestring execfile issubclass print super"
64
+ "bin file iter property tuple bool filter len range type"
65
+ "bytearray float list raw_input unichr callable format locals reduce unicode"
66
+ "chr frozenset long reload vars classmethod getattr map repr xrange"
67
+ "cmp globals max reversed zip compile hasattr memoryview round __import__"
68
+ "complex hash min set apply delattr help next setattr buffer"
69
+ "dict hex object slice coerce dir id oct sorted intern ";
70
var pythonBuiltinsL = pythonBuiltins.split(" ").join("() ").split(" ");
71
var pythonBuiltinsU = pythonBuiltins.toUpperCase().split(" ").join("() ").split(" ");
72
73
function getCompletions(token, context) {
74
var found = [], start = token.string;
75
function maybeAdd(str) {
76
if (str.lastIndexOf(start, 0) == 0 && !arrayContains(found, str)) found.push(str);
77
}
78
79
function gatherCompletions(_obj) {
80
forEach(pythonBuiltinsL, maybeAdd);
81
forEach(pythonBuiltinsU, maybeAdd);
82
forEach(pythonKeywordsL, maybeAdd);
83
forEach(pythonKeywordsU, maybeAdd);
84
}
85
86
if (context) {
87
// If this is a property, see if it belongs to some object we can
88
// find in the current environment.
89
var obj = context.pop(), base;
90
91
if (obj.type == "variable")
92
base = obj.string;
93
else if(obj.type == "variable-3")
94
base = ":" + obj.string;
95
96
while (base != null && context.length)
97
base = base[context.pop().string];
98
if (base != null) gatherCompletions(base);
99
}
100
return found;
101
}
102
});
103
104