Эх сурвалжийг харах

HUE-7062 [autocomplete] Extract commonly used autocomplete functionality to SqlUtils

Johan Ahlen 8 жил өмнө
parent
commit
ff380e2

+ 3 - 59
desktop/core/src/desktop/static/desktop/js/sqlAutocompleter3.js

@@ -175,34 +175,10 @@ var AutocompleteResults = (function () {
       var result = self.entries();
 
       if (self.filter()) {
-        var lowerCaseFilter = self.filter().toLowerCase();
-        result = result.filter(function (suggestion) {
-          // TODO: Extend with fuzzy matches
-          var foundIndex = suggestion.value.toLowerCase().indexOf(lowerCaseFilter);
-          if (foundIndex !== -1) {
-            if (foundIndex === 0 || (suggestion.filterValue && suggestion.filterValue.toLowerCase().indexOf(lowerCaseFilter) === 0)) {
-              suggestion.filterWeight = 3;
-            } else  {
-              suggestion.filterWeight = 2;
-            }
-          } else {
-            if (suggestion.details && suggestion.details.comment && lowerCaseFilter.indexOf(' ') === -1) {
-              foundIndex = suggestion.details.comment.toLowerCase().indexOf(lowerCaseFilter);
-              if (foundIndex !== -1) {
-                suggestion.filterWeight = 1;
-                suggestion.matchComment = true;
-              }
-            }
-          }
-          if (foundIndex !== -1) {
-            suggestion.matchIndex = foundIndex;
-            suggestion.matchLength = self.filter().length;
-            return true;
-          }
-          return false;
-        });
+        result = SqlUtils.autocompleteFilter(self.filter(), result);
         huePubSub.publish('hue.ace.autocompleter.match.updated');
       }
+
       updateCategories(result);
 
       var activeCategory = self.activeCategory();
@@ -221,39 +197,7 @@ var AutocompleteResults = (function () {
         return activeCategory === CATEGORIES.ALL || activeCategory === suggestion.category || (activeCategory === CATEGORIES.POPULAR && suggestion.popular());
       });
 
-      result.sort(function (a, b) {
-        if (self.filter()) {
-          if (typeof a.filterWeight !== 'undefined' && typeof b.filterWeight !== 'undefined' && b.filterWeight !== a.filterWeight) {
-            return b.filterWeight - a.filterWeight;
-          }
-          if (typeof a.filterWeight !== 'undefined' && typeof b.filterWeight === 'undefined') {
-            return -1;
-          }
-          if (typeof a.filterWeight === 'undefined' && typeof b.filterWeight !== 'undefined') {
-            return 1;
-          }
-        }
-        if (self.sortOverride && self.sortOverride.partitionColumnsFirst) {
-          if (a.partitionKey && !b.partitionKey) {
-            return -1;
-          }
-          if (b.partitionKey && !a.partitionKey) {
-            return 1;
-          }
-        }
-        var aWeight = a.category.weight + (a.weightAdjust || 0);
-        var bWeight = b.category.weight + (b.weightAdjust || 0);
-        if (typeof aWeight !== 'undefined' && typeof bWeight !== 'undefined' && bWeight !== aWeight) {
-          return bWeight - aWeight;
-        }
-        if (typeof aWeight !== 'undefined' && typeof bWeight === 'undefined') {
-          return -1;
-        }
-        if (typeof aWeight === 'undefined' && typeof bWeight !== 'undefined') {
-          return 1;
-        }
-        return a.value.localeCompare(b.value);
-      });
+      SqlUtils.sortSuggestions(result, self.filter(), self.sortOverride);
       self.sortOverride = null;
       return result;
     }).extend({ rateLimit: 200 });

+ 68 - 2
desktop/core/src/desktop/static/desktop/js/sqlUtils.js

@@ -45,7 +45,73 @@ var SqlUtils = (function () {
     TABLES: true, TABLESAMPLE: true, TBLPROPERTIES: true, TERMINATED: true, TEXTFILE: true, THEN: true, TIMESTAMP: true, TINYINT: true, TO: true, TRUE: true, UNCACHED: true, UNION: true, UPDATE_FN: true, UPSERT: true, USE: true, USING: true, VALUES: true, VIEW: true, WHEN: true, WHERE: true, WITH: true
   };
 
+  var autocompleteFilter = function (filter, entries) {
+    var lowerCaseFilter = filter.toLowerCase();
+    return entries.filter(function (suggestion) {
+      // TODO: Extend with fuzzy matches
+      var foundIndex = suggestion.value.toLowerCase().indexOf(lowerCaseFilter);
+      if (foundIndex !== -1) {
+        if (foundIndex === 0 || (suggestion.filterValue && suggestion.filterValue.toLowerCase().indexOf(lowerCaseFilter) === 0)) {
+          suggestion.filterWeight = 3;
+        } else  {
+          suggestion.filterWeight = 2;
+        }
+      } else {
+        if (suggestion.details && suggestion.details.comment && lowerCaseFilter.indexOf(' ') === -1) {
+          foundIndex = suggestion.details.comment.toLowerCase().indexOf(lowerCaseFilter);
+          if (foundIndex !== -1) {
+            suggestion.filterWeight = 1;
+            suggestion.matchComment = true;
+          }
+        }
+      }
+      if (foundIndex !== -1) {
+        suggestion.matchIndex = foundIndex;
+        suggestion.matchLength = filter.length;
+        return true;
+      }
+      return false;
+    });
+  };
+
+  var sortSuggestions = function (suggestions, filter, sortOverride) {
+    suggestions.sort(function (a, b) {
+      if (filter) {
+        if (typeof a.filterWeight !== 'undefined' && typeof b.filterWeight !== 'undefined' && b.filterWeight !== a.filterWeight) {
+          return b.filterWeight - a.filterWeight;
+        }
+        if (typeof a.filterWeight !== 'undefined' && typeof b.filterWeight === 'undefined') {
+          return -1;
+        }
+        if (typeof a.filterWeight === 'undefined' && typeof b.filterWeight !== 'undefined') {
+          return 1;
+        }
+      }
+      if (sortOverride && sortOverride.partitionColumnsFirst) {
+        if (a.partitionKey && !b.partitionKey) {
+          return -1;
+        }
+        if (b.partitionKey && !a.partitionKey) {
+          return 1;
+        }
+      }
+      var aWeight = a.category.weight + (a.weightAdjust || 0);
+      var bWeight = b.category.weight + (b.weightAdjust || 0);
+      if (typeof aWeight !== 'undefined' && typeof bWeight !== 'undefined' && bWeight !== aWeight) {
+        return bWeight - aWeight;
+      }
+      if (typeof aWeight !== 'undefined' && typeof bWeight === 'undefined') {
+        return -1;
+      }
+      if (typeof aWeight === 'undefined' && typeof bWeight !== 'undefined') {
+        return 1;
+      }
+      return a.value.localeCompare(b.value);
+    });
+  };
+
   return {
+    autocompleteFilter : autocompleteFilter,
     backTickIfNeeded: function (sourceType, identifier) {
       if (identifier.indexOf('`') === 0) {
         return identifier;
@@ -67,7 +133,7 @@ var SqlUtils = (function () {
     },
     locationEquals: function (a, b) {
       return a && b && a.first_line === b.first_line && a.first_column === b.first_column && a.last_line === b.last_line && a.last_column === b.last_column;
-    }
+    },
+    sortSuggestions: sortSuggestions
   }
-
 })();