javascript-hint.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. var Pos = CodeMirror.Pos;
  12. function forEach(arr, f) {
  13. for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]);
  14. }
  15. function arrayContains(arr, item) {
  16. if (!Array.prototype.indexOf) {
  17. var i = arr.length;
  18. while (i--) {
  19. if (arr[i] === item) {
  20. return true;
  21. }
  22. }
  23. return false;
  24. }
  25. return arr.indexOf(item) != -1;
  26. }
  27. function scriptHint(editor, keywords, getToken, options) {
  28. // Find the token at the cursor
  29. var cur = editor.getCursor(), token = getToken(editor, cur);
  30. if (/\b(?:string|comment)\b/.test(token.type)) return;
  31. token.state = CodeMirror.innerMode(editor.getMode(), token.state).state;
  32. // If it's not a 'word-style' token, ignore the token.
  33. if (!/^[\w$_]*$/.test(token.string)) {
  34. token = {start: cur.ch, end: cur.ch, string: "", state: token.state,
  35. type: token.string == "." ? "property" : null};
  36. } else if (token.end > cur.ch) {
  37. token.end = cur.ch;
  38. token.string = token.string.slice(0, cur.ch - token.start);
  39. }
  40. var tprop = token;
  41. // If it is a property, find out what it is a property of.
  42. while (tprop.type == "property") {
  43. tprop = getToken(editor, Pos(cur.line, tprop.start));
  44. if (tprop.string != ".") return;
  45. tprop = getToken(editor, Pos(cur.line, tprop.start));
  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. function javascriptHint(editor, options) {
  54. return scriptHint(editor, javascriptKeywords,
  55. function (e, cur) {return e.getTokenAt(cur);},
  56. options);
  57. };
  58. CodeMirror.registerHelper("hint", "javascript", javascriptHint);
  59. function getCoffeeScriptToken(editor, cur) {
  60. // This getToken, it is for coffeescript, imitates the behavior of
  61. // getTokenAt method in javascript.js, that is, returning "property"
  62. // type and treat "." as indepenent token.
  63. var token = editor.getTokenAt(cur);
  64. if (cur.ch == token.start + 1 && token.string.charAt(0) == '.') {
  65. token.end = token.start;
  66. token.string = '.';
  67. token.type = "property";
  68. }
  69. else if (/^\.[\w$_]*$/.test(token.string)) {
  70. token.type = "property";
  71. token.start++;
  72. token.string = token.string.replace(/\./, '');
  73. }
  74. return token;
  75. }
  76. function coffeescriptHint(editor, options) {
  77. return scriptHint(editor, coffeescriptKeywords, getCoffeeScriptToken, options);
  78. }
  79. CodeMirror.registerHelper("hint", "coffeescript", coffeescriptHint);
  80. var stringProps = ("charAt charCodeAt indexOf lastIndexOf substring substr slice trim trimLeft trimRight " +
  81. "toUpperCase toLowerCase split concat match replace search").split(" ");
  82. var arrayProps = ("length concat join splice push pop shift unshift slice reverse sort indexOf " +
  83. "lastIndexOf every some filter forEach map reduce reduceRight ").split(" ");
  84. var funcProps = "prototype apply call bind".split(" ");
  85. var javascriptKeywords = ("break case catch continue debugger default delete do else false finally for function " +
  86. "if in instanceof new null return switch throw true try typeof var void while with").split(" ");
  87. var coffeescriptKeywords = ("and break catch class continue delete do else extends false finally for " +
  88. "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(" ");
  89. function forAllProps(obj, callback) {
  90. if (!Object.getOwnPropertyNames || !Object.getPrototypeOf) {
  91. for (var name in obj) callback(name)
  92. } else {
  93. for (var o = obj; o; o = Object.getPrototypeOf(o))
  94. Object.getOwnPropertyNames(o).forEach(callback)
  95. }
  96. }
  97. function getCompletions(token, context, keywords, options) {
  98. var found = [], start = token.string, global = options && options.globalScope || window;
  99. function maybeAdd(str) {
  100. if (str.lastIndexOf(start, 0) == 0 && !arrayContains(found, str)) found.push(str);
  101. }
  102. function gatherCompletions(obj) {
  103. if (typeof obj == "string") forEach(stringProps, maybeAdd);
  104. else if (obj instanceof Array) forEach(arrayProps, maybeAdd);
  105. else if (obj instanceof Function) forEach(funcProps, maybeAdd);
  106. forAllProps(obj, maybeAdd)
  107. }
  108. if (context && context.length) {
  109. // If this is a property, see if it belongs to some object we can
  110. // find in the current environment.
  111. var obj = context.pop(), base;
  112. if (obj.type && obj.type.indexOf("variable") === 0) {
  113. if (options && options.additionalContext)
  114. base = options.additionalContext[obj.string];
  115. if (!options || options.useGlobalScope !== false)
  116. base = base || global[obj.string];
  117. } else if (obj.type == "string") {
  118. base = "";
  119. } else if (obj.type == "atom") {
  120. base = 1;
  121. } else if (obj.type == "function") {
  122. if (global.jQuery != null && (obj.string == '$' || obj.string == 'jQuery') &&
  123. (typeof global.jQuery == 'function'))
  124. base = global.jQuery();
  125. else if (global._ != null && (obj.string == '_') && (typeof global._ == 'function'))
  126. base = global._();
  127. }
  128. while (base != null && context.length)
  129. base = base[context.pop().string];
  130. if (base != null) gatherCompletions(base);
  131. } else {
  132. // If not, just look in the global object and any local scope
  133. // (reading into JS mode internals to get at the local and global variables)
  134. for (var v = token.state.localVars; v; v = v.next) maybeAdd(v.name);
  135. for (var v = token.state.globalVars; v; v = v.next) maybeAdd(v.name);
  136. if (!options || options.useGlobalScope !== false)
  137. gatherCompletions(global);
  138. forEach(keywords, maybeAdd);
  139. }
  140. return found;
  141. }
  142. });