| 1 |
(function () { |
| 2 |
function forEach(arr, f) { |
| 3 |
for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]); |
| 4 |
} |
| 5 |
|
| 6 |
function arrayContains(arr, item) { |
| 7 |
if (!Array.prototype.indexOf) { |
| 8 |
var i = arr.length; |
| 9 |
while (i--) { |
| 10 |
if (arr[i] === item) { |
| 11 |
return true; |
| 12 |
} |
| 13 |
} |
| 14 |
return false; |
| 15 |
} |
| 16 |
return arr.indexOf(item) != -1; |
| 17 |
} |
| 18 |
|
| 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. |
| 23 |
|
| 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}; |
| 27 |
} |
| 28 |
|
| 29 |
if (!context) var context = []; |
| 30 |
context.push(tprop); |
| 31 |
|
| 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(" "); |
| 37 |
} |
| 38 |
|
| 39 |
return {list: completionList, |
| 40 |
from: CodeMirror.Pos(cur.line, token.start), |
| 41 |
to: CodeMirror.Pos(cur.line, token.end)}; |
| 42 |
} |
| 43 |
|
| 44 |
function pythonHint(editor) { |
| 45 |
return scriptHint(editor, pythonKeywordsU, function (e, cur) {return e.getTokenAt(cur);}); |
| 46 |
} |
| 47 |
CodeMirror.pythonHint = pythonHint; // deprecated |
| 48 |
CodeMirror.registerHelper("hint", "python", pythonHint); |
| 49 |
|
| 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(" "); |
| 54 |
|
| 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(" "); |
| 65 |
|
| 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); |
| 70 |
} |
| 71 |
|
| 72 |
function gatherCompletions(_obj) { |
| 73 |
forEach(pythonBuiltinsL, maybeAdd); |
| 74 |
forEach(pythonBuiltinsU, maybeAdd); |
| 75 |
forEach(pythonKeywordsL, maybeAdd); |
| 76 |
forEach(pythonKeywordsU, maybeAdd); |
| 77 |
} |
| 78 |
|
| 79 |
if (context) { |
| 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; |
| 83 |
|
| 84 |
if (obj.type == "variable") |
| 85 |
base = obj.string; |
| 86 |
else if(obj.type == "variable-3") |
| 87 |
base = ":" + obj.string; |
| 88 |
|
| 89 |
while (base != null && context.length) |
| 90 |
base = base[context.pop().string]; |
| 91 |
if (base != null) gatherCompletions(base); |
| 92 |
} |
| 93 |
return found; |
| 94 |
} |
| 95 |
})(); |
| 96 |
|