1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2026-03-08 03:55:48 +01:00

Optimization for async completion result processing.

This commit is contained in:
Kris Maglione
2011-10-03 01:25:28 -04:00
parent 0a5f0190c3
commit 6ed1da087f
2 changed files with 77 additions and 30 deletions

View File

@@ -1029,8 +1029,11 @@ var CommandLine = Module("commandline", {
*/ */
Completions: Class("Completions", { Completions: Class("Completions", {
init: function init(input, session) { init: function init(input, session) {
let self = this;
this.context = CompletionContext(input.QueryInterface(Ci.nsIDOMNSEditableElement).editor); this.context = CompletionContext(input.QueryInterface(Ci.nsIDOMNSEditableElement).editor);
this.context.onUpdate = this.closure._reset; this.context.onUpdate = function onUpdate() { self.asyncUpdate(this); };
this.editor = input.editor; this.editor = input.editor;
this.input = input; this.input = input;
this.session = session; this.session = session;
@@ -1222,14 +1225,23 @@ var CommandLine = Module("commandline", {
this.preview(); this.preview();
}, },
_reset: function _reset() { asyncUpdate: function asyncUpdate(context) {
let value = this.editor.selection.focusNode.textContent; let value = this.editor.selection.focusNode.textContent;
this.prefix = value.substring(0, this.start); this.prefix = value.substring(0, this.start);
this.value = value.substring(this.start, this.caret); this.value = value.substring(this.start, this.caret);
this.suffix = value.substring(this.caret); this.suffix = value.substring(this.caret);
this.itemList.update(); this.itemList.updateContext(context);
this.itemList.selectItem(this.selected);
let group = this.itemList.selectedGroup;
if (group && group.context == context && this.completion) {
this.selected = null;
if (group.selectedIdx != null)
this.selected = group.getOffset(group.selectedIdx);
this.completion = this.selected in this.items ? this.items[this.selected].result
: this.value;
}
this.preview(); this.preview();
}, },
@@ -1746,21 +1758,27 @@ var ItemList = Class("ItemList", {
this.visible = true; this.visible = true;
}, },
updateOffsets: function updateOffsets() {
let total = this.itemCount;
let count = 0;
for each (let group in this.activeGroups) {
group.offsets = { start: count, end: total - count - group.itemCount };
count += group.itemCount;
}
},
update: function update() { update: function update() {
DOM(this.nodes.completions).empty(); DOM(this.nodes.completions).empty();
let container = DOM(this.nodes.completions); let container = DOM(this.nodes.completions);
let groups = this.activeGroups; let groups = this.activeGroups;
let total = this.itemCount;
let count = 0;
for each (let group in groups) { for each (let group in groups) {
group.reset(); group.reset();
group.offsets = { start: count, end: total - count - group.itemCount };
count += group.itemCount;
container.append(group.nodes.root); container.append(group.nodes.root);
} }
this.updateOffsets();
DOM(this.nodes.noCompletions).toggle(!groups.length); DOM(this.nodes.noCompletions).toggle(!groups.length);
this.startPos = null; this.startPos = null;
@@ -1769,6 +1787,21 @@ var ItemList = Class("ItemList", {
this._resize.tell(); this._resize.tell();
}, },
updateContext: function updateContext(context) {
let group = this.getGroup(context);
this.updateOffsets();
if (~this.activeGroups.indexOf(group))
group.update();
else {
DOM(group.nodes.root).remove();
if (this.selectedGroup == group)
this.selectedGroup = null;
}
this.select(this.selectedGroup, this.selectedGroup.selectedIdx);
},
draw: function draw() { draw: function draw() {
for each (let group in this.activeGroups) for each (let group in this.activeGroups)
group.draw(); group.draw();
@@ -1786,7 +1819,7 @@ var ItemList = Class("ItemList", {
onResize: function onResize() { onResize: function onResize() {
if (this.selectedGroup) { if (this.selectedGroup) {
this.nodes.root.scrollIntoView(true); this.nodes.root.scrollIntoView(true);
DOM(this.selectedGroup.selectItem).scrollIntoView(); DOM(this.selectedGroup.selectRow).scrollIntoView();
} }
}, },
@@ -1819,8 +1852,9 @@ var ItemList = Class("ItemList", {
} }
}, },
select: function select(context, index, position) { select: function select(group, index, position) {
let group = this.getGroup(context); if (group && !(group instanceof ItemList.Group))
group = this.getGroup(group);
if (this.selectedGroup && (!group || group != this.selectedGroup)) if (this.selectedGroup && (!group || group != this.selectedGroup))
this.selectedGroup.selectedIdx = null; this.selectedGroup.selectedIdx = null;
@@ -1922,8 +1956,8 @@ var ItemList = Class("ItemList", {
get rescrollFunc() { get rescrollFunc() {
let container = this.nodes.itemsContainer; let container = this.nodes.itemsContainer;
let pos = DOM(container).rect.top; let pos = DOM(container).rect.top;
let start = DOM(this.getItem(this.range.start)).rect.top; let start = DOM(this.getRow(this.range.start)).rect.top;
let height = DOM(this.getItem(this.range.end - 1)).rect.bottom - start || 0; let height = DOM(this.getRow(this.range.end - 1)).rect.bottom - start || 0;
let scroll = start + container.scrollTop - pos; let scroll = start + container.scrollTop - pos;
return function () { return function () {
container.style.height = height + "px"; container.style.height = height + "px";
@@ -1931,6 +1965,24 @@ var ItemList = Class("ItemList", {
} }
}, },
reset: function reset() {
this.nodes = {};
this.generatedRange = ItemList.Range(0, 0);
DOM.fromXML(this.rootXML, this.doc, this.nodes);
},
update: function update() {
this.generatedRange = ItemList.Range(0, 0);
DOM(this.nodes.items).empty();
if (this.context.message)
DOM(this.nodes.message).empty().append(<>{this.context.message}</>);
if (!this.selectedRow)
this.selectedIdx = null;
},
draw: function draw() { draw: function draw() {
DOM(this.nodes.contents).toggle(!this.collapsed); DOM(this.nodes.contents).toggle(!this.collapsed);
if (this.collapsed) if (this.collapsed)
@@ -1980,28 +2032,21 @@ var ItemList = Class("ItemList", {
} }
}, },
reset: function reset() { getRow: function getRow(idx) this.context.getRow(idx),
this.nodes = {};
this.generatedRange = ItemList.Range(0, 0);
DOM.fromXML(this.rootXML, this.doc, this.nodes);
},
getItem: function getItem(idx) this.context.getRow(idx),
getOffset: function getOffset(idx) this.offsets.start + (idx || 0), getOffset: function getOffset(idx) this.offsets.start + (idx || 0),
get selectedItem() this.getItem(this._selectedIdx), get selectedRow() this.getRow(this._selectedIdx),
get selectedIdx() this._selectedIdx, get selectedIdx() this._selectedIdx,
set selectedIdx(idx) { set selectedIdx(idx) {
if (this.selectedItem && this._selectedIdx != idx) if (this.selectedRow && this._selectedIdx != idx)
DOM(this.selectedItem).attr("selected", null); DOM(this.selectedRow).attr("selected", null);
this._selectedIdx = idx; this._selectedIdx = idx;
if (this.selectedItem) if (this.selectedRow)
DOM(this.selectedItem).attr("selected", true); DOM(this.selectedRow).attr("selected", true);
} }
}), }),

View File

@@ -304,10 +304,12 @@ var CompletionContext = Class("CompletionContext", {
this._completions = items; this._completions = items;
this.itemCache[this.key] = items; this.itemCache[this.key] = items;
} }
if (this._completions) if (this._completions)
this.hasItems = this._completions.length > 0; this.hasItems = this._completions.length > 0;
if (this.updateAsync && !this.noUpdate) if (this.updateAsync && !this.noUpdate)
this.onUpdate(); util.trapErrors("onUpdate", this);
}, },
get createRow() this._createRow || template.completionRow, // XXX get createRow() this._createRow || template.completionRow, // XXX
@@ -1015,7 +1017,7 @@ var Completion = Module("completion", {
}; };
service.startSearch(context.filter, "", context.result, { service.startSearch(context.filter, "", context.result, {
onSearchResult: function onSearchResult(search, result) { onSearchResult: util.wrapCallback(function onSearchResult(search, result) {
if (result.searchResult <= result.RESULT_SUCCESS) if (result.searchResult <= result.RESULT_SUCCESS)
running[provider] = null; running[provider] = null;
@@ -1024,7 +1026,7 @@ var Completion = Module("completion", {
{ url: result.getValueAt(i), title: result.getCommentAt(i), icon: result.getImageAt(i) } { url: result.getValueAt(i), title: result.getCommentAt(i), icon: result.getImageAt(i) }
for (i in util.range(0, result.matchCount)) for (i in util.range(0, result.matchCount))
]; ];
}, }),
get onUpdateSearchResult() this.onSearchResult get onUpdateSearchResult() this.onSearchResult
}); });
running[provider] = true; running[provider] = true;