diff --git a/src/printer.ts b/src/printer.ts index 730a39a..0eaf76a 100644 --- a/src/printer.ts +++ b/src/printer.ts @@ -31,14 +31,35 @@ export const print: Printer["print"] = (path) => { }; const printExpression = (node: Expression): builders.Doc => { - return builders.group(["{{", " ", node.content, " ", "}}"], { - shouldBreak: node.ownLine || node.content.includes("\n"), - }); + const multiline = node.content.includes("\n"); + + return builders.group( + builders.join(" ", [ + "{{", + multiline + ? builders.indent([getMultilineGroup(node.content)]) + : node.content, + multiline ? [builders.hardline, "}}"] : "}}", + ]), + { + shouldBreak: node.ownLine, + } + ); }; const printStatement = (node: Statement): builders.Doc => { + const multiline = node.content.includes("\n"); + const statemnt = builders.group( - ["{%", node.delimiter, " ", node.content, " ", node.delimiter, "%}"], + builders.join(" ", [ + ["{%", node.delimiter], + multiline + ? builders.indent(getMultilineGroup(node.content)) + : node.content, + multiline + ? [builders.hardline, node.delimiter, "%}"] + : [node.delimiter, "%}"], + ]), { shouldBreak: node.ownLine } ); @@ -147,6 +168,14 @@ export const embed: Printer["embed"] = ( return [...mapped, builders.hardline]; }; +const getMultilineGroup = (content: String): builders.Group => { + return builders.group( + content.split("\n").map((line, i) => { + return [builders.hardline, line.trim()]; + }) + ); +}; + const splitAtElse = (node: Node): string[] => { const elseNodes = Object.values(node.nodes).filter( (n) => diff --git a/test/cases/expression_multiline/expected.html b/test/cases/expression_multiline/expected.html index 3b415be..59b75ec 100644 --- a/test/cases/expression_multiline/expected.html +++ b/test/cases/expression_multiline/expected.html @@ -1,7 +1,9 @@
- {{ { + {{ + { 'dict': 'of', 'key': 'and', 'value': 'pairs' - } }} + } + }}
diff --git a/test/cases/expression_multiline/input.html b/test/cases/expression_multiline/input.html index daebc91..20a2798 100644 --- a/test/cases/expression_multiline/input.html +++ b/test/cases/expression_multiline/input.html @@ -2,8 +2,8 @@ {{ { 'dict': 'of', - 'key': 'and', - 'value': 'pairs' + 'key': 'and', +'value': 'pairs' } }} \ No newline at end of file diff --git a/test/cases/statement_long/expected.html b/test/cases/statement_long/expected.html new file mode 100644 index 0000000..d719dc4 --- /dev/null +++ b/test/cases/statement_long/expected.html @@ -0,0 +1,10 @@ + diff --git a/test/cases/statement_long/input.html b/test/cases/statement_long/input.html new file mode 100644 index 0000000..9d04e12 --- /dev/null +++ b/test/cases/statement_long/input.html @@ -0,0 +1,9 @@ + \ No newline at end of file