Skip to content

Commit

Permalink
fix(formatter): function index was not formatted correctly
Browse files Browse the repository at this point in the history
closes #693
  • Loading branch information
christopherpickering committed Jun 26, 2023
1 parent 6d63a6a commit a197200
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/djlint/formatter/indent.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,15 +387,16 @@ def format_function(config: Config, html: str, match: re.Match) -> str:
leading_space = match.group(1)
open_bracket = match.group(2)
tag = match.group(3).strip()
close_bracket = match.group(5)
index = (match.group(5) or "").strip()
close_bracket = match.group(6)
contents = format_data(
config,
match.group(4).strip()[1:-1],
len(f"{open_bracket} {tag}() {close_bracket}"),
leading_space,
)

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

if config.no_set_formatting is False:
func = partial(format_set, config, beautified_code)
Expand All @@ -414,7 +415,7 @@ def format_function(config: Config, html: str, match: re.Match) -> str:
# format function contents
beautified_code = re.sub(
re.compile(
r"([ ]*)({{-?\+?)[ ]*?((?:(?!}}).)*?\w)(\((?:\"[^\"]*\"|'[^']*'|[^\)])*?\)[ ]*)((?:(?!}}).)*?-?\+?}})",
r"([ ]*)({{-?\+?)[ ]*?((?:(?!}}).)*?\w)(\((?:\"[^\"]*\"|'[^']*'|[^\)])*?\)[ ]*)((?:\[[^\]]*?\]|\.\d+)[ ]*)?((?:(?!}}).)*?-?\+?}})",
flags=re.IGNORECASE | re.MULTILINE | re.VERBOSE | re.DOTALL,
),
func,
Expand Down
6 changes: 6 additions & 0 deletions tests/test_nunjucks/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
({}),
id="test quoting",
),
pytest.param(
('{{ item.split("/")[1] }}\n' '{{ item.split("/").123 }}'),
('{{ item.split("/")[1] }}\n' '{{ item.split("/").123 }}\n'),
({}),
id="test index",
),
pytest.param(
(
"{{ myfunc({\n"
Expand Down

0 comments on commit a197200

Please sign in to comment.