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