Browse Source

HUE-6361 [editor] Move gutter marker to the new Ace location handler

Johan Ahlen 8 years ago
parent
commit
927888014e

+ 82 - 34
desktop/core/src/desktop/static/desktop/js/ko.hue-bindings.js

@@ -3350,6 +3350,7 @@
       if (window.Worker) {
       if (window.Worker) {
         self.attachSqlWorker();
         self.attachSqlWorker();
       }
       }
+      self.attachGutterHandler();
     }
     }
 
 
     AceLocationHandler.prototype.attachCursorLocator = function () {
     AceLocationHandler.prototype.attachCursorLocator = function () {
@@ -3380,6 +3381,37 @@
       });
       });
     };
     };
 
 
+    AceLocationHandler.prototype.attachGutterHandler = function () {
+      var self = this;
+      var lastMarkedGutterLines = [];
+
+      var changedSubscription = huePubSub.subscribe('editor.active.statement.changed', function (statementDetails) {
+        if (statementDetails.id !== self.editorId || !statementDetails.activeStatement) {
+          return;
+        }
+        var leadingEmptyLineCount = 0;
+        var leadingWhiteSpace = statementDetails.activeStatement.statement.match(/^\s+/);
+        if (leadingWhiteSpace) {
+          var lineBreakMatch = leadingWhiteSpace[0].match(/(\r\n)|(\n)|(\r)/g);
+          if (lineBreakMatch) {
+            leadingEmptyLineCount = lineBreakMatch.length;
+          }
+        }
+
+        while(lastMarkedGutterLines.length) {
+          self.editor.session.removeGutterDecoration(lastMarkedGutterLines.shift(), 'ace-active-gutter-decoration');
+        }
+        for (var line = statementDetails.activeStatement.location.first_line - 1 + leadingEmptyLineCount; line < statementDetails.activeStatement.location.last_line; line ++) {
+          lastMarkedGutterLines.push(line);
+          self.editor.session.addGutterDecoration(line, 'ace-active-gutter-decoration');
+        }
+      });
+
+      self.disposeFunctions.push(function () {
+        changedSubscription.remove();
+      });
+    };
+
     AceLocationHandler.prototype.attachStatementLocator = function () {
     AceLocationHandler.prototype.attachStatementLocator = function () {
       var self = this;
       var self = this;
 
 
@@ -3394,54 +3426,70 @@
           (parseLocation.first_line < row && row === parseLocation.last_line && column <= parseLocation.last_column);
           (parseLocation.first_line < row && row === parseLocation.last_line && column <= parseLocation.last_column);
       };
       };
 
 
-      var updateStatementLocations = function () {
+      var lastKnownStatements = [];
+
+      var updateActiveStatement = function () {
+        var precedingStatements = [];
+        var activeStatement = null;
+        var followingStatements = [];
+
+        var cursorPosition = self.editor.getCursorPosition();
+        var found = false;
+        lastKnownStatements.forEach(function (statement) {
+          if (isPointInside(statement.location, cursorPosition)) {
+            found = true;
+            activeStatement = statement;
+          } else if (!found) {
+            if (precedingStatements.length === STATEMENT_COUNT_AROUND_ACTIVE) {
+              precedingStatements.shift();
+            }
+            precedingStatements.push(statement);
+          } else if (found && followingStatements.length < STATEMENT_COUNT_AROUND_ACTIVE) {
+            followingStatements.push(statement);
+          }
+        });
+
+        huePubSub.publish('editor.active.statement.changed', {
+          id: self.editorId,
+          totalStatementCount: lastKnownStatements.length,
+          precedingStatements: precedingStatements,
+          activeStatement: activeStatement,
+          followingStatements: followingStatements,
+        });
+      };
+
+      var parseForStatements = function () {
         window.clearTimeout(changeThrottle);
         window.clearTimeout(changeThrottle);
         changeThrottle = window.setTimeout(function () {
         changeThrottle = window.setTimeout(function () {
-          var precedingStatements = [];
-          var activeStatement = null;
-          var followingStatements = [];
-          var totalStatementCount = 0;
-
-          var cursorPosition = self.editor.getCursorPosition();
-          var found = false;
           try {
           try {
-            var statements = sqlStatementsParser.parse(self.editor.getValue());
-            totalStatementCount = statements.length;
-            statements.forEach(function (statement) {
-              if (isPointInside(statement.location, cursorPosition)) {
-                found = true;
-                activeStatement = statement;
-              } else if (!found) {
-                if (precedingStatements.length === STATEMENT_COUNT_AROUND_ACTIVE) {
-                  precedingStatements.shift();
-                }
-                precedingStatements.push(statement);
-              } else if (found && followingStatements.length < STATEMENT_COUNT_AROUND_ACTIVE) {
-                followingStatements.push(statement);
-              }
-            });
+            lastKnownStatements = sqlStatementsParser.parse(self.editor.getValue());
           } catch (error) {
           } catch (error) {
             console.warn('Could not parse statements!');
             console.warn('Could not parse statements!');
             console.warn(error);
             console.warn(error);
           }
           }
-
-          huePubSub.publish('editor.active.statements.changed', {
-            id: self.editorId,
-            totalStatementCount: totalStatementCount,
-            precedingStatements: precedingStatements,
-            activeStatement: activeStatement,
-            followingStatements: followingStatements,
-          });
         }, 200);
         }, 200);
       };
       };
 
 
