elixir-lang / elixir

Elixir is a dynamic, functional language for building scalable and maintainable applications
https://elixir-lang.org/
Apache License 2.0
24.62k stars 3.38k forks source link

Code formatter re arranges comments in structs which causes loss of context #6725

Closed minhajuddin closed 7 years ago

minhajuddin commented 7 years ago

Code formatter re arranges comments in structs which causes loss of context

Environment

Current behavior

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

Expected behavior

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

ericmj commented 7 years ago

I think this is one of those cases where you have to fix the comments manually after running the formatter.

stavro commented 7 years ago

This is documented as intentional: https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/code.ex#L291-L329

josevalim commented 7 years ago

We are now keeping the struct over multilple lines but the comments will be moved out, yes. :)

josevalim commented 7 years ago

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