From 9ed3393be09e4b4dfd625f021fa85138ea937861 Mon Sep 17 00:00:00 2001 From: Christopher Pickering Date: Tue, 26 Jul 2022 08:12:03 -0500 Subject: [PATCH] fix(preserve blank lines): fixed duplicate blank lines being inserted after h-space, Fixed extra blank lines being added to beginning of file closes #300 --- src/djlint/formatter/expand.py | 10 +++++-- .../test_preserve_blank_lines/html_one.html | 3 ++ .../test_preserve_blank_lines/html_two.html | 1 + .../test_preserve_blank_lines/test_config.py | 28 ++++++++++++++++++- 4 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 tests/test_config/test_preserve_blank_lines/html_one.html create mode 100644 tests/test_config/test_preserve_blank_lines/html_two.html diff --git a/src/djlint/formatter/expand.py b/src/djlint/formatter/expand.py index 85470f9..bd2afad 100644 --- a/src/djlint/formatter/expand.py +++ b/src/djlint/formatter/expand.py @@ -23,6 +23,9 @@ def expand_html(html: str, config: Config) -> str: if inside_ignored_block(config, html, match): return match.group(1) + if out_format == "\n%s" and match.start() == 0: + return match.group(1) + return out_format % match.group(1) html_tags = config.break_html_tags @@ -45,7 +48,7 @@ def expand_html(html: str, config: Config) -> str: # html tags - break after html = re.sub( re.compile( - rf"({{}}])*>)(?=[^\n])", + rf"({{}}])*>)(?!\s*?\n)(?=[^\n])", flags=re.IGNORECASE | re.VERBOSE, ), add_right, @@ -54,7 +57,7 @@ def expand_html(html: str, config: Config) -> str: # template tag breaks def should_i_move_template_tag(out_format: str, match: re.Match) -> str: - # ensure template tag is not inside an html tag + # ensure template tag is not inside an html tag and also not the first line of the file if inside_ignored_block(config, html, match): return match.group(1) @@ -68,13 +71,14 @@ def expand_html(html: str, config: Config) -> str: html[: match.end()], re.MULTILINE | re.VERBOSE, ): + if out_format == "\n%s" and match.start() == 0: + return match.group(1) return out_format % match.group(1) return match.group(1) # template tags # break before - html = re.sub( re.compile( break_char diff --git a/tests/test_config/test_preserve_blank_lines/html_one.html b/tests/test_config/test_preserve_blank_lines/html_one.html new file mode 100644 index 0000000..c5ac12b --- /dev/null +++ b/tests/test_config/test_preserve_blank_lines/html_one.html @@ -0,0 +1,3 @@ +{% block someblock %}{% endblock %} +
+
diff --git a/tests/test_config/test_preserve_blank_lines/html_two.html b/tests/test_config/test_preserve_blank_lines/html_two.html new file mode 100644 index 0000000..e54a273 --- /dev/null +++ b/tests/test_config/test_preserve_blank_lines/html_two.html @@ -0,0 +1 @@ +
diff --git a/tests/test_config/test_preserve_blank_lines/test_config.py b/tests/test_config/test_preserve_blank_lines/test_config.py index 2381d69..0f812c6 100644 --- a/tests/test_config/test_preserve_blank_lines/test_config.py +++ b/tests/test_config/test_preserve_blank_lines/test_config.py @@ -5,7 +5,7 @@ run:: pytest tests/test_config/test_preserve_blank_lines/test_config.py --cov=src/djlint --cov-branch \ --cov-report xml:coverage.xml --cov-report term-missing - pytest tests/test_config/test_preserve_blank_lines/test_config.py::test_config + pytest tests/test_config/test_preserve_blank_lines/test_config.py::test_whitespace """ # pylint: disable=C0116 @@ -27,3 +27,29 @@ def test_config(runner: CliRunner) -> None: ) assert result.exit_code == 0 + + +def test_whitespace(runner: CliRunner) -> None: + # blank line should not be added before template tags + result = runner.invoke( + djlint, + [ + "tests/test_config/test_preserve_blank_lines/html_one.html", + "--check", + "--preserve-blank-lines", + ], + ) + + assert result.exit_code == 0 + + # blank line should not be added before html tags + result = runner.invoke( + djlint, + [ + "tests/test_config/test_preserve_blank_lines/html_two.html", + "--check", + "--preserve-blank-lines", + ], + ) + print(result.output) + assert result.exit_code == 0