Răsfoiți Sursa

HUE-6202 [editor] Add pubsub to get the statement at the active cursor position

Johan Ahlen 8 ani în urmă
părinte
comite
15e1732

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

@@ -3712,7 +3712,7 @@
       var changeSelectionListener = 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() });
+          huePubSub.publish('editor.active.cursor.location', { id: $el.attr("id"), position: editor.getCursorPosition(), editor: editor });
         }, 100);
         snippet.selectedStatement(editor.getSelectedText());
       });
@@ -3722,7 +3722,7 @@
       });
 
       var cursorLocationSub = huePubSub.subscribe('get.active.editor.cursor.location', function () {
-        huePubSub.publish('editor.active.cursor.location', { id: $el.attr("id"), position: editor.getCursorPosition() });
+        huePubSub.publish('editor.active.cursor.location', { id: $el.attr("id"), position: editor.getCursorPosition(), editor: editor });
       });
 
       disposeFunctions.push(function () {

+ 20 - 2
desktop/core/src/desktop/templates/assist.mako

@@ -1782,12 +1782,13 @@ from notebook.conf import get_ordered_interpreters
         self.disposals = [];
 
         self.activeCursorLocation = ko.observable();
+        self.activeStatementLocation = ko.observable();
         self.locationIndex = ko.observable({});
 
         self.activeSourceType = ko.observable();
         self.activeTables = ko.observableArray();
         self.activeColumns = ko.observableArray();
-        self.activeRisks = ko.observable({})
+        self.activeRisks = ko.observable({});
         self.hasActiveRisks = ko.pureComputed(function () {
            return Object.keys(self.activeRisks()).length > 0;
         });
@@ -1807,6 +1808,20 @@ from notebook.conf import get_ordered_interpreters
           }).join('.');
         };
 
+        var AceRange = ace.require('ace/range').Range;
+        var findStatementTextAtCursor = function () {
+          if (!self.activeStatementLocation() || !self.activeCursorLocation()) {
+            return; // undefined when unknown
+          }
+          var statementLoc = self.activeStatementLocation();
+          var editor = self.activeCursorLocation().editor;
+          return editor.session.getTextRange(new AceRange(statementLoc.first_line - 1, statementLoc.first_column - 1, statementLoc.last_line - 1, statementLoc.last_column - 1))
+        };
+
+        self.disposals.push(huePubSub.subscribe('get.active.editor.statement', function () {
+          huePubSub.publish('set.active.editor.statement', findStatementTextAtCursor());
+        }).remove);
+
         var initActive = function () {
           if (!self.activeCursorLocation()) {
             huePubSub.publish('get.active.editor.cursor.location');
@@ -1859,7 +1874,10 @@ from notebook.conf import get_ordered_interpreters
             var columnIndex = {};
 
             activeLocations.forEach(function (location) {
-              if (location.type === 'table' && location.identifierChain.length <= 2) {
+              if (location.type === 'statement' && self.activeStatementLocation() !== location.location) {
+                self.activeStatementLocation(location.location);
+                huePubSub.publish('active.editor.statement.changed', findStatementTextAtCursor());
+              } else if (location.type === 'table' && location.identifierChain.length <= 2) {
                 // tableIndex is used to make sure we only add each table once
                 tableIndex[createQualifiedIdentifier(location.identifierChain)] = { name: location.identifierChain[location.identifierChain.length - 1].name, identifierChain: location.identifierChain }
               } else if (location.type === 'column') {