From 9398ffed1c0e408b3c3badd7581b0da1ae65cca4 Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Mon, 24 Nov 2008 21:40:02 +0000 Subject: [PATCH] More completion stuff --- content/bookmarks.js | 4 +-- content/completion.js | 71 +++++++++++++++++++++---------------------- content/io.js | 6 ++++ content/template.js | 10 ++++-- content/util.js | 10 +++++- 5 files changed, 59 insertions(+), 42 deletions(-) diff --git a/content/bookmarks.js b/content/bookmarks.js index d8906c0e..3a4ea6e2 100644 --- a/content/bookmarks.js +++ b/content/bookmarks.js @@ -350,7 +350,7 @@ function Bookmarks() //{{{ return { get format() ({ - keys: ["url", "title"], + keys: { text: "url", description: "title", icon: "icon" }, process: [template.icon, template.bookmarkDescription] }), @@ -495,7 +495,7 @@ function Bookmarks() //{{{ if (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; diff --git a/content/completion.js b/content/completion.js index 0372b4d2..0b16d72a 100644 --- a/content/completion.js +++ b/content/completion.js @@ -37,9 +37,9 @@ function CompletionContext(editor, name, offset) if (!name) name = ""; + let self = this; if (editor instanceof arguments.callee) { - let self = this; let parent = editor; name = parent.name + "/" + name; this.contexts = parent.contexts; @@ -53,6 +53,7 @@ function CompletionContext(editor, name, offset) this.anchored = parent.anchored; this.parent = parent; this.offset = parent.offset + (offset || 0); + this.keys = util.cloneObject(this.parent.keys); ["editor", "filterFunc", "keys", "title", "top"].forEach(function (key) self[key] = parent[key]); ["contextList", "onUpdate", "selectionTypes", "tabPressed", "updateAsync", "value"].forEach(function (key) { @@ -69,7 +70,7 @@ function CompletionContext(editor, name, offset) this.editor = editor; this.filterFunc = completion.filter; this.title = ["Completions"]; - this.keys = [0, 1]; + this.keys = { text: 0, description: 1, icon: "icon" }; this.top = this; this.offset = offset || 0; this.tabPressed = false; @@ -83,6 +84,7 @@ function CompletionContext(editor, name, offset) this.cache = {}; this.process = []; this._completions = []; // FIXME + this.getKey = function (item, key) item.item[self.keys[key]]; } CompletionContext.prototype = { // Temporary @@ -173,22 +175,27 @@ CompletionContext.prototype = { return items; let self = this; - let text = function (item) item[self.keys[0]]; + let text = function (item) item[self.keys["text"]]; 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.filter, this.anchored); + completion.getKey = null; + return this.cache.filtered; }, get process() // FIXME { + let self = this; let process = this._process; process = [process[0] || template.icon, process[1] || function (item, k) k]; let first = process[0]; let filter = this.filter; 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; }, set process(process) @@ -809,8 +816,7 @@ function Completion() //{{{ for (let [,item] in Iterator(list)) { - // FIXME: Temporary kludge - let text = item.item ? item.item[0] || item.text : item[0]; + let text = completion.getKey(item, "text"); var complist = text instanceof Array ? text : [text]; for (let [,compitem] in Iterator(complist)) { @@ -841,7 +847,7 @@ function Completion() //{{{ 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]; for (let [,compitem] in Iterator(complist)) { @@ -876,6 +882,10 @@ function Completion() //{{{ 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) { if (!(funcs instanceof Array)) @@ -908,14 +918,11 @@ function Completion() //{{{ // generic filter function, also builds substrings needed // for :set wildmode=list:longest, if necessary - filter: function filter(array, filter, matchFromBeginning, favicon) + filter: function filter(array, filter, matchFromBeginning) { - let result; if (matchFromBeginning) - result = buildLongestStartingSubstring(array, filter, favicon); - else - result = buildLongestCommonSubstring(array, filter, favicon); - return result; + return buildLongestStartingSubstring(array, filter); + return buildLongestCommonSubstring(array, filter); }, // cancel any ongoing search @@ -1018,19 +1025,11 @@ function Completion() //{{{ autocmdEvent: function autocmdEvent(filter) [0, this.filter(config.autocommands, filter)], - bookmark: function bookmark(filter) + bookmark: function bookmark(context) { - return { - start: 0, - items: bookmarks.get(filter).map(function (bmark) { - // temporary, until we have moved all completions to objects - bmark[0] = bmark.url; - bmark[1] = bmark.title; - - bmark.text = bmark.url; - return bmark; - }) - }; + context.title = ["Bookmark", "Title"]; + context.format = bookmarks.format; + context.completions = bookmarks.get(context.filter) }, buffer: function buffer(filter) @@ -1102,7 +1101,8 @@ function Completion() //{{{ { context.title = ["Command"]; 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)], @@ -1143,7 +1143,7 @@ function Completion() //{{{ let [, prefix, junk] = context.filter.match(/^(:*\d*)\w*(.?)/) || []; context.advance(prefix.length) if (!junk) - return this.command(context); + return context.fork("", 0, this.command); // dynamically get completions as specified with the command's completer function let command = commands.get(cmd); @@ -1184,6 +1184,7 @@ function Completion() //{{{ context.title = ["Path", "Type"]; if (tail) context.advance(dir.length); + context.keys = { text: 0, description: 1, icon: 2 }; context.anchored = true; context.key = dir; context.generate = function generate() @@ -1202,7 +1203,8 @@ function Completion() //{{{ return files.map( 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) {} @@ -1255,6 +1257,7 @@ function Completion() //{{{ let engines = bookmarks.getSearchEngines(); context.title = ["Search Keywords"]; + context.keys = { text: 0, description: 1, icon: 2 }; context.completions = keywords.concat(engines); context.anchored = true; @@ -1396,23 +1399,19 @@ function Completion() //{{{ s: this.search, f: this.file, S: this.searchEngineSuggest, - b: function b(context) - { - context.title = ["Bookmark", "Title"]; - context.format = bookmarks.format; - context.completions = bookmarks.get(context.filter) - }, + b: this.bookmark, l: function l(context) { if (!completionService) return context.title = ["Smart Completions"]; + context.keys.icon = 2; context.incomplete = true; context.hasItems = context.completions.length > 0; // XXX context.filterFunc = function (items) items; let timer = new util.Timer(50, 100, function (result) { 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)) ]; context.incomplete = result.searchResult >= result.RESULT_NOMATCH_ONGOING; diff --git a/content/io.js b/content/io.js index 0f301d94..5197a7c3 100644 --- a/content/io.js +++ b/content/io.js @@ -347,6 +347,12 @@ function IO() //{{{ ////////////////////// PUBLIC SECTION ////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////{{{ + liberator.registerObserver("load_completion", function () + { + completion.setFunctionCompleter([ioManager.getFile, ioManager.expandPath], + [function (obj, arg) completion.runCompleter("file", arg)]); + }); + var ioManager = { MODE_RDONLY: 0x01, diff --git a/content/template.js b/content/template.js index 328eb763..cbd9b764 100644 --- a/content/template.js +++ b/content/template.js @@ -43,8 +43,8 @@ const template = { } else { - var text = context.process[0](item, item.text || item.item[context.keys[0]]); - var desc = context.process[1](item, item.item[context.keys[1]]); + var text = context.process[0].call(context, item, item.text || context.getKey(item, "text")); + var desc = context.process[1].call(context, item, context.getKey(item, "description")); } return