javascript-hint.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 == "." ? "property" : null};
  24. }
  25. // If it is a property, find out what it is a property of.
  26. while (tprop.className == "property") {
  27. tprop = getToken(editor, {line: cur.line, ch: tprop.start});
  28. if (tprop.string != ".") return;
  29. tprop = getToken(editor, {line: cur.line, ch: tprop.start});
  30. if (tprop.string == ')') {
  31. var level = 1;
  32. do {
  33. tprop = getToken(editor, {line: cur.line, ch: tprop.start});
  34. switch (tprop.string) {
  35. case ')': level++; break;
  36. case '(': level--; break;
  37. default: break;
  38. }
  39. } while (level > 0);
  40. tprop = getToken(editor, {line: cur.line, ch: tprop.start});
  41. if (tprop.className == 'variable')
  42. tprop.className = 'function';
  43. else return; // no clue
  44. }
  45. if (!context) var context = [];
  46. context.push(tprop);
  47. }
  48. return {list: getCompletions(token, context, keywords),
  49. from: {line: cur.line, ch: token.start},
  50. to: {line: cur.line, ch: token.end}};
  51. }
  52. CodeMirror.javascriptHint = function(editor) {
  53. return scriptHint(editor, javascriptKeywords,
  54. function (e, cur) {return e.getTokenAt(cur);});
  55. };
  56. function getCoffeeScriptToken(editor, cur) {
  57. // This getToken, it is for coffeescript, imitates the behavior of
  58. // getTokenAt method in javascript.js, that is, returning "property"
  59. // type and treat "." as indepenent token.
  60. var token = editor.getTokenAt(cur);
  61. if (cur.ch == token.start + 1 && token.string.charAt(0) == '.') {
  62. token.end = token.start;
  63. token.string = '.';
  64. token.className = "property";
  65. }
  66. else if (/^\.[\w$_]*$/.test(token.string)) {
  67. token.className = "property";
  68. token.start++;
  69. token.string = token.string.replace(/\./, '');
  70. }
  71. return token;
  72. }
  73. CodeMirror.coffeescriptHint = function(editor) {
  74. return scriptHint(editor, coffeescriptKeywords, getCoffeeScriptToken);
  75. };
  76. var stringProps = ("charAt charCodeAt indexOf lastIndexOf substring substr slice trim trimLeft trimRight " +
  77. "toUpperCase toLowerCase split concat match replace search").split(" ");
  78. var arrayProps = ("length concat join splice push pop shift unshift slice reverse sort indexOf " +
  79. "lastIndexOf every some filter forEach map reduce reduceRight ").split(" ");
  80. var funcProps = "prototype apply call bind".split(" ");
  81. var javascriptKeywords = ("break case catch continue debugger default delete do else false finally for function " +
  82. "if in instanceof new null return switch throw true try typeof var void while with").split(" ");
  83. var coffeescriptKeywords = ("and break catch class continue delete do else extends false finally for " +
  84. "if in instanceof isnt new no not null of off on or return switch then throw true try typeof until void while with yes").split(" ");
  85. function getCompletions(token, context, keywords) {
  86. var found = [], start = token.string;
  87. function maybeAdd(str) {
  88. if (str.indexOf(start) == 0 && !arrayContains(found, str)) found.push(str);
  89. }
  90. function gatherCompletions(obj) {
  91. if (typeof obj == "string") forEach(stringProps, maybeAdd);
  92. else if (obj instanceof Array) forEach(arrayProps, maybeAdd);
  93. else if (obj instanceof Function) forEach(funcProps, maybeAdd);
  94. for (var name in obj) maybeAdd(name);
  95. }
  96. if (context) {
  97. // If this is a property, see if it belongs to some object we can
  98. // find in the current environment.
  99. var obj = context.pop(), base;
  100. if (obj.className == "variable")
  101. base = window[obj.string];
  102. else if (obj.className == "string")
  103. base = "";
  104. else if (obj.className == "atom")
  105. base = 1;
  106. else if (obj.className == "function") {
  107. if (window.jQuery != null && (obj.string == '$' || obj.string == 'jQuery') &&
  108. (typeof window.jQuery == 'function'))
  109. base = window.jQuery();
  110. else if (window._ != null && (obj.string == '_') && (typeof window._ == 'function'))
  111. base = window._();
  112. }
  113. while (base != null && context.length)
  114. base = base[context.pop().string];
  115. if (base != null) gatherCompletions(base);
  116. }
  117. else {
  118. // If not, just look in the window object and any local scope
  119. // (reading into JS mode internals to get at the local variables)
  120. for (var v = token.state.localVars; v; v = v.next) maybeAdd(v.name);
  121. gatherCompletions(window);
  122. forEach(keywords, maybeAdd);
  123. }
  124. return found;
  125. }
  126. })();