Skip to content

Commit a8e4fcd

Browse files
author
José Valim
committed
Reduce limit as we traverse collections on inspect
1 parent a70ca83 commit a8e4fcd

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

lib/elixir/lib/inspect/algebra.ex

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -453,21 +453,23 @@ defmodule Inspect.Algebra do
453453
"..."
454454
end
455455

456-
defp do_surround_many([h], _limit, opts, fun, _sep) do
457-
fun.(h, opts)
456+
defp do_surround_many([h], limit, opts, fun, _sep) do
457+
fun.(h, %{opts | limit: limit})
458458
end
459459

460460
defp do_surround_many([h|t], limit, opts, fun, sep) when is_list(t) do
461+
limit = decrement(limit)
461462
glue(
462-
concat(fun.(h, opts), sep),
463-
do_surround_many(t, decrement(limit), opts, fun, sep)
463+
concat(fun.(h, %{opts | limit: limit}), sep),
464+
do_surround_many(t, limit, opts, fun, sep)
464465
)
465466
end
466467

467-
defp do_surround_many([h|t], _limit, opts, fun, _sep) do
468+
defp do_surround_many([h|t], limit, opts, fun, _sep) do
469+
limit = decrement(limit)
468470
glue(
469-
concat(fun.(h, opts), @tail_separator),
470-
fun.(t, opts)
471+
concat(fun.(h, %{opts | limit: limit}), @tail_separator),
472+
fun.(t, %{opts | limit: limit})
471473
)
472474
end
473475

lib/elixir/test/elixir/inspect_test.exs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,19 @@ defmodule Inspect.ListTest do
190190
assert inspect([{:b, 1}, {:a, 1}]) == "[b: 1, a: 1]"
191191
end
192192

193-
test :unproper do
193+
test :improper do
194194
assert inspect([:foo | :bar]) == "[:foo | :bar]"
195195

196196
assert inspect([1,2,3,4,5|42], [pretty: true, width: 1]) == "[1,\n 2,\n 3,\n 4,\n 5 |\n 42]"
197197
end
198198

199+
test :nested do
200+
assert inspect(Enum.reduce(1..100, [0], &[&2 , Integer.to_string(&1)]), [limit: 5]) ==
201+
"[[[[[[...], ...], \"97\"], \"98\"], \"99\"], \"100\"]"
202+
assert inspect(Enum.reduce(1..100, [0], &[&2 | Integer.to_string(&1)]), [limit: 5]) ==
203+
"[[[[[[...] | \"96\"] | \"97\"] | \"98\"] | \"99\"] | \"100\"]"
204+
end
205+
199206
test :codepoints do
200207
assert inspect('é') == "[233]"
201208
end

0 commit comments

Comments
 (0)