codemirror-pig-hint.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. if (CodeMirror.isTable) {
  39. var line = editor.getLine(cur.line);
  40. var USING_CLAUSE = "USING org.apache.hcatalog.pig.HCatLoader();";
  41. if (token.string.indexOf(USING_CLAUSE) > -1) {
  42. token.end = token.end - USING_CLAUSE.length;
  43. }
  44. token.string = token.string.substring(1, cur.ch - line.indexOf(token.string));
  45. token.start = token.start + 1;
  46. token.end = token.end - 1;
  47. }
  48. else {
  49. token.string = token.string.substring(1, token.string.length);
  50. }
  51. }
  52. if (token.string.indexOf("/") > -1){
  53. token.string = token.string.substring(token.string.lastIndexOf("/") + 1);
  54. }
  55. if (!/^[\w$_]*$/.test(token.string)) {
  56. token = tprop = {start: cur.ch, end: cur.ch, string: "", state: token.state,
  57. className: token.string == ":" ? "pig-type" : null};
  58. }
  59. if (!context) var context = [];
  60. context.push(tprop);
  61. var completionList = getCompletions(token, context);
  62. //prevent autocomplete for last word, instead show dropdown with one word
  63. if (completionList.length == 1) {
  64. completionList.push(" ");
  65. }
  66. return {list: completionList,
  67. from: CodeMirror.Pos(cur.line, token.start),
  68. to: CodeMirror.Pos(cur.line, token.end)};
  69. }
  70. CodeMirror.isPath = false;
  71. CodeMirror.isTable = false;
  72. CodeMirror.isHCatHint = false;
  73. CodeMirror.currentFiles = [];
  74. CodeMirror.catalogTables = "";
  75. CodeMirror.pigHint = function (editor) {
  76. return scriptHint(editor, pigCaseInsensitive, function (e, cur) {
  77. return e.getTokenAt(cur);
  78. });
  79. };
  80. var pigCaseInsensitive = "and any all arrange as asc bag by bytearray boolean cache cat cd chararray cogroup cp cross " +
  81. "datetime %declare %default define desc describe distinct double du dump e eval exec explain f filter " +
  82. "flatten float foreach full generate group help if illustrate import inner input int into is join kill" +
  83. "l left limit load long ls map matches mkdir mv not null onschema or order outer output parallel pig pwd" +
  84. "quit register right rm rmf run sample set ship split stderr stdin stdout store stream through tuple union using";
  85. var pigCaseInsensitiveU = pigCaseInsensitive.toUpperCase().split(" ");
  86. var pigCaseInsensitiveL = pigCaseInsensitive.toLowerCase().split(" ");
  87. var pigCaseSensitive = "AVG BinStorage CONCAT copyFromLocal copyToLocal COUNT DIFF MAX MIN PigDump PigStorage SIZE SUM TextLoader TOKENIZE".split(" ");
  88. function getCompletions(token, context) {
  89. var catalogTablesL = CodeMirror.catalogTables.toLowerCase().split(" ");
  90. var found = [], start = token.string, extraFound = [];
  91. function maybeAdd(str) {
  92. var stripped = strip(str).replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,'').replace(/\s+/g,' ');
  93. if (stripped.toLowerCase().indexOf(start.toLowerCase()) == 0 && !arrayContains(found, str)) found.push(str);
  94. }
  95. function maybeAddToExtra(str) {
  96. if (str.toLowerCase().indexOf(start.toLowerCase()) == 0 && !arrayContains(found, str)) extraFound.push(str);
  97. }
  98. function strip(html){
  99. if (jQuery) {
  100. return $("<div>").html(html).text();
  101. }
  102. else {
  103. var tmp = document.createElement("DIV");
  104. tmp.innerHTML = html;
  105. return tmp.textContent || tmp.innerText;
  106. }
  107. }
  108. function gatherCompletions(obj) {
  109. if (CodeMirror.isPath || (obj.indexOf("'") == 0 && !CodeMirror.isTable)){
  110. forEach(CodeMirror.currentFiles, maybeAdd);
  111. }
  112. else if (CodeMirror.isTable) {
  113. forEach(catalogTablesL, maybeAddToExtra);
  114. }
  115. else {
  116. forEach(pigCaseInsensitiveU, maybeAdd);
  117. forEach(pigCaseInsensitiveL, maybeAdd);
  118. forEach(pigCaseSensitive, maybeAdd);
  119. forEach(CodeMirror.availableVariables, maybeAddToExtra);
  120. }
  121. if (CodeMirror.isHCatHint){
  122. maybeAdd("<i class='fa fa-magic'></i> USING org.apache.hcatalog.pig.HCatLoader();");
  123. }
  124. }
  125. if (context) {
  126. // If this is a property, see if it belongs to some object we can
  127. // find in the current environment.
  128. var obj = context.pop(), base;
  129. base = obj.string;
  130. while (base != null && context.length)
  131. base = base[context.pop().string];
  132. if (base != null) gatherCompletions(base);
  133. }
  134. return extraFound.sort().concat(found.sort());
  135. }
  136. })();