javascript-hint.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/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. var innerMode = CodeMirror.innerMode(editor.getMode(), token.state);
  32. if (innerMode.mode.helperType === "json") return;
  33. token.state = innerMode.state;
  34. // If it's not a 'word-style' token, ignore the token.
  35. if (!/^[\w$_]*$/.test(token.string)) {
  36. token = {start: cur.ch, end: cur.ch, string: "", state: token.state,
  37. type: token.string == "." ? "property" : null};
  38. } else if (token.end > cur.ch) {
  39. token.end = cur.ch;
  40. token.string = token.string.slice(0, cur.ch - token.start);
  41. }
  42. var tprop = token;
  43. // If it is a property, find out what it is a property of.
  44. while (tprop.type == "property") {
  45. tprop = getToken(editor, Pos(cur.line, tprop.start));
  46. if (tprop.string != ".") return;
  47. tprop = getToken(editor, Pos(cur.line, tprop.start));
  48. if (!context) var context = [];
  49. context.push(tprop);
  50. }
  51. return {list: getCompletions(token, context, keywords, options),
  52. from: Pos(cur.line, token.start),
  53. to: Pos(cur.line, token.end)};
  54. }
  55. function javascriptHint(editor, options) {
  56. return scriptHint(editor, javascriptKeywords,
  57. function (e, cur) {return e.getTokenAt(cur);},
  58. options);
  59. };
  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 independent 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.registerHelper("hint", "coffeescript", coffeescriptHint);
  82. var stringProps = ("charAt charCodeAt indexOf lastIndexOf substring substr slice trim trimLeft trimRight " +
  83. "toUpperCase toLowerCase split concat match replace search").split(" ");
  84. var arrayProps = ("length concat join splice push pop shift unshift slice reverse sort indexOf " +
  85. "lastIndexOf every some filter forEach map reduce reduceRight ").split(" ");
  86. var funcProps = "prototype apply call bind".split(" ");
  87. var javascriptKeywords = ("break case catch class const continue debugger default delete do else export extends false finally for function " +
  88. "if in import instanceof new null return super switch this throw true try typeof var void while with yield").split(" ");
  89. var coffeescriptKeywords = ("and break catch class continue delete do else extends false finally for " +
  90. "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(" ");
  91. function forAllProps(obj, callback) {
  92. if (!Object.getOwnPropertyNames || !Object.getPrototypeOf) {
  93. for (var name in obj) callback(name)
  94. } else {
  95. for (var o = obj; o; o = Object.getPrototypeOf(o))
  96. Object.getOwnPropertyNames(o).forEach(callback)
  97. }
  98. }
  99. function getCompletions(token, context, keywords, options) {
  100. var found = [], start = token.string, global = options && options.globalScope || window;
  101. function maybeAdd(str) {
  102. if (str.lastIndexOf(start, 0) == 0 && !arrayContains(found, str)) found.push(str);
  103. }
  104. function gatherCompletions(obj) {
  105. if (typeof obj == "string") forEach(stringProps, maybeAdd);
  106. else if (obj instanceof Array) forEach(arrayProps, maybeAdd);
  107. else if (obj instanceof Function) forEach(funcProps, maybeAdd);
  108. forAllProps(obj, maybeAdd)
  109. }
  110. if (context && context.length) {
  111. // If this is a property, see if it belongs to some object we can
  112. // find in the current environment.
  113. var obj = context.pop(), base;
  114. if (obj.type && obj.type.indexOf("variable") === 0) {
  115. if (options && options.additionalContext)
  116. base = options.additionalContext[obj.string];
  117. if (!options || options.useGlobalScope !== false)
  118. base = base || global[obj.string];
  119. } else if (obj.type == "string") {
  120. base = "";
  121. } else if (obj.type == "atom") {
  122. base = 1;
  123. } else if (obj.type == "function") {
  124. if (global.jQuery != null && (obj.string == '$' || obj.string == 'jQuery') &&
  125. (typeof global.jQuery == 'function'))
  126. base = global.jQuery();
  127. else if (global._ != null && (obj.string == '_') && (typeof global._ == 'function'))
  128. base = global._();
  129. }
  130. while (base != null && context.length)
  131. base = base[context.pop().string];
  132. if (base != null) gatherCompletions(base);
  133. } else {
  134. // If not, just look in the global object, any local scope, and optional additional-context
  135. // (reading into JS mode internals to get at the local and global variables)
  136. for (var v = token.state.localVars; v; v = v.next) maybeAdd(v.name);
  137. for (var c = token.state.context; c; c = c.prev)
  138. for (var v = c.vars; v; v = v.next) maybeAdd(v.name)
  139. for (var v = token.state.globalVars; v; v = v.next) maybeAdd(v.name);
  140. if (options && options.additionalContext != null)
  141. for (var key in options.additionalContext)
  142. maybeAdd(key);
  143. if (!options || options.useGlobalScope !== false)
  144. gatherCompletions(global);
  145. forEach(keywords, maybeAdd);
  146. }
  147. return found;
  148. }
  149. });