Эх сурвалжийг харах

HUE-6361 [editor] Use the new statement parser to identify statement locations

Johan Ahlen 8 жил өмнө
parent
commit
7187cab

+ 24 - 2
desktop/core/src/desktop/static/desktop/js/aceSqlWorker.js

@@ -16,6 +16,7 @@
 
 var version = 17;
 importScripts('/static/desktop/js/autocomplete/sqlParseSupport.js?version=' + version);
+importScripts('/static/desktop/js/autocomplete/sqlStatementsParser.js?version=' + version);
 importScripts('/static/desktop/js/autocomplete/sql.js?version=' + version);
 importScripts('/static/desktop/js/sqlFunctions.js?version=' + version);
 
@@ -31,8 +32,29 @@ importScripts('/static/desktop/js/sqlFunctions.js?version=' + version);
     clearTimeout(this.throttle);
     this.throttle = setTimeout(function () {
       if (msg.data) {
-        var parseResult = sql.parseSql(msg.data.text + ' ', '', msg.data.type, false);
-        postMessage(parseResult);
+        var statements = sqlStatementsParser.parse(msg.data.text);
+        var locations = [];
+        // For now we'll only extract the locations
+        statements.forEach(function (statement) {
+          locations.push(statement);
+          try {
+            var sqlParseResult =  sql.parseSql(statement.statement + ' ', '', msg.data.type, false);
+            if (sqlParseResult.locations) {
+              sqlParseResult.locations.forEach(function (location) {
+                if (location.type !== 'statement') {
+                  if (location.location.first_line === 1) {
+                    location.location.first_column += statement.location.first_column;
+                    location.location.last_column += statement.location.first_column;
+                  }
+                  location.location.first_line += statement.location.first_line - 1;
+                  location.location.last_line += statement.location.first_line - 1;
+                  locations.push(location);
+                }
+              })
+            }
+          } catch (error) {}
+        });
+        postMessage({ locations: locations });
       }
     }, 400);
   }

+ 6 - 6
desktop/core/src/desktop/templates/assist.mako

@@ -1820,7 +1820,7 @@ from notebook.conf import get_ordered_interpreters
         self.disposals = [];
 
         self.activeCursorLocation = ko.observable();
-        self.activeStatementLocation = ko.observable();
+        self.activeStatement = ko.observable();
         self.locationIndex = ko.observable({});
 
         self.activeSourceType = ko.observable();
@@ -1859,13 +1859,13 @@ from notebook.conf import get_ordered_interpreters
         var AceRange = ace.require('ace/range').Range;
         var lastMarkedGutterLines = [];
         var findStatementTextAtCursor = function () {
-          if (!self.activeStatementLocation() || !self.activeCursorLocation()) {
+          if (!self.activeStatement() || !self.activeCursorLocation()) {
             return; // undefined when unknown
           }
-          var statementLoc = self.activeStatementLocation();
+          var statementLoc = self.activeStatement().location;
 
           var editor = self.activeCursorLocation().editor;
-          var statementAtCursor = editor.session.getTextRange(new AceRange(statementLoc.first_line - 1, statementLoc.first_column - 1, statementLoc.last_line - 1, statementLoc.last_column - 1));
+          var statementAtCursor = self.activeStatement().statement;
 
           var leadingEmptyLineCount = 0;
           var leadingWhiteSpace = statementAtCursor.match(/^\s+/);
@@ -1945,8 +1945,8 @@ from notebook.conf import get_ordered_interpreters
             var columnIndex = {};
 
             activeLocations.forEach(function (location) {
-              if (location.type === 'statement' && self.activeStatementLocation() !== location.location) {
-                self.activeStatementLocation(location.location);
+              if (location.type === 'statement' && (!self.activeStatement() || self.activeStatement().location !== location.location)) {
+                self.activeStatement(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