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