Commit | Line | Data |
---|---|---|
837afb80 TD |
1 | (function(mod) { |
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 | |
7 | mod(CodeMirror); | |
8 | })(function(CodeMirror) { | |
9 | "use strict"; | |
10 | ||
77b7b761 TD |
11 | function forEach(arr, f) { |
12 | for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]); | |
13 | } | |
14 | ||
15 | function arrayContains(arr, item) { | |
16 | if (!Array.prototype.indexOf) { | |
17 | var i = arr.length; | |
18 | while (i--) { | |
19 | if (arr[i] === item) { | |
20 | return true; | |
21 | } | |
22 | } | |
23 | return false; | |
24 | } | |
25 | return arr.indexOf(item) != -1; | |
26 | } | |
27 | ||
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. | |
32 | ||
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}; | |
36 | } | |
37 | ||
38 | if (!context) var context = []; | |
39 | context.push(tprop); | |
40 | ||
41 | var completionList = getCompletions(token, context); | |
42 | completionList = completionList.sort(); | |
77b7b761 TD |
43 | |
44 | return {list: completionList, | |
45 | from: CodeMirror.Pos(cur.line, token.start), | |
46 | to: CodeMirror.Pos(cur.line, token.end)}; | |
47 | } | |
48 | ||
837afb80 | 49 | function pythonHint(editor) { |
77b7b761 | 50 | return scriptHint(editor, pythonKeywordsU, function (e, cur) {return e.getTokenAt(cur);}); |
837afb80 TD |
51 | } |
52 | CodeMirror.registerHelper("hint", "python", pythonHint); | |
77b7b761 TD |
53 | |
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(" "); | |
58 | ||
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(" "); | |
69 | ||
70 | function getCompletions(token, context) { | |
71 | var found = [], start = token.string; | |
72 | function maybeAdd(str) { | |
837afb80 | 73 | if (str.lastIndexOf(start, 0) == 0 && !arrayContains(found, str)) found.push(str); |
77b7b761 TD |
74 | } |
75 | ||
76 | function gatherCompletions(_obj) { | |
77 | forEach(pythonBuiltinsL, maybeAdd); | |
78 | forEach(pythonBuiltinsU, maybeAdd); | |
79 | forEach(pythonKeywordsL, maybeAdd); | |
80 | forEach(pythonKeywordsU, maybeAdd); | |
81 | } | |
82 | ||
83 | if (context) { | |
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; | |
87 | ||
88 | if (obj.type == "variable") | |
89 | base = obj.string; | |
90 | else if(obj.type == "variable-3") | |
91 | base = ":" + obj.string; | |
92 | ||
93 | while (base != null && context.length) | |
94 | base = base[context.pop().string]; | |
95 | if (base != null) gatherCompletions(base); | |
96 | } | |
97 | return found; | |
98 | } | |
837afb80 | 99 | }); |