Skip to content

Commit

Permalink
Formatter - Keep intentional newlines when formatting comments
Browse files Browse the repository at this point in the history
This PR improves the HTML formatter so that it keeps at least one line
(which usually are lines we want to keep intentionally) when formatting
comments.
  • Loading branch information
feliperenan committed Oct 29, 2024
1 parent 4d3def7 commit ca3f7a7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
38 changes: 29 additions & 9 deletions lib/phoenix_live_view/html_algebra.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,37 @@ defmodule Phoenix.LiveView.HTMLAlgebra do
concat(doc, next_doc)
end)

force_unfit? =
Enum.any?(block, fn
{:text, text, %{newlines: newlines}} -> newlines > 0 or String.contains?(text, "\n")
_ -> false
{force_unfit?, meta} =
Enum.find_value(block, fn
{:text, text, meta} ->
{String.contains?(text, "\n"), meta}

_ ->
{false, %{}}
end)

if force_unfit? do
concat |> force_unfit() |> group()
else
concat |> group()
end
doc =
if force_unfit? do
concat |> force_unfit() |> group()
else
concat |> group()
end

newline_or_empty_before_text =
if meta[:newlines_before_text] && meta[:newlines_before_text] > 1 do
line()
else
empty()
end

newline_or_empty_after_text =
if meta[:newlines_after_text] && meta[:newlines_after_text] > 1 do
line()
else
empty()
end

concat([newline_or_empty_before_text, doc, newline_or_empty_after_text])
end

defp block_to_algebra([head | tail], context) do
Expand Down
7 changes: 6 additions & 1 deletion lib/phoenix_live_view/html_formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,12 @@ defmodule Phoenix.LiveView.HTMLFormatter do
stack,
source
) do
to_tree(tokens, [{:html_comment, [{:text, String.trim(text), %{}}]} | buffer], stack, source)
meta = %{
newlines_before_text: count_newlines_until_text(text, 0),
newlines_after_text: text |> String.reverse() |> count_newlines_until_text(0)
}

to_tree(tokens, [{:html_comment, [{:text, String.trim(text), meta}]} | buffer], stack, source)
end

defp to_tree([{:text, text, _meta} | tokens], buffer, stack, source) do
Expand Down
36 changes: 36 additions & 0 deletions test/phoenix_live_view/html_formatter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2100,6 +2100,42 @@ defmodule Phoenix.LiveView.HTMLFormatterTest do
""")
end

test "keep intentional lines breaks from HTML comments" do
assert_formatter_doesnt_change("""
<h1>Title</h1>
<!-- comment -->
<p>Text</p>
""")

assert_formatter_doesnt_change("""
<h1>Title</h1>
<!-- comment -->
<p>Text</p>
""")

assert_formatter_output(
"""
<h1>Title</h1>
<!-- comment -->
<p>Text</p>
""",
"""
<h1>Title</h1>
<!-- comment -->
<p>Text</p>
"""
)
end

# TODO: Remove this `if` when we require Elixir 1.14+
if function_exported?(EEx, :tokenize, 2) do
test "handle EEx comments" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ defmodule Phoenix.LiveView.Integrations.HTMLFormatterTest do
</td>
<% end %>
</section>
<!-- comment -->
<div>
<p>Hello</p>
Expand Down

0 comments on commit ca3f7a7

Please sign in to comment.