Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions lib/elixir/lib/inspect/algebra.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ defmodule Inspect.Algebra do
do: format(w, k, t, acc)

defp format(w, _, i, _, doc_line(), t, acc),
do: format(w, i, t, <<acc::binary, indent(i)::binary>>)
do: format(w, i, t, <<acc::binary, @newline, spaces(i)::binary>>)

defp format(w, k, i, m, doc_cons(x, y), t, acc),
do: format(w, k, i, m, x, [{i, m, y} | t], acc)
Expand Down Expand Up @@ -1325,14 +1325,14 @@ defmodule Inspect.Algebra do
if w == :infinity or m == :flat or fits?(w, k, true, t) do
format(w, k, t, <<acc::binary, s::binary>>)
else
format(w, i, t, <<acc::binary, indent(i)::binary>>)
format(w, i, t, <<acc::binary, @newline, spaces(i)::binary>>)
end
end

# Strict breaks are conditional to the mode
defp format(w, k, i, mode, doc_break(s, :strict), t, acc) do
if mode == :break do
format(w, i, t, <<acc::binary, indent(i)::binary>>)
format(w, i, t, <<acc::binary, @newline, spaces(i)::binary>>)
else
format(w, k + byte_size(s), t, <<acc::binary, s::binary>>)
end
Expand Down Expand Up @@ -1388,16 +1388,26 @@ defmodule Inspect.Algebra do
end

defp collapse(t, max, count, i) do
[:binary.copy("\n", min(max, count)), :binary.copy(" ", i) | t]
[newlines(min(max, count)), spaces(i) | t]
end

defp newlines(0), do: ""
defp newlines(1), do: @newline
defp newlines(2), do: @newline <> @newline
defp newlines(n), do: :binary.copy(@newline, n)

defp strip_whitespace(" " <> rest), do: strip_whitespace(rest)
defp strip_whitespace(rest), do: rest

defp apply_nesting(_, k, :cursor), do: k
defp apply_nesting(_, _, :reset), do: 0
defp apply_nesting(i, _, j), do: i + j

defp indent(0), do: @newline
defp indent(i), do: @newline <> :binary.copy(" ", i)
for i <- 0..16 do
defp spaces(unquote(i)) do
unquote(:binary.copy(" ", i))
end
end

defp spaces(i), do: :binary.copy(" ", i)
end
Loading