Skip to content

Commit 421a77d

Browse files
maennchenjosevalim
authored andcommitted
Embedded Elixir Expressions in Formatter Plugins (#11587)
Support Sigils inside embedded expressions in files formatted by a formatter plugin.
1 parent cc9e9b2 commit 421a77d

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

lib/mix/lib/mix/tasks/format.ex

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,15 @@ defmodule Mix.Tasks.Format do
288288
end
289289
end
290290

291-
formatter_opts = Keyword.put(formatter_opts, :plugins, plugins)
291+
sigils =
292+
for plugin <- plugins,
293+
sigil <- find_sigils_from_plugins(plugin, formatter_opts),
294+
do: {sigil, &plugin.format(&1, &2 ++ formatter_opts)}
295+
296+
formatter_opts =
297+
formatter_opts
298+
|> Keyword.put(:plugins, plugins)
299+
|> Keyword.put(:sigils, sigils)
292300

293301
if deps == [] and subs == [] do
294302
{{formatter_opts, []}, sources}
@@ -537,12 +545,7 @@ defmodule Mix.Tasks.Format do
537545
defp stdin_or_wildcard(path), do: path |> Path.expand() |> Path.wildcard(match_dot: true)
538546

539547
defp elixir_format(content, formatter_opts) do
540-
sigils =
541-
for plugin <- Keyword.fetch!(formatter_opts, :plugins),
542-
sigil <- find_sigils_from_plugins(plugin, formatter_opts),
543-
do: {sigil, &plugin.format(&1, &2 ++ formatter_opts)}
544-
545-
IO.iodata_to_binary([Code.format_string!(content, [sigils: sigils] ++ formatter_opts), ?\n])
548+
IO.iodata_to_binary([Code.format_string!(content, formatter_opts), ?\n])
546549
end
547550

548551
defp find_sigils_from_plugins(plugin, formatter_opts) do

lib/mix/test/mix/tasks/format_test.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,13 @@ defmodule Mix.Tasks.FormatTest do
247247

248248
def features(opts) do
249249
assert opts[:from_formatter_exs] == :yes
250-
[extensions: ~w(.w)]
250+
[extensions: ~w(.w), sigils: [:W]]
251251
end
252252

253253
def format(contents, opts) do
254254
assert opts[:from_formatter_exs] == :yes
255255
assert opts[:extension] == ".w"
256+
assert [W: _fun] = opts[:sigils]
256257
contents |> String.split(~r/\s/) |> Enum.join("\n")
257258
end
258259
end

0 commit comments

Comments
 (0)