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

HUE-7390 [autocomplete] Fall back to only autocomplete the active statement in case there are surrounding errors

Johan Ahlen 8 жил өмнө
parent
commit
a5dde90

+ 26 - 0
desktop/core/src/desktop/static/desktop/js/sqlAutocompleter3.js

@@ -1834,6 +1834,32 @@ var SqlAutocompleter3 = (function () {
     try {
       var parseResult = sqlAutocompleteParser.parseSql(self.editor().getTextBeforeCursor(), self.editor().getTextAfterCursor(), self.snippet.type(), false);
 
+      // Fall back to the active statement only on errors.
+      if (parseResult.errors && parseResult.errors.length > 0 && self.snippet.positionStatement() && self.snippet.positionStatement().location) {
+        var beforeCursor = self.editor().session.getTextRange({
+          start: {
+            row: self.snippet.positionStatement().location.first_line - 1,
+            column: self.snippet.positionStatement().location.first_column
+          },
+          end: {
+            row: self.editor().getCursorPosition().row,
+            column: self.editor().getCursorPosition().column
+          }
+        });
+
+        var afterCursor = self.editor().session.getTextRange({
+          start: {
+            row: self.editor().getCursorPosition().row,
+            column: self.editor().getCursorPosition().column
+          },
+          end: {
+            row: self.snippet.positionStatement().location.last_line - 1,
+            column: self.snippet.positionStatement().location.last_column
+          }
+        });
+        parseResult = sqlAutocompleteParser.parseSql(beforeCursor, afterCursor, self.snippet.type(), false);
+      }
+
       if (typeof hueDebug !== 'undefined' && hueDebug.showParseResult) {
         console.log(parseResult);
       }

+ 25 - 11
desktop/core/src/desktop/static/desktop/spec/sqlAutocompleter3Spec.js

@@ -348,7 +348,13 @@
 
     describe('SqlAutocomplete3', function () {
 
-      var createSubject = function (dialect, textBeforeCursor, textAfterCursor) {
+      var createSubject = function (dialect, textBeforeCursor, textAfterCursor, positionStatement) {
+        var editor = ace.edit();
+        editor.setValue(textBeforeCursor);
+        var actualCursorPosition = editor.getCursorPosition();
+        editor.setValue(textBeforeCursor + textAfterCursor);
+        editor.moveCursorToPosition(actualCursorPosition);
+
         return new SqlAutocompleter3({
           snippet: {
             type: function () {
@@ -356,17 +362,11 @@
             },
             database: function () {
               'default'
-            }
+            },
+            positionStatement: ko.observable(positionStatement)
           },
-          editor: function() {
-            return {
-              getTextBeforeCursor: function () {
-                return textBeforeCursor;
-              },
-              getTextAfterCursor: function () {
-                return textAfterCursor;
-              }
-            }
+          editor: function () {
+            return editor
           }
         })
       };
@@ -385,6 +385,20 @@
         expect(subject.suggestions.filtered().length).toBeGreaterThan(0);
       });
 
+      it('should fallback to the active query when there are surrounding errors', function () {
+        var subject = createSubject('hive', 'SELECT FROMzzz bla LIMIT 1; SELECT ', ' FROM bla', { location: { first_line: 1, last_line: 1, first_column: 27, last_column: 52 }});
+        expect(subject.suggestions.filtered().length).toBe(0);
+        subject.autocomplete();
+        expect(subject.suggestions.filtered().length).toBeGreaterThan(0);
+      });
+
+      it('should only fallback to the active query when there are surrounding errors if there\'s an active query', function () {
+        var subject = createSubject('hive', 'SELECT FROMzzz bla LIMIT 1; SELECT ', ' FROM bla');
+        expect(subject.suggestions.filtered().length).toBe(0);
+        subject.autocomplete();
+        expect(subject.suggestions.filtered().length).toBe(0);
+      });
+
       it('should suggest columns from subqueries', function () {
         var subject = createSubject('hive', 'SELECT ', ' FROM customers, (SELECT app FROM web_logs) AS subQ;');
         expect(subject.suggestions.filtered().length).toBe(0);