From f5eb7297ff0f8d25e8662a486a0cc722d2fc7740 Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Sat, 18 Nov 2017 11:40:47 -0500 Subject: [PATCH] auto formatter customization --- dom.d | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/dom.d b/dom.d index 3c88d07..c62e760 100644 --- a/dom.d +++ b/dom.d @@ -1321,16 +1321,16 @@ class Document : FileResource { Do NOT use for anything other than eyeball debugging, because whitespace may be significant content in XML. +/ - string toPrettyString(bool insertComments = false) const { + string toPrettyString(bool insertComments = false, int indentationLevel = 0, string indentWith = "\t") const { string s = prolog; if(insertComments) s ~= ""; - s ~= root.toPrettyString(insertComments); + s ~= root.toPrettyString(insertComments, indentationLevel, indentWith); foreach(a; piecesAfterRoot) - s ~= a.toPrettyString(insertComments); + s ~= a.toPrettyString(insertComments, indentationLevel, indentWith); return s; } @@ -3088,13 +3088,13 @@ class Element { return writeToAppender(); } - protected string toPrettyStringIndent(bool insertComments, int indentationLevel) const { + protected string toPrettyStringIndent(bool insertComments, int indentationLevel, string indentWith) const { string s; if(insertComments) s ~= ""; return s; @@ -3104,7 +3104,7 @@ class Element { Writes out with formatting. Be warned: formatting changes the contents. Use ONLY for eyeball debugging. +/ - string toPrettyString(bool insertComments = false, int indentationLevel = 0) const { + string toPrettyString(bool insertComments = false, int indentationLevel = 0, string indentWith = "\t") const { // first step is to concatenate any consecutive text nodes to simplify // the white space analysis. this changes the tree! but i'm allowed since @@ -3150,12 +3150,17 @@ class Element { } } - string s = toPrettyStringIndent(insertComments, indentationLevel); + string s = toPrettyStringIndent(insertComments, indentationLevel, indentWith); s ~= "<"; s ~= tagName; - foreach(n, v ; attributes) { + // i sort these for consistent output. might be more legible + // but especially it keeps it the same for diff purposes. + import std.algorithm : sort; + auto keys = sort(attributes.keys); + foreach(n; keys) { + auto v = attributes[n]; s ~= " "; s ~= n; s ~= "=\""; @@ -3178,12 +3183,12 @@ class Element { foreach(child; children) { assert(child !is null); - s ~= child.toPrettyString(insertComments, indentationLevel + 1); + s ~= child.toPrettyString(insertComments, indentationLevel + 1, indentWith); } // see comment above if(!(children.length == 1 && children[0].nodeType == NodeType.Text && children[0].nodeValue.strip.length)) - s ~= toPrettyStringIndent(insertComments, indentationLevel); + s ~= toPrettyStringIndent(insertComments, indentationLevel, indentWith); s ~= ""; } @@ -4189,7 +4194,7 @@ class BangInstruction : SpecialElement { return where.data[start .. $]; } - override string toPrettyString(bool, int) const { + override string toPrettyString(bool, int, string) const { string s; s ~= "