javascript-hint.js 5.8 KB

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