Skip to content

Commit f628d95

Browse files
authored
Add optimistic/pessimistic groups to Inspect.Algebra (#14403)
This is a simplified implementation of next_break_fits with fewer corner cases.
1 parent 93a4a0f commit f628d95

File tree

4 files changed

+202
-161
lines changed

4 files changed

+202
-161
lines changed

lib/elixir/lib/calendar/date_range.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,12 @@ defmodule Date.Range do
171171
when step < 0 and first_days < last_days,
172172
do: 0
173173

174-
defp size(%Date.Range{first_in_iso_days: first_days, last_in_iso_days: last_days, step: step}),
175-
do: abs(div(last_days - first_days, step)) + 1
174+
defp size(%Date.Range{
175+
first_in_iso_days: first_days,
176+
last_in_iso_days: last_days,
177+
step: step
178+
}),
179+
do: abs(div(last_days - first_days, step)) + 1
176180

177181
# TODO: Remove me on v2.0
178182
defp size(

lib/elixir/lib/code/formatter.ex

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -617,9 +617,12 @@ defmodule Code.Formatter do
617617
end
618618

619619
doc =
620-
with_next_break_fits(next_break_fits?(right_arg, state), right, fn right ->
621-
concat(group(left), group(nest(glue(op, group(right)), 2, :break)))
622-
end)
620+
concat(
621+
group(left),
622+
with_next_break_fits(next_break_fits?(right_arg, state), right, fn right ->
623+
nest(glue(op, right), 2, :break)
624+
end)
625+
)
623626

624627
{doc, state}
625628
end
@@ -818,9 +821,8 @@ defmodule Code.Formatter do
818821

819822
{" " <> op_string,
820823
with_next_break_fits(next_break_fits?, right, fn right ->
821-
right = nest(concat(break(), group(right)), nesting, :break)
822-
right = if eol?, do: force_unfit(right), else: right
823-
group(right)
824+
right = nest(concat(break(), right), nesting, :break)
825+
if eol?, do: force_unfit(right), else: right
824826
end)}
825827
end
826828

@@ -1262,16 +1264,15 @@ defmodule Code.Formatter do
12621264
args_doc =
12631265
if skip_parens? do
12641266
left_doc
1265-
|> concat(next_break_fits(group(right_doc, :inherit), :enabled))
1267+
|> concat(group(right_doc, :optimistic))
12661268
|> nest(:cursor, :break)
12671269
else
12681270
right_doc =
12691271
right_doc
12701272
|> nest(2, :break)
12711273
|> concat(break(""))
12721274
|> concat(")")
1273-
|> group(:inherit)
1274-
|> next_break_fits(:enabled)
1275+
|> group(:optimistic)
12751276

12761277
concat(nest(left_doc, 2, :break), right_doc)
12771278
end
@@ -1314,27 +1315,24 @@ defmodule Code.Formatter do
13141315
|> concat(args_doc)
13151316
|> nest(2)
13161317
|> concat(extra)
1317-
|> group()
13181318

13191319
skip_parens? ->
13201320
" "
13211321
|> concat(args_doc)
13221322
|> concat(extra)
1323-
|> group()
13241323

13251324
true ->
13261325
"("
13271326
|> concat(break(""))
13281327
|> nest(2, :break)
13291328
|> concat(args_doc)
13301329
|> concat(extra)
1331-
|> group()
13321330
end
13331331

13341332
if next_break_fits? do
1335-
{next_break_fits(doc, :disabled), state}
1333+
{group(doc, :pessimistic), state}
13361334
else
1337-
{doc, state}
1335+
{group(doc), state}
13381336
end
13391337
end
13401338

@@ -1800,10 +1798,17 @@ defmodule Code.Formatter do
18001798

18011799
doc =
18021800
case args do
1803-
[_ | _] -> concat_to_last_group(doc, ",")
1804-
[] when last_arg_mode == :force_comma -> concat_to_last_group(doc, ",")
1805-
[] when last_arg_mode == :next_break_fits -> next_break_fits(doc, :enabled)
1806-
[] when last_arg_mode == :none -> doc
1801+
[_ | _] ->
1802+
concat_to_last_group(doc, ",")
1803+
1804+
[] when last_arg_mode == :force_comma ->
1805+
concat_to_last_group(doc, ",")
1806+
1807+
[] when last_arg_mode == :next_break_fits ->
1808+
doc |> ungroup_if_group() |> group(:optimistic)
1809+
1810+
[] when last_arg_mode == :none ->
1811+
doc
18071812
end
18081813

18091814
{{doc, @empty, 1}, state}
@@ -2321,11 +2326,14 @@ defmodule Code.Formatter do
23212326
defp with_next_break_fits(condition, doc, fun) do
23222327
if condition do
23232328
doc
2324-
|> next_break_fits(:enabled)
2329+
|> group(:optimistic)
23252330
|> fun.()
2326-
|> next_break_fits(:disabled)
2331+
|> group(:pessimistic)
23272332
else
2328-
fun.(doc)
2333+
doc
2334+
|> group()
2335+
|> fun.()
2336+
|> group()
23292337
end
23302338
end
23312339

0 commit comments

Comments
 (0)