Skip to content

Commit 1202ee3

Browse files
Fix formatting of multi-line expressions in template
1 parent f2e47f4 commit 1202ee3

3 files changed

Lines changed: 103 additions & 4 deletions

File tree

ruff_cgx/template_formatter.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import textwrap
23

34
from collagraph.sfc.parser import Comment, TextElement
45

@@ -156,7 +157,7 @@ def sort_attr(attr):
156157
return f"{len(SORTING)}{attr}"
157158

158159

159-
def format_attribute(key, value):
160+
def format_attribute(key, value, indent="", single_attribute=True):
160161
"""
161162
Format an attribute key-value pair.
162163
@@ -176,6 +177,9 @@ def format_attribute(key, value):
176177
return f'{key}="{formatted_value}"'
177178
else:
178179
formatted_value = format_python_expression(value)
180+
formatted_value = textwrap.indent(
181+
formatted_value, indent + ("" if single_attribute else INDENT)
182+
).lstrip()
179183
return f'{key}="{formatted_value}"'
180184

181185
return f'{key}="{value}"'
@@ -194,12 +198,14 @@ def format_node(node, depth: int) -> list[str]:
194198
if node.attrs:
195199
if len(node.attrs) == 1:
196200
key, val = next(iter(node.attrs.items()))
197-
attr = format_attribute(key, val)
201+
attr = format_attribute(key, val, indent, single_attribute=True)
198202
start = f"{start} {attr}"
199203
else:
200204
attrs = []
201205
for key in sorted(node.attrs, key=sort_attr):
202-
attr = format_attribute(key, node.attrs[key])
206+
attr = format_attribute(
207+
key, node.attrs[key], indent, single_attribute=False
208+
)
203209
attrs.append(f"{indent}{INDENT}{attr}")
204210

205211
start = "\n".join([start, *attrs])

ruff_cgx/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,8 @@ def run_ruff_format(
291291
if use_single_quotes:
292292
config_file = Path(directory) / "ruff.toml"
293293
config_file.write_text(
294-
'[format]\nquote-style = "single"\n', encoding="utf-8"
294+
'indent-width = 2\n[format]\nquote-style = "single"\n',
295+
encoding="utf-8",
295296
)
296297
ruff_command.extend(["--config", str(config_file)])
297298

tests/test_formatter.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,3 +678,95 @@ class Meta(cg.Component):
678678
).lstrip()
679679

680680
assert formatted == expected
681+
682+
683+
def test_format_multiline_dict_template():
684+
content = textwrap.dedent(
685+
"""
686+
<template>
687+
<item :value="{'foo': 'foo', 'bar': 'bar', 'test': some_really_long_name_such_that_it_should_wrap()}" />
688+
</template>
689+
690+
<script>
691+
import collagraph as cg
692+
693+
694+
class Test(cg.Component):
695+
def some_really_long_name_such_that_it_should_wrap(self):
696+
return 2
697+
</script>
698+
"""
699+
).lstrip()
700+
701+
formatted = format_cgx_content(content)
702+
expected = textwrap.dedent(
703+
"""
704+
<template>
705+
<item :value="{
706+
'foo': 'foo',
707+
'bar': 'bar',
708+
'test': some_really_long_name_such_that_it_should_wrap(),
709+
}" />
710+
</template>
711+
712+
<script>
713+
import collagraph as cg
714+
715+
716+
class Test(cg.Component):
717+
def some_really_long_name_such_that_it_should_wrap(self):
718+
return 2
719+
</script>
720+
"""
721+
).lstrip()
722+
723+
assert formatted == expected
724+
725+
726+
def test_format_multiline_dict_template_multi_attrs():
727+
content = textwrap.dedent(
728+
"""
729+
<template>
730+
<wrapper>
731+
<item :value="{'foo': 'foo','bar': 'bar','test':'some_really_long_name_such_that_it_should_wrap'}" :foo="foo" :bar="bar" />
732+
</wrapper>
733+
</template>
734+
735+
<script>
736+
import collagraph as cg
737+
738+
739+
class Item(cg.Component):
740+
pass
741+
</script>
742+
"""
743+
).lstrip()
744+
745+
formatted = format_cgx_content(content)
746+
expected = textwrap.dedent(
747+
"""
748+
<template>
749+
<wrapper>
750+
<item
751+
:bar="bar"
752+
:foo="foo"
753+
:value="{
754+
'foo': 'foo',
755+
'bar': 'bar',
756+
'test': 'some_really_long_name_such_that_it_should_wrap',
757+
}"
758+
/>
759+
</wrapper>
760+
</template>
761+
762+
<script>
763+
import collagraph as cg
764+
765+
766+
class Item(cg.Component):
767+
pass
768+
</script>
769+
"""
770+
).lstrip()
771+
772+
assert formatted == expected

0 commit comments

Comments
 (0)