1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-20 21:08:12 +01:00

Speed up opening of the completion list and substring completion, significantly.

This commit is contained in:
Kris Maglione
2010-09-23 21:49:46 -04:00
parent 8de49e4764
commit 2fd301c81a
2 changed files with 36 additions and 10 deletions

View File

@@ -213,6 +213,11 @@ const CompletionContext = Class("CompletionContext", {
get allItems() { get allItems() {
try { try {
let self = this; let self = this;
let allItems = this.contextList.map(function (context) context.hasItems && context.items);
if (this.cache.allItems && array.equals(this.cache.allItems, allItems))
return this.cache.allItemsResult;
this.cache.allItems = allItems;
let minStart = Math.min.apply(Math, [context.offset for ([k, context] in Iterator(this.contexts)) if (context.hasItems && context.items.length)]); let minStart = Math.min.apply(Math, [context.offset for ([k, context] in Iterator(this.contexts)) if (context.hasItems && context.items.length)]);
if (minStart == Infinity) if (minStart == Infinity)
minStart = 0; minStart = 0;
@@ -226,7 +231,9 @@ const CompletionContext = Class("CompletionContext", {
__proto__: item __proto__: item
})); }));
}); });
return { start: minStart, items: array.flatten(items), longestSubstring: this.longestAllSubstring }; this.cache.allItemsResult = { start: minStart, items: array.flatten(items) };
memoize(this.cache.allItemsResult, "longestSubstring", function () self.longestAllSubstring);
return this.cache.allItemsResult;
} }
catch (e) { catch (e) {
dactyl.reportError(e); dactyl.reportError(e);
@@ -242,6 +249,9 @@ const CompletionContext = Class("CompletionContext", {
return context.substrings.map(function (s) prefix + s); return context.substrings.map(function (s) prefix + s);
}); });
/* TODO: Deal with sub-substrings for multiple contexts again.
* Possibly.
*/
let substrings = lists.reduce( let substrings = lists.reduce(
function (res, list) res.filter(function (str) list.some(function (s) s.substr(0, str.length) == str)), function (res, list) res.filter(function (str) list.some(function (s) s.substr(0, str.length) == str)),
lists.pop()); lists.pop());
@@ -264,7 +274,7 @@ const CompletionContext = Class("CompletionContext", {
set completions(items) { set completions(items) {
// Accept a generator // Accept a generator
if (!isArray(items)) if (!isArray(items))
items = [x for (x in Iterator(items))]; items = [x for (x in Iterator(items || []))];
if (this._completions !== items) { if (this._completions !== items) {
delete this.cache.filtered; delete this.cache.filtered;
delete this.cache.filter; delete this.cache.filter;
@@ -456,8 +466,7 @@ const CompletionContext = Class("CompletionContext", {
let filter = fixCase(this.filter); let filter = fixCase(this.filter);
if (this.anchored) { if (this.anchored) {
var compare = function compare(text, s) text.substr(0, s.length) == s; var compare = function compare(text, s) text.substr(0, s.length) == s;
var substrings = util.map(util.range(filter.length, text.length + 1), var substrings = [text.substring(filter.length)];
function (end) text.substring(0, end));
} }
else { else {
var compare = function compare(text, s) text.indexOf(s) >= 0; var compare = function compare(text, s) text.indexOf(s) >= 0;
@@ -466,14 +475,31 @@ const CompletionContext = Class("CompletionContext", {
let idx; let idx;
let length = filter.length; let length = filter.length;
while ((idx = text.indexOf(filter, start)) > -1 && idx < text.length) { while ((idx = text.indexOf(filter, start)) > -1 && idx < text.length) {
for (let end in util.range(idx + length, text.length + 1)) substrings.push(text.substring(idx));
substrings.push(text.substring(idx, end));
start = idx + 1; start = idx + 1;
} }
} }
substrings = items.reduce( substrings = items.reduce(function (res, item)
function (res, item) res.filter(function (str) compare(fixCase(item.unquoted || item.text), str)), res.map(function (list) {
substrings); var m, len = list.length;
var n = list.length;
var i = 0;
while (n) {
m = Math.floor(n / 2);
let s = list[i + m];
let keep = compare(fixCase(item.text), list.substring(0, i + m));
if (!keep)
len = i + m;
if (!keep || m == 0)
n = m;
else {
i += m;
n = n - m;
}
}
return len == list.length ? list : list.substr(0, len);
}),
substrings);
let quote = this.quote; let quote = this.quote;
if (quote) if (quote)
substrings = substrings.map(function (str) quote[0] + quote[1](str)); substrings = substrings.map(function (str) quote[0] + quote[1](str));

View File

@@ -946,7 +946,7 @@ const array = Class("array", Array, {
* @returns {boolean} * @returns {boolean}
*/ */
equals: function (ary1, ary2) equals: function (ary1, ary2)
ary1.length == ary2.length && Array.every(ary1, function (e, i) e == ary2[i]), ary1.length === ary2.length && Array.every(ary1, function (e, i) e === ary2[i]),
/** /**
* Flattens an array, such that all elements of the array are * Flattens an array, such that all elements of the array are