From 6a7477870ddce1b6aba1c02e06805f83dbc51686 Mon Sep 17 00:00:00 2001 From: davidodenwald Date: Sun, 19 Mar 2023 15:49:41 +0100 Subject: [PATCH] align indentation of multiline expressions and statements --- src/printer.ts | 37 +++++++++++++++++-- test/cases/expression_multiline/expected.html | 6 ++- test/cases/expression_multiline/input.html | 4 +- test/cases/statement_long/expected.html | 10 +++++ test/cases/statement_long/input.html | 9 +++++ 5 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 test/cases/statement_long/expected.html create mode 100644 test/cases/statement_long/input.html 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 @@ +
    + {% + for href, caption in [ + ('index.html', 'Index'), + ('about.html', 'About'), + ('downloads.html', 'Downloads')] + %} +
  • {{ caption }}
  • + {% endfor %} +
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 @@ +
    +{% for href, caption in [ +('index.html', 'Index'), + ('about.html', 'About'), + ('downloads.html', 'Downloads')] +%} +
  • {{ caption }}
  • +{% endfor %} +
\ No newline at end of file