diff --git a/src/djlint/formatter/indent.py b/src/djlint/formatter/indent.py index 8a8bccef5..c73553427 100644 --- a/src/djlint/formatter/indent.py +++ b/src/djlint/formatter/indent.py @@ -387,7 +387,8 @@ 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], @@ -395,7 +396,7 @@ def format_function(config: Config, html: str, match: re.Match) -> str: 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) @@ -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, diff --git a/tests/test_nunjucks/test_functions.py b/tests/test_nunjucks/test_functions.py index 26d7bd9d9..cf5fede77 100644 --- a/tests/test_nunjucks/test_functions.py +++ b/tests/test_nunjucks/test_functions.py @@ -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"