python-hint.js 3.5 KB

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