Prechádzať zdrojové kódy

[frontend] Enabled predict typeahead when connector optimizer is set to api

Johan Åhlén 4 rokov pred
rodič
commit
b32adccfa2

+ 4 - 3
desktop/core/src/desktop/js/apps/editor/components/aceEditor/AceEditor.vue

@@ -54,7 +54,7 @@
     ParsedLocation,
     SqlParserProvider
   } from 'parse/types';
-  import { EditorInterpreter } from 'config/types';
+  import { Connector, EditorInterpreter } from 'config/types';
   import { SqlReferenceProvider } from 'sql/reference/types';
   import { hueWindow } from 'types/types';
   import huePubSub from 'utils/huePubSub';
@@ -518,9 +518,10 @@
           e.text = removeUnicodes(e.text);
         };
 
-        if ((<hueWindow>window).ENABLE_PREDICT) {
+        const connector: Connector = executor.value.connector();
+        if (sqlAnalyzerProvider.value && connector.optimizer === 'api') {
           try {
-            attachPredictTypeahead(editor, executor.value.connector(), sqlAnalyzerProvider.value);
+            attachPredictTypeahead(editor, connector, sqlAnalyzerProvider.value);
           } catch (e) {
             console.warn('Failed attaching predict typeahead...');
             console.error(e);

+ 26 - 20
desktop/core/src/desktop/js/apps/editor/components/aceEditor/acePredict.ts

@@ -64,7 +64,9 @@ export const attachPredictTypeahead = (
 
   const updatePredictTypeahead = () => {
     if (activePredictPromise) {
-      activePredictPromise.cancel();
+      try {
+        activePredictPromise.cancel();
+      } catch {}
     }
     defer(() => {
       const editorText = editor.getValue();
@@ -76,26 +78,30 @@ export const attachPredictTypeahead = (
       ) {
         removeActivePredict();
       }
-      if (editorText.length && !activePredict) {
-        sqlAnalyzer
-          .predict({
-            beforeCursor: editor.getTextBeforeCursor(),
-            afterCursor: editor.getTextAfterCursor()
-          })
-          .then(({ prediction }) => {
-            if (prediction !== lastPrediction) {
-              const beforeCursor = editor.getTextBeforeCursor();
-              if (prediction && prediction.toLowerCase().startsWith(beforeCursor.toLowerCase())) {
-                setActivePredict(beforeCursor + prediction.slice(beforeCursor.length));
-              } else {
-                removeActivePredict();
+      try {
+        if (editorText.length && !activePredict) {
+          sqlAnalyzer
+            .predict({
+              beforeCursor: editor.getTextBeforeCursor(),
+              afterCursor: editor.getTextAfterCursor()
+            })
+            .then(({ prediction }) => {
+              if (prediction !== lastPrediction) {
+                const beforeCursor = editor.getTextBeforeCursor();
+                if (prediction && prediction.toLowerCase().startsWith(beforeCursor.toLowerCase())) {
+                  setActivePredict(beforeCursor + prediction.slice(beforeCursor.length));
+                } else {
+                  removeActivePredict();
+                }
               }
-            }
-            lastPrediction = prediction;
-          })
-          .catch(() => {
-            removeActivePredict();
-          });
+              lastPrediction = prediction;
+            })
+            .catch(() => {
+              removeActivePredict();
+            });
+        }
+      } catch {
+        removeActivePredict();
       }
     });
   };

+ 3 - 0
desktop/core/src/desktop/js/apps/editor/components/sqlScratchpad/SqlScratchpad.vue

@@ -26,6 +26,7 @@
           :id="id"
           :ace-options="aceOptions"
           :executor="executor"
+          :sql-analyzer-provider="sqlAnalyzerRepository"
           :sql-parser-provider="sqlParserProvider"
           :sql-reference-provider="sqlReferenceProvider"
           @active-statement-changed="onActiveStatementChanged"
@@ -91,6 +92,7 @@
   import { login } from 'api/auth';
   import { setBaseUrl } from 'api/utils';
   import contextCatalog from 'catalog/contextCatalog';
+  import sqlAnalyzerRepository from 'catalog/analyzer/sqlAnalyzerRepository';
   import HueIcons from 'components/icons/HueIcons.vue';
   import HueButton from 'components/HueButton.vue';
   import Spinner from 'components/Spinner.vue';
@@ -264,6 +266,7 @@
         logs,
         logsVisible,
         onActiveStatementChanged,
+        sqlAnalyzerRepository,
         sqlParserProvider,
         sqlReferenceProvider
       };