[Proposal] add colored output to 'did you mean' suggestions

60 views
Skip to first unread message

Daniel Kukuła

unread,
Jan 19, 2022, 1:05:07 PM1/19/22
to elixir-lang-core
ExUnit has this nice feature that it's showing the differences between the expectation and result in different colors. It would be nice to have something similar with the `did you mean suggestions`.
Rationale:
It's not the first time that when I made a typo and the compiler suggest the right function/module I'm struggling to see the difference between my code and the suggestions. With short strings it's easy but I got deeply nested modules and sometimes it takes some time to spot the typo.

José Valim

unread,
Jan 21, 2022, 6:08:14 AM1/21/22
to elixir-lang-core
I would love to see how a prototype of this would work. One difficulty to keep in mind is that, for a diff to work, you need to show both options side by side. And I am not sure if repeating what you typed wrong will be helpful. But someone has to play with those ideas before we are sure. :)

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/5751b69a-1364-4419-8d8a-273252c9755an%40googlegroups.com.

Daniel Kukuła

unread,
Jan 21, 2022, 1:26:09 PM1/21/22
to elixir-lang-core
I think it can be done on the suggestion only - showing different colors like here - second example is currently selected but you can see it with the same background
compare.png

Daniel Kukuła

unread,
Jan 21, 2022, 1:52:23 PM1/21/22
to elixir-lang-core
I used this for the example if someone want's to play with it:

defmodule Colorize do
  def compare() do
    """
    IO.inspect-IO.innspect
    IO.inspect-IO.ispect
    IO.inspect-IO.imspect
    IO.inspect-IO.inspcet
    """
    |> String.split("\n", trim: true)
    |> Enum.each(fn x ->
      [l, r] = String.split(x, "-")
      compare(l, r)
    end)
  end

  def compare(expected, expected), do: expected

  def compare(expected, rendered) do
    {%ExUnit.Diff{left: {_, _, left}, right: {_, _, right}}, _} =
      ExUnit.Diff.compute(expected, rendered, :==)

    IO.puts(["did you mean: ", colorize(left, right)])
  end

  defp colorize([{_, _, added}, r1], [{_, _, deleted}, r2]) do
    [added(added), deleted(deleted), colorize(r1, r2)]
  end

  defp colorize([], []), do: []
  defp colorize([{_, _, added} | r1], r2), do: [added(added), colorize(r1, r2)]
  defp colorize(r1, [{_, _, deleted} | r2]), do: [deleted(deleted), colorize(r1, r2)]
  defp colorize([{_, _, added}], []), do: added(added)
  defp colorize([], [{_, _, deleted}]), do: deleted(deleted)
  defp colorize([h | r1], [h | r2]), do: [h, colorize(r1, r2)]

  defp added(text), do: IO.ANSI.format([:green_background, text])
  defp deleted(text), do: IO.ANSI.format([:red_background, text])
end
Reply all
Reply to author
Forward
0 new messages