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

Created on 9 Oct 2017  路  4Comments  路  Source: elixir-lang/elixir

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

Environment

  • Elixir & Erlang versions (elixir --version): Erlang 20, Elixir 1.6.0-dev bb26227f7
  • Operating system: Ubuntu 16.04

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

Formatter

Most helpful comment

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

All 4 comments

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

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
Was this page helpful?
0 / 5 - 0 ratings