Browse Source

HUE-7280 [editor] Mark the executed statement in the gutter when multiple statements are selected

This also improves the location matching performance and removes some redundant and unused code.
Johan Ahlen 8 years ago
parent
commit
a690eeb

+ 72 - 77
desktop/core/src/desktop/static/desktop/js/ko.hue-bindings.js

@@ -3448,40 +3448,11 @@
 
 
       self.disposeFunctions = [];
       self.disposeFunctions = [];
 
 
-      self.attachCursorLocator();
       self.attachStatementLocator();
       self.attachStatementLocator();
       self.attachSqlWorker();
       self.attachSqlWorker();
       self.attachGutterHandler();
       self.attachGutterHandler();
     }
     }
 
 
-    AceLocationHandler.prototype.attachCursorLocator = function () {
-      var self = this;
-      var changeCursorThrottle = -1;
-      var changeCursorListener = self.editor.selection.on('changeCursor', function () {
-        window.clearTimeout(changeCursorThrottle);
-        changeCursorThrottle = window.setTimeout(function () {
-          huePubSub.publish('editor.active.cursor.location', {
-            id: self.editorId,
-            position: self.editor.getCursorPosition(),
-            editor: self.editor
-          });
-        }, 150);
-      });
-
-      self.disposeFunctions.push(function () {
-        window.clearTimeout(changeCursorThrottle);
-        self.editor.selection.off('changeCursor', changeCursorListener);
-      });
-
-      var cursorLocationSub = huePubSub.subscribe('get.active.editor.cursor.location', function () {
-        huePubSub.publish('editor.active.cursor.location', { id: self.editorId, position: self.editor.getCursorPosition(), editor: self.editor });
-      });
-
-      self.disposeFunctions.push(function () {
-        cursorLocationSub.remove();
-      });
-    };
-
     AceLocationHandler.prototype.attachGutterHandler = function () {
     AceLocationHandler.prototype.attachGutterHandler = function () {
       var self = this;
       var self = this;
       var lastMarkedGutterLines = [];
       var lastMarkedGutterLines = [];
@@ -3515,23 +3486,44 @@
 
 
     AceLocationHandler.prototype.attachStatementLocator = function () {
     AceLocationHandler.prototype.attachStatementLocator = function () {
       var self = this;
       var self = this;
+      var lastKnownStatements = [];
+      var activeStatement;
 
 
-      var isPointInside = function (parseLocation, acePosition) {
-        var row = acePosition.row + 1; // ace positioning has 0 based rows while the parser has 1
-        var column = acePosition.column;
+      var isPointInside = function (parseLocation, editorPosition) {
+        var row = editorPosition.row + 1; // ace positioning has 0 based rows while the parser has 1
+        var column = editorPosition.column;
         return (parseLocation.first_line < row && row < parseLocation.last_line) ||
         return (parseLocation.first_line < row && row < parseLocation.last_line) ||
-          (parseLocation.first_line === row && row === parseLocation.last_line && parseLocation.first_column <= column && column <= parseLocation.last_column) ||
+          (parseLocation.first_line === row && row === parseLocation.last_line && parseLocation.first_column <= column && column < parseLocation.last_column) ||
           (parseLocation.first_line === row && row < parseLocation.last_line && column >= parseLocation.first_column) ||
           (parseLocation.first_line === row && row < parseLocation.last_line && column >= parseLocation.first_column) ||
-          (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 lastKnownStatements = [];
-      var activeStatement;
+      var lastExecutingStatement = null;
       var updateActiveStatement = function (cursorChange) {
       var updateActiveStatement = function (cursorChange) {
-        var cursorPosition = self.editor.getCursorPosition();
+        var selectionRange = self.editor.getSelectionRange();
+        var editorLocation = selectionRange.start;
+        if (selectionRange.start.row !== selectionRange.end.row || selectionRange.start.column !== selectionRange.end.column) {
+          if (!cursorChange && self.snippet.result && self.snippet.result.statement_range()) {
+            var executingStatement = self.snippet.result.statement_range();
+            // Row and col are 0 for both start and end on execute, so if the selection hasn't changed we'll use last known executed statement
+            if (executingStatement.start.row === 0 && executingStatement.start.column === 0 && executingStatement.end.row === 0 && executingStatement.end.column === 0 && lastExecutingStatement) {
+              executingStatement = lastExecutingStatement;
+            }
+            if (executingStatement.start.row === 0) {
+              editorLocation.column += executingStatement.start.column;
+            } else if (executingStatement.start.row !== 0 || executingStatement.start.column !== 0) {
+              editorLocation.row += executingStatement.start.row;
+              editorLocation.column = executingStatement.start.column;
+            }
+            lastExecutingStatement = executingStatement;
+          } else  {
+            lastExecutingStatement = null;
+          }
+        }
+
         if (cursorChange && activeStatement) {
         if (cursorChange && activeStatement) {
           // Don't update when cursor stays in the same statement
           // Don't update when cursor stays in the same statement
-          if (isPointInside(activeStatement.location, cursorPosition)) {
+          if (isPointInside(activeStatement.location, editorLocation)) {
             return;
             return;
           }
           }
         }
         }
@@ -3541,21 +3533,31 @@
 
 
         var found = false;
         var found = false;
         var statementIndex = 0;
         var statementIndex = 0;
-        lastKnownStatements.forEach(function (statement) {
-          if (isPointInside(statement.location, cursorPosition)) {
-            statementIndex++;
-            found = true;
-            activeStatement = statement;
-          } else if (!found) {
-            statementIndex++;
-            if (precedingStatements.length === STATEMENT_COUNT_AROUND_ACTIVE) {
-              precedingStatements.shift();
+        if (lastKnownStatements.length === 1) {
+          activeStatement = lastKnownStatements[0];
+        } else {
+          lastKnownStatements.forEach(function (statement) {
+            if (isPointInside(statement.location, editorLocation)) {
+              statementIndex++;
+              found = true;
+              activeStatement = statement;
+            } else if (!found) {
+              statementIndex++;
+              if (precedingStatements.length === STATEMENT_COUNT_AROUND_ACTIVE) {
+                precedingStatements.shift();
+              }
+              precedingStatements.push(statement);
+            } else if (found && followingStatements.length < STATEMENT_COUNT_AROUND_ACTIVE) {
+              followingStatements.push(statement);
             }
             }
-            precedingStatements.push(statement);
-          } else if (found && followingStatements.length < STATEMENT_COUNT_AROUND_ACTIVE) {
-            followingStatements.push(statement);
+          });
+
+          // Can happen if multiple statements and the cursor is after the last one
+          if (!found) {
+            precedingStatements.pop();
+            activeStatement = lastKnownStatements[lastKnownStatements.length - 1];
           }
           }
-        });
+        }
 
 
         huePubSub.publish('editor.active.statement.changed', {
         huePubSub.publish('editor.active.statement.changed', {
           id: self.editorId,
           id: self.editorId,
@@ -3567,7 +3569,7 @@
         });
         });
 
 
         if (activeStatement) {
         if (activeStatement) {
-          self.checkForSyntaxErrors(activeStatement.location, cursorPosition);
+          self.checkForSyntaxErrors(activeStatement.location, editorLocation);
         }
         }
       };
       };
 
 
@@ -3583,40 +3585,32 @@
         }
         }
       };
       };
 
 
-      var changeThrottle = -1;
-      var updateThrottle = -1;
-
+      var changeThrottle = window.setTimeout(parseForStatements, 0);
+      var updateThrottle = window.setTimeout(updateActiveStatement, 0);
       var cursorChangePaused = false; // On change the cursor is also moved, this limits the calls while typing
       var cursorChangePaused = false; // On change the cursor is also moved, this limits the calls while typing
-      var forceUpdateSubscription = huePubSub.subscribe('editor.identify.statement.locations', function (editorId) {
-        if (self.editorId === editorId) {
-          cursorChangePaused = true;
-          window.clearTimeout(updateThrottle);
-          window.clearTimeout(changeThrottle);
-          parseForStatements();
-          updateActiveStatement();
-          cursorChangePaused = false;
-        }
-      });
 
 
-      var cursorSubscription = huePubSub.subscribe('editor.active.cursor.location', function (locationDetails) {
+      var lastStart;
+      var changeSelectionListener = self.editor.on('changeSelection', function () {
         if (cursorChangePaused) {
         if (cursorChangePaused) {
           return;
           return;
         }
         }
-        if (self.editorId === locationDetails.id) {
+        window.clearTimeout(changeThrottle);
+        changeThrottle = window.setTimeout(function () {
+          var newStart = self.editor.getSelectionRange().start;
+          if (lastStart && lastStart.row === newStart.row && lastStart.column === newStart.column) {
+            return;
+          }
           window.clearTimeout(updateThrottle);
           window.clearTimeout(updateThrottle);
-          updateThrottle = window.setTimeout(function () {
-            updateActiveStatement(true);
-          }, 100);
-        }
+          updateActiveStatement(true);
+          lastStart = newStart;
+        }, 100);
       });
       });
 
 
-      changeThrottle = window.setTimeout(parseForStatements, 0);
-      updateThrottle = window.setTimeout(updateActiveStatement, 0);
-
       var changeListener = self.editor.on("change", function () {
       var changeListener = self.editor.on("change", function () {
         window.clearTimeout(changeThrottle);
         window.clearTimeout(changeThrottle);
         cursorChangePaused = true;
         cursorChangePaused = true;
         changeThrottle = window.setTimeout(function () {
         changeThrottle = window.setTimeout(function () {
+          window.clearTimeout(updateThrottle);
           parseForStatements();
           parseForStatements();
           updateActiveStatement();
           updateActiveStatement();
           cursorChangePaused = false;
           cursorChangePaused = false;
@@ -3625,20 +3619,21 @@
 
 
       var locateSubscription = huePubSub.subscribe('editor.refresh.statement.locations', function (snippet) {
       var locateSubscription = huePubSub.subscribe('editor.refresh.statement.locations', function (snippet) {
         if (snippet === self.snippet) {
         if (snippet === self.snippet) {
+          cursorChangePaused = true;
           window.clearTimeout(changeThrottle);
           window.clearTimeout(changeThrottle);
           window.clearTimeout(updateThrottle);
           window.clearTimeout(updateThrottle);
           parseForStatements();
           parseForStatements();
           updateActiveStatement();
           updateActiveStatement();
+          cursorChangePaused = false;
         }
         }
       });
       });
 
 
       self.disposeFunctions.push(function () {
       self.disposeFunctions.push(function () {
         window.clearTimeout(changeThrottle);
         window.clearTimeout(changeThrottle);
         window.clearTimeout(updateThrottle);
         window.clearTimeout(updateThrottle);
-        self.editor.off("change", changeListener);
-        forceUpdateSubscription.remove();
+        self.editor.off('changeSelection', changeSelectionListener);
+        self.editor.off('change', changeListener);
         locateSubscription.remove();
         locateSubscription.remove();
-        cursorSubscription.remove();
       });
       });
     };
     };
 
 

+ 1 - 1
desktop/core/src/desktop/static/desktop/js/sqlAutocompleter3.js

@@ -1877,7 +1877,7 @@ var SqlAutocompleter3 = (function () {
       // This could happen in case the user is editing at the borders of the statement and the locations haven't
       // This could happen in case the user is editing at the borders of the statement and the locations haven't
       // been updated yet, in that case we have to force a location update before parsing
       // been updated yet, in that case we have to force a location update before parsing
       if (!parseResult) {
       if (!parseResult) {
-        huePubSub.publish('editor.identify.statement.locations', self.snippet.id());
+        huePubSub.publish('editor.refresh.statement.locations', self.snippet);
         parseResult = self.parseActiveStatement();
         parseResult = self.parseActiveStatement();
       }
       }
     } catch (e) {
     } catch (e) {