pig-hint.js 5.0 KB

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