javascript-hint.js 5.8 KB

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