Skip to content

Commit 9a162b5

Browse files
author
José Valim
committed
Inspect.Algebra.surround_many/6 now expects Inspect.Opts
1 parent a2ddb6a commit 9a162b5

File tree

3 files changed

+66
-22
lines changed

3 files changed

+66
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
* Deprecations
2424
* [IEx] IEx color configuration expects a list of atoms instead of a string with colors separated by comma
25+
* [Inspect] `Inspect.Algebra.surround_many/6` now expects Inspect.Opts instead of an integer limit
2526
* [Inspect] `Inspect.Algebra.pretty/2` is deprecated in favor of `Inspect.Algebra.format/2` that instead returns iodata. This function was used only by documentation examples and it is unlikely to affect actual code
2627
* [IO] `IO.ANSI.terminal?` is deprecated in favor of `IO.ANSI.enabled?`
2728
* [IO] `IO.ANSI.escape/2` and `IO.ANSI.escape_fragment/2` is deprecated in favor of `IO.ANSI.format/2` and `IO.ANSI.format_fragment/2`

lib/elixir/lib/inspect.ex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,9 @@ defimpl Inspect, for: List do
297297
lists == :as_char_lists or (lists == :infer and printable?(thing)) ->
298298
<< ?', Inspect.BitString.escape(IO.chardata_to_string(thing), ?') :: binary, ?' >>
299299
keyword?(thing) ->
300-
surround_many("[", thing, "]", opts.limit, &keyword(&1, opts))
300+
surround_many("[", thing, "]", opts, &keyword/2)
301301
true ->
302-
surround_many("[", thing, "]", opts.limit, &to_doc(&1, opts))
302+
surround_many("[", thing, "]", opts, &to_doc/2)
303303
end
304304
end
305305

@@ -346,7 +346,7 @@ defimpl Inspect, for: Tuple do
346346
def inspect({}, _opts), do: "{}"
347347

348348
def inspect(tuple, opts) do
349-
surround_many("{", Tuple.to_list(tuple), "}", opts.limit, &to_doc(&1, opts))
349+
surround_many("{", Tuple.to_list(tuple), "}", opts, &to_doc/2)
350350
end
351351
end
352352

@@ -357,14 +357,14 @@ defimpl Inspect, for: Map do
357357

358358
def inspect(map, name, opts) do
359359
map = :maps.to_list(map)
360-
surround_many("%" <> name <> "{", map, "}", opts.limit, traverse_fun(map, opts))
360+
surround_many("%" <> name <> "{", map, "}", opts, traverse_fun(map))
361361
end
362362

363-
defp traverse_fun(list, opts) do
363+
defp traverse_fun(list) do
364364
if Inspect.List.keyword?(list) do
365-
&Inspect.List.keyword(&1, opts)
365+
&Inspect.List.keyword/2
366366
else
367-
&to_map(&1, opts)
367+
&to_map/2
368368
end
369369
end
370370

lib/elixir/lib/inspect/algebra.ex

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -406,52 +406,95 @@ defmodule Inspect.Algebra do
406406
end
407407

