javascript-hint.js 5.6 KB

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