Skip to content

Commit 31bf5df

Browse files
author
José Valim
committed
Allow groups to inherit the parent break
1 parent 53cc4e1 commit 31bf5df

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

lib/elixir/lib/code/formatter.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ defmodule Code.Formatter do
10501050
defp call_args_to_algebra_with_no_parens_keywords(left, right, context, state) do
10511051
{left_doc, state} = args_to_algebra(left, state, &quoted_to_algebra(&1, context, &2))
10521052
{right_doc, state} = quoted_to_algebra(right, context, state)
1053-
right_doc = break(" ") |> concat(right_doc) |> group()
1053+
right_doc = break(" ") |> concat(right_doc) |> group(:inherit)
10541054

10551055
doc =
10561056
with_next_break_fits(true, right_doc, fn right_doc ->

lib/elixir/lib/inspect/algebra.ex

+13-9
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ defmodule Inspect.Algebra do
209209
quote do: {:doc_break, unquote(break), unquote(mode)}
210210
end
211211

212-
@typep doc_group :: {:doc_group, t}
213-
defmacrop doc_group(group) do
214-
quote do: {:doc_group, unquote(group)}
212+
@typep doc_group :: {:doc_group, t, :inherit | :self}
213+
defmacrop doc_group(group, mode) do
214+
quote do: {:doc_group, unquote(group), unquote(mode)}
215215
end
216216

217217
@typep doc_fits :: {:doc_fits, t, :enabled | :disabled}
@@ -695,8 +695,8 @@ defmodule Inspect.Algebra do
695695
696696
"""
697697
@spec group(t) :: doc_group
698-
def group(doc) when is_doc(doc) do
699-
doc_group(doc)
698+
def group(doc, mode \\ :self) when is_doc(doc) do
699+
doc_group(doc, mode)
700700
end
701701

702702
@doc ~S"""
@@ -812,12 +812,12 @@ defmodule Inspect.Algebra do
812812
defp fits?(_, _, []), do: true
813813

814814
defp fits?(w, k, [{i, _, doc_fits(x, :disabled)} | t]), do: fits?(w, k, [{i, :no_fitting, x} | t])
815-
defp fits?(w, k, [{i, :no_fitting, doc_group(x)} | t]), do: fits?(w, k, [{i, :no_fitting, x} | t])
815+
defp fits?(w, k, [{i, :no_fitting, doc_group(x, _)} | t]), do: fits?(w, k, [{i, :no_fitting, x} | t])
816816
defp fits?(w, k, [{i, :no_fitting, doc_fits(x, _)} | t]), do: fits?(w, k, [{i, :no_fitting, x} | t])
817817

818818
defp fits?(w, k, [{i, _, doc_fits(x, :enabled)} | t]), do: fits?(w, k, [{i, :next_fits, x} | t])
819819
defp fits?(w, k, [{i, :next_fits, doc_force(x)} | t]), do: fits?(w, k, [{i, :next_fits, x} | t])
820-
defp fits?(w, k, [{i, :next_fits, doc_group(x)} | t]), do: fits?(w, k, [{i, :next_fits, x} | t])
820+
defp fits?(w, k, [{i, :next_fits, doc_group(x, _)} | t]), do: fits?(w, k, [{i, :next_fits, x} | t])
821821
defp fits?(_, _, [{_, :next_fits, doc_break(_, _)} | _]), do: true
822822
defp fits?(_, _, [{_, :next_fits, :doc_line} | _]), do: true
823823

@@ -828,7 +828,7 @@ defmodule Inspect.Algebra do
828828
defp fits?(w, k, [{i, m, doc_color(x, _)} | t]), do: fits?(w, k, [{i, m, x} | t])
829829
defp fits?(w, k, [{i, m, doc_nest(x, _, :break)} | t]), do: fits?(w, k, [{i, m, x} | t])
830830
defp fits?(w, k, [{i, m, doc_nest(x, j, _)} | t]), do: fits?(w, k, [{apply_nesting(i, k, j), m, x} | t])
831-
defp fits?(w, k, [{i, _, doc_group(x)} | t]), do: fits?(w, k, [{i, :flat, x} | t])
831+
defp fits?(w, k, [{i, _, doc_group(x, _)} | t]), do: fits?(w, k, [{i, :flat, x} | t])
832832
defp fits?(w, k, [{_, _, doc_string(_, l)} | t]), do: fits?(w, k + l, t)
833833
defp fits?(w, k, [{_, _, s} | t]) when is_binary(s), do: fits?(w, k + byte_size(s), t)
834834
defp fits?(_, _, [{_, _, doc_force(_)} | _]), do: false
@@ -877,7 +877,11 @@ defmodule Inspect.Algebra do
877877
end
878878

879879
# Groups must do the fitting decision.
880-
defp format(w, k, [{i, _, doc_group(x)} | t]) do
880+
defp format(w, k, [{i, :break, doc_group(x, :inherit)} | t]) do
881+
format(w, k, [{i, :break, x} | t])
882+
end
883+
884+
defp format(w, k, [{i, _, doc_group(x, _)} | t]) do
881885
if w == :infinity or fits?(w, k, [{i, :flat, x}]) do
882886
format(w, k, [{i, :flat, x} | t])
883887
else

lib/elixir/test/elixir/code_formatter/general_test.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ bar)
678678
"""
679679
end
680680

681-
test "as funciton arguments" do
681+
test "as function arguments" do
682682
assert_same """
683683
fun(
684684
(

lib/elixir/test/elixir/code_formatter/integration_test.exs

+12
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,16 @@ defmodule Code.Formatter.IntegrationTest do
180180
end)
181181
"""
182182
end
183+
184+
test "long comprehension" do
185+
assert_same """
186+
for %{app: app, opts: opts, top_level: true} <- Mix.Dep.cached(),
187+
Keyword.get(opts, :app, true),
188+
Keyword.get(opts, :runtime, true),
189+
not Keyword.get(opts, :optional, false),
190+
app not in included_applications,
191+
app not in included_applications,
192+
do: app
193+
"""
194+
end
183195
end

0 commit comments

Comments
 (0)