408408
@doc ~S"""
409-
Maps and glues a collection of items together using the given separator
410-
and surrounds them. A limit can be passed which, once reached, stops
411-
gluing and outputs "..." instead.
409+
Maps and glues a collection of items.
410+
411+
It uses the given left and right as surrounding and a separator for
412+
each item. A limit can be passed which, once reached, stops gluing
413+
and outputs "..." instead.
412414
413415
## Examples
414416
415-
iex> doc = Inspect.Algebra.surround_many("[", Enum.to_list(1..5), "]", :infinity, &Integer.to_string(&1))
417+
iex> doc = Inspect.Algebra.surround_many("[", Enum.to_list(1..5), "]",
418+
...> %Inspect.Opts{limit: :infinity}, fn i, _opts -> to_string(i) end)
416419
iex> Inspect.Algebra.format(doc, 5) |> IO.iodata_to_binary
417420
"[1,\n 2,\n 3,\n 4,\n 5]"
418421
419-
iex> doc = Inspect.Algebra.surround_many("[", Enum.to_list(1..5), "]", 3, &Integer.to_string(&1))
422+
iex> doc = Inspect.Algebra.surround_many("[", Enum.to_list(1..5), "]",
423+
...> %Inspect.Opts{limit: 3}, fn i, _opts -> to_string(i) end)
420424
iex> Inspect.Algebra.format(doc, 20) |> IO.iodata_to_binary
421425
"[1, 2, 3, ...]"
422426
423-
iex> doc = Inspect.Algebra.surround_many("[", Enum.to_list(1..5), "]", 3, &Integer.to_string(&1), "!")
427+
iex> doc = Inspect.Algebra.surround_many("[", Enum.to_list(1..5), "]",
428+
...> %Inspect.Opts{limit: 3}, fn i, _opts -> to_string(i) end, "!")
424429
iex> Inspect.Algebra.format(doc, 20) |> IO.iodata_to_binary
425430
"[1! 2! 3! ...]"
426431
427432
"""
428433
@spec surround_many(binary, [any], binary, integer | :infinity, (term -> t), binary) :: t
429-
def surround_many(left, docs, right, limit, fun, separator \\ @surround_separator)
434+
def surround_many(left, docs, right, opts, fun, separator \\ @surround_separator) do
435+
if is_integer(opts) do
436+
IO.write :stderr, "Inspect.Algebra.surround_many/6 with an integer limit is deprecated, " <>
437+
"please pass Inspect.Opts instead\n#{Exception.format_stacktrace}"
438+
old_surround_many(left, docs, right, opts, fun, separator)
439+
else
440+
do_surround_many(left, docs, right, opts.limit, opts, fun, separator)
441+
end
442+
end
443+
444+
def do_surround_many(left, [], right, _, _opts, _fun, _) do
445+
concat(left, right)
446+
end
447+
448+
def do_surround_many(left, docs, right, limit, _opts, fun, sep) do
449+
surround(left, do_surround_many(docs, limit, _opts, fun, sep), right)
450+
end
451+
452+
defp do_surround_many(_, 0, _opts, _fun, _sep) do
453+
"..."
454+
end
455+
456+
defp do_surround_many([h], _limit, opts, fun, _sep) do
457+
fun.(h, opts)
458+
end
459+
460+
defp do_surround_many([h|t], limit, opts, fun, sep) when is_list(t) do
461+
glue(
462+
concat(fun.(h, opts), sep),
463+
do_surround_many(t, decrement(limit), opts, fun, sep)
464+
)
465+
end
466+
467+
defp do_surround_many([h|t], _limit, opts, fun, _sep) do
468+
glue(
469+
concat(fun.(h, opts), @tail_separator),
470+
fun.(t, opts)
471+
)
472+
end
430473

431-
def surround_many(left, [], right, _, _fun, _) do
474+
def old_surround_many(left, [], right, _, _fun, _) do
432475
concat(left, right)
433476
end
434477

435-
def surround_many(left, docs, right, limit, fun, sep) do
436-
surround(left, surround_many(docs, limit, fun, sep), right)
478+
def old_surround_many(left, docs, right, limit, fun, sep) do
479+
surround(left, old_surround_many(docs, limit, fun, sep), right)
437480
end
438481

439-
defp surround_many(_, 0, _fun, _sep) do
482+
defp old_surround_many(_, 0, _fun, _sep) do
440483
"..."
441484
end
442485

443-
defp surround_many([h], _limit, fun, _sep) do
486+
defp old_surround_many([h], _limit, fun, _sep) do
444487
fun.(h)
445488
end
446489

447-
defp surround_many([h|t], limit, fun, sep) when is_list(t) do
490+
defp old_surround_many([h|t], limit, fun, sep) when is_list(t) do
448491
glue(
449492
concat(fun.(h), sep),
450-
surround_many(t, decrement(limit), fun, sep)
493+
old_surround_many(t, decrement(limit), fun, sep)
451494
)
452495
end
453496

454-
defp surround_many([h|t], _limit, fun, _sep) do
497+
defp old_surround_many([h|t], _limit, fun, _sep) do
455498
glue(
456499
concat(fun.(h), @tail_separator),
457500
fun.(t)

0 commit comments

Comments
 (0)