codemirror-clike-hint.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. //Call this from outside environment to add more words to the list that might be specific to your component
  18. //or taken from a current view model. They will show first in the list.
  19. CodeMirror.addKeywords=function(keywords){
  20. addedKeywords=keywords;
  21. };
  22. function scriptHint(editor, _keywords, getToken) {
  23. // Find the token at the cursor
  24. var cur = editor.getCursor(), token = getToken(editor, cur), tprop = token;
  25. // If it's not a 'word-style' token, ignore the token.
  26. if (!/^[\w$_]*$/.test(token.string)) {
  27. token = tprop = {start: cur.ch, end: cur.ch, string: "", state: token.state,
  28. className: null};
  29. }
  30. if (!context) var context = [];
  31. context.push(tprop);
  32. var completionList = getCompletions(token, context, _keywords);
  33. return {list: completionList,
  34. from: CodeMirror.Pos(cur.line, token.start),
  35. to: CodeMirror.Pos(cur.line, token.end)};
  36. }
  37. //Offers completion for C-sharp, C++, Java and Scala
  38. function csharpHint(editor) {
  39. return scriptHint(editor, getAllKeywords(csharpKeywords), function (e, cur) {return e.getTokenAt(cur);});
  40. }
  41. CodeMirror.csharpHint = csharpHint; // deprecated
  42. function javaHint(editor) {
  43. return scriptHint(editor, getAllKeywords(javaKeywords), function (e, cur) {return e.getTokenAt(cur);});
  44. }
  45. CodeMirror.javaHint = javaHint; // deprecated
  46. function scalaHint(editor) {
  47. return scriptHint(editor, getAllKeywords(scalaKeywords), function (e, cur) {return e.getTokenAt(cur);});
  48. }
  49. CodeMirror.scalaHint = scalaHint; // deprecated
  50. var addedKeywords="";
  51. //below are all the reserved words plus some of the more common method names
  52. //but the list is no way pretending to be complete
  53. var clikeKeywordString="abstract assert boolean break byte case catch char class const do default double else enum false "+
  54. "finally for float goto if int interface long new null private protected public return short static string switch throw "+
  55. "this try var void true using volatile while ";
  56. var csharpKeywords="as await base bool checked continue decimal delegate event explicit extern finally fixed "+
  57. "foreach implicit in internal is lock namespace object operator out override params readonly ref sbyte sealed sizeof stackalloc "+
  58. "typeof uint ulong unchecked unsafe ushort virtual add alias ascending descending dynamic from get "+
  59. "global group into join let orderby partial remove select set value var yield "+
  60. "Byte Char Contains Count DateTime DateTimeOffset Decimal Double IndexOf "+
  61. "LastIndexOf FirstIndexOf Format Substring Trim Length continue delete function Remove Split Replace with "+
  62. "Range Guid Int16 Int32 Int64 Object SByte Single String TimeSpan UInt16 UInt32 UInt64 virtual";
  63. var javaKeywords="concat extends final format implements import instanceof native "+
  64. "package strictfp super synchronized throws transient "+
  65. "Boolean Byte Character CharSequence Class ClassLoader Cloneable Comparable "+
  66. "Compiler Double Exception Float Integer length Long Math Number Object Package Pair Process printl"+
  67. "Runtime Runnable SecurityManager Short StackTraceElement StrictMath String "+
  68. "replace replaceAll replaceFirst substring toUpperCase toLowerCase trim"+
  69. "StringBuffer System Thread ThreadGroup ThreadLocal Throwable Triple Void";
  70. var scalaKeywords="assume require print println printf readLine readBoolean readByte readShort " +
  71. "readChar readInt readLong readFloat readDouble " +
  72. "AnyVal App Application Array BufferedIterator BigDecimal BigInt Char Console Either " +
  73. "Enumeration Equiv Error Exception Fractional Function IndexedSeq Integral Iterable " +
  74. "Iterator List Map Numeric Nil NotNull Option Ordered Ordering PartialFunction PartialOrdering " +
  75. "Product Proxy Range Responder Seq Serializable Set Specializable Stream StringBuilder " +
  76. "StringContext Symbol Throwable Traversable TraversableOnce Tuple Unit Vector :: #:: ";
  77. function getAllKeywords(keywordString) {
  78. var newKeywords=(addedKeywords+clikeKeywordString+keywordString).split(" ");
  79. return newKeywords;
  80. }
  81. function getCompletions(token, context, _keywords) {
  82. var found = [], start = token.string;
  83. function maybeAdd(str) {
  84. //searches with all lower case
  85. if (str.indexOf(start.toLowerCase()) == 0 && !arrayContains(found, str)) found.push(str);
  86. //searches with first letter upper case
  87. if (str.indexOf(start.charAt(0).toUpperCase() + start.slice(1)) == 0 && !arrayContains(found, str)) found.push(str);
  88. }
  89. function gatherCompletions(_obj) {
  90. forEach(_keywords,maybeAdd);
  91. }
  92. if (context) {
  93. var obj = context.pop(), base;
  94. if (obj.type== "variable")
  95. base = obj.string;
  96. while (base != null && context.length)
  97. base = base[context.pop().string];
  98. if (base != null) gatherCompletions(base);
  99. }
  100. return found;
  101. }
  102. })();