1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2026-02-18 08:05:47 +01:00

Experimentally move more modules to global modules.

--HG--
rename : common/content/completion.js => common/modules/completion.jsm
rename : common/content/javascript.js => common/modules/javascript.jsm
This commit is contained in:
Kris Maglione
2011-01-03 23:41:48 -05:00
parent d661d60cb6
commit ee2bba53ca
5 changed files with 152 additions and 103 deletions

View File

@@ -6,7 +6,13 @@
// given in the LICENSE.txt file included with this file. // given in the LICENSE.txt file included with this file.
"use strict"; "use strict";
/** @scope modules */ try {
Components.utils.import("resource://dactyl/base.jsm");
defineModule("completion", {
exports: ["CompletionContext", "Completion", "completion"],
use: ["config", "template", "util"]
});
/** /**
* Creates a new completion context. * Creates a new completion context.
@@ -36,9 +42,9 @@ var CompletionContext = Class("CompletionContext", {
let parent = editor; let parent = editor;
name = parent.name + "/" + name; name = parent.name + "/" + name;
this.autoComplete = options.get("autocomplete").getKey(name); this.autoComplete = this.options.get("autocomplete").getKey(name);
this.sortResults = options.get("wildsort").getKey(name); this.sortResults = this.options.get("wildsort").getKey(name);
this.wildcase = options.get("wildcase").getKey(name); this.wildcase = this.options.get("wildcase").getKey(name);
this.contexts = parent.contexts; this.contexts = parent.contexts;
if (name in this.contexts) if (name in this.contexts)
@@ -237,7 +243,7 @@ var CompletionContext = Class("CompletionContext", {
return this.cache.allItemsResult; return this.cache.allItemsResult;
} }
catch (e) { catch (e) {
dactyl.reportError(e); util.reportError(e);
return { start: 0, items: [], longestAllSubstring: "" }; return { start: 0, items: [], longestAllSubstring: "" };
} }
}, },
@@ -385,7 +391,7 @@ var CompletionContext = Class("CompletionContext", {
this.itemCache[this.key] = res; this.itemCache[this.key] = res;
} }
catch (e) { catch (e) {
dactyl.reportError(e); util.reportError(e);
this.message = "Error: " + e; this.message = "Error: " + e;
} }
} }
@@ -446,7 +452,7 @@ var CompletionContext = Class("CompletionContext", {
delete this._substrings; delete this._substrings;
if (!this.forceAnchored) if (!this.forceAnchored)
this.anchored = options.get("wildanchor").getKey(this.name, this.anchored); this.anchored = this.options.get("wildanchor").getKey(this.name, this.anchored);
// Item matchers // Item matchers
if (this.ignoreCase) if (this.ignoreCase)
@@ -484,7 +490,7 @@ var CompletionContext = Class("CompletionContext", {
} }
catch (e) { catch (e) {
this.message = "Error: " + e; this.message = "Error: " + e;
dactyl.reportError(e); util.reportError(e);
return []; return [];
} }
}, },
@@ -645,7 +651,7 @@ var CompletionContext = Class("CompletionContext", {
fork: function fork(name, offset, self, completer) { fork: function fork(name, offset, self, completer) {
if (isString(completer)) if (isString(completer))
completer = self[completer]; completer = self[completer];
let context = CompletionContext(this, name, offset); let context = this.constructor(this, name, offset);
if (this.contextList.indexOf(context) < 0) if (this.contextList.indexOf(context) < 0)
this.contextList.push(context); this.contextList.push(context);
@@ -833,40 +839,44 @@ var Completion = Module("completion", {
get setFunctionCompleter() JavaScript.setCompleter, // Backward compatibility get setFunctionCompleter() JavaScript.setCompleter, // Backward compatibility
// FIXME Local: function (dactyl, modules, window) ({
_runCompleter: function _runCompleter(name, filter, maxItems) { options: modules.options,
let context = CompletionContext(filter);
context.maxItems = maxItems;
let res = context.fork.apply(context, ["run", 0, this, name].concat(Array.slice(arguments, 3)));
if (res) {
if (Components.stack.caller.name === "runCompleter") // FIXME
return { items: res.map(function (i) ({ item: i })) };
context.contexts["/run"].completions = res;
}
context.wait(true);
return context.allItems;
},
runCompleter: function runCompleter(name, filter, maxItems) { // FIXME
return this._runCompleter.apply(this, Array.slice(arguments)) _runCompleter: function _runCompleter(name, filter, maxItems) {
.items.map(function (i) i.item); let context = modules.CompletionContext(filter);
}, context.maxItems = maxItems;
let res = context.fork.apply(context, ["run", 0, this, name].concat(Array.slice(arguments, 3)));
if (res) {
if (Components.stack.caller.name === "runCompleter") // FIXME
return { items: res.map(function (i) ({ item: i })) };
context.contexts["/run"].completions = res;
}
context.wait(true);
return context.allItems;
},
listCompleter: function listCompleter(name, filter, maxItems) { runCompleter: function runCompleter(name, filter, maxItems) {
let context = CompletionContext(filter || ""); return this._runCompleter.apply(this, Array.slice(arguments))
context.maxItems = maxItems; .items.map(function (i) i.item);
context.fork.apply(context, ["list", 0, completion, name].concat(Array.slice(arguments, 3))); },
context = context.contexts["/list"];
context.wait();
commandline.commandOutput( listCompleter: function listCompleter(name, filter, maxItems) {
<div highlight="Completions"> let context = modules.CompletionContext(filter || "");
{ template.map(context.contextList.filter(function (c) c.hasItems && c.items.length), context.maxItems = maxItems;
function (context) context.fork.apply(context, ["list", 0, completion, name].concat(Array.slice(arguments, 3)));
template.completionRow(context.title, "CompTitle") + context = context.contexts["/list"];
template.map(context.items, function (item) context.createRow(item), null, 100)) } context.wait();
</div>);
}, modules.commandline.commandOutput(
<div highlight="Completions">
{ template.map(context.contextList.filter(function (c) c.hasItems && c.items.length),
function (context)
template.completionRow(context.title, "CompTitle") +
template.map(context.items, function (item) context.createRow(item), null, 100)) }
</div>);
},
}),
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
////////////////////// COMPLETION TYPES //////////////////////////////////////// ////////////////////// COMPLETION TYPES ////////////////////////////////////////
@@ -882,14 +892,14 @@ var Completion = Module("completion", {
let start = 0; let start = 0;
let skip = 0; let skip = 0;
if (options["urlseparator"]) if (this.options["urlseparator"])
skip = context.filter.match("^.*" + options["urlseparator"]); // start after the last 'urlseparator' skip = context.filter.match("^.*" + this.options["urlseparator"]); // start after the last 'urlseparator'
if (skip) if (skip)
context.advance(skip[0].length); context.advance(skip[0].length);
if (complete == null) if (complete == null)
complete = options["complete"]; complete = this.options["complete"];
if (/^about:/.test(context.filter)) { if (/^about:/.test(context.filter)) {
context.fork("about", 6, this, function (context) { context.fork("about", 6, this, function (context) {
@@ -960,11 +970,22 @@ var Completion = Module("completion", {
}, { }, {
UrlCompleter: Struct("name", "description", "completer") UrlCompleter: Struct("name", "description", "completer")
}, { }, {
commands: function () { init: function init(dactyl, modules, window) {
init.superapply(this, arguments);
modules.CompletionContext = Class("CompletionContext", CompletionContext, {
init: function init() {
this.options = modules.options;
return init.superapply(this, arguments);
}
});
},
commands: function (dactyl, modules, window) {
const { commands, completion } = modules;
commands.add(["contexts"], commands.add(["contexts"],
"List the completion contexts used during completion of an Ex command", "List the completion contexts used during completion of an Ex command",
function (args) { function (args) {
commandline.commandOutput( modules.commandline.commandOutput(
<div highlight="Completions"> <div highlight="Completions">
{ template.completionRow(["Context", "Title"], "CompTitle") } { template.completionRow(["Context", "Title"], "CompTitle") }
{ template.map(completion.contextList || [], function (item) template.completionRow(item, "CompItem")) } { template.map(completion.contextList || [], function (item) template.completionRow(item, "CompItem")) }
@@ -980,7 +1001,8 @@ var Completion = Module("completion", {
literal: 0 literal: 0
}); });
}, },
options: function () { options: function (dactyl, modules, window) {
const { completion, options } = modules;
let wildmode = { let wildmode = {
completer: function (context) [ completer: function (context) [
// Why do we need ""? // Why do we need ""?
@@ -1042,4 +1064,8 @@ var Completion = Module("completion", {
} }
}); });
endModule();
} catch(e){ if (!e.stack) e = Error(e); dump(e.fileName+":"+e.lineNumber+": "+e+"\n" + e.stack); }
// vim: set fdm=marker sw=4 ts=4 et: // vim: set fdm=marker sw=4 ts=4 et:

View File

@@ -28,7 +28,7 @@ var IO = Module("io", {
this._oldcwd = null; this._oldcwd = null;
}, },
Local: function (dactyl, modules, window) let ({ Script, plugins } = modules) ({ Local: function (dactyl, modules, window) let ({ Script, io, plugins } = modules) ({
init: function init() { init: function init() {
this._processDir = services.directory.get("CurWorkD", Ci.nsIFile); this._processDir = services.directory.get("CurWorkD", Ci.nsIFile);

View File

@@ -4,24 +4,39 @@
// given in the LICENSE.txt file included with this file. // given in the LICENSE.txt file included with this file.
"use strict"; "use strict";
try {
Components.utils.import("resource://dactyl/base.jsm");
defineModule("javascript", {
exports: ["JavaScript", "javascript"],
use: ["services", "template", "util"],
});
// TODO: Clean this up. // TODO: Clean this up.
var JavaScript = Module("javascript", { var JavaScript = Module("javascript", {
init: function () { Local: function (dactyl, modules, window) ({
this._stack = []; init: function () {
this._functions = []; this.modules = modules;
this._top = {}; // The element on the top of the stack. this.window = window
this._last = ""; // The last opening char pushed onto the stack.
this._lastNonwhite = ""; // Last non-whitespace character we saw.
this._lastChar = ""; // Last character we saw, used for \ escaping quotes.
this._str = "";
this._lastIdx = 0; this._stack = [];
this._functions = [];
this._top = {}; // The element on the top of the stack.
this._last = ""; // The last opening char pushed onto the stack.
this._lastNonwhite = ""; // Last non-whitespace character we saw.
this._lastChar = ""; // Last character we saw, used for \ escaping quotes.
this._str = "";
this._cacheKey = null; this._lastIdx = 0;
this._nullSandbox = Cu.Sandbox("about:blank"); this._cacheKey = null;
},
this._nullSandbox = Cu.Sandbox("about:blank");
},
}),
newContext: function () this.modules.newContext(this.modules.userContext),
get completers() JavaScript.completers, // For backward compatibility get completers() JavaScript.completers, // For backward compatibility
@@ -39,7 +54,7 @@ var JavaScript = Module("javascript", {
return; return;
let seen = isinstance(obj, ["Sandbox"]) ? set(JavaScript.magicalNames) : {}; let seen = isinstance(obj, ["Sandbox"]) ? set(JavaScript.magicalNames) : {};
let globals = values(toplevel && window === obj ? JavaScript.globalNames : []); let globals = values(toplevel && this.window === obj ? this.globalNames : []);
for (let key in iter(globals, properties(obj, !toplevel, true))) for (let key in iter(globals, properties(obj, !toplevel, true)))
if (!set.add(seen, key)) if (!set.add(seen, key))
yield key; yield key;
@@ -60,12 +75,12 @@ var JavaScript = Module("javascript", {
return []; return [];
if (isinstance(obj, ["Sandbox"]) && !toplevel) // Temporary hack. if (isinstance(obj, ["Sandbox"]) && !toplevel) // Temporary hack.
return []; return [];
if (jsmodules.isPrototypeOf(obj) && !toplevel) if (this.modules.jsmodules.isPrototypeOf(obj) && !toplevel)
return []; return [];
let completions = [k for (k in this.iter(obj, toplevel))]; let completions = [k for (k in this.iter(obj, toplevel))];
if (obj === modules) // Hack. if (obj === this.modules) // Hack.
completions = completions.concat([k for (k in this.iter(jsmodules, toplevel))]); completions = completions.concat([k for (k in this.iter(this.modules.jsmodules, toplevel))]);
return completions; return completions;
}, },
@@ -82,9 +97,9 @@ var JavaScript = Module("javascript", {
context[JavaScript.EVAL_EXPORT] = function export(obj) cache[key] = obj; context[JavaScript.EVAL_EXPORT] = function export(obj) cache[key] = obj;
try { try {
if (tmp != null) // Temporary hack until bug 609949 is fixed. if (tmp != null) // Temporary hack until bug 609949 is fixed.
dactyl.userEval(JavaScript.EVAL_EXPORT + "(" + arg + ")", context, "[Command Line Completion]", 1); this.modules.dactyl.userEval(JavaScript.EVAL_EXPORT + "(" + arg + ")", context, "[Command Line Completion]", 1);
else else
cache[key] = dactyl.userEval(arg, context, "[Command Line Completion]", 1); cache[key] = this.modules.dactyl.userEval(arg, context, "[Command Line Completion]", 1);
return cache[key]; return cache[key];
} }
@@ -254,7 +269,7 @@ var JavaScript = Module("javascript", {
_getObj: function (frame, stop) { _getObj: function (frame, stop) {
let statement = this._get(frame, 0, "statements") || 0; // Current statement. let statement = this._get(frame, 0, "statements") || 0; // Current statement.
let prev = statement; let prev = statement;
let obj = window; let obj = this.window;
let cacheKey; let cacheKey;
for (let [, dot] in Iterator(this._get(frame).dots.concat(stop))) { for (let [, dot] in Iterator(this._get(frame).dots.concat(stop))) {
if (dot < statement) if (dot < statement)
@@ -287,9 +302,9 @@ var JavaScript = Module("javascript", {
this._cacheKey = null; this._cacheKey = null;
let obj = [[this.cache.evalContext, "Local Variables"], let obj = [[this.cache.evalContext, "Local Variables"],
[userContext, "Global Variables"], [this.modules.userContext, "Global Variables"],
[modules, "modules"], [this.modules, "modules"],
[window, "window"]]; // Default objects; [this.window, "window"]]; // Default objects;
// Is this an object dereference? // Is this an object dereference?
if (dot < statement) // No. if (dot < statement) // No.
dot = statement - 1; dot = statement - 1;
@@ -322,7 +337,7 @@ var JavaScript = Module("javascript", {
_complete: function (objects, key, compl, string, last) { _complete: function (objects, key, compl, string, last) {
const self = this; const self = this;
if (!window.Object.getOwnPropertyNames && !options["jsdebugger"] && !this.context.message) if (!this.window.Object.getOwnPropertyNames && !services.debugger.isOn && !this.context.message)
this.context.message = "For better completion data, please enable the JavaScript debugger (:set jsdebugger)"; this.context.message = "For better completion data, please enable the JavaScript debugger (:set jsdebugger)";
let orig = compl; let orig = compl;
@@ -430,14 +445,13 @@ var JavaScript = Module("javascript", {
this._buildStack.call(this, context.filter); this._buildStack.call(this, context.filter);
} }
catch (e) { catch (e) {
if (e.message != "Invalid JS")
dactyl.reportError(e);
this._lastIdx = 0; this._lastIdx = 0;
util.assert(!e.message, e.message);
return null; return null;
} }
this.context.getCache("evalled", Object); this.context.getCache("evalled", Object);
this.context.getCache("evalContext", function () newContext(userContext)); this.context.getCache("evalContext", this.closure.newContext);
// Okay, have parse stack. Figure out what we're completing. // Okay, have parse stack. Figure out what we're completing.
@@ -586,27 +600,15 @@ var JavaScript = Module("javascript", {
this._top.offset = o; this._top.offset = o;
} }
return null; return null;
} },
}, {
EVAL_TMP: "__dactyl_eval_tmp",
EVAL_EXPORT: "__dactyl_eval_export",
/** magicalNames: Class.memoize(function () Object.getOwnPropertyNames(Cu.Sandbox(this.window), true).sort()),
* A map of argument completion functions for named methods. The
* signature and specification of the completion function
* are fairly complex and yet undocumented.
*
* @see JavaScript.setCompleter
*/
completers: {},
magicalNames: Class.memoize(function () Object.getOwnPropertyNames(Cu.Sandbox(window), true).sort()),
/** /**
* A list of properties of the global object which are not * A list of properties of the global object which are not
* enumerable by any standard method. * enumerable by any standard method.
*/ */
globalNames: Class.memoize(function () array.uniq([ globalNames: Class.memoize(function () let (self = this) array.uniq([
"Array", "ArrayBuffer", "AttributeName", "Boolean", "Components", "Array", "ArrayBuffer", "AttributeName", "Boolean", "Components",
"CSSFontFaceStyleDecl", "CSSGroupRuleRuleList", "CSSNameSpaceRule", "CSSFontFaceStyleDecl", "CSSGroupRuleRuleList", "CSSNameSpaceRule",
"CSSRGBColor", "CSSRect", "ComputedCSSStyleDeclaration", "Date", "CSSRGBColor", "CSSRect", "ComputedCSSStyleDeclaration", "Date",
@@ -624,8 +626,21 @@ var JavaScript = Module("javascript", {
"isXMLName", "parseFloat", "parseInt", "undefined", "uneval" "isXMLName", "parseFloat", "parseInt", "undefined", "uneval"
].concat([k.substr(6) for (k in keys(Ci)) if (/^nsIDOM/.test(k))]) ].concat([k.substr(6) for (k in keys(Ci)) if (/^nsIDOM/.test(k))])
.concat([k.substr(3) for (k in keys(Ci)) if (/^nsI/.test(k))]) .concat([k.substr(3) for (k in keys(Ci)) if (/^nsI/.test(k))])
.concat(JavaScript.magicalNames) .concat(this.magicalNames)
.filter(function (k) k in window))), .filter(function (k) k in self.window))),
}, {
EVAL_TMP: "__dactyl_eval_tmp",
EVAL_EXPORT: "__dactyl_eval_export",
/**
* A map of argument completion functions for named methods. The
* signature and specification of the completion function
* are fairly complex and yet undocumented.
*
* @see JavaScript.setCompleter
*/
completers: {},
/** /**
* Installs argument string completers for a set of functions. * Installs argument string completers for a set of functions.
@@ -658,12 +673,16 @@ var JavaScript = Module("javascript", {
return arguments[0]; return arguments[0];
} }
}, { }, {
completion: function () { init: function init(dactyl, modules, window) {
completion.javascript = this.closure.complete; init.superapply(this, arguments);
},
completion: function (dactyl, modules, window) {
const { completion, javascript } = modules;
completion.javascript = javascript.closure.complete;
completion.javascriptCompleter = JavaScript; // Backwards compatibility. completion.javascriptCompleter = JavaScript; // Backwards compatibility.
}, },
options: function () { options: function (dactyl, modules, window) {
options.add(["jsdebugger", "jsd"], modules.options.add(["jsdebugger", "jsd"],
"Enable the JavaScript debugger service for use in JavaScript completion", "Enable the JavaScript debugger service for use in JavaScript completion",
"boolean", false, { "boolean", false, {
setter: function (value) { setter: function (value) {
@@ -678,4 +697,8 @@ var JavaScript = Module("javascript", {
} }
}); });
endModule();
} catch(e){ if (!e.stack) e = Error(e); dump(e.fileName+":"+e.lineNumber+": "+e+"\n" + e.stack); }
// vim: set fdm=marker sw=4 ts=4 et: // vim: set fdm=marker sw=4 ts=4 et:

View File

@@ -146,24 +146,24 @@ var Overlay = Module("Overlay", {
let prefix = [BASE]; let prefix = [BASE];
["base", ["base",
"completion",
"config", "config",
"util", "javascript",
"services",
"overlay", "overlay",
"prefs", "prefs",
"storage" "services",
"storage",
"util"
].forEach(function (name) require(jsmodules, name)); ].forEach(function (name) require(jsmodules, name));
prefix.unshift("chrome://" + config.name + "/content/"); prefix.unshift("chrome://" + config.name + "/content/");
["javascript", ["dactyl",
"dactyl",
"modes", "modes",
"abbreviations", "abbreviations",
"autocommands", "autocommands",
"buffer", "buffer",
"commandline", "commandline",
"commands", "commands",
"completion",
"editor", "editor",
"events", "events",
"finder", "finder",
@@ -212,6 +212,8 @@ var Overlay = Module("Overlay", {
deferredInit[mod].push(init(func, mod)); deferredInit[mod].push(init(func, mod));
} }
} }
for (let [, fn] in iter(deferredInit[module.constructor.className] || []))
fn();
} }
function load(module, prereq, frame) { function load(module, prereq, frame) {
@@ -239,8 +241,6 @@ var Overlay = Module("Overlay", {
modules[module.className] = defineModule.time(module.className, "init", module); modules[module.className] = defineModule.time(module.className, "init", module);
init(modules[module.className]); init(modules[module.className]);
for (let [, fn] in iter(deferredInit[module.className] || []))
fn();
} }
catch (e) { catch (e) {
util.dump("Loading " + (module && module.className) + ":"); util.dump("Loading " + (module && module.className) + ":");

View File

@@ -1501,6 +1501,6 @@ var Math = update(Object.create(GlobalMath), {
endModule(); endModule();
} catch(e){ if (isString(e)) e = Error(e); dump(e.fileName+":"+e.lineNumber+": "+e+"\n" + e.stack); } } catch(e){ if (!e.stack) e = Error(e); dump(e.fileName+":"+e.lineNumber+": "+e+"\n" + e.stack); }
// vim: set fdm=marker sw=4 ts=4 et ft=javascript: // vim: set fdm=marker sw=4 ts=4 et ft=javascript: