Selaa lähdekoodia

HUE-4031 [editor] Add a dedicated web worker for syntax checking

Johan Ahlen 8 vuotta sitten
vanhempi
commit
3c77ccf

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

@@ -3377,6 +3377,7 @@
       self.editor = editor;
       self.editorId = editorId;
       self.snippet = snippet;
+      self.aceSqlSyntaxWorker = null;
 
       self.disposeFunctions = [];
 
@@ -3497,8 +3498,12 @@
           totalStatementCount: lastKnownStatements.length,
           precedingStatements: precedingStatements,
           activeStatement: activeStatement,
-          followingStatements: followingStatements,
+          followingStatements: followingStatements
         });
+
+        if (activeStatement) {
+          self.checkForSyntaxErrors(activeStatement.location, cursorPosition);
+        }
       };
 
       var parseForStatements = function () {
@@ -3560,12 +3565,40 @@
       });
     };
 
+    AceLocationHandler.prototype.checkForSyntaxErrors = function (statementLocation, cursorPosition) {
+      var self = this;
+      if (self.aceSqlSyntaxWorker !== null) {
+        var AceRange = ace.require('ace/range').Range;
+        var beforeCursor = self.editor.session.getTextRange(new AceRange(statementLocation.first_line - 1, statementLocation.first_column, cursorPosition.row, cursorPosition.column));
+        var afterCursor = self.editor.session.getTextRange(new AceRange(cursorPosition.row, cursorPosition.column, statementLocation.last_line - 1, statementLocation.last_column));
+        self.aceSqlSyntaxWorker.postMessage({ beforeCursor: beforeCursor, afterCursor: afterCursor, statementLocation: statementLocation, type: self.snippet.type() });
+      }
+    };
+
+    AceLocationHandler.prototype.attachSqlSyntaxWorker = function () {
+      var self = this;
+      if (window.Worker) {
+        self.aceSqlSyntaxWorker = new Worker('/desktop/workers/aceSqlSyntaxWorker.js?bust=' + Math.random());
+        self.aceSqlSyntaxWorker.onmessage = function(e) {
+          // TODO: Add error marking e.data.syntaxError
+          console.log(e.data);
+        }
+      }
+    };
+
+    AceLocationHandler.prototype.detachSqlSyntaxWorker = function () {
+      if (self.aceSqlSyntaxWorker !== null) {
+        self.aceSqlSyntaxWorker.terminate();
+        self.aceSqlSyntaxWorker = null;
+      }
+    };
+
     AceLocationHandler.prototype.attachSqlWorker = function () {
       var self = this;
 
       var apiHelper = ApiHelper.getInstance();
       var activeTokens = [];
-      var aceSqlWorker = new Worker('/desktop/workers/aceSqlWorker.js');
+      var aceSqlWorker = new Worker('/desktop/workers/aceSqlLocationWorker.js?bust=' + Math.random());
       var workerIsReady = false;
 
       self.disposeFunctions.push(function () {
@@ -3696,6 +3729,7 @@
 
     AceLocationHandler.prototype.dispose = function () {
       var self = this;
+      self.detachSqlSyntaxWorker();
       self.disposeFunctions.forEach(function (dispose) {
         dispose();
       })
@@ -3824,8 +3858,6 @@
         setShowGutter: true
       };
 
-      var errorHighlightingEnabled = snippet.getApiHelper().getFromTotalStorage('hue.ace', 'errorHighlightingEnabled', false);
-
       editor.customMenuOptions = {
         setEnableAutocompleter: function (enabled) {
           editor.setOption('enableBasicAutocompletion', enabled);
@@ -3866,11 +3898,22 @@
       };
 
       if (window.Worker) {
-        editor.customMenuOptions.setExperimentalErrorHighlighting = function (enabled) {
+        var errorHighlightingEnabled = snippet.getApiHelper().getFromTotalStorage('hue.ace', 'errorHighlightingEnabled', false);
+
+        if (errorHighlightingEnabled) {
+          aceLocationHandler.attachSqlSyntaxWorker();
+        }
+
+        editor.customMenuOptions.setErrorHighlighting = function (enabled) {
           errorHighlightingEnabled = enabled;
           snippet.getApiHelper().setInTotalStorage('hue.ace', 'errorHighlightingEnabled', enabled);
+          if (enabled) {
+            aceLocationHandler.attachSqlSyntaxWorker();
+          } else {
+            aceLocationHandler.detachSqlSyntaxWorker();
+          }
         };
-        editor.customMenuOptions.getExperimentalErrorHighlighting = function () {
+        editor.customMenuOptions.getErrorHighlighting = function () {
           return errorHighlightingEnabled;
         };
       }

+ 0 - 0
desktop/core/src/desktop/templates/ace_sql_worker.mako → desktop/core/src/desktop/templates/ace_sql_location_worker.mako


+ 39 - 0
desktop/core/src/desktop/templates/ace_sql_syntax_worker.mako

@@ -0,0 +1,39 @@
+## Licensed to Cloudera, Inc. under one
+## or more contributor license agreements.  See the NOTICE file
+## distributed with this work for additional information
+## regarding copyright ownership.  Cloudera, Inc. licenses this file
+## to you under the Apache License, Version 2.0 (the
+## "License"); you may not use this file except in compliance
+## with the License.  You may obtain a copy of the License at
+##
+##     http://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+
+var version = 1;
+importScripts('${ static('desktop/js/autocomplete/sqlParseSupport.js') }' + '?version=' + version);
+importScripts('${ static('desktop/js/autocomplete/sqlSyntaxParser.js') }' + '?version=' + version);
+
+(function () {
+
+  this.throttle = -1;
+
+  this.onmessage = function (msg) {
+    if (msg.data.ping) {
+      postMessage({ ping: true });
+      return;
+    }
+    clearTimeout(this.throttle);
+    this.throttle = setTimeout(function () {
+      var sqlParseResult = sqlSyntaxParser.parseSyntax(msg.data.beforeCursor, msg.data.afterCursor, msg.data.type, false);
+      postMessage({
+        syntaxError: sqlParseResult,
+        statementLocation: msg.data.statementLocation
+      });
+    }, 400);
+  }
+})();

+ 2 - 1
desktop/core/src/desktop/urls.py

@@ -107,7 +107,8 @@ dynamic_patterns += patterns('desktop.views',
   (r'^jasmine', 'jasmine'),
 
   # Web workers
-  (r'^desktop/workers/aceSqlWorker.js', 'ace_sql_worker'),
+  (r'^desktop/workers/aceSqlLocationWorker.js', 'ace_sql_location_worker'),
+  (r'^desktop/workers/aceSqlSyntaxWorker.js', 'ace_sql_syntax_worker'),
 
   # Unsupported browsers
   (r'^boohoo$','unsupported'),

+ 5 - 2
desktop/core/src/desktop/views.py

@@ -327,8 +327,11 @@ def jasmine(request):
   return render('jasmine.mako', request, None)
 
 
-def ace_sql_worker(request):
-  return HttpResponse(render('ace_sql_worker.mako', request, None), content_type="application/javascript")
+def ace_sql_location_worker(request):
+  return HttpResponse(render('ace_sql_location_worker.mako', request, None), content_type="application/javascript")
+
+def ace_sql_syntax_worker(request):
+  return HttpResponse(render('ace_sql_syntax_worker.mako', request, None), content_type="application/javascript")
 
 def assist_m(request):
   return render('assist_m.mako', request, None)