Skip to content

Commit 5158843

Browse files
committed
Respect attributes newlines if written as such
1 parent b194e3c commit 5158843

File tree

2 files changed

+45
-37
lines changed

2 files changed

+45
-37
lines changed

lib/phoenix_live_view/html_algebra.ex

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,7 @@ defmodule Phoenix.LiveView.HTMLAlgebra do
243243
end
244244

245245
group =
246-
concat([
247-
"<#{name}",
248-
build_attrs(attrs, "", context.attr_formatters, context.opts),
249-
">",
250-
doc,
251-
"</#{name}>"
252-
])
246+
concat([format_tag_open(name, attrs, context), doc, "</#{name}>"])
253247
|> group()
254248

255249
{:block, group}
@@ -308,21 +302,11 @@ defmodule Phoenix.LiveView.HTMLAlgebra do
308302

309303
defp to_algebra({:tag_self_close, name, attrs}, context) do
310304
doc =
311-
case attrs do
312-
[attr] ->
313-
concat([
314-
"<#{name} ",
315-
render_attribute(attr, context.attr_formatters, context.opts),
316-
" />"
317-
])
318-
319-
attrs ->
320-
concat([
321-
"<#{name}",
322-
build_attrs(attrs, " ", context.attr_formatters, context.opts),
323-
"/>"
324-
])
325-
end
305+
concat([
306+
"<#{name}",
307+
build_attrs(attrs, " ", context.attr_formatters, context.opts),
308+
"/>"
309+
])
326310

327311
{:inline, group(doc)}
328312
end
@@ -438,18 +422,32 @@ defmodule Phoenix.LiveView.HTMLAlgebra do
438422

439423
defp build_attrs([], on_break, _formatters, _opts), do: on_break
440424

425+
defp build_attrs([attr], on_break, formatters, opts) do
426+
concat([" ", render_attribute(attr, formatters, opts), on_break])
427+
end
428+
441429
defp build_attrs(attrs, on_break, formatters, opts) do
442-
attrs
443-
|> Enum.sort_by(&attrs_sorter/1)
444-
|> Enum.reduce(
445-
empty(),
446-
&concat([&2, break(" "), render_attribute(&1, formatters, opts)])
447-
)
448-
|> nest(2)
449-
|> concat(break(on_break))
450-
|> group()
430+
doc =
431+
attrs
432+
|> Enum.sort_by(&attrs_sorter/1)
433+
|> Enum.reduce(
434+
empty(),
435+
&concat([&2, break(" "), render_attribute(&1, formatters, opts)])
436+
)
437+
|> nest(2)
438+
|> concat(break(on_break))
439+
440+
if distinct_lines?(attrs, -1) do
441+
doc |> force_unfit() |> group()
442+
else
443+
group(doc)
444+
end
451445
end
452446

447+
defp distinct_lines?([{_, _, %{line: line}} | _], line), do: false
448+
defp distinct_lines?([{_, _, %{line: line}} | tail], _line), do: distinct_lines?(tail, line)
449+
defp distinct_lines?([], _line), do: true
450+
453451
@attrs_order %{
454452
":let" => 1,
455453
":for" => 2,
@@ -466,9 +464,6 @@ defmodule Phoenix.LiveView.HTMLAlgebra do
466464
end
467465
end
468466

469-
defp format_tag_open(name, [attr], context),
470-
do: concat(["<#{name} ", render_attribute(attr, context.attr_formatters, context.opts), ">"])
471-
472467
defp format_tag_open(name, attrs, context),
473468
do: concat(["<#{name}", build_attrs(attrs, "", context.attr_formatters, context.opts), ">"])
474469

test/phoenix_live_view/html_formatter_test.exs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,21 @@ defmodule Phoenix.LiveView.HTMLFormatterTest do
294294
assert_formatter_doesnt_change(input)
295295
end
296296

297+
test "keep attributes in separate lines if written as such" do
298+
input = """
299+
<Component
300+
foo="..."
301+
bar="..."
302+
baz="..."
303+
qux="..."
304+
>
305+
Foo
306+
</Component>
307+
"""
308+
309+
assert_formatter_doesnt_change(input)
310+
end
311+
297312
test "break attributes into multiple lines in case it doesn't fit 98 characters (default)" do
298313
input = """
299314
<div foo="..........." bar="....................." baz="................." qux="....................">
@@ -1882,9 +1897,7 @@ defmodule Phoenix.LiveView.HTMLFormatterTest do
18821897
<script phx-no-format><%= raw(js_code()) %></script>
18831898
""",
18841899
"""
1885-
<script
1886-
phx-no-format
1887-
><%= raw(js_code()) %></script>
1900+
<script phx-no-format><%= raw(js_code()) %></script>
18881901
""",
18891902
line_length: 5
18901903
)

0 commit comments

Comments
 (0)