2 if (typeof exports
== "object" && typeof module
== "object") // CommonJS
3 mod(require("../../lib/codemirror"));
4 else if (typeof define
== "function" && define
.amd
) // AMD
5 define(["../../lib/codemirror"], mod
);
6 else // Plain browser env
8 })(function(CodeMirror
) {
11 function forEach(arr
, f
) {
12 for (var i
= 0, e
= arr
.length
; i
< e
; ++i
) f(arr
[i
]);
15 function arrayContains(arr
, item
) {
16 if (!Array
.prototype.indexOf
) {
19 if (arr
[i
] === item
) {
25 return arr
.indexOf(item
) != -1;
28 function scriptHint(editor
, _keywords
, getToken
) {
29 // Find the token at the cursor
30 var cur
= editor
.getCursor(), token
= getToken(editor
, cur
), tprop
= token
;
31 // If it's not a 'word-style' token, ignore the token.
33 if (!/^[\w$_]*$/.test(token
.string
)) {
34 token
= tprop
= {start
: cur
.ch
, end
: cur
.ch
, string
: "", state
: token
.state
,
35 className
: token
.string
== ":" ? "python-type" : null};
38 if (!context
) var context
= [];
41 var completionList
= getCompletions(token
, context
);
42 completionList
= completionList
.sort();
44 return {list
: completionList
,
45 from: CodeMirror
.Pos(cur
.line
, token
.start
),
46 to
: CodeMirror
.Pos(cur
.line
, token
.end
)};
49 function pythonHint(editor
) {
50 return scriptHint(editor
, pythonKeywordsU
, function (e
, cur
) {return e
.getTokenAt(cur
);});
52 CodeMirror
.registerHelper("hint", "python", pythonHint
);
54 var pythonKeywords
= "and del from not while as elif global or with assert else if pass yield"
55 + "break except import print class exec in raise continue finally is return def for lambda try";
56 var pythonKeywordsL
= pythonKeywords
.split(" ");
57 var pythonKeywordsU
= pythonKeywords
.toUpperCase().split(" ");
59 var pythonBuiltins
= "abs divmod input open staticmethod all enumerate int ord str "
60 + "any eval isinstance pow sum basestring execfile issubclass print super"
61 + "bin file iter property tuple bool filter len range type"
62 + "bytearray float list raw_input unichr callable format locals reduce unicode"
63 + "chr frozenset long reload vars classmethod getattr map repr xrange"
64 + "cmp globals max reversed zip compile hasattr memoryview round __import__"
65 + "complex hash min set apply delattr help next setattr buffer"
66 + "dict hex object slice coerce dir id oct sorted intern ";
67 var pythonBuiltinsL
= pythonBuiltins
.split(" ").join("() ").split(" ");
68 var pythonBuiltinsU
= pythonBuiltins
.toUpperCase().split(" ").join("() ").split(" ");
70 function getCompletions(token
, context
) {
71 var found
= [], start
= token
.string
;
72 function maybeAdd(str
) {
73 if (str
.lastIndexOf(start
, 0) == 0 && !arrayContains(found
, str
)) found
.push(str
);
76 function gatherCompletions(_obj
) {
77 forEach(pythonBuiltinsL
, maybeAdd
);
78 forEach(pythonBuiltinsU
, maybeAdd
);
79 forEach(pythonKeywordsL
, maybeAdd
);
80 forEach(pythonKeywordsU
, maybeAdd
);
84 // If this is a property, see if it belongs to some object we can
85 // find in the current environment.
86 var obj
= context
.pop(), base
;
88 if (obj
.type
== "variable")
90 else if(obj
.type
== "variable-3")
91 base
= ":" + obj
.string
;
93 while (base
!= null && context
.length
)
94 base
= base
[context
.pop().string
];
95 if (base
!= null) gatherCompletions(base
);