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.
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};
29 if (!context) var context = [];
32 var completionList = getCompletions(token, context);
33 completionList = completionList.sort();
34 //prevent autocomplete for last word, instead show dropdown with one word
35 if(completionList.length == 1) {
36 completionList.push(" ");
39 return {list: completionList,
40 from: CodeMirror.Pos(cur.line, token.start),
41 to: CodeMirror.Pos(cur.line, token.end)};
44 function pythonHint(editor) {
45 return scriptHint(editor, pythonKeywordsU, function (e, cur) {return e.getTokenAt(cur);});
47 CodeMirror.pythonHint = pythonHint; // deprecated
48 CodeMirror.registerHelper("hint", "python", pythonHint);
50 var pythonKeywords = "and del from not while as elif global or with assert else if pass yield"
51 + "break except import print class exec in raise continue finally is return def for lambda try";
52 var pythonKeywordsL = pythonKeywords.split(" ");
53 var pythonKeywordsU = pythonKeywords.toUpperCase().split(" ");
55 var pythonBuiltins = "abs divmod input open staticmethod all enumerate int ord str "
56 + "any eval isinstance pow sum basestring execfile issubclass print super"
57 + "bin file iter property tuple bool filter len range type"
58 + "bytearray float list raw_input unichr callable format locals reduce unicode"
59 + "chr frozenset long reload vars classmethod getattr map repr xrange"
60 + "cmp globals max reversed zip compile hasattr memoryview round __import__"
61 + "complex hash min set apply delattr help next setattr buffer"
62 + "dict hex object slice coerce dir id oct sorted intern ";
63 var pythonBuiltinsL = pythonBuiltins.split(" ").join("() ").split(" ");
64 var pythonBuiltinsU = pythonBuiltins.toUpperCase().split(" ").join("() ").split(" ");
66 function getCompletions(token, context) {
67 var found = [], start = token.string;
68 function maybeAdd(str) {
69 if (str.indexOf(start) == 0 && !arrayContains(found, str)) found.push(str);
72 function gatherCompletions(_obj) {
73 forEach(pythonBuiltinsL, maybeAdd);
74 forEach(pythonBuiltinsU, maybeAdd);
75 forEach(pythonKeywordsL, maybeAdd);
76 forEach(pythonKeywordsU, maybeAdd);
80 // If this is a property, see if it belongs to some object we can
81 // find in the current environment.
82 var obj = context.pop(), base;
84 if (obj.type == "variable")
86 else if(obj.type == "variable-3")
87 base = ":" + obj.string;
89 while (base != null && context.length)
90 base = base[context.pop().string];
91 if (base != null) gatherCompletions(base);