   @doc ~S"""
   Colors a document if the `color_key` has a color in the options.
   """
-  @spec color(t, Inspect.Opts.color_key, Inspect.Opts.t) :: doc_color
+  @spec color(t, Inspect.Opts.color_key(), Inspect.Opts.t()) :: doc_color
   def color(doc, color_key, %Inspect.Opts{syntax_colors: syntax_colors}) when is_doc(doc) do
     if precolor = Keyword.get(syntax_colors, color_key) do
       postcolor = Keyword.get(syntax_colors, :reset, :reset)

      "[1! 2! 3! ...]"

  """
  @spec container_doc(t, [any], t, Inspect.Opts.t, (term, Inspect.Opts.t -> t), keyword()) :: t
  def container_doc(left, collection, right, inspect, fun, opts \\ [])
      when is_doc(left) and is_list(collection) and is_doc(right) and
             is_function(fun, 2) and is_list(opts) do
    case collection do
      [] ->
        concat(left, right)

  @typep mode :: :break | :flat | :next_fits | :no_fitting

  @spec fits?(integer, integer, [{integer, mode, t}]) :: boolean
  defp fits?(w, k, _) when k > w, do: false
  defp fits?(_, _, []), do: true

  defp fits?(w, k, [{i, _, doc_fits(x, :disabled)} | t]),
    do: fits?(w, k, [{i, :no_fitting, x} | t])

  defp fits?(w, k, [{i, :no_fitting, doc_group(x, _)} | t]),
    do: fits?(w, k, [{i, :no_fitting, x} | t])

  defp fits?(w, k, [{i, :no_fitting, doc_fits(x, _)} | t]),
    do: fits?(w, k, [{i, :no_fitting, x} | t])

  defp fits?(w, k, [{i, _, doc_fits(x, :enabled)} | t]), do: fits?(w, k, [{i, :next_fits, x} | t])
  defp fits?(w, k, [{i, :next_fits, doc_force(x)} | t]), do: fits?(w, k, [{i, :next_fits, x} | t])

  defp fits?(w, k, [{i, :next_fits, doc_group(x, _)} | t]),
    do: fits?(w, k, [{i, :next_fits, x} | t])

  defp fits?(_, _, [{_, :next_fits, doc_break(_, _)} | _]), do: true
  defp fits?(_, _, [{_, :next_fits, :doc_line} | _]), do: true

  defp fits?(w, k, [{_, _, :doc_nil} | t]), do: fits?(w, k, t)
  defp fits?(w, _, [{i, _, :doc_line} | t]), do: fits?(w, i, t)
  defp fits?(w, _, [{i, _, doc_collapse(_)} | t]), do: fits?(w, i, t)
  defp fits?(w, k, [{i, m, doc_cons(x, y)} | t]), do: fits?(w, k, [{i, m, x} | [{i, m, y} | t]])
  defp fits?(w, k, [{i, m, doc_color(x, _)} | t]), do: fits?(w, k, [{i, m, x} | t])
  defp fits?(w, k, [{i, m, doc_nest(x, _, :break)} | t]), do: fits?(w, k, [{i, m, x} | t])

  defp fits?(w, k, [{i, m, doc_nest(x, j, _)} | t]),
    do: fits?(w, k, [{apply_nesting(i, k, j), m, x} | t])

  defp fits?(w, k, [{i, _, doc_group(x, _)} | t]), do: fits?(w, k, [{i, :flat, x} | t])
  defp fits?(w, k, [{_, _, doc_string(_, l)} | t]), do: fits?(w, k + l, t)
  defp fits?(w, k, [{_, _, s} | t]) when is_binary(s), do: fits?(w, k + byte_size(s), t)
  defp fits?(_, _, [{_, _, doc_force(_)} | _]), do: false
  defp fits?(_, _, [{_, :break, doc_break(_, _)} | _]), do: true
  defp fits?(w, k, [{_, _, doc_break(s, _)} | t]), do: fits?(w, k + byte_size(s), t)

  @spec format(integer | :infinity, integer, [{integer, mode, t}]) :: [binary]
  defp format(_, _, []), do: []
  defp format(w, k, [{_, _, :doc_nil} | t]), do: format(w, k, t)
  defp format(w, _, [{i, _, :doc_line} | t]), do: [indent(i) | format(w, i, t)]
  defp format(w, k, [{i, m, doc_cons(x, y)} | t]), do: format(w, k, [{i, m, x} | [{i, m, y} | t]])
  defp format(w, k, [{i, m, doc_color(x, c)} | t]), do: [ansi(c) | format(w, k, [{i, m, x} | t])]
  defp format(w, k, [{_, _, doc_string(s, l)} | t]), do: [s | format(w, k + l, t)]
  defp format(w, k, [{_, _, s} | t]) when is_binary(s), do: [s | format(w, k + byte_size(s), t)]
  defp format(w, k, [{i, m, doc_force(x)} | t]), do: format(w, k, [{i, m, x} | t])
  defp format(w, k, [{i, m, doc_fits(x, _)} | t]), do: format(w, k, [{i, m, x} | t])
  defp format(w, _, [{i, _, doc_collapse(max)} | t]), do: collapse(format(w, i, t), max, 0, i)

  # Flex breaks are not conditional to the mode
  defp format(w, k, [{i, _, doc_break(s, :flex)} | t]) do
