Ver código fonte

HUE-6202 [editor] Improve multi-statement location reporting

Johan Ahlen 8 anos atrás
pai
commit
0469d0d8e3

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

@@ -31,30 +31,8 @@ importScripts('/static/desktop/js/sqlFunctions.js?version=' + version);
     clearTimeout(this.throttle);
     this.throttle = setTimeout(function () {
       if (msg.data) {
-        var errors = [];
-        var locations = [];
-        var lineCount = 0;
-        msg.data.text.split(';').forEach(function (statement) {
-          var parseResult = sql.parseSql(statement + ' ', '', msg.data.type, false);
-          if (parseResult.errors) {
-            parseResult.errors.forEach(function (error) {
-              if (error.token.indexOf('CURSOR') === -1) {
-                error.loc.first_line += lineCount;
-                error.loc.last_line += lineCount;
-                errors.push(error);
-              }
-            })
-          }
-          if (parseResult.locations) {
-            parseResult.locations.forEach(function (location) {
-              location.location.first_line += lineCount;
-              location.location.last_line += lineCount;
-              locations.push(location);
-            })
-          }
-          lineCount += statement.split(/\r\n|\r|\n/).length - 1;
-        });
-        postMessage({ errors: errors, locations: locations });
+        var parseResult = sql.parseSql(msg.data.text + ' ', '', msg.data.type, false);
+        postMessage(parseResult);
       }
     }, 400);
   }

+ 28 - 0
desktop/core/src/desktop/static/desktop/spec/autocomplete/sqlSpecLocations.js

@@ -74,6 +74,34 @@
       });
     });
 
+    it('should report locations for "select x from x;select y from y;"', function () {
+      assertLocations({
+        beforeCursor: 'select x from x;select y from y;',
+        expectedLocations: [
+          { type: 'statement', location: { first_line: 1, last_line: 1, first_column: 1, last_column: 16 } },
+          { type: 'column', location: { first_line: 1, last_line: 1, first_column: 8, last_column: 9 }, identifierChain: [{ name: 'x' }], tables: [{ identifierChain: [{ name: 'x' }] }] },
+          { type: 'table', location: { first_line: 1, last_line: 1, first_column: 15, last_column: 16 }, identifierChain: [{ name: 'x' }] },
+          { type: 'statement', location: { first_line: 1, last_line: 1, first_column: 17, last_column: 32 } },
+          { type: 'column', location: { first_line: 1, last_line: 1, first_column: 24, last_column: 25 }, identifierChain: [{ name: 'y' }], tables: [{ identifierChain: [{ name: 'y' }] }] },
+          { type: 'table', location: { first_line: 1, last_line: 1, first_column: 31, last_column: 32 }, identifierChain: [{ name: 'y' }] }
+        ]
+      });
+    });
+
+    it('should report locations for "-- comment\nselect x from x;\n\n\nselect y from y;"', function () {
+      assertLocations({
+        beforeCursor: '-- comment\nselect x from x;\n\n\nselect y from y;',
+        expectedLocations: [
+          { type: 'statement', location: { first_line: 1, last_line: 2, first_column: 1, last_column: 16 } },
+          { type: 'column', location: { first_line: 2, last_line: 2, first_column: 8, last_column: 9 }, identifierChain: [{ name: 'x' }], tables: [{ identifierChain: [{ name: 'x' }] }] },
+          { type: 'table', location: { first_line: 2, last_line: 2, first_column: 15, last_column: 16 }, identifierChain: [{ name: 'x' }] },
+          { type: 'statement', location: { first_line: 2, last_line: 5, first_column: 17, last_column: 16 } },
+          { type: 'column', location: { first_line: 5, last_line: 5, first_column: 8, last_column: 9 }, identifierChain: [{ name: 'y' }], tables: [{ identifierChain: [{ name: 'y' }] }] },
+          { type: 'table', location: { first_line: 5, last_line: 5, first_column: 15, last_column: 16 }, identifierChain: [{ name: 'y' }] }
+        ]
+      });
+    });
+
     it('should report locations for "select x from x, y;"', function () {
       assertLocations({
         beforeCursor: 'select x from x, y;',