1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-23 13:52:26 +01:00

completions are now objects (containing an completions array), in order to allow things like having a common createRow function which is used for displaying :bmarks<cr> as well as for showing :bmarks foo<tab>

We still allow completion functions to return arrays, for now at least.
This commit is contained in:
Martin Stubenschrott
2008-11-10 04:00:13 +00:00
parent b3418425bd
commit 25549a0081
3 changed files with 63 additions and 49 deletions

View File

@@ -65,7 +65,7 @@ function Completion() //{{{
//let foo = ["", "IGNORED", "FAILURE", "NOMATCH", "SUCCESS", "NOMATCH_ONGOING", "SUCCESS_ONGOING"]; //let foo = ["", "IGNORED", "FAILURE", "NOMATCH", "SUCCESS", "NOMATCH_ONGOING", "SUCCESS_ONGOING"];
historyCache = comp; historyCache = comp;
commandline.setCompletions(completionCache.concat(historyCache)); commandline.setCompletions({ start: 0, get completions() { return completionCache.concat(historyCache); } });
}); });
function Javascript() function Javascript()
@@ -782,7 +782,31 @@ function Completion() //{{{
////////////////////// COMPLETION TYPES //////////////////////////////////////// ////////////////////// COMPLETION TYPES ////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{ /////////////////////////////////////////////////////////////////////////////{{{
bookmark: function bookmark(filter) [0, bookmarks.get(filter)], bookmark: function (filter)
{
return {
start: 0,
get completions() { return bookmarks.get(filter) },
createRow: function (item)
<ul class="hl-CompItem">
<li class="hl-CompIcon"><img src={item.icon || ""}/></li>
<li class="hl-CompResult">{util.clip(item.title || "", 50)}</li>
<li style="width: 100%">
<a href="#" class="hl-URL">{item.url}</a>&#160;
{
!(item.extra && item.extra.length) ? "" :
<span class="extra-info">
({
template.map(item.extra, function (e)
<>{e[0]}: <span class={e[2]}>{e[1]}</span></>,
<>&#xa0;</>/* Non-breaking space */)
})
</span>
}
</li>
</ul>
};
},
buffer: function buffer(filter) buffer: function buffer(filter)
{ {
@@ -902,26 +926,27 @@ function Completion() //{{{
} }
cacheFilter["ex"] = str; cacheFilter["ex"] = str;
var [count, cmd, special, args] = commands.parseCommand(str);
var completions = [];
var start = 0;
var exLength = 0;
// if there is no space between the command name and the cursor // if there is no space between the command name and the cursor
// then get completions of the command name // then get completions of the command name
var [count, cmd, special, args] = commands.parseCommand(str);
var matches = str.match(/^(:*\d*)\w*$/); var matches = str.match(/^(:*\d*)\w*$/);
if (matches) if (matches)
return [matches[1].length, this.command(cmd)[1]]; return { start: matches[1].length, completions: this.command(cmd)[1] };
// dynamically get completions as specified with the command's completer function // dynamically get completions as specified with the command's completer function
var compObject = { start: 0, completions: [] };
var exLength = 0;
var command = commands.get(cmd); var command = commands.get(cmd);
if (command && command.completer) if (command && command.completer)
{ {
matches = str.match(/^:*\d*(?:\w+[\s!]|!)\s*/); matches = str.match(/^:*\d*(?:\w+[\s!]|!)\s*/);
exLength = matches ? matches[0].length : 0; exLength = matches ? matches[0].length : 0;
[start, completions] = command.completer.call(this, args, special); compObject = command.completer.call(this, args, special);
if (compObject instanceof Array) // for now at least, let completion functions return arrays instead of objects
compObject = { start: compObject[0], completions: compObject[1] };
} }
return [exLength + start, completions]; compObject.start += exLength;
return compObject;
}, },
// TODO: support file:// and \ or / path separators on both platforms // TODO: support file:// and \ or / path separators on both platforms

View File

