Skip to content
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

Pass sigil/modifiers through to formatter #11348

Merged
merged 6 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions lib/elixir/lib/code/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,15 @@ defmodule Code.Formatter do

sigils =
Map.new(sigils, fn {key, value} ->
with true <- is_atom(key) and is_function(value, 1),
with true <- is_atom(key) and is_function(value, 2),
[char] <- Atom.to_charlist(key),
true <- char in ?A..?Z do
{char, value}
else
_ ->
raise ArgumentError,
":sigils must be a keyword list with a single uppercased letter as key and an " <>
"anonymous function expecting a single argument as value, got: #{inspect(sigils)}"
"anonymous function expecting two arguments as value, got: #{inspect(sigils)}"
end
end)

Expand Down Expand Up @@ -1320,7 +1320,7 @@ defmodule Code.Formatter do
entries =
case state.sigils do
%{^name => callback} ->
case callback.(hd(entries)) do
case callback.(hd(entries), sigil: {List.to_atom([name]), List.to_string(modifiers)}) do
maartenvanvliet marked this conversation as resolved.
Show resolved Hide resolved
binary when is_binary(binary) ->
[binary]

Expand Down
24 changes: 20 additions & 4 deletions lib/elixir/test/elixir/code_formatter/general_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ defmodule Code.Formatter.GeneralTest do
~W/foo bar baz/
"""

formatter = &(&1 |> String.split(~r/ +/) |> Enum.join(" "))
formatter = fn content, opts ->
assert opts == [sigil: {:W, ""}]
content |> String.split(~r/ +/) |> Enum.join(" ")
end

assert_format bad, good, sigils: [W: formatter]

bad = """
Expand All @@ -136,7 +140,11 @@ defmodule Code.Formatter.GeneralTest do
var = ~W/foo bar baz/abc
"""

formatter = &(&1 |> String.split(~r/ +/) |> Enum.join(" "))
formatter = fn content, opts ->
assert opts == [sigil: {:W, "abc"}]
content |> String.split(~r/ +/) |> Enum.join(" ")
end

assert_format bad, good, sigils: [W: formatter]
end

Expand All @@ -153,7 +161,11 @@ defmodule Code.Formatter.GeneralTest do
'''
"""

formatter = &(&1 |> String.split(~r/ +/) |> Enum.join(" "))
formatter = fn content, opts ->
assert opts == [sigil: {:W, ""}]
content |> String.split(~r/ +/) |> Enum.join(" ")
end

assert_format bad, good, sigils: [W: formatter]

bad = """
Expand All @@ -176,7 +188,11 @@ defmodule Code.Formatter.GeneralTest do
end
"""

formatter = &(&1 |> String.split(~r/ +/) |> Enum.join("\n"))
formatter = fn content, opts ->
assert opts == [sigil: {:W, "abc"}]
content |> String.split(~r/ +/) |> Enum.join("\n")
end

assert_format bad, good, sigils: [W: formatter]
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mix/lib/mix/tasks/format.ex
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ defmodule Mix.Tasks.Format do
sigils =
for plugin <- Keyword.fetch!(formatter_opts, :plugins),
sigil <- find_sigils_from_plugins(plugin, formatter_opts),
do: {sigil, &plugin.format(&1, formatter_opts)}
do: {sigil, &plugin.format(&1, &2 ++ formatter_opts)}

IO.iodata_to_binary([Code.format_string!(content, [sigils: sigils] ++ formatter_opts), ?\n])
end
Expand Down
19 changes: 17 additions & 2 deletions lib/mix/test/mix/tasks/format_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,12 @@ defmodule Mix.Tasks.FormatTest do

def features(opts) do
assert opts[:from_formatter_exs] == :yes
[sigils: [:W], extensions: ~w(.w)]
[sigils: [:W]]
end

def format(contents, opts) do
assert opts[:from_formatter_exs] == :yes
assert opts[:sigil] == {:W, "abc"}
contents |> String.split(~r/\s/) |> Enum.join("\n")
end
end
Expand Down Expand Up @@ -240,12 +241,26 @@ defmodule Mix.Tasks.FormatTest do
end)
end

defmodule Elixir.ExtensionWPlugin do
@behaviour Mix.Tasks.Format

def features(opts) do
assert opts[:from_formatter_exs] == :yes
[extensions: ~w(.w)]
end

def format(contents, opts) do
assert opts[:from_formatter_exs] == :yes
maartenvanvliet marked this conversation as resolved.
Show resolved Hide resolved
contents |> String.split(~r/\s/) |> Enum.join("\n")
end
end

test "uses extension plugins from .formatter.exs", context do
in_tmp(context.test, fn ->
File.write!(".formatter.exs", """
[
inputs: ["a.w"],
plugins: [SigilWPlugin],
plugins: [ExtensionWPlugin],
from_formatter_exs: :yes
]
""")
Expand Down