Skip to content

Commit

Permalink
fix(formatter): fix cases where ignore was still formatting set and f…
Browse files Browse the repository at this point in the history
…unction code

closes #659
  • Loading branch information
christopherpickering committed May 22, 2023
1 parent 9b9bd3f commit ab878ea
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/djlint/formatter/indent.py
Expand Up @@ -339,7 +339,10 @@ def format_data(config: Config, contents: str, tag_size: int, leading_space) ->
contents = contents.strip()
return (f"\n{leading_space}").join(contents.splitlines())

def format_set(config: Config, match: re.Match) -> str:
def format_set(config: Config, html: str, match: re.Match) -> str:
if inside_ignored_block(config, html, match):
return match.group()

leading_space = match.group(1)
open_bracket = match.group(2)
tag = match.group(3)
Expand All @@ -361,7 +364,9 @@ def format_set(config: Config, match: re.Match) -> str:

return f"{leading_space}{open_bracket} {tag} {contents} {close_bracket}"

def format_function(config: Config, match: re.Match) -> str:
def format_function(config: Config, html: str, match: re.Match) -> str:
if inside_ignored_block(config, html, match):
return match.group()
leading_space = match.group(1)
open_bracket = match.group(2)
tag = match.group(3).strip()
Expand All @@ -375,7 +380,7 @@ def format_function(config: Config, match: re.Match) -> str:

return f"{leading_space}{open_bracket} {tag}({contents}) {close_bracket}"

func = partial(format_set, config)
func = partial(format_set, config, beautified_code)
# format set contents
beautified_code = re.sub(
re.compile(
Expand All @@ -386,7 +391,7 @@ def format_function(config: Config, match: re.Match) -> str:
beautified_code,
)

func = partial(format_function, config)
func = partial(format_function, config, beautified_code)
# format function contents
beautified_code = re.sub(
re.compile(
Expand Down
17 changes: 17 additions & 0 deletions tests/test_nunjucks/test_macros.py
Expand Up @@ -13,6 +13,23 @@
("{% macro 'cool' %}\n" " <div>some html</div>\n" "{% endmacro %}\n"),
id="macro_tag",
),
pytest.param(
(
"<ul>\n"
" {# djlint:off #}\n"
" <li>{{foo(1)}}</li>\n"
" {# djlint:on #}\n"
"</ul>"
),
(
"<ul>\n"
" {# djlint:off #}\n"
" <li>{{foo(1)}}</li>\n"
" {# djlint:on #}\n"
"</ul>\n"
),
id="ignored code should not be touched",
),
]


Expand Down
18 changes: 18 additions & 0 deletions tests/test_nunjucks/test_set.py
Expand Up @@ -139,6 +139,24 @@
({}),
id="set block",
),
pytest.param(
(
"<ul>\n"
" {# djlint:off #}\n"
" <li>{%set a=[{'x':1}]%}</li>\n"
" {# djlint:on #}\n"
"</ul>"
),
(
"<ul>\n"
" {# djlint:off #}\n"
" <li>{%set a=[{'x':1}]%}</li>\n"
" {# djlint:on #}\n"
"</ul>\n"
),
({"max_line_length": 1}),
id="ignored code should not be touched",
),
]


Expand Down

0 comments on commit ab878ea

Please sign in to comment.