[editor] Limit updates when cursor stays in the same statement and add throttle for change detection
AceLocationHandler.prototype.attachStatementLocator = function () {
var self = this;
- var changeThrottle = -1;
-
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 lastKnownStatements = [];
-
- var updateActiveStatement = function () {
+ var activeStatement;
+ var updateActiveStatement = function (cursorChange) {
+ var cursorPosition = self.editor.getCursorPosition();
+ if (cursorChange && activeStatement) {
+ // Don't update when cursor stays in the same statement
+ if (isPointInside(activeStatement.location, cursorPosition)) {
+ return;
+ }
+ }
var precedingStatements = [];
- var activeStatement = null;
var followingStatements = [];
+ activeStatement = null;
- var cursorPosition = self.editor.getCursorPosition();
var found = false;
var statementIndex = 0;
lastKnownStatements.forEach(function (statement) {
};
var parseForStatements = function () {
- window.clearTimeout(changeThrottle);
- changeThrottle = window.setTimeout(function () {
try {
lastKnownStatements = sqlStatementsParser.parse(self.editor.getValue());
} catch (error) {
console.warn('Could not parse statements!');
console.warn(error);
}
- }, 200);
};
+ var changeThrottle = -1;
+ var updateThrottle = -1;
+
+ var cursorChangePaused = false; // On change the cursor is also moved, this limits the calls while typing
var cursorSubscription = huePubSub.subscribe('editor.active.cursor.location', function (locationDetails) {
+ if (cursorChangePaused) {
+ return;
+ }
if (self.editorId === locationDetails.id) {
- updateActiveStatement();
+ window.clearTimeout(updateThrottle);
+ updateThrottle = window.setTimeout(function () {
+ updateActiveStatement(true);
+ }, 100);
}
});
- window.setTimeout(function () {
- parseForStatements();
- updateActiveStatement();
- }, 0);
+ changeThrottle = window.setTimeout(parseForStatements, 0);
+ updateThrottle = window.setTimeout(updateActiveStatement, 0);
var changeListener = self.editor.on("change", function () {
+ window.clearTimeout(changeThrottle);
+ cursorChangePaused = true;
+ changeThrottle = window.setTimeout(function () {
parseForStatements();
updateActiveStatement();
+ cursorChangePaused = false;
+ }, 500);
});
self.disposeFunctions.push(function () {
window.clearTimeout(changeThrottle);
+ window.clearTimeout(updateThrottle);
self.editor.off("change", changeListener);
cursorSubscription.remove();
});