@@ -140,30 +140,14 @@ const template = {
bookmarks: function (header, items) bookmarks: function (header, items)
{ {
let createRow = completion.bookmark("").createRow;
return this.generic( return this.generic(
<table> <table>
<tr align="left" class="hl-Title"> <tr align="left" class="hl-Title">
<th/><th>{header}</th><th>URL</th> <th/><th>{header}</th><th>URL</th>
</tr> </tr>
{ {
this.map(items, function (item) this.map(items, function (item) createRow(item))
<ul class="hl-CompItem">
<li class="hl-CompIcon"><img src={item.icon || ""}/></li>
<li class="hl-CompResult">{util.clip(item.title || "", 50)}</li>
<li style="width: 100%">
<a href="#" class="hl-URL">{item.url}</a>&#160;
{
!(item.extra && item.extra.length) ? "" :
<span class="extra-info">
({
template.map(item.extra, function (e)
<>{e[0]}: <span class={e[2]}>{e[1]}</span></>,
<>&#xa0;</>/* Non-breaking space */)
})
</span>
}
</li>
</ul>)
} }
</table>); </table>);
}, },

View File

@@ -116,8 +116,8 @@ function CommandLine() //{{{
var autocompleteTimer = new util.Timer(201, 300, function (command) { var autocompleteTimer = new util.Timer(201, 300, function (command) {
if (events.feedingKeys) if (events.feedingKeys)
return; return;
let [start, compl] = completion.ex(command);
commandline.setCompletions(compl, start); commandline.setCompletions(completion.ex(command));
}); });
// the containing box for the promptWidget and commandWidget // the containing box for the promptWidget and commandWidget
@@ -832,15 +832,18 @@ function CommandLine() //{{{
completionIndex = -1; completionIndex = -1;
completionPrefix = command.substring(0, commandWidget.selectionStart); completionPrefix = command.substring(0, commandWidget.selectionStart);
completionPostfix = command.substring(commandWidget.selectionStart); completionPostfix = command.substring(commandWidget.selectionStart);
var res = liberator.triggerCallback("complete", currentExtendedMode, completionPrefix); var compObject = liberator.triggerCallback("complete", currentExtendedMode, completionPrefix);
if (res) if (compObject)
[completionStartIndex, completions] = res; {
completionStartIndex = compObject.start;
completions = compObject.completions;
}
// sort the completion list // sort the completion list
if (options.get("wildoptions").has("sort")) if (options.get("wildoptions").has("sort"))
completions.sort(function (a, b) String.localeCompare(a[0], b[0])); compObject.completions.sort(function (a, b) String.localeCompare(a[0], b[0]));
completionList.setItems(completions); completionList.setItems(compObject);
} }
if (completions.length == 0) if (completions.length == 0)
@@ -1217,7 +1220,7 @@ function CommandLine() //{{{
}, },
// to allow asynchronous adding of completions // to allow asynchronous adding of completions
setCompletions: function (compl, start) setCompletions: function (completionObject)
{ {
if (liberator.mode != modes.COMMAND_LINE) if (liberator.mode != modes.COMMAND_LINE)
return; return;
@@ -1227,17 +1230,18 @@ function CommandLine() //{{{
return completionList.hide(); return completionList.hide();
*/ */
completionList.setItems(compl); let newCompletions = completionObject.completions;
completionList.setItems(completionObject);
if (completionIndex >= 0 && completionIndex < compl.length && completionIndex < completions.length) if (completionIndex >= 0 && completionIndex < newCompletions.length && completionIndex < completions.length)
{ {
if (compl[completionIndex][0] != completions[completionIndex][0]) if (newCompletions[completionIndex][0] != completions[completionIndex][0])
completionIndex = -1; completionIndex = -1;
} }
else else
completionIndex = -1; completionIndex = -1;
completions = compl; completions = newCompletions;
completionList.selectItem(completionIndex); completionList.selectItem(completionIndex);
if (options.get("wildoptions").has("auto")) if (options.get("wildoptions").has("auto"))
completionList.show(); completionList.show();
@@ -1246,8 +1250,8 @@ function CommandLine() //{{{
completionPrefix = command.substring(0, commandWidget.selectionStart); completionPrefix = command.substring(0, commandWidget.selectionStart);
completionPostfix = command.substring(commandWidget.selectionStart); completionPostfix = command.substring(commandWidget.selectionStart);
if (typeof start == "number") if (typeof completionObject.start == "number")
completionStartIndex = start; completionStartIndex = completionObject.start;
}, },
resetCompletions: function () resetCompletions: function ()
@@ -1276,8 +1280,6 @@ function ItemList(id) //{{{
const CONTEXT_LINES = 3; const CONTEXT_LINES = 3;
var maxItems = 20; var maxItems = 20;
var minItems = 2;
var incrementalFill = true; // make display faster, but does not show scrollbar
var completionElements = []; var completionElements = [];
var iframe = document.getElementById(id); var iframe = document.getElementById(id);
@@ -1294,6 +1296,7 @@ function ItemList(id) //{{{
doc.body.appendChild(doc.createTextNode("")); doc.body.appendChild(doc.createTextNode(""));
var completions = []; // a reference to the Array of completions var completions = []; // a reference to the Array of completions
var completionObject = {};
var startIndex = -1; // The index of the first displayed item var startIndex = -1; // The index of the first displayed item
var endIndex = -1; // The index one *after* the last displayed item var endIndex = -1; // The index one *after* the last displayed item
var selIndex = -1; // The index of the currently selected element var selIndex = -1; // The index of the currently selected element
@@ -1312,8 +1315,8 @@ function ItemList(id) //{{{
commandline.updateOutputHeight(false); commandline.updateOutputHeight(false);
} }
// TODO: temporary, to be changed/removed // TODO: move to completions?
function createRow([b, c, a], dom) function createDefaultRow([b, c, a], dom)
{ {
/* Kludge until we have completion contexts. */ /* Kludge until we have completion contexts. */
let map = completion.filterMap; let map = completion.filterMap;
@@ -1359,6 +1362,7 @@ function ItemList(id) //{{{
if (offset == null || offset - startIndex == 0 || offset < 0 || completions.length && offset >= completions.length) if (offset == null || offset - startIndex == 0 || offset < 0 || completions.length && offset >= completions.length)
return; return;
let createRow = ("createRow" in completionObject) ? completionObject.createRow : createDefaultRow;
startIndex = offset; startIndex = offset;
endIndex = Math.min(startIndex + maxItems, completions.length); endIndex = Math.min(startIndex + maxItems, completions.length);
@@ -1405,7 +1409,7 @@ function ItemList(id) //{{{
div = util.xmlToDom(xml, doc); div = util.xmlToDom(xml, doc);
completionBody = div.getElementsByClassName("hl-Completions")[0]; completionBody = div.getElementsByClassName("hl-Completions")[0];
//completionElements = completionBody.childNodes; //completionElements = completionBody.childNodes; // Kris: This is broken for things like :dia pr<tab>
completionElements = div.getElementsByClassName("hl-CompItem"); completionElements = div.getElementsByClassName("hl-CompItem");
doc.body.replaceChild(div, doc.body.firstChild); doc.body.replaceChild(div, doc.body.firstChild);
autoSize(); autoSize();
@@ -1418,7 +1422,7 @@ function ItemList(id) //{{{
return { return {
clear: function () { this.setItems([]); doc.body.innerHTML = ""; }, clear: function () { this.setItems(); doc.body.innerHTML = ""; },
hide: function () { container.collapsed = true; }, hide: function () { container.collapsed = true; },
show: function show() show: function show()
{ {
@@ -1438,7 +1442,8 @@ function ItemList(id) //{{{
setItems: function setItems(items, selectedItem) setItems: function setItems(items, selectedItem)
{ {
startIndex = endIndex = selIndex = -1; startIndex = endIndex = selIndex = -1;
completions = items || []; completionObject = items || { start: 0, completions: [] };
completions = completionObject.completions || []; // TODO: remove?
if (typeof selectedItem == "number") if (typeof selectedItem == "number")
{ {
this.selectItem(selectedItem); this.selectItem(selectedItem);