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

Precompute completion string matching function.

This commit is contained in:
Kris Maglione
2010-09-17 06:15:13 -04:00
parent 8b0d9586b2
commit a5213c3760
14 changed files with 68 additions and 63 deletions

0
common/content/autocommands.js Executable file → Normal file
View File

View File

@@ -366,6 +366,7 @@ const Bookmarks = Module("bookmarks", {
type: CommandOption.INT type: CommandOption.INT
} }
] ]
// Not privateData, since we don't treat bookmarks as private
}); });
commands.add(["delbm[arks]"], commands.add(["delbm[arks]"],
@@ -457,7 +458,7 @@ const Bookmarks = Module("bookmarks", {
for (let val in Iterator(extra || [])) { for (let val in Iterator(extra || [])) {
let [k, v] = val; // Need block scope here for the closure let [k, v] = val; // Need block scope here for the closure
if (v) if (v)
context.filters.push(function (item) this._match(v, item[k])); context.filters.push(function (item) this.matchString(v, item[k]));
} }
context.completions = bookmarkcache.bookmarks; context.completions = bookmarkcache.bookmarks;
completion.urls(context, tags); completion.urls(context, tags);

View File

@@ -1292,16 +1292,13 @@ const Buffer = Module("buffer", {
context.title = ["Stylesheet", "Location"]; context.title = ["Stylesheet", "Location"];
// unify split style sheets // unify split style sheets
let styles = {}; let styles = array.toObject([s.title, []] for (s in values(buffer.alternateStyleSheets)));
buffer.alternateStyleSheets.forEach(function (style) { buffer.alternateStyleSheets.forEach(function (style) {
if (!(style.title in styles))
styles[style.title] = [];
styles[style.title].push(style.href || "inline"); styles[style.title].push(style.href || "inline");
}); });
context.completions = [[s, styles[s].join(", ")] for (s in styles)]; context.completions = [[title, href.join(", ")] for ([title, href] in Iterator(styles))];
}; };
completion.buffer = function buffer(context) { completion.buffer = function buffer(context) {
@@ -1327,7 +1324,7 @@ const Buffer = Module("buffer", {
let process = context.process[0]; let process = context.process[0];
context.process = [function (item, text) context.process = [function (item, text)
<> <>
<span highlight="Indicator" style="display: inline-block; width: 1.5em; text-align: center">{item.item.indicator}</span> <span highlight="Indicator" style="display: inline-block;">{item.item.indicator}</span>
{ process.call(this, item, text) } { process.call(this, item, text) }
</>]; </>];

View File

@@ -105,7 +105,8 @@ const CommandLine = Module("commandline", {
this._autocompleteTimer = Timer(200, 500, function autocompleteTell(tabPressed) { this._autocompleteTimer = Timer(200, 500, function autocompleteTell(tabPressed) {
if (!events.feedingKeys && self._completions && options.get("autocomplete").values.length) { if (!events.feedingKeys && self._completions && options.get("autocomplete").values.length) {
self._completions.complete(true, false); self._completions.complete(true, false);
self._completions.itemList.show(); if (self._completions)
self._completions.itemList.show();
} }
}); });

32
common/content/completion.js Executable file → Normal file
View File

@@ -295,14 +295,13 @@ const CompletionContext = Class("CompletionContext", {
let res = {}; let res = {};
for (let i in Iterator(this.keys)) { for (let i in Iterator(this.keys)) {
let [k, v] = i; let [k, v] = i;
let _k = "_" + k;
if (typeof v == "string" && /^[.[]/.test(v)) if (typeof v == "string" && /^[.[]/.test(v))
v = Function("i", "return i" + v); v = Function("i", "return i" + v);
if (typeof v == "function") if (typeof v == "function")
res.__defineGetter__(k, function () _k in this ? this[_k] : (this[_k] = v(this.item))); res.__defineGetter__(k, function () Class.replaceProperty(this, k, v(this.item)));
else else
res.__defineGetter__(k, function () _k in this ? this[_k] : (this[_k] = this.item[v])); res.__defineGetter__(k, function () Class.replaceProperty(this, k, this.item[v]));
res.__defineSetter__(k, function (val) this[_k] = val); res.__defineSetter__(k, function (val) Class.replaceProperty(this, k, val));
} }
return res; return res;
}, },
@@ -370,6 +369,15 @@ const CompletionContext = Class("CompletionContext", {
let self = this; let self = this;
delete this._substrings; delete this._substrings;
if (this.ignoreCase)
this.matchString = this.anchored ?
function (filter, str) String.toLowerCase(str).indexOf(filter.toLowerCase()) == 0 :
function (filter, str) String.toLowerCase(str).indexOf(filter.toLowerCase()) >= 0;
else
this.matchString = this.anchored ?
function (filter, str) String.indexOf(str, filter) == 0 :
function (filter, str) String.indexOf(str, filter) >= 0;
let proto = this.proto; let proto = this.proto;
let filtered = this.filterFunc(items.map(function (item) ({ __proto__: proto, item: item }))); let filtered = this.filterFunc(items.map(function (item) ({ __proto__: proto, item: item })));
if (this.maxItems) if (this.maxItems)
@@ -543,19 +551,8 @@ const CompletionContext = Class("CompletionContext", {
catch (e) {} catch (e) {}
}, },
// FIXME
_match: function _match(filter, str) {
if (this.ignoreCase) {
filter = filter.toLowerCase();
str = str.toLowerCase();
}
if (this.anchored)
return str.substr(0, filter.length) == filter;
return str.indexOf(filter) > -1;
},
match: function match(str) { match: function match(str) {
return this._match(this.filter, str); return this.matchString(this.filter, str);
}, },
reset: function reset() { reset: function reset() {
@@ -614,8 +611,7 @@ const CompletionContext = Class("CompletionContext", {
} }
}, { }, {
Sort: { Sort: {
number: function (a, b) parseInt(b) - parseInt(a) || String.localeCompare(a, b), number: function (a, b) parseInt(a.text) - parseInt(b.text) || String.localeCompare(a.text, b.text),
unsorted: null unsorted: null
}, },

View File

@@ -166,7 +166,7 @@ const ConfigBase = Class(ModuleBase, {
GradientLeft background-color: magenta; GradientLeft background-color: magenta;
GradientRight background-color: white; GradientRight background-color: white;
Indicator color: blue; Indicator color: blue; width: 1.5em; text-align: center;
Filter font-weight: bold; Filter font-weight: bold;
Keyword color: red; Keyword color: red;

View File

@@ -842,7 +842,7 @@ const Dactyl = Module("dactyl", {
urls = [str]; urls = [str];
return urls.map(function (url) { return urls.map(function (url) {
if (/^[.~]?\//.test(url)) { if (/^(\.{0,2}|~)\//.test(url)) {
try { try {
// Try to find a matching file. // Try to find a matching file.
let file = io.File(url); let file = io.File(url);
@@ -906,14 +906,7 @@ const Dactyl = Module("dactyl", {
return func.apply(self || this, Array.slice(arguments, 2)); return func.apply(self || this, Array.slice(arguments, 2));
} }
catch (e) { catch (e) {
if (e instanceof FailedAssertion) { dactyl.reportError(e);
if (e.message)
dactyl.echoerr(e.message);
else
dactyl.beep();
}
else
dactyl.reportError(e);
return undefined; return undefined;
} }
}, },
@@ -925,6 +918,14 @@ const Dactyl = Module("dactyl", {
* @param {Object} error The error object. * @param {Object} error The error object.
*/ */
reportError: function (error) { reportError: function (error) {
if (error instanceof FailedAssertion) {
if (error.message)
dactyl.echoerr(error.message);
else
dactyl.beep();
return;
}
if (Cu.reportError) if (Cu.reportError)
Cu.reportError(error); Cu.reportError(error);

View File

@@ -167,9 +167,9 @@ const Events = Module("events", {
* @param {string} macro The name for the macro. * @param {string} macro The name for the macro.
*/ */
startRecording: function (macro) { startRecording: function (macro) {
// TODO: ignore this like Vim? // TODO: ignore this like Vim?
dactyl.assert(/[a-zA-Z0-9]/.test(macro), dactyl.assert(/[a-zA-Z0-9]/.test(macro),
"E354: Invalid register name: '" + macro + "'"); "E354: Invalid register name: '" + macro + "'");
modes.isRecording = true; modes.isRecording = true;

4
common/content/io.js Executable file → Normal file
View File

@@ -314,7 +314,7 @@ lookup:
*/ */
source: function (filename, silent) { source: function (filename, silent) {
let wasSourcing = this.sourcing; let wasSourcing = this.sourcing;
dactyl.dump("sourcing " + filename); defmodule.loadLog.push("sourcing " + filename);
let time = Date.now(); let time = Date.now();
try { try {
var file = io.File(filename); var file = io.File(filename);
@@ -432,7 +432,7 @@ lookup:
dactyl.echoerr(message); dactyl.echoerr(message);
} }
finally { finally {
dactyl.dump("done sourcing " + filename + ": " + (Date.now() - time) + "ms"); defmodule.loadLog.push("done sourcing " + filename + ": " + (Date.now() - time) + "ms");
this.sourcing = wasSourcing; this.sourcing = wasSourcing;
} }
}, },

View File

@@ -89,8 +89,8 @@ window.addEventListener("load", function onLoad() {
const loaded = set(["init"]); const loaded = set(["init"]);
function init(module) { function init(module) {
function init(func) function init(func, mod)
function () func.call(module, dactyl, modules, window); function () defmodule.time(module.name || module.constructor.name, mod, func, module, dactyl, modules, window);
set.add(loaded, module.constructor.name); set.add(loaded, module.constructor.name);
for (let [mod, func] in Iterator(module.INIT)) { for (let [mod, func] in Iterator(module.INIT)) {
@@ -98,7 +98,7 @@ window.addEventListener("load", function onLoad() {
init(func)(); init(func)();
else { else {
deferredInit[mod] = deferredInit[mod] || []; deferredInit[mod] = deferredInit[mod] || [];
deferredInit[mod].push(init(func)); deferredInit[mod].push(init(func, mod));
} }
} }
} }
@@ -121,21 +121,19 @@ window.addEventListener("load", function onLoad() {
for (let dep in values(module.requires)) for (let dep in values(module.requires))
load(Module.constructors[dep], module.name); load(Module.constructors[dep], module.name);
dump("Load" + (isstring(prereq) ? " " + prereq + " dependency: " : ": ") + module.name); defmodule.loadLog.push("Load" + (isstring(prereq) ? " " + prereq + " dependency: " : ": ") + module.name);
if (frame && frame.filename) if (frame && frame.filename)
dump(" from: " + frame.filename + ":" + frame.lineNumber); defmodule.loadLog.push(" from: " + frame.filename + ":" + frame.lineNumber);
delete modules[module.name]; delete modules[module.name];
modules[module.name] = module(); modules[module.name] = defmodule.time(module.name, "init", module);
init(modules[module.name]); init(modules[module.name]);
for (let [, fn] in iter(deferredInit[module.name] || [])) for (let [, fn] in iter(deferredInit[module.name] || []))
fn(); fn();
} }
catch (e) { catch (e) {
dump("Loading " + (module && module.name) + ": " + e); dump("Loading " + (module && module.name) + ": " + e + "\n" + (e.stack || ""));
if (e.stack)
dump(e.stack);
} }
return modules[module.name]; return modules[module.name];
} }

View File

@@ -327,7 +327,7 @@ const Option = Class("Option", {
re.result = arguments.length == 2 ? result : !bang; re.result = arguments.length == 2 ? result : !bang;
return re; return re;
}, },
unparseRegex: function (re) re.bang + re.source + (typeof re.result == "string" ? "=" + re.result : ""), unparseRegex: function (re) re.bang + re.source + (typeof re.result == "string" ? ":" + re.result : ""),
getKey: { getKey: {
stringlist: function (k) this.values.indexOf(k) >= 0, stringlist: function (k) this.values.indexOf(k) >= 0,
@@ -342,7 +342,7 @@ const Option = Class("Option", {
joinValues: { joinValues: {
charlist: function (vals) vals.join(""), charlist: function (vals) vals.join(""),
stringlist: function (vals) vals.join(","), stringlist: function (vals) vals.join(","),
stringmap: function (vals) [k + "=" + v for ([k, v] in Iterator(vals))].join(","), stringmap: function (vals) [k + ":" + v for ([k, v] in Iterator(vals))].join(","),
regexlist: function (vals) vals.map(Option.unparseRegex).join(","), regexlist: function (vals) vals.map(Option.unparseRegex).join(","),
}, },
@@ -351,10 +351,10 @@ const Option = Class("Option", {
boolean: function (value) value == "true" || value == true ? true : false, boolean: function (value) value == "true" || value == true ? true : false,
charlist: function (value) Array.slice(value), charlist: function (value) Array.slice(value),
stringlist: function (value) (value === "") ? [] : value.split(","), stringlist: function (value) (value === "") ? [] : value.split(","),
stringmap: function (value) array(v.split(":") for (v in values(value.split(",")))).toObject(), stringmap: function (value) array(util.split(v, /:/g, 2) for (v in values(value.split(",")))).toObject(),
regexlist: function (value) (value === "") ? [] : value.split(",").map(Option.parseRegex), regexlist: function (value) (value === "") ? [] : value.split(",").map(Option.parseRegex),
regexmap: function (value) value.split(",").map(function (v) v.split(":")) regexmap: function (value) value.split(",").map(function (v) util.split(v, /:/g, 2))
.map(function ([k, v]) v != null ? Option.parseRegex(k, v) : Option.parseRegex('.?', k)) .map(function ([k, v]) v != null ? Option.parseRegex(k, v) : Option.parseRegex(".?", k))
}, },
ops: { ops: {
@@ -388,7 +388,7 @@ const Option = Class("Option", {
stringmap: function (operator, values, scope, invert) { stringmap: function (operator, values, scope, invert) {
values = Array.concat(values); values = Array.concat(values);
orig = [k + "=" + v for ([k, v] in Iterator(this.values))]; orig = [k + ":" + v for ([k, v] in Iterator(this.values))];
switch (operator) { switch (operator) {
case "+": case "+":
@@ -1321,7 +1321,7 @@ const Options = Module("options", {
case "regexmap": case "regexmap":
let vals = context.filter.split(","); let vals = context.filter.split(",");
target = vals.pop() || ""; target = vals.pop() || "";
len = target.length - (target.indexOf("=") + 1); len = target.length - (target.indexOf(":") + 1);
break; break;
case "charlist": case "charlist":
len = 0; len = 0;

0
common/content/statusline.js Executable file → Normal file
View File

View File

@@ -17,7 +17,7 @@ let currentModule;
function defmodule(name, module, params) { function defmodule(name, module, params) {
module.NAME = name; module.NAME = name;
module.EXPORTED_SYMBOLS = params.exports || []; module.EXPORTED_SYMBOLS = params.exports || [];
dump("defmodule " + name + "\n"); defmodule.loadLog.push("defmodule " + name);
for(let [, mod] in Iterator(params.require || [])) for(let [, mod] in Iterator(params.require || []))
require(module, mod); require(module, mod);
@@ -30,10 +30,22 @@ function defmodule(name, module, params) {
} }
currentModule = module; currentModule = module;
} }
defmodule.loadLog = [];
// Object.defineProperty(defmodule.loadLog, "push", { value: function (val) { dump(val + "\n"); this[this.length] = val } });
defmodule.modules = []; defmodule.modules = [];
defmodule.times = {};
defmodule.time = function (major, minor, func, self) {
let time = Date.now();
let res = func.apply(self, Array.slice(arguments, 4));
let delta = Date.now() - time;
defmodule.times[major] = (defmodule.times[major] || 0) + delta;
if (minor)
defmodule.times[major + ":" + minor] = (defmodule.times[major + ":" + minor] || 0) + delta;
return res;
}
function endmodule() { function endmodule() {
dump("endmodule " + currentModule.NAME + "\n"); defmodule.loadLog.push("endmodule " + currentModule.NAME);
loaded[currentModule.NAME] = 1; loaded[currentModule.NAME] = 1;
for(let [, mod] in Iterator(use[currentModule.NAME] || [])) for(let [, mod] in Iterator(use[currentModule.NAME] || []))
require(mod, currentModule.NAME, "use"); require(mod, currentModule.NAME, "use");
@@ -41,7 +53,7 @@ function endmodule() {
function require(obj, name, from) { function require(obj, name, from) {
try { try {
dump((from || "require") + ": loading " + name + " into " + obj.NAME + "\n"); defmodule.loadLog.push((from || "require") + ": loading " + name + " into " + obj.NAME);
Cu.import("resource://dactyl/" + name + ".jsm", obj); Cu.import("resource://dactyl/" + name + ".jsm", obj);
} }
catch (e) { catch (e) {

View File

@@ -699,12 +699,11 @@ const Util = Module("Util", {
if (!re.global) if (!re.global)
re = RegExp(re.source || re, "g"); re = RegExp(re.source || re, "g");
let match, start = 0, res = []; let match, start = 0, res = [];
while ((match = re.exec(str)) && --limit && match[0].length) { while (--limit && (match = re.exec(str)) && match[0].length) {
res.push(str.substring(start, match.index)); res.push(str.substring(start, match.index));
start = match.index + match[0].length; start = match.index + match[0].length;
} }
if (limit) res.push(str.substring(start));
res.push(str.substring(start));
return res; return res;
}, },