pig-hint.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. function scriptHint(editor, keywords, getToken) {
  18. // Find the token at the cursor
  19. var cur = editor.getCursor(), token = getToken(editor, cur), tprop = token;
  20. // If it's not a 'word-style' token, ignore the token.
  21. if (!/^[\w$_]*$/.test(token.string)) {
  22. token = tprop = {start: cur.ch, end: cur.ch, string: "", state: token.state,
  23. className: token.string == ":" ? "pig-type" : null};
  24. }
  25. if (!context) var context = [];
  26. context.push(tprop);
  27. var completionList = getCompletions(token, context);
  28. completionList = completionList.sort();
  29. //prevent autocomplete for last word, instead show dropdown with one word
  30. if(completionList.length == 1) {
  31. completionList.push(" ");
  32. }
  33. return {list: completionList,
  34. from: {line: cur.line, ch: token.start},
  35. to: {line: cur.line, ch: token.end}};
  36. }
  37. CodeMirror.pigHint = function(editor) {
  38. return scriptHint(editor, pigKeywordsU, function (e, cur) {return e.getTokenAt(cur);});
  39. };
  40. function toTitleCase(str) {
  41. return str.replace(/(?:^|\s)\w/g, function(match) {
  42. return match.toUpperCase();
  43. });
  44. }
  45. var pigKeywords = "VOID IMPORT RETURNS DEFINE LOAD FILTER FOREACH ORDER CUBE DISTINCT COGROUP "
  46. + "JOIN CROSS UNION SPLIT INTO IF OTHERWISE ALL AS BY USING INNER OUTER ONSCHEMA PARALLEL "
  47. + "PARTITION GROUP AND OR NOT GENERATE FLATTEN ASC DESC IS STREAM THROUGH STORE MAPREDUCE "
  48. + "SHIP CACHE INPUT OUTPUT STDERROR STDIN STDOUT LIMIT SAMPLE LEFT RIGHT FULL EQ GT LT GTE LTE "
  49. + "NEQ MATCHES TRUE FALSE";
  50. var pigKeywordsU = pigKeywords.split(" ");
  51. var pigKeywordsL = pigKeywords.toLowerCase().split(" ");
  52. var pigTypes = "BOOLEAN INT LONG FLOAT DOUBLE CHARARRAY BYTEARRAY BAG TUPLE MAP";
  53. var pigTypesU = pigTypes.split(" ");
  54. var pigTypesL = pigTypes.toLowerCase().split(" ");
  55. var pigBuiltins = "ABS ACOS ARITY ASIN ATAN AVG BAGSIZE BINSTORAGE BLOOM BUILDBLOOM CBRT CEIL "
  56. + "CONCAT COR COS COSH COUNT COUNT_STAR COV CONSTANTSIZE CUBEDIMENSIONS DIFF DISTINCT DOUBLEABS "
  57. + "DOUBLEAVG DOUBLEBASE DOUBLEMAX DOUBLEMIN DOUBLEROUND DOUBLESUM EXP FLOOR FLOATABS FLOATAVG "
  58. + "FLOATMAX FLOATMIN FLOATROUND FLOATSUM GENERICINVOKER INDEXOF INTABS INTAVG INTMAX INTMIN "
  59. + "INTSUM INVOKEFORDOUBLE INVOKEFORFLOAT INVOKEFORINT INVOKEFORLONG INVOKEFORSTRING INVOKER "
  60. + "ISEMPTY JSONLOADER JSONMETADATA JSONSTORAGE LAST_INDEX_OF LCFIRST LOG LOG10 LOWER LONGABS "
  61. + "LONGAVG LONGMAX LONGMIN LONGSUM MAX MIN MAPSIZE MONITOREDUDF NONDETERMINISTIC OUTPUTSCHEMA "
  62. + "PIGSTORAGE PIGSTREAMING RANDOM REGEX_EXTRACT REGEX_EXTRACT_ALL REPLACE ROUND SIN SINH SIZE "
  63. + "SQRT STRSPLIT SUBSTRING SUM STRINGCONCAT STRINGMAX STRINGMIN STRINGSIZE TAN TANH TOBAG "
  64. + "TOKENIZE TOMAP TOP TOTUPLE TRIM TEXTLOADER TUPLESIZE UCFIRST UPPER UTF8STORAGECONVERTER";
  65. var pigBuiltinsU = pigBuiltins.split(" ").join("() ").split(" ");
  66. var pigBuiltinsL = pigBuiltins.toLowerCase().split(" ").join("() ").split(" ");
  67. var pigBuiltinsC = ("BagSize BinStorage Bloom BuildBloom ConstantSize CubeDimensions DoubleAbs "
  68. + "DoubleAvg DoubleBase DoubleMax DoubleMin DoubleRound DoubleSum FloatAbs FloatAvg FloatMax "
  69. + "FloatMin FloatRound FloatSum GenericInvoker IntAbs IntAvg IntMax IntMin IntSum "
  70. + "InvokeForDouble InvokeForFloat InvokeForInt InvokeForLong InvokeForString Invoker "
  71. + "IsEmpty JsonLoader JsonMetadata JsonStorage LongAbs LongAvg LongMax LongMin LongSum MapSize "
  72. + "MonitoredUDF Nondeterministic OutputSchema PigStorage PigStreaming StringConcat StringMax "
  73. + "StringMin StringSize TextLoader TupleSize Utf8StorageConverter").split(" ").join("() ").split(" ");
  74. function getCompletions(token, context) {
  75. var found = [], start = token.string;
  76. function maybeAdd(str) {
  77. if (str.indexOf(start) == 0 && !arrayContains(found, str)) found.push(str);
  78. }
  79. function gatherCompletions(obj) {
  80. if(obj == ":") {
  81. forEach(pigTypesL, maybeAdd);
  82. }
  83. else {
  84. forEach(pigBuiltinsU, maybeAdd);
  85. forEach(pigBuiltinsL, maybeAdd);
  86. forEach(pigBuiltinsC, maybeAdd);
  87. forEach(pigTypesU, maybeAdd);
  88. forEach(pigTypesL, maybeAdd);
  89. forEach(pigKeywordsU, maybeAdd);
  90. forEach(pigKeywordsL, maybeAdd);
  91. }
  92. }
  93. if (context) {
  94. // If this is a property, see if it belongs to some object we can
  95. // find in the current environment.
  96. var obj = context.pop(), base;
  97. if (obj.className == "pig-word")
  98. base = obj.string;
  99. else if(obj.className == "pig-type")
  100. base = ":" + obj.string;
  101. while (base != null && context.length)
  102. base = base[context.pop().string];
  103. if (base != null) gatherCompletions(base);
  104. }
  105. return found;
  106. }
  107. })();