From 4973a32466cf945f67bac5d8da2649fd41eff6f4 Mon Sep 17 00:00:00 2001 From: Timo Ludwig Date: Mon, 8 May 2023 23:04:31 +0200 Subject: [PATCH 1/2] feat(formatter): Add option to not condense multi-line-tags --- docs/src/_data/configuration.json | 23 +++++ src/djlint/__init__.py | 7 ++ src/djlint/formatter/condense.py | 13 ++- src/djlint/formatter/expand.py | 2 +- src/djlint/settings.py | 7 ++ .../test_line_break_after_multiline_tag.py | 95 +++++++++++++++++++ 6 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 tests/test_html/test_line_break_after_multiline_tag.py diff --git a/docs/src/_data/configuration.json b/docs/src/_data/configuration.json index 28e5f55b2..aac12ed9f 100644 --- a/docs/src/_data/configuration.json +++ b/docs/src/_data/configuration.json @@ -252,6 +252,29 @@ } ] }, + { + "name": "line_break_after_multiline_tag", + "tags": ["formatter"], + "description": { + "en": "Do not condense the content of multi-line tags into the line of the last attribute.", + "ru": "Не сжимайте содержимое многострочных тегов в строку последнего атрибута.", + "fr": "Ne pas condenser le contenu des balises multilignes dans la ligne du dernier attribut." + }, + "usage": [ + { + "name": "pyproject.toml", + "value": "line_break_after_multiline_tag=true" + }, + { + "name": ".djlintrc", + "value": "\"line_break_after_multiline_tag\": \"true\"" + }, + { + "name": "cli", + "value": "--line-break-after-multiline-tag" + } + ] + }, { "name": "profile", "tags": ["linter", "formatter"], diff --git a/src/djlint/__init__.py b/src/djlint/__init__.py index e25c946c9..88021a508 100644 --- a/src/djlint/__init__.py +++ b/src/djlint/__init__.py @@ -157,6 +157,11 @@ default="", help="Add an additional blank line before {% ... %} tag groups.", ) +@click.option( + "--line-break-after-multiline-tag", + is_flag=True, + help="Do not condense the content of multi-line tags into the line of the last attribute.", +) @click.option( "--custom-blocks", type=str, @@ -247,6 +252,7 @@ def main( ignore_blocks: str, blank_line_after_tag: str, blank_line_before_tag: str, + line_break_after_multiline_tag: bool, custom_blocks: str, custom_html: str, exclude: str, @@ -284,6 +290,7 @@ def main( ignore_blocks=ignore_blocks, blank_line_after_tag=blank_line_after_tag, blank_line_before_tag=blank_line_before_tag, + line_break_after_multiline_tag=line_break_after_multiline_tag, custom_blocks=custom_blocks, custom_html=custom_html, exclude=exclude, diff --git a/src/djlint/formatter/condense.py b/src/djlint/formatter/condense.py index 77de4d262..c81aa6269 100644 --- a/src/djlint/formatter/condense.py +++ b/src/djlint/formatter/condense.py @@ -134,12 +134,17 @@ def condense_html(html, config): def condense_line(config: Config, html: str, match: re.Match) -> str: """Put contents on a single line if below max line length.""" + # match.group(1) contains the opening tag, which may be split over multiple lines + if config.line_break_after_multiline_tag: + combined_length = len(match.group(1) + match.group(3) + match.group(4)) + else: + combined_length = len( + match.group(1).splitlines()[-1] + match.group(3) + match.group(4) + ) + if ( not inside_ignored_block(config, html, match) - and ( - len(match.group(1).splitlines()[-1] + match.group(3) + match.group(4)) - < config.max_line_length - ) + and combined_length < config.max_line_length and if_blank_line_after_match(config, match.group(3)) and if_blank_line_before_match(config, match.group(3)) ): diff --git a/src/djlint/formatter/expand.py b/src/djlint/formatter/expand.py index d661dcfcd..f435143f3 100644 --- a/src/djlint/formatter/expand.py +++ b/src/djlint/formatter/expand.py @@ -20,7 +20,7 @@ def add_html_line(out_format: str, match: re.Match) -> str: Do not add whitespace if the tag is in a non indent block. - Do not add whiatespace if the tag is a in a template block + Do not add whitespace if the tag is a in a template block """ if inside_ignored_block(config, html, match): return match.group(1) diff --git a/src/djlint/settings.py b/src/djlint/settings.py index 2c6f8a680..73756c7f5 100644 --- a/src/djlint/settings.py +++ b/src/djlint/settings.py @@ -237,6 +237,7 @@ def __init__( custom_blocks: str = "", blank_line_after_tag: str = "", blank_line_before_tag: str = "", + line_break_after_multiline_tag: bool = False, custom_html: str = "", exclude: str = "", extend_exclude: str = "", @@ -430,6 +431,12 @@ def __init__( str ] = blank_line_before_tag or djlint_settings.get("blank_line_before_tag", None) + # add line break after multi-line tags + self.line_break_after_multiline_tag: bool = ( + line_break_after_multiline_tag + or djlint_settings.get("line_break_after_multiline_tag", False) + ) + # contents of tags will not be formatted self.ignored_block_opening: str = r"""