javascript-hint.js 5.5 KB

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