How to programatically format code using plugins

I’m building a library that’s constantly re generating certain modules, building them from string templates. The generated modules include certain sigils that are nicely formatted using a custom plugin.

I need to format the string before it touches disk, because there are also file watchers, who would receive the event twice if I format as a second separate step. That’s why I can’t just call System.cmd("mix format") after creating the file.

Is there something equivalent to using Code.format_string!/2 but with the ability of passing a plugin list?


mix format - reads from standard input and prints to standard output. I haven’t tested it but I suspect you can use it in conjunction with heredoc and skip the writing to stdin. If you want to batch the formatting you can first write to temporary files and format them with one call to mix format.

After a very unsucessful attempt to communicate with mix format - using Port, I ended up doing this:

  def write_and_format_module(file_path, content) do
    filename = for _ <- 1..5, into: "", do: <<Enum.random('0123456789abcdef')>>
    tmp_file = "/tmp/tmp_#{filename}.ex"
    :ok = File.write(tmp_file, content)
    System.cmd("mix", ["format", tmp_file])
    File.rename(tmp_file, file_path)"Re generated: #{file_path}")

You can go even simpler:

def write_and_format_module(file_path, content) do
    command = """
    mix format - <<STDIN > #{file_path}
    {"", 0} ="Re generated: #{file_path}")

awesome, thanks!

1 Like

If I understand right you want to format in dev mode and therefore you can access Mix. In this case you can use this:

> iex -S mix
iex(1)> {formatter, _opts} = Mix.Tasks.Format.formatter_for_file("source.ex")
{#Function<19.89531025/1 in Mix.Tasks.Format.find_formatter_for_file/2>,
   sigils: [],
   plugins: [FreedomFormatter],
   trailing_comma: true,
   inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
   locals_without_parens: [noop: 1]
iex(2)> formatter.("""
...(2)> defmodule    Foo do
...(2)> def foo do
...(2)> noop 5
...(2)> [
...(2)> 1,
...(2)> 2,
...(2)> ]
...(2)> end
...(2)> end
...(2)> """) |> IO.puts()
defmodule Foo do
  def foo do
    noop 5



The "source.ex" given to formatter_for_file does not have to be an existing file.


Thanks! this was it :slight_smile:

  def write_and_format_module(file_path, content) do
    :ok = file_path |> Path.dirname() |> File.mkdir_p()
    {formatter, _} = Mix.Tasks.Format.formatter_for_file("source.ex")
    File.write(file_path, formatter.(content))
    IO.puts( <> "* creating " <> IO.ANSI.reset() <> file_path)