From ce51debcdc7bbcb7adb3dfd07afb5f45a88b9eff Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Wed, 28 Nov 2012 18:09:50 -0800 Subject: [PATCH] Death to E4X and stuff. --- common/modules/completion.jsm | 35 ++++++++++--------- common/modules/highlight.jsm | 12 +++---- common/modules/template.jsm | 63 +++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 24 deletions(-) diff --git a/common/modules/completion.jsm b/common/modules/completion.jsm index 8197208d..9780f289 100644 --- a/common/modules/completion.jsm +++ b/common/modules/completion.jsm @@ -10,8 +10,9 @@ defineModule("completion", { exports: ["CompletionContext", "Completion", "completion"] }, this); +lazyRequire("dom", ["DOM"]); lazyRequire("messages", ["_", "messages"]); -lazyRequire("template", ["template"]); +lazyRequire("template", ["template", "template_"]); /** * Creates a new completion context. @@ -654,17 +655,16 @@ var CompletionContext = Class("CompletionContext", { if (cache) { if (idx in this.items && !(idx in this.cache.rows)) try { - cache[idx] = util.xmlToDom(this.createRow(this.items[idx]), - doc || this.doc); + cache[idx] = DOM.fromJSON(this.createRow(this.items[idx]), + doc || this.doc); } catch (e) { util.reportError(e); - XML.ignoreWhitespace = XML.prettyPrinting = false; - cache[idx] = util.xmlToDom( -
-
  • {this.text} 
  • -
  • {e} 
  • -
    , doc || this.doc); + cache[idx] = DOM.fromJSON( + ["div", { highlight: "CompItem", style: "white-space: nowrap" }, + ["li", { highlight: "CompResult" }, this.text + "\u00a0"], + ["li", { highlight: "CompDesc ErrorMsg" }, e + "\u00a0"]], + doc || this.doc); } return cache[idx]; } @@ -935,11 +935,10 @@ var Completion = Module("completion", { contexts = context.contextList.slice(-1); modules.commandline.commandOutput( -
    - { template.map(contexts, function (context) - template.completionRow(context.title, "CompTitle") + - template.map(context.items, function (item) context.createRow(item), null, 100)) } -
    ); + ["div", { highlight: "Completions" }, + template_.map(contexts, function (context) + [template.completionRow(context.title, "CompTitle"), + template_.map(context.items, function (item) context.createRow(item), null, 100)])]); }, }), @@ -1120,10 +1119,10 @@ var Completion = Module("completion", { "List the completion contexts used during completion of an Ex command", function (args) { modules.commandline.commandOutput( -
    - { template.completionRow(["Context", "Title"], "CompTitle") } - { template.map(completion.contextList || [], function (item) template.completionRow(item, "CompItem")) } -
    ); + ["div", { highlight: "Completions" }, + template.completionRow(["Context", "Title"], "CompTitle"), + template_.map(completion.contextList || [], + function (item) template.completionRow(item, "CompItem"))]); }, { argCount: "*", diff --git a/common/modules/highlight.jsm b/common/modules/highlight.jsm index e6f18f39..77f00642 100644 --- a/common/modules/highlight.jsm +++ b/common/modules/highlight.jsm @@ -10,7 +10,7 @@ defineModule("highlight", { }); lazyRequire("styles", ["Styles", "styles"]); -lazyRequire("template", ["template"]); +lazyRequire("template", ["template", "template_"]); var Highlight = Struct("class", "selector", "sites", "defaultExtends", "defaultValue", @@ -345,14 +345,14 @@ var Highlights = Module("Highlight", { if (!modify) modules.commandline.commandOutput( - template.tabular(["Key", "Sample", "Link", "CSS"], + template_.tabular(["Key", "Sample", "Link", "CSS"], ["padding: 0 1em 0 0; vertical-align: top; max-width: 16em; overflow: hidden;", "text-align: center"], ([h.class, - XXX, - template.map(h.extends, function (s) template.highlight(s), <>,), - template.highlightRegexp(h.value, /\b[-\w]+(?=:)|\/\*.*?\*\//g, - function (match) {match}) + ["span", { style: "text-align: center; line-height: 1em;" + h.value + style }, "XXX"], + template_.map(h.extends, function (s) template.highlight(s), ","), + template_.highlightRegexp(h.value, /\b[-\w]+(?=:)|\/\*.*?\*\//g, + function (match) ["span", { highlight: match[0] == "/" ? "Comment" : "Key" }, match]) ] for (h in highlight) if (!key || h.class.indexOf(key) > -1)))); diff --git a/common/modules/template.jsm b/common/modules/template.jsm index e6eafc5a..0b3917fc 100644 --- a/common/modules/template.jsm +++ b/common/modules/template.jsm @@ -573,6 +573,56 @@ var Template_ = Module("Template_", { return res; }, + + highlightFilter: function highlightFilter(str, filter, highlight, isURI) { + if (isURI) + str = util.losslessDecodeURI(str); + + return this.highlightSubstrings(str, (function () { + if (filter.length == 0) + return; + + let lcstr = String.toLowerCase(str); + let lcfilter = filter.toLowerCase(); + let start = 0; + while ((start = lcstr.indexOf(lcfilter, start)) > -1) { + yield [start, filter.length]; + start += filter.length; + } + })(), highlight || template.filter); + }, + + highlightRegexp: function highlightRegexp(str, re, highlight) { + return this.highlightSubstrings(str, (function () { + for (let res in util.regexp.iterate(re, str)) + yield [res.index, res[0].length, res.wholeMatch ? [res] : res]; + })(), highlight || template.filter); + }, + + highlightSubstrings: function highlightSubstrings(str, iter, highlight) { + if (!isString(str)) + return str; + + if (str == "") + return DOM.DOMString(str); + + let s = [""]; + let start = 0; + let n = 0, _i; + for (let [i, length, args] in iter) { + if (i == _i || i < _i) + break; + _i = i; + + XML.ignoreWhitespace = false; + s.push(str.substring(start, i), + highlight.apply(this, Array.concat(args || str.substr(i, length)))); + start = i + length; + } + s.push(str.substr(start)); + return s; + }, + table: function table(title, data, indent) { let table = ["table", {}, ["tr", { highlight: "Title", align: "left" }, @@ -585,6 +635,19 @@ var Template_ = Module("Template_", { if (table[3].length) return table; }, + + tabular: function tabular(headings, style, iter) { + let self = this; + // TODO: This might be mind-bogglingly slow. We'll see. + return ["table", {}, + ["tr", { highlight: "Title", align: "left" }, + this.map(headings, function (h) + ["th", {}, h])], + this.map(iter, function (row) + ["tr", {}, + self.map(Iterator(row), function ([i, d]) + ["td", { style: style[i] || "" }, d])])]; + }, }); endModule();