Merge pull request #2272 from adamjimenez/autocomplete-exact

Autocomplete exactMatch option
This commit is contained in:
Harutyun Amirjanyan 2014-12-12 17:21:39 +04:00
commit 232b3c0f8c

View file

@ -42,6 +42,7 @@ var snippetManager = require("./snippets").snippetManager;
var Autocomplete = function() { var Autocomplete = function() {
this.autoInsert = true; this.autoInsert = true;
this.autoSelect = true; this.autoSelect = true;
this.exactMatch = false;
this.keyboardHandler = new HashHandler(); this.keyboardHandler = new HashHandler();
this.keyboardHandler.bindKeys(this.commands); this.keyboardHandler.bindKeys(this.commands);
@ -306,6 +307,10 @@ var Autocomplete = function() {
return; return;
this.completions = new FilteredList(matches); this.completions = new FilteredList(matches);
if (this.exactMatch)
this.completions.exactMatch = true;
this.completions.setFilter(prefix); this.completions.setFilter(prefix);
var filtered = this.completions.filtered; var filtered = this.completions.filtered;
@ -416,6 +421,7 @@ var FilteredList = function(array, filterText, mutateData) {
this.all = array; this.all = array;
this.filtered = array; this.filtered = array;
this.filterText = filterText || ""; this.filterText = filterText || "";
this.exactMatch = false;
}; };
(function(){ (function(){
this.setFilter = function(str) { this.setFilter = function(str) {
@ -452,23 +458,29 @@ var FilteredList = function(array, filterText, mutateData) {
var matchMask = 0; var matchMask = 0;
var penalty = 0; var penalty = 0;
var index, distance; var index, distance;
// caption char iteration is faster in Chrome but slower in Firefox, so lets use indexOf
for (var j = 0; j < needle.length; j++) { if (this.exactMatch) {
// TODO add penalty on case mismatch if (needle !== caption.substr(0, needle.length))
var i1 = caption.indexOf(lower[j], lastIndex + 1);
var i2 = caption.indexOf(upper[j], lastIndex + 1);
index = (i1 >= 0) ? ((i2 < 0 || i1 < i2) ? i1 : i2) : i2;
if (index < 0)
continue loop; continue loop;
distance = index - lastIndex - 1; }else{
if (distance > 0) { // caption char iteration is faster in Chrome but slower in Firefox, so lets use indexOf
// first char mismatch should be more sensitive for (var j = 0; j < needle.length; j++) {
if (lastIndex === -1) // TODO add penalty on case mismatch
penalty += 10; var i1 = caption.indexOf(lower[j], lastIndex + 1);
penalty += distance; var i2 = caption.indexOf(upper[j], lastIndex + 1);
index = (i1 >= 0) ? ((i2 < 0 || i1 < i2) ? i1 : i2) : i2;
if (index < 0)
continue loop;
distance = index - lastIndex - 1;
if (distance > 0) {
// first char mismatch should be more sensitive
if (lastIndex === -1)
penalty += 10;
penalty += distance;
}
matchMask = matchMask | (1 << index);
lastIndex = index;
} }
matchMask = matchMask | (1 << index);
lastIndex = index;
} }
item.matchMask = matchMask; item.matchMask = matchMask;
item.exactMatch = penalty ? 0 : 1; item.exactMatch = penalty ? 0 : 1;