-      window.setTimeout(updateStatementLocations, 0);
+      var cursorSubscription = huePubSub.subscribe('editor.active.cursor.location', function (locationDetails) {
+        if (self.editorId === locationDetails.id) {
+          updateActiveStatement();
+        }
+      });
 
 
-      var changeListener = self.editor.on("change", updateStatementLocations);
+      window.setTimeout(function () {
+        parseForStatements();
+        updateActiveStatement();
+      }, 0);
+
+      var changeListener = self.editor.on("change", function () {
+        parseForStatements();
+        updateActiveStatement();
+      });
 
 
       self.disposeFunctions.push(function () {
       self.disposeFunctions.push(function () {
         window.clearTimeout(changeThrottle);
         window.clearTimeout(changeThrottle);
         self.editor.off("change", changeListener);
         self.editor.off("change", changeListener);
+        cursorSubscription.remove();
       });
       });
     };
     };
 
 
@@ -3557,7 +3605,7 @@
       };
       };
 
 
 
 
-      var statementSubscription = huePubSub.subscribe('editor.active.statements.changed', function (statementDetails) {
+      var statementSubscription = huePubSub.subscribe('editor.active.statement.changed', function (statementDetails) {
         if (self.snippet.type() === 'hive' || snippet.snippet.type() === 'impala') {
         if (self.snippet.type() === 'hive' || snippet.snippet.type() === 'impala') {
           whenWorkerIsReady(function () {
           whenWorkerIsReady(function () {
             aceSqlWorker.postMessage({ statementDetails: statementDetails, type: self.snippet.type() });
             aceSqlWorker.postMessage({ statementDetails: statementDetails, type: self.snippet.type() });

+ 1 - 19
desktop/core/src/desktop/templates/assist.mako

@@ -1857,7 +1857,7 @@ from notebook.conf import get_ordered_interpreters
         };
         };
 
 
         var AceRange = ace.require('ace/range').Range;
         var AceRange = ace.require('ace/range').Range;
-        var lastMarkedGutterLines = [];
+
         var findStatementTextAtCursor = function () {
         var findStatementTextAtCursor = function () {
           if (!self.activeStatement() || !self.activeCursorLocation()) {
           if (!self.activeStatement() || !self.activeCursorLocation()) {
             return; // undefined when unknown
             return; // undefined when unknown
@@ -1865,24 +1865,6 @@ from notebook.conf import get_ordered_interpreters
           var statementLoc = self.activeStatement().location;
           var statementLoc = self.activeStatement().location;
 
 
           var editor = self.activeCursorLocation().editor;
           var editor = self.activeCursorLocation().editor;
-          var statementAtCursor = self.activeStatement().statement;
-
-          var leadingEmptyLineCount = 0;
-          var leadingWhiteSpace = statementAtCursor.match(/^\s+/);
-          if (leadingWhiteSpace) {
-            var lineBreakMatch = leadingWhiteSpace[0].match(/^(\r\n)|(\n)|(\r)/g);
-            if (lineBreakMatch) {
-              leadingEmptyLineCount = lineBreakMatch.length;
-            }
-          }
-
-          while(lastMarkedGutterLines.length) {
-            editor.session.removeGutterDecoration(lastMarkedGutterLines.shift(), 'ace-active-gutter-decoration');
-          }
-          for (var line = statementLoc.first_line - 1 + leadingEmptyLineCount; line < statementLoc.last_line; line ++) {
-            lastMarkedGutterLines.push(line);
-            editor.session.addGutterDecoration(line, 'ace-active-gutter-decoration');
-          }
 
 
           return editor.session.getTextRange(new AceRange(statementLoc.first_line - 1, statementLoc.first_column - 1, statementLoc.last_line - 1, statementLoc.last_column - 1));
           return editor.session.getTextRange(new AceRange(statementLoc.first_line - 1, statementLoc.first_column - 1, statementLoc.last_line - 1, statementLoc.last_column - 1));
         };
         };