javascript-hint.js 5.4 KB

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