瀏覽代碼

HUE-5826 [assist] Show assistant info for only the focused statement

Johan Ahlen 8 年之前
父節點
當前提交
15564edd2b

文件差異過大導致無法顯示
+ 0 - 0
desktop/core/src/desktop/static/desktop/js/autocomplete/sql.js


+ 19 - 1
desktop/core/src/desktop/static/desktop/js/ko.hue-bindings.js

@@ -3291,7 +3291,13 @@
             });
           }
 
-          huePubSub.publish('editor.active.locations', { type: snippet.type(), locations: e.data.locations });
+          var lastKnownLocations = { id: $el.attr("id"), type: snippet.type(), locations: e.data.locations };
+          huePubSub.publish('editor.active.locations', lastKnownLocations);
+
+          huePubSub.subscribe('get.active.editor.locations', function () {
+            huePubSub.publish('editor.active.cursor.location', lastKnownLocations);
+          });
+
 
           // Clear out old parse locations to prevent them from being shown when there's a syntax error in the statement
           while(activeTokens.length > 0) {
@@ -3460,6 +3466,18 @@
         }
       });
 
+      var changeSelectionThrottle = -1;
+      editor.selection.on("changeSelection", function () {
+        window.clearTimeout(changeSelectionThrottle);
+        changeSelectionThrottle = window.setTimeout(function () {
+          huePubSub.publish('editor.active.cursor.location', { id: $el.attr("id"), position: editor.getCursorPosition() });
+        }, 100);
+      });
+
+      huePubSub.subscribe('get.active.editor.locations', function () {
+        huePubSub.publish('editor.active.cursor.location', { id: $el.attr("id"), position: editor.getCursorPosition() });
+      });
+
       editor.selection.on("changeSelection", function () {
         snippet.selectedStatement(editor.getSelectedText());
       });

+ 65 - 23
desktop/core/src/desktop/templates/assist.mako

@@ -1969,8 +1969,9 @@ from notebook.conf import ENABLE_QUERY_BUILDER
     <br/>
     <ul data-bind="foreach: activeTables">
       <li>
-        <span data-bind="text: $data"></span> <i class="fa fa-info"></i> <i class="fa fa-fw fa-clock-o muted" title="02/01/2017 10:15 PM"></i>
-      </i>
+        <span data-bind="text: name"></span> <i class="fa fa-info"></i> <i class="fa fa-fw fa-clock-o muted" title="02/01/2017 10:15 PM"></i>
+##         <span data-bind="text: name"></span> <i class="fa fa-info"></i> <i class="fa fa-fw fa-clock-o muted" title="02/01/2017 10:15 PM"></i>
+      </li>
     </ul>
 
     <form class="form-horizontal">
@@ -2021,35 +2022,76 @@ from notebook.conf import ENABLE_QUERY_BUILDER
 
         self.disposals = [];
 
-        self.activeType = ko.observable();
+        self.activeCursorLocation = ko.observable();
+        self.locationIndex = ko.observable({});
 
-        self.lastLocationsPerType = ko.observable({});
+        self.activeSourceType = ko.observable();
+        self.activeTables = ko.observableArray();
+        self.activeColumns = ko.observableArray();
 
-        self.activeLocations = ko.pureComputed(function () {
-          return self.lastLocationsPerType()[self.activeType()] ? self.lastLocationsPerType()[self.activeType()] : [];
-        });
-        self.activeTables = ko.pureComputed(function () {
-          var allTables = $.grep(self.activeLocations(), function(item) { return item.type == 'table'; });
-          var tables = [];
-          $.each(allTables, function(i, item) {
-            var tableName = item.identifierChain[item.identifierChain.length - 1].name;
-            if (tables.indexOf(tableName) == -1) {
-              tables.push(tableName);
-            }
-          });
-          return tables;
-        });
+        var isPointInside = function (location, row, col) {
+          return (location.first_line < row && row < location.last_line) ||
+              (location.first_line === row && row === location.last_line && location.first_column <= col && col <= location.last_column) ||
+              (location.first_line === row && row < location.last_line && col >= location.first_column) ||
+              (location.first_line < row && row === location.last_line && col <= location.last_column);
+        };
 
-        self.disposals.push(huePubSub.subscribe('active.snippet.type.changed', self.activeType).remove);
+        var createQualifiedIdentifier = function (identifierChain) {
+          return $.map(identifierChain, function (identifier) {
+            return identifier.name;
+          }).join('.');
+        };
 
-        huePubSub.subscribeOnce('set.active.snippet.type', self.activeType);
+        var initActive = function () {
+          if (typeof self.activeCursorLocation() !== 'undefined' && typeof self.locationIndex()[self.activeCursorLocation().id] !== 'undefined') {
+            var locations = self.locationIndex()[self.activeCursorLocation().id].locations;
+            self.activeSourceType(self.locationIndex()[self.activeCursorLocation().id].type);
+            var statementFound = false;
+
+            var tableIndex = {};
+            var columnIndex = {};
+            for (var i = 0; i < locations.length; i++) {
+              var location = locations[i];
+              if (location.type === 'statement') {
+                if (statementFound) {
+                  break;
+                }
+                var cursorPos = self.activeCursorLocation().position;
+                if (isPointInside(location.location, cursorPos.row+1, cursorPos.column+1)) {
+                  statementFound = true;
+                }
+              } else if (statementFound && location.type === 'table') {
+                tableIndex[createQualifiedIdentifier(location.identifierChain)] = { name: location.identifierChain[location.identifierChain.length - 1].name, identifierChain: location.identifierChain }
+              } else if (statementFound && locations[i].type === 'column') {
+                columnIndex[createQualifiedIdentifier(location.identifierChain)] = { name: location.identifierChain[location.identifierChain.length - 1].name, identifierChain: location.identifierChain }
+              }
+            }
+            self.activeTables($.map(tableIndex, function (value) {
+              return value;
+            }));
+            self.activeColumns($.map(columnIndex, function (value) {
+              return value;
+            }));
+          }
+        };
+
+        huePubSub.subscribeOnce('set.active.snippet.type', self.activeSourceType);
         huePubSub.publish('get.active.snippet.type');
+        self.disposals.push(huePubSub.subscribe('active.snippet.type.changed', self.activeSourceType).remove);
+
+        self.disposals.push(huePubSub.subscribe('editor.active.cursor.location', function (location) {
+          self.activeCursorLocation(location);
+          initActive();
+        }).remove);
 
         self.disposals.push(huePubSub.subscribe('editor.active.locations', function (activeLocations) {
-          var locationsIndex = self.lastLocationsPerType();
-          locationsIndex[activeLocations.type] = activeLocations.locations;
-          self.lastLocationsPerType(locationsIndex);
+          var index = self.locationIndex();
+          index[activeLocations.id] = activeLocations;
+          self.locationIndex(index);
+          initActive();
         }).remove);
+
+        huePubSub.publish('get.active.editor.locations');
       }
 
       AssistantPanel.prototype.dispose = function () {

部分文件因文件數量過多而無法顯示