Browse Source

HUE-1218 [pig] Autocomplete aliases

Variables are in the autocomplete now
Enrico Berti 12 năm trước cách đây
mục cha
commit
1ee35c0

+ 47 - 5
apps/pig/src/pig/templates/app.mako

@@ -466,7 +466,33 @@ ${ commonheader(_('Pig'), "pig", user, "100px") | n,unicode }
 
     var scriptEditor = $("#scriptEditor")[0];
 
+    function storeVariables() {
+      CodeMirror.availableVariables = [];
+      var _val = codeMirror.getValue();
+      var _groups = _val.replace(/==/gi, "").split("=");
+      $.each(_groups, function (cnt, item) {
+        if (cnt < _groups.length - 1) {
+          var _blocks = $.trim(item).replace(/\n/gi, " ").split(" ");
+          CodeMirror.availableVariables.push(_blocks[_blocks.length - 1]);
+        }
+        if (item.toLowerCase().indexOf("split") > -1 && item.toLowerCase().indexOf("into") > -1) {
+          try {
+            var _split = item.substring(item.toLowerCase().indexOf("into"));
+            var _possibleVariables = $.trim(_split.substring(4, _split.indexOf(";"))).split(",");
+            $.each(_possibleVariables, function (icnt, iitem) {
+              if (iitem.toLowerCase().indexOf("if") > -1) {
+                CodeMirror.availableVariables.push($.trim(iitem).split(" ")[0]);
+              }
+            });
+          }
+          catch (e) {
+          }
+        }
+      });
+    }
+
     CodeMirror.commands.autocomplete = function (cm) {
+      storeVariables();
       var _line = codeMirror.getLine(codeMirror.getCursor().line);
       var _partial = _line.substring(0, codeMirror.getCursor().ch);
       if (_partial.indexOf("'") > -1 && _partial.indexOf("'") == _partial.lastIndexOf("'") && (_partial.toLowerCase().indexOf("load") > -1
@@ -496,7 +522,7 @@ ${ commonheader(_('Pig'), "pig", user, "100px") | n,unicode }
       readOnly: false,
       lineNumbers: true,
       mode: "text/x-pig",
-      extraKeys: {"Ctrl-Space": "autocomplete"},
+      extraKeys: {"Shift-Space": "autocomplete"},
       onKeyEvent: function (e, s) {
         if (s.type == "keyup") {
           if (s.keyCode == 191) {
@@ -767,8 +793,16 @@ ${ commonheader(_('Pig'), "pig", user, "100px") | n,unicode }
       },
       "edit/:scriptId": function (scriptId) {
         if (scriptId !== "undefined" && scriptId != viewModel.currentScript().id()) {
-          viewModel.loadScript(scriptId);
-          $(document).trigger("loadEditor");
+          dashboardLoadedInterval = window.setInterval(function () {
+            if (viewModel.isDashboardLoaded) {
+              window.clearInterval(dashboardLoadedInterval);
+              viewModel.loadScript(scriptId);
+              if (viewModel.currentScript().id() == -1) {
+                viewModel.newScript();
+              }
+              $(document).trigger("loadEditor");
+            }
+          }, 200);
         }
         showSection("editor", "edit");
       },
@@ -777,8 +811,16 @@ ${ commonheader(_('Pig'), "pig", user, "100px") | n,unicode }
       },
       "properties/:scriptId": function (scriptId) {
         if (scriptId !== "undefined" && scriptId != viewModel.currentScript().id()) {
-          viewModel.loadScript(scriptId);
-          $(document).trigger("loadEditor");
+          dashboardLoadedInterval = window.setInterval(function () {
+            if (viewModel.isDashboardLoaded) {
+              window.clearInterval(dashboardLoadedInterval);
+              viewModel.loadScript(scriptId);
+              if (viewModel.currentScript().id() == -1) {
+                viewModel.newScript();
+              }
+              $(document).trigger("loadEditor");
+            }
+          }, 200);
         }
         showSection("editor", "properties");
       },

+ 8 - 4
desktop/core/static/js/Source/jHue/codemirror-pig-hint.js

@@ -54,7 +54,6 @@
     context.push(tprop);
 
     var completionList = getCompletions(token, context);
-    completionList = completionList.sort();
     //prevent autocomplete for last word, instead show dropdown with one word
     if (completionList.length == 1) {
       completionList.push(" ");
@@ -78,7 +77,7 @@
       + "JOIN CROSS UNION SPLIT INTO IF OTHERWISE ALL AS BY USING INNER OUTER ONSCHEMA PARALLEL "
       + "PARTITION GROUP AND OR NOT GENERATE FLATTEN ASC DESC IS STREAM THROUGH STORE MAPREDUCE "
       + "SHIP CACHE INPUT OUTPUT STDERROR STDIN STDOUT LIMIT SAMPLE LEFT RIGHT FULL EQ GT LT GTE LTE "
-      + "NEQ MATCHES TRUE FALSE";
+      + "NEQ MATCHES TRUE FALSE DUMP";
   var pigKeywordsU = pigKeywords.split(" ");
   var pigKeywordsL = pigKeywords.toLowerCase().split(" ");
 
@@ -107,13 +106,17 @@
       + "StringMin StringSize TextLoader TupleSize Utf8StorageConverter").split(" ").join("() ").split(" ");
 
   function getCompletions(token, context) {
-    var found = [], start = token.string;
+    var found = [], start = token.string, extraFound = [];
 
     function maybeAdd(str) {
       var stripped = strip(str).replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,'').replace(/\s+/g,' ');
       if (stripped.indexOf(start) == 0 && !arrayContains(found, str)) found.push(str);
     }
 
+    function maybeAddToExtra(str) {
+      if (str.indexOf(start) == 0 && !arrayContains(found, str)) extraFound.push(str);
+    }
+
     function strip(html){
       var tmp = document.createElement("DIV");
       tmp.innerHTML = html;
@@ -136,6 +139,7 @@
           forEach(pigTypesL, maybeAdd);
           forEach(pigKeywordsU, maybeAdd);
           forEach(pigKeywordsL, maybeAdd);
+          forEach(CodeMirror.availableVariables, maybeAddToExtra);
         }
       }
     }
@@ -150,6 +154,6 @@
         base = base[context.pop().string];
       if (base != null) gatherCompletions(base);
     }
-    return found;
+    return extraFound.sort().concat(found.sort());
   }
 })();