Code formatter re arranges comments in structs which causes loss of context
Before formatting my code looks like below:
defmodule Request do
defstruct uri: %URI{},
method: :get,
http_version: :http1_1,
headers: [], # e.g. [{"User-Agent", "Danny"}, {"Accept", "text/html"}]
body: [] # iodata/iolist
end
After formatting my code looks like below:
defmodule Request do
# e.g. [{"User-Agent", "Danny"}, {"Accept", "text/html"}]
# iodata/iolist
defstruct uri: %URI{}, method: :get, http_version: :http1_1, headers: [], body: []
end
As you can see the comments are moved from their place to the top of the struct definition. This causes in the loss of context
I think this is one of those cases where you have to fix the comments manually after running the formatter.
This is documented as intentional: https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/code.ex#L291-L329
We are now keeping the struct over multilple lines but the comments will be moved out, yes. :)
Btw, this was added to master at some point. The code above will be formatted to:
defmodule Request do
defstruct uri: %URI{},
method: :get,
http_version: :http1_1,
# e.g. [{"User-Agent", "Danny"}, {"Accept", "text/html"}]
headers: [],
# iodata/iolist
body: []
end
Most helpful comment
Btw, this was added to master at some point. The code above will be formatted to: