Skip to content

Commit

Permalink
Merge branch 'comprehensions-review'
Browse files Browse the repository at this point in the history
  • Loading branch information
gkatsev committed Apr 28, 2016
2 parents 9599dc9 + 475e02a commit 06d695b
Show file tree
Hide file tree
Showing 26 changed files with 1,033 additions and 218 deletions.
66 changes: 59 additions & 7 deletions common/content/abbreviations.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,23 @@ const Abbreviations = Module("abbreviations", {
*/
get merged() {
let result = [];
let lhses = util.Array.uniq([lhs for ([, mabbrevs] in Iterator(this.abbrevs)) for (lhs of Object.keys(mabbrevs))].sort());

/* assert start */
// function assert(condition, bookmark) { dump(bookmark+': '); if (!condition) dump('FAILED\n'); else dump('PASSED\n'); }
//
// let before = util.Array.uniq([lhs for ([, mabbrevs] in Iterator(this.abbrevs)) for (lhs of Object.keys(mabbrevs))].sort());
// let after = util.Array.uniq(
// Array.from(values(this.abbrevs))
// .reduce((abbrevs, mabbrev) => [...abbrevs, ...Object.keys(mabbrev)], [])
// );
//
// assert(JSON.stringify(before) == JSON.stringify(after), '#1 in abbrevations.js');
/* assert end */

let lhses = util.Array.uniq(
Array.from(values(this.abbrevs))
.reduce((abbrevs, mabbrev) => [...abbrevs, ...Object.keys(mabbrev)], [])
);

for (let lhs of lhses) {
let exists = {};
Expand Down Expand Up @@ -208,7 +224,17 @@ const Abbreviations = Module("abbreviations", {
completion.abbreviation = function abbreviation(context, args, modes) {
if (args.completeArg == 0) {
let abbrevs = abbreviations.merged.filter(function (abbr) abbr.inModes(modes));
context.completions = [[abbr.lhs, abbr.rhs] for (abbr of abbrevs)];

/* assert start */
// function assert(condition, bookmark) { dump(bookmark+': '); if (!condition) dump('FAILED\n'); else dump('PASSED\n'); }
//
// let before = [[abbr.lhs, abbr.rhs] for (abbr of abbrevs)];
// let after = abbrevs.map(abbr => [abbr.lhs, abbr.rhs]);
//
// assert(JSON.stringify(before) == JSON.stringify(after), '#2 in abbreviations.js');
/* assert end */

context.completions = abbrevs.map(abbr => [abbr.lhs, abbr.rhs]);
}
};
},
Expand Down Expand Up @@ -249,15 +275,41 @@ const Abbreviations = Module("abbreviations", {
completion.abbreviation(context, args, modes)
},
literal: 0,
serial: function () [ {
serial: function () {
/* assert start */
// function assert(condition, bookmark) { dump(bookmark+': '); if (!condition) dump('FAILED\n'); else dump('PASSED\n'); }
//
// let before = [
// {
// command: this.name,
// arguments: [abbr.lhs],
// literalArg: abbr.rhs,
// options: abbr.rhs instanceof Function ? {"-javascript": null} : {}
// }
// for ([, abbr] in Iterator(abbreviations.merged))
// if (abbr.modesEqual(modes))
// ];
// let after = abbreviations.merged
// .filter(abbr => abbr.modesEqual(modes))
// .map(abbr => ({
// command: this.name,
// arguments: [abbr.lhs],
// literalArg: abbr.rhs,
// options: abbr.rhs instanceof Function ? {"-javascript": null} : {}
// }));
//
// assert(JSON.stringify(before) == JSON.stringify(after), '#3 in abbreviations.js');
/* assert end */

return abbreviations.merged
.filter(abbr => abbr.modesEqual(modes))
.map(abbr => ({
command: this.name,
arguments: [abbr.lhs],
literalArg: abbr.rhs,
options: abbr.rhs instanceof Function ? {"-javascript": null} : {}
}
for ([, abbr] in Iterator(abbreviations.merged))
if (abbr.modesEqual(modes))
]
}));
}
});

commands.add([ch ? ch + "una[bbrev]" : "una[bbreviate]"],
Expand Down
13 changes: 12 additions & 1 deletion common/content/autocommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,18 @@ const AutoCommands = Module("autocommands", {

completion.macro = function macro(context) {
context.title = ["Macro", "Keys"];
context.completions = [item for (item in events.getMacros())];

// NOTE: was unable to execute this assert
/* assert start */
// function assert(condition, bookmark) { dump(bookmark+': '); if (!condition) dump('FAILED\n'); else dump('PASSED\n'); }
//
// let before = [item for (item in events.getMacros())];
// let after = Array.from(events.getMacros());
//
// assert(JSON.stringify(before) == JSON.stringify(after), '#1 in autocommands.js');
/* assert end */

context.completions = Array.from(events.getMacros());
};
},
options: function () {
Expand Down
50 changes: 47 additions & 3 deletions common/content/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@ const Cu = Components.utils;

function array(obj) {
if (isgenerator(obj))
obj = [k for (k in obj)];
/* assert start */
// function assert(condition, bookmark) { dump(bookmark+': '); if (!condition) dump('FAILED\n'); else dump('PASSED\n'); }
//
// let original = Array.from(obj);
// obj = iter(original);
// let before = [k for (k in obj)];
// obj = iter(original);
// let after = Array.from(obj);
//
// assert(JSON.stringify(before) == JSON.stringify(after), '#1 in base.js');
/* assert end */

obj = Array.from(obj);
else if (obj.length)
obj = Array.slice(obj);
return util.Array(obj);
Expand Down Expand Up @@ -66,13 +78,35 @@ function iter(obj) {
catch (e) {}
})();
if (isinstance(obj, [HTMLCollection, NodeList]))
return (node for (node of obj));
/* assert start */
// function assert(condition, bookmark) { dump(bookmark+': '); if (!condition) dump('FAILED\n'); else dump('PASSED\n'); }
//
// let before = (node for (node of obj));
// let after = (function* () {
// for (node of obj) {
// yield node;
// }
// })();
//
// assert(JSON.stringify(Array.from(before)) == JSON.stringify(Array.from(after)), '#2 in base.js');
/* assert end */

return (function* () {
for (node of obj) {
yield node;
}
})();
if ((typeof NamedNodeMap !== "undefined" && obj instanceof NamedNodeMap) ||
(typeof MozNamedAttrMap !== "undefined" && obj instanceof MozNamedAttrMap))
return (function () {
for (let i = 0, len = obj.length; i < len; ++i)
yield [obj[i].name, obj[i]];
})();
if (obj instanceof Array)
return (function () {
for (value of obj)
yield value;
})();
return Iterator(obj);
}

Expand Down Expand Up @@ -401,7 +435,17 @@ const StructBase = Class("StructBase", {
// Iterator over our named members
__iterator__: function () {
let self = this;
return ([k, self[i]] for ([i, k] in Iterator(self.members)))

/* assert start */
// function assert(condition, bookmark) { dump(bookmark+': '); if (!condition) dump('FAILED\n'); else dump('PASSED\n'); }
//
// let before = ([k, self[i]] for ([i, k] in Iterator(self.members)));
// let after = iter(Object.keys(self.members).map(i => [self.members[i], self[i]]));
//
// assert(JSON.stringify(Array.from(before)) == JSON.stringify(Array.from(after)), '#3 in base.js');
/* assert end */

return iter(Object.keys(self.members).map(i => [self.members[i], self[i]]));
}
}, {
/**
Expand Down
36 changes: 23 additions & 13 deletions common/content/bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ const Bookmarks = Module("bookmarks", {
this.__defineGetter__("store", function () store);
this.__defineGetter__("bookmarks", function () this.load());

this.__defineGetter__("keywords",
function () [Keyword(k.keyword, k.title, k.icon, k.url) for (k of self.bookmarks) if (k.keyword)]);
this.__defineGetter__("keywords", function () {
return self.bookmarks.filter(k => k.keyword)
.map(k => Keyword(k.keyword, k.title, k.icon, k.url));
});

this.__iterator__ = function () (val for (val of self.bookmarks));
this.__iterator__ = function () iter(self.bookmarks);

function loadBookmark(node) {
if (node.uri == null) // How does this happen?
Expand Down Expand Up @@ -335,7 +337,8 @@ const Bookmarks = Module("bookmarks", {
let results = [];
try {
results = JSON.parse(resp.responseText)[1];
results = [[item, ""] for ([k, item] in Iterator(results)) if (typeof item == "string")];
results = results.filter((item, k) => typeof item == "string")
.map((item, k) => [item, ""]);
}
catch (e) {}
if (!callback)
Expand Down Expand Up @@ -502,11 +505,13 @@ const Bookmarks = Module("bookmarks", {
"Show jumplist",
function () {
let sh = history.session;
let jumps = [[idx == sh.index ? ">" : "",
Math.abs(idx - sh.index),
val.title,
xml`<a highlight="URL" href=${val.URI.spec}>${val.URI.spec}</a>`]
for ([idx, val] in Iterator(sh))];
let jumps = Array.from(iter(sh))
.map(([idx, val]) => [
idx == sh.index ? ">" : "",
Math.abs(idx - sh.index),
val.title,
xml`<a highlight="URL" href=${val.URI.spec}>${val.URI.spec}</a>`
]);
let list = template.tabular([{ header: "Jump", style: "color: red", colspan: 2 },
{ header: "", style: "text-align: right", highlight: "Number" },
{ header: "Title", style: "width: 250px; max-width: 500px; overflow: hidden" },
Expand All @@ -525,9 +530,12 @@ const Bookmarks = Module("bookmarks", {
args.completeFilter = have.pop();

let prefix = filter.substr(0, filter.length - args.completeFilter.length);
let tags = util.Array.uniq(util.Array.flatten([b.tags for (b of bookmarks._cache.bookmarks)]));
let tags = util.Array.uniq(
util.Array.flatten(bookmarks._cache.bookmarks.map(b => b.tags))
);

return [[prefix + tag, tag] for (tag of tags) if (have.indexOf(tag) < 0)];
return tags.filter(tag => have.indexOf(tag) < 0)
.map(tag => [prefix + tag, tag]);
}

function title(context, args) {
Expand All @@ -539,8 +547,10 @@ const Bookmarks = Module("bookmarks", {
}

function keyword(context, args) {
let keywords = util.Array.uniq(util.Array.flatten([b.keyword for (b of bookmarks._cache.keywords)]));
return [[kw, kw] for (kw of keywords)];
let keywords = util.Array.uniq(
util.Array.flatten(bookmarks._cache.keywords.map(b => b.keyword))
);
return keywords.map(kw => [kw, kw]);
}

commands.add(["bma[rk]"],
Expand Down
67 changes: 60 additions & 7 deletions common/content/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ const Buffer = Module("buffer", {
* buffer. Only returns style sheets for the 'screen' media type.
*/
get alternateStyleSheets() {
// NOTE: getAllStyleSheets seems to be not supported
let stylesheets = window.getAllStyleSheets(config.browser.contentWindow);

return stylesheets.filter(
Expand Down Expand Up @@ -1371,7 +1372,17 @@ const Buffer = Module("buffer", {
styles[style.title].push(style.href || "inline");
});

context.completions = [[s, styles[s].join(", ")] for (s in styles)];
// NOTE: was unable to execute this assert
/* assert start */
// function assert(condition, bookmark) { dump(bookmark+': '); if (!condition) dump('FAILED\n'); else dump('PASSED\n'); }
//
// let before = [[s, styles[s].join(", ")] for (s in styles)];
// let after = Object.keys(styles).map(s => [s, styles[s].join(", ")]);
//
// assert(JSON.stringify(before) == JSON.stringify(after), '#1 in buffer.js');
/* assert end */

context.completions = Object.keys(styles).map(s => [s, styles[s].join(", ")]);
};

const UNTITLED_LABEL = "(Untitled)";
Expand Down Expand Up @@ -1457,7 +1468,18 @@ const Buffer = Module("buffer", {

if (flags & this.buffer.VISIBLE) {
context.title = ["Buffers"];
context.completions = [item for (item in generateTabs(tabs || config.tabbrowser.visibleTabs))];

/* assert start */
// function assert(condition, bookmark) { dump(bookmark+': '); if (!condition) dump('FAILED\n'); else dump('PASSED\n'); }
//
// let before = [item for (item in generateTabs(tabs || config.tabbrowser.visibleTabs))];
// let after = Array.from(generateTabs(tabs || config.tabbrowser.visibleTabs));
//
// dump(JSON.stringify(before));
// assert(JSON.stringify(before) == JSON.stringify(after), '#2 in buffer.js');
/* assert end */

context.completions = Array.from(generateTabs(tabs || config.tabbrowser.visibleTabs));
}

if (!liberator.has("tabgroup") || !tabGroup.TV)
Expand All @@ -1472,7 +1494,7 @@ const Buffer = Module("buffer", {
let groupName = group.getTitle();
context.fork("GROUP_" + group.id, 0, this, function (context) {
context.title = [groupName || UNTITLED_LABEL];
context.completions = [item for (item in generateGroupList(group, groupName))];
context.completions = Array.from(generateGroupList(group, groupName));
});
}
}
Expand Down Expand Up @@ -1670,9 +1692,29 @@ const Buffer = Module("buffer", {
"textarea[not(@disabled) and not(@readonly)]",
"iframe"];

let elements = [m for (m in util.evaluateXPath(xpath))
if(m.getClientRects().length && (!(m instanceof HTMLIFrameElement) || Editor.windowIsEditable(m.contentWindow)))
];
/* assert start */
// function assert(condition, bookmark) { dump(bookmark+': '); if (!condition) dump('FAILED\n'); else dump('PASSED\n'); }
//
// let after = [];
// for (m in util.evaluateXPath(xpath)) {
// if (m.getClientRects().length
// && (!(m instanceof HTMLIFrameElement) || Editor.windowIsEditable(m.contentWindow)))
// after.push(m);
// }
// let before = [m for (m in util.evaluateXPath(xpath))
// if(m.getClientRects().length && (!(m instanceof HTMLIFrameElement) || Editor.windowIsEditable(m.contentWindow)))
// ];
//
// assert(JSON.stringify(before) == JSON.stringify(after), '#3 in buffer.js');
/* assert end */

// NOTE: Array.from doesn't work here
let elements = [];
for (let m in util.evaluateXPath(xpath)) {
if (m.getClientRects().length
&& (!(m instanceof HTMLIFrameElement) || Editor.windowIsEditable(m.contentWindow)))
elements.push(m);
}

liberator.assert(elements.length > 0);
buffer.focusElement(elements[util.Math.constrain(count, 1, elements.length) - 1]);
Expand Down Expand Up @@ -1795,7 +1837,18 @@ const Buffer = Module("buffer", {
"Desired info in the :pageinfo output",
"charlist", "gfm",
{
completer: function (context) [[k, v[1]] for ([k, v] in Iterator(buffer.pageInfo))]
completer: function (context) {
/* assert start */
// function assert(condition, bookmark) { dump(bookmark+': '); if (!condition) dump('FAILED\n'); else dump('PASSED\n'); }
//
// let before = [[k, v[1]] for ([k, v] in Iterator(buffer.pageInfo))];
// let after = Object.keys(buffer.pageInfo).map(k => [k, buffer.pageInfo[k][1]]);
//
// assert(JSON.stringify(before) == JSON.stringify(after), '#4 in buffer.js');
/* assert end */

return Object.keys(buffer.pageInfo).map(k => [k, buffer.pageInfo[k][1]]);
}
});

options.add(["scroll", "scr"],
Expand Down
Loading

0 comments on commit 06d695b

Please sign in to comment.