1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2026-04-04 08:13:33 +02:00

More completion stuff

This commit is contained in:
Kris Maglione
2008-11-24 21:40:02 +00:00
parent ccdf6f1011
commit 9398ffed1c
5 changed files with 59 additions and 42 deletions

View File

@@ -350,7 +350,7 @@ function Bookmarks() //{{{
return { return {
get format() ({ get format() ({
keys: ["url", "title"], keys: { text: "url", description: "title", icon: "icon" },
process: [template.icon, template.bookmarkDescription] process: [template.icon, template.bookmarkDescription]
}), }),
@@ -495,7 +495,7 @@ function Bookmarks() //{{{
if (engine.alias != newAlias) if (engine.alias != newAlias)
engine.alias = newAlias; engine.alias = newAlias;
searchEngines.push({ 0: engine.alias, 1: engine.description, icon: engine.iconURI.spec }); searchEngines.push([engine.alias, engine.description, engine.iconURI.spec]);
} }
return searchEngines; return searchEngines;

View File

@@ -37,9 +37,9 @@ function CompletionContext(editor, name, offset)
if (!name) if (!name)
name = ""; name = "";
let self = this;
if (editor instanceof arguments.callee) if (editor instanceof arguments.callee)
{ {
let self = this;
let parent = editor; let parent = editor;
name = parent.name + "/" + name; name = parent.name + "/" + name;
this.contexts = parent.contexts; this.contexts = parent.contexts;
@@ -53,6 +53,7 @@ function CompletionContext(editor, name, offset)
this.anchored = parent.anchored; this.anchored = parent.anchored;
this.parent = parent; this.parent = parent;
this.offset = parent.offset + (offset || 0); this.offset = parent.offset + (offset || 0);
this.keys = util.cloneObject(this.parent.keys);
["editor", "filterFunc", "keys", "title", "top"].forEach(function (key) ["editor", "filterFunc", "keys", "title", "top"].forEach(function (key)
self[key] = parent[key]); self[key] = parent[key]);
["contextList", "onUpdate", "selectionTypes", "tabPressed", "updateAsync", "value"].forEach(function (key) { ["contextList", "onUpdate", "selectionTypes", "tabPressed", "updateAsync", "value"].forEach(function (key) {
@@ -69,7 +70,7 @@ function CompletionContext(editor, name, offset)
this.editor = editor; this.editor = editor;
this.filterFunc = completion.filter; this.filterFunc = completion.filter;
this.title = ["Completions"]; this.title = ["Completions"];
this.keys = [0, 1]; this.keys = { text: 0, description: 1, icon: "icon" };
this.top = this; this.top = this;
this.offset = offset || 0; this.offset = offset || 0;
this.tabPressed = false; this.tabPressed = false;
@@ -83,6 +84,7 @@ function CompletionContext(editor, name, offset)
this.cache = {}; this.cache = {};
this.process = []; this.process = [];
this._completions = []; // FIXME this._completions = []; // FIXME
this.getKey = function (item, key) item.item[self.keys[key]];
} }
CompletionContext.prototype = { CompletionContext.prototype = {
// Temporary // Temporary
@@ -173,22 +175,27 @@ CompletionContext.prototype = {
return items; return items;
let self = this; let self = this;
let text = function (item) item[self.keys[0]]; let text = function (item) item[self.keys["text"]];
if (self.quote) if (self.quote)
text = function (item) self.quote(item[self.keys[0]]); text = function (item) self.quote(item[self.keys["text"]]);
completion.getKey = this.getKey; // XXX
this.cache.filtered = this.filterFunc(items.map(function (item) ({ text: text(item), item: item })), this.cache.filtered = this.filterFunc(items.map(function (item) ({ text: text(item), item: item })),
this.filter, this.anchored); this.filter, this.anchored);
completion.getKey = null;
return this.cache.filtered; return this.cache.filtered;
}, },
get process() // FIXME get process() // FIXME
{ {
let self = this;
let process = this._process; let process = this._process;
process = [process[0] || template.icon, process[1] || function (item, k) k]; process = [process[0] || template.icon, process[1] || function (item, k) k];
let first = process[0]; let first = process[0];
let filter = this.filter; let filter = this.filter;
if (!this.anchored) if (!this.anchored)
process[0] = function (item, text) first(item, template.highlightFilter(item.text, filter)); process[0] = function (item, text) first.call(self, item, template.highlightFilter(item.text, filter));
return process; return process;
}, },
set process(process) set process(process)
@@ -809,8 +816,7 @@ function Completion() //{{{
for (let [,item] in Iterator(list)) for (let [,item] in Iterator(list))
{ {
// FIXME: Temporary kludge let text = completion.getKey(item, "text");
let text = item.item ? item.item[0] || item.text : item[0];
var complist = text instanceof Array ? text : [text]; var complist = text instanceof Array ? text : [text];
for (let [,compitem] in Iterator(complist)) for (let [,compitem] in Iterator(complist))
{ {
@@ -841,7 +847,7 @@ function Completion() //{{{
for (let [,item] in Iterator(list)) for (let [,item] in Iterator(list))
{ {
let text = item.item ? item.item[0] || item.text : item[0]; let text = completion.getKey(item, "text");
var complist = text instanceof Array ? text : [text]; var complist = text instanceof Array ? text : [text];
for (let [,compitem] in Iterator(complist)) for (let [,compitem] in Iterator(complist))
{ {
@@ -876,6 +882,10 @@ function Completion() //{{{
return { return {
// FIXME
get getKey() this._getKey || function (item, key) item[{ text: 0, description: 1, icon: 2}[key]],
set getKey(getKey) this._getKey = getKey,
setFunctionCompleter: function setFunctionCompleter(funcs, completers) setFunctionCompleter: function setFunctionCompleter(funcs, completers)
{ {
if (!(funcs instanceof Array)) if (!(funcs instanceof Array))
@@ -908,14 +918,11 @@ function Completion() //{{{
// generic filter function, also builds substrings needed // generic filter function, also builds substrings needed
// for :set wildmode=list:longest, if necessary // for :set wildmode=list:longest, if necessary
filter: function filter(array, filter, matchFromBeginning, favicon) filter: function filter(array, filter, matchFromBeginning)
{ {
let result;
if (matchFromBeginning) if (matchFromBeginning)
result = buildLongestStartingSubstring(array, filter, favicon); return buildLongestStartingSubstring(array, filter);
else return buildLongestCommonSubstring(array, filter);
result = buildLongestCommonSubstring(array, filter, favicon);
return result;
}, },
// cancel any ongoing search // cancel any ongoing search
@@ -1018,19 +1025,11 @@ function Completion() //{{{
autocmdEvent: function autocmdEvent(filter) [0, this.filter(config.autocommands, filter)], autocmdEvent: function autocmdEvent(filter) [0, this.filter(config.autocommands, filter)],
bookmark: function bookmark(filter) bookmark: function bookmark(context)
{ {
return { context.title = ["Bookmark", "Title"];
start: 0, context.format = bookmarks.format;
items: bookmarks.get(filter).map(function (bmark) { context.completions = bookmarks.get(context.filter)
// temporary, until we have moved all completions to objects
bmark[0] = bmark.url;
bmark[1] = bmark.title;
bmark.text = bmark.url;
return bmark;
})
};
}, },
buffer: function buffer(filter) buffer: function buffer(filter)
@@ -1102,7 +1101,8 @@ function Completion() //{{{
{ {
context.title = ["Command"]; context.title = ["Command"];
context.anchored = true; context.anchored = true;
context.completions = [[c.longNames, c.description] for (c in commands)]; context.keys = { text: "longNames", description: "description" };
context.completions = [k for (k in commands)];
}, },
dialog: function dialog(filter) [0, this.filter(config.dialogs, filter)], dialog: function dialog(filter) [0, this.filter(config.dialogs, filter)],
@@ -1143,7 +1143,7 @@ function Completion() //{{{
let [, prefix, junk] = context.filter.match(/^(:*\d*)\w*(.?)/) || []; let [, prefix, junk] = context.filter.match(/^(:*\d*)\w*(.?)/) || [];
context.advance(prefix.length) context.advance(prefix.length)
if (!junk) if (!junk)
return this.command(context); return context.fork("", 0, this.command);
// dynamically get completions as specified with the command's completer function // dynamically get completions as specified with the command's completer function
let command = commands.get(cmd); let command = commands.get(cmd);
@@ -1184,6 +1184,7 @@ function Completion() //{{{
context.title = ["Path", "Type"]; context.title = ["Path", "Type"];
if (tail) if (tail)
context.advance(dir.length); context.advance(dir.length);
context.keys = { text: 0, description: 1, icon: 2 };
context.anchored = true; context.anchored = true;
context.key = dir; context.key = dir;
context.generate = function generate() context.generate = function generate()
@@ -1202,7 +1203,8 @@ function Completion() //{{{
return files.map( return files.map(
function (file) [(tail ? file.leafName : dir + file.leafName).replace(" ", "\\ ", "g"), function (file) [(tail ? file.leafName : dir + file.leafName).replace(" ", "\\ ", "g"),
file.isDirectory() ? "Directory" : "File"] file.isDirectory() ? "Directory" : "File",
"moz-icon:" + (file.leafName.match(/\.\w+$/) || "")]
); );
} }
catch (e) {} catch (e) {}
@@ -1255,6 +1257,7 @@ function Completion() //{{{
let engines = bookmarks.getSearchEngines(); let engines = bookmarks.getSearchEngines();
context.title = ["Search Keywords"]; context.title = ["Search Keywords"];
context.keys = { text: 0, description: 1, icon: 2 };
context.completions = keywords.concat(engines); context.completions = keywords.concat(engines);
context.anchored = true; context.anchored = true;
@@ -1396,23 +1399,19 @@ function Completion() //{{{
s: this.search, s: this.search,
f: this.file, f: this.file,
S: this.searchEngineSuggest, S: this.searchEngineSuggest,
b: function b(context) b: this.bookmark,
{
context.title = ["Bookmark", "Title"];
context.format = bookmarks.format;
context.completions = bookmarks.get(context.filter)
},
l: function l(context) l: function l(context)
{ {
if (!completionService) if (!completionService)
return return
context.title = ["Smart Completions"]; context.title = ["Smart Completions"];
context.keys.icon = 2;
context.incomplete = true; context.incomplete = true;
context.hasItems = context.completions.length > 0; // XXX context.hasItems = context.completions.length > 0; // XXX
context.filterFunc = function (items) items; context.filterFunc = function (items) items;
let timer = new util.Timer(50, 100, function (result) { let timer = new util.Timer(50, 100, function (result) {
context.completions = [ context.completions = [
{ 0: result.getValueAt(i), 1: result.getCommentAt(i), icon: result.getImageAt(i) } [result.getValueAt(i), result.getCommentAt(i), result.getImageAt(i)]
for (i in util.range(0, result.matchCount)) for (i in util.range(0, result.matchCount))
]; ];
context.incomplete = result.searchResult >= result.RESULT_NOMATCH_ONGOING; context.incomplete = result.searchResult >= result.RESULT_NOMATCH_ONGOING;

View File

@@ -347,6 +347,12 @@ function IO() //{{{
////////////////////// PUBLIC SECTION ////////////////////////////////////////// ////////////////////// PUBLIC SECTION //////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{ /////////////////////////////////////////////////////////////////////////////{{{
liberator.registerObserver("load_completion", function ()
{
completion.setFunctionCompleter([ioManager.getFile, ioManager.expandPath],
[function (obj, arg) completion.runCompleter("file", arg)]);
});
var ioManager = { var ioManager = {
MODE_RDONLY: 0x01, MODE_RDONLY: 0x01,

View File

@@ -43,8 +43,8 @@ const template = {
} }
else else
{ {
var text = context.process[0](item, item.text || item.item[context.keys[0]]); var text = context.process[0].call(context, item, item.text || context.getKey(item, "text"));
var desc = context.process[1](item, item.item[context.keys[1]]); var desc = context.process[1].call(context, item, context.getKey(item, "description"));
} }
return <ul class={class || "hl-CompItem"}> return <ul class={class || "hl-CompItem"}>
@@ -68,7 +68,11 @@ const template = {
} }
</>, </>,
icon: function (item, text) <><span class="hl-CompIcon">{item.item.icon ? <img src={item.item.icon}/> : <></>}</span>{text}</>, icon: function (item, text)
{
let icon = this.getKey(item, "icon");
return <><span class="hl-CompIcon">{icon ? <img src={icon}/> : <></>}</span>{text}</>
},
filter: function (str) <span class="hl-Filter">{str}</span>, filter: function (str) <span class="hl-Filter">{str}</span>,

View File

@@ -130,13 +130,21 @@ const util = { //{{{
}; };
}, },
compareIgnoreCase: function (a, b) String.localeCompare(a.toLowerCase(), b.toLowerCase()), cloneObject: function (obj)
{
let newObj = {};
for (let [k, v] in Iterator(obj))
newObj[k] = v;
return newObj;
},
clip: function (str, length) clip: function (str, length)
{ {
return str.length <= length ? str : str.substr(0, length - 3) + "..."; return str.length <= length ? str : str.substr(0, length - 3) + "...";
}, },
compareIgnoreCase: function (a, b) String.localeCompare(a.toLowerCase(), b.toLowerCase()),
computedStyle: function (node) computedStyle: function (node)
{ {
while (node instanceof Text && node.parentNode) while (node instanceof Text && node.parentNode)