-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Output diffs on mix format --check-formatted #12109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
lib/mix/lib/mix/text_diff.ex
Outdated
old = String.split(old, "\n") | ||
new = String.split(new, "\n") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
old = String.split(old, "\n") | |
new = String.split(new, "\n") | |
old = String.split(old, ["\r\n", "\n"]) | |
new = String.split(new, ["\r\n", "\n"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But then what happens when two files are equal except for \r\n
-> \n
? So maybe it is better to keep it by your current split?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, splitting with \n
should be better. The last commit makes also changed \r
visible.
iex(1)> Mix.TextDiff.format("a\r\nb\r\n", "a\nb\nc\n") |> IO.iodata_to_binary() |> IO.puts()
1 - |a↵
2 - |b↵
1 + |a
2 + |b
3 + |c
:ok
iex(2)> Mix.TextDiff.format("a\r\n", "a\n") |> IO.iodata_to_binary() |> IO.puts()
1 - |a↵
1 + |a
:ok
iex(3)> Mix.TextDiff.format("a\n", "a\r\n") |> IO.iodata_to_binary() |> IO.puts()
1 - |a
1 + |a↵
:ok
@NickNeck holy moly, this is awesome! ❤️ I think we want to keep the |
@NickNeck let's make |
I'm not sure, but maybe in the future it might be interesting to have a public |
lib/mix/lib/mix/tasks/format.ex
Outdated
defp to_diffs(files) do | ||
Enum.map_join(files, "\n", fn | ||
{:stdin, unfomatted, fomatted} -> | ||
IO.iodata_to_binary([IO.ANSI.reset(), Mix.TextDiff.format(unfomatted, fomatted)]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm probably overthinking this, but just wondering: maybe we don't need to cast it as a binary here and we can build an IO list?
- removing calls to
IO.iodata_to_binary/1
- replacing
map_join/3
bymap_intersperse/3
Co-authored-by: José Valim <jose.valim@gmail.com>
@NickNeck looks great! We just need to move TextDiff around and we can ship it! 👍 |
lib/mix/lib/mix/tasks/format.ex
Outdated
Enum.map_join(files, "\n", &" * #{&1 |> to_string() |> Path.relative_to_cwd()}") | ||
defp to_diffs(files) do | ||
Enum.map_intersperse(files, "\n", fn | ||
{:stdin, unfomatted, fomatted} -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're missing an "r" on these
@josevalim I have moved |
@NickNeck looks great! Can you please look at the failing tests? Actually, I would suggest calling |
A suggestion for #12100 . The output would look like:

In case a single line was changed the changed spaces get a background colour. For multiple deleted or inserted lines just the text gets a colour.
I have written this for another project but maybe it fits in here.