Skip to content

Commit

Permalink
Dedent multiline content
Browse files Browse the repository at this point in the history
  • Loading branch information
SmileyChris authored and davidodenwald committed Apr 28, 2024
1 parent 09cc121 commit b2a35c0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 19 deletions.
24 changes: 19 additions & 5 deletions src/printer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AstPath, Printer, Doc, Options } from "prettier";
import { AstPath, Doc, Options, Printer } from "prettier";
import { builders, utils } from "prettier/doc";
import { Placeholder, Node, Expression, Statement, Block } from "./jinja";
import { extendedOptions } from "./index";
import { Block, Expression, Node, Placeholder, Statement } from "./jinja";

const NOT_FOUND = -1;

Expand Down Expand Up @@ -195,10 +195,24 @@ export const embed: Printer<Node>["embed"] = () => {
};

const getMultilineGroup = (content: String): builders.Group => {
// Dedent the content by the minimum indentation of any non-blank lines.
const lines = content.split("\n");
const minIndent = Math.min(
...lines
.slice(1) // can't be the first line
.filter((line) => line.trim())
.map((line) => line.search(/\S/)),
);

return builders.group(
content.split("\n").map((line, i) => {
return [builders.hardline, line.trim()];
}),
lines.map((line, i) => [
builders.hardline,
i === 0
? line.trim() // don't dedent the first line
: line.trim()
? line.slice(minIndent).trimEnd()
: "",
]),
);
};

Expand Down
6 changes: 3 additions & 3 deletions test/cases/expression_multiline/expected.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<div>
{{
{
'dict': 'of',
'key': 'and',
'value': 'pairs'
'dict': 'of',
'key': 'and',
'value': 'pairs'
}
}}
</div>
10 changes: 5 additions & 5 deletions test/cases/expression_multiline/input.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<div>
{{
{
'dict': 'of',
'key': 'and',
'value': 'pairs'
}
{
'dict': 'of',
'key': 'and',
'value': 'pairs'
}
}}
</div>
7 changes: 4 additions & 3 deletions test/cases/statement_long/expected.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<ul>
{%
for href, caption in [
('index.html', 'Index'),
('about.html', 'About'),
('downloads.html', 'Downloads')]
('index.html', 'Index'),
('about.html', 'About'),
('downloads.html', 'Downloads')
]
%}
<li><a href="{{ href }}">{{ caption }}</a></li>
{% endfor %}
Expand Down
7 changes: 4 additions & 3 deletions test/cases/statement_long/input.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<ul>
{% for href, caption in [
('index.html', 'Index'),
('about.html', 'About'),
('downloads.html', 'Downloads')]
('index.html', 'Index'),
('about.html', 'About'),
('downloads.html', 'Downloads')
]
%}
<li><a href="{{ href }}">{{ caption }}</a></li>
{% endfor %}
Expand Down

0 comments on commit b2a35c0

Please sign in to comment.