Autocomplete exactMatch option

Option to show only exact matches in autocomplete
This commit is contained in:
Adam Jimenez 2014-12-12 10:27:45 +00:00
commit cfdb223e7a

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,6 +458,11 @@ var FilteredList = function(array, filterText, mutateData) {
var matchMask = 0; var matchMask = 0;
var penalty = 0; var penalty = 0;
var index, distance; var index, distance;
if (this.exactMatch) {
if (needle !== caption.substr(0, needle.length))
continue loop;
}else{
// caption char iteration is faster in Chrome but slower in Firefox, so lets use indexOf // caption char iteration is faster in Chrome but slower in Firefox, so lets use indexOf
for (var j = 0; j < needle.length; j++) { for (var j = 0; j < needle.length; j++) {
// TODO add penalty on case mismatch // TODO add penalty on case mismatch
@ -470,6 +481,7 @@ var FilteredList = function(array, filterText, mutateData) {
matchMask = matchMask | (1 << index); matchMask = matchMask | (1 << index);
lastIndex = index; lastIndex = index;
} }
}
item.matchMask = matchMask; item.matchMask = matchMask;
item.exactMatch = penalty ? 0 : 1; item.exactMatch = penalty ? 0 : 1;
item.score = (item.score || 0) - penalty; item.score = (item.score || 0) - penalty;