codemirror-pig-hint.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. (function () {
  17. function forEach(arr, f) {
  18. for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]);
  19. }
  20. function arrayContains(arr, item) {
  21. if (!Array.prototype.indexOf) {
  22. var i = arr.length;
  23. while (i--) {
  24. if (arr[i] === item) {
  25. return true;
  26. }
  27. }
  28. return false;
  29. }
  30. return arr.indexOf(item) != -1;
  31. }
  32. function scriptHint(editor, _keywords, getToken) {
  33. // Find the token at the cursor
  34. var cur = editor.getCursor(), token = getToken(editor, cur), tprop = token;
  35. // If it's not a 'word-style' token, ignore the token.
  36. if (token.string.indexOf("'") == 0){
  37. CodeMirror.isPath = !CodeMirror.isTable;
  38. token.string = token.string.substring(1, token.string.length);
  39. }
  40. if (token.string.indexOf("/") > -1){
  41. token.string = token.string.substring(token.string.lastIndexOf("/") + 1);
  42. }
  43. if (!/^[\w$_]*$/.test(token.string)) {
  44. token = tprop = {start: cur.ch, end: cur.ch, string: "", state: token.state,
  45. className: token.string == ":" ? "pig-type" : null};
  46. }
  47. if (!context) var context = [];
  48. context.push(tprop);
  49. var completionList = getCompletions(token, context);
  50. //prevent autocomplete for last word, instead show dropdown with one word
  51. if (completionList.length == 1) {
  52. completionList.push(" ");
  53. }
  54. return {list: completionList,
  55. from: CodeMirror.Pos(cur.line, token.start),
  56. to: CodeMirror.Pos(cur.line, token.end)};
  57. }
  58. CodeMirror.isPath = false;
  59. CodeMirror.isTable = false;
  60. CodeMirror.isHCatHint = false;
  61. CodeMirror.currentFiles = [];
  62. CodeMirror.catalogTables = "";
  63. CodeMirror.pigHint = function (editor) {
  64. return scriptHint(editor, pigCaseInsensitive, function (e, cur) {
  65. return e.getTokenAt(cur);
  66. });
  67. };
  68. var pigCaseInsensitive = "and any all arrange as asc bag by bytearray boolean cache cat cd chararray cogroup cp cross " +
  69. "datetime %declare %default define desc describe distinct double du dump e eval exec explain f filter " +
  70. "flatten float foreach full generate group help if illustrate import inner input int into is join kill" +
  71. "l left limit load long ls map matches mkdir mv not null onschema or order outer output parallel pig pwd" +
  72. "quit register right rm rmf run sample set ship split stderr stdin stdout store stream through tuple union using";
  73. var pigCaseInsensitiveU = pigCaseInsensitive.toUpperCase().split(" ");
  74. var pigCaseInsensitiveL = pigCaseInsensitive.toLowerCase().split(" ");
  75. var pigCaseSensitive = "AVG BinStorage CONCAT copyFromLocal copyToLocal COUNT DIFF MAX MIN PigDump PigStorage SIZE SUM TextLoader TOKENIZE".split(" ");
  76. function getCompletions(token, context) {
  77. var catalogTablesL = CodeMirror.catalogTables.toLowerCase().split(" ");
  78. var found = [], start = token.string, extraFound = [];
  79. function maybeAdd(str) {
  80. var stripped = strip(str).replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,'').replace(/\s+/g,' ');
  81. if (stripped.toLowerCase().indexOf(start.toLowerCase()) == 0 && !arrayContains(found, str)) found.push(str);
  82. }
  83. function maybeAddToExtra(str) {
  84. if (str.toLowerCase().indexOf(start.toLowerCase()) == 0 && !arrayContains(found, str)) extraFound.push(str);
  85. }
  86. function strip(html){
  87. if (jQuery) {
  88. return $("<div>").html(html).text();
  89. }
  90. else {
  91. var tmp = document.createElement("DIV");
  92. tmp.innerHTML = html;
  93. return tmp.textContent || tmp.innerText;
  94. }
  95. }
  96. function gatherCompletions(obj) {
  97. if (CodeMirror.isPath || (obj.indexOf("'") == 0 && !CodeMirror.isTable)){
  98. forEach(CodeMirror.currentFiles, maybeAdd);
  99. }
  100. else if (CodeMirror.isTable) {
  101. forEach(catalogTablesL, maybeAddToExtra);
  102. }
  103. else {
  104. forEach(pigCaseInsensitiveU, maybeAdd);
  105. forEach(pigCaseInsensitiveL, maybeAdd);
  106. forEach(pigCaseSensitive, maybeAdd);
  107. forEach(CodeMirror.availableVariables, maybeAddToExtra);
  108. }
  109. if (CodeMirror.isHCatHint){
  110. maybeAdd("<i class='fa fa-magic'></i> USING org.apache.hcatalog.pig.HCatLoader();");
  111. }
  112. }
  113. if (context) {
  114. // If this is a property, see if it belongs to some object we can
  115. // find in the current environment.
  116. var obj = context.pop(), base;
  117. base = obj.string;
  118. while (base != null && context.length)
  119. base = base[context.pop().string];
  120. if (base != null) gatherCompletions(base);
  121. }
  122. return extraFound.sort().concat(found.sort());
  123. }
  124. })();