codemirror-python-hint.js 3.4 KB

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