AdRoll / rebar3_format

Erlang Formatter for Rebar3
https://tech.nextroll.com/blog/dev/2020/02/25/erlang-rebar3-format.html
MIT License
109 stars 21 forks source link

Comments in type definitions get moved around #343

Open jesperes opened 1 year ago

jesperes commented 1 year ago

Describe the bug Given the following code

-module(format_bug).

-export([foo/0]).

-type foo() :: a_very_long_atom_which_almost_spans_the_entire_line_1 %% comment a
             | a_slightly_shorter_arom                               %% comment b
             | ok.

-spec foo() -> foo().
foo() ->
  ok.

Formatting this using default options yields

-module(format_bug).

-export([foo/0]).

-type foo() ::
  a_very_long_atom_which_almost_spans_the_entire_line_1 |
  a_slightly_shorter_arom |
  ok. %% comment a
                                    %% comment b

-spec foo() -> foo().
foo() ->
  ok.

Note how (1) comments are moved around and (2) indentation of them is completely garbled. Moving comments like this is IMHO is a big no-no. This prevents me from putting a comment on a specific part on the type specification, which is very annoying.

To Reproduce See above.

Expected behavior I would have expected comments to not be moved relative to the surrounding source code.

Rebar3 Log Run rebar3 with DEBUG=1 and paste its output here.

> DEBUG=t ERL_FLAGS="-enable-feature all" rebar3 format
===> Load global config file /home/jespereskilson/.config/rebar3/rebar.config
===> Fetching rebar3_format (from {git,"https://github.com/AdRoll/rebar3_format.git",
                         {tag,"v1.2.1"}})
===> 25.3 satisfies the requirement for minimum OTP version 23
===> Fetching katana_code v2.0.0
===> Making request to get package katana_code from repo hexpm (#{api_url => <<"https://hex.pm/api">>,name => <<"hexpm">>,
         repo_name => <<"hexpm">>,repo_organization => undefined,
         repo_url => <<"https://repo.hex.pm">>,repo_verify => true,
         repo_verify_origin => true})
===> Version cached at /home/jespereskilson/.cache/rebar3/hex/hexpm/packages/katana_code-2.0.0.tar is up to date, reusing it
===> 25.3 satisfies the requirement for minimum OTP version 23
===> Compile (apps)
===> Running hooks for compile in app katana_code (/home/jespereskilson/tmp/format_bug/_build/default/plugins/katana_code) with configuration:
===>    {pre_hooks, []}.
===> Running hooks for compile in app rebar3_format (/home/jespereskilson/tmp/format_bug/_build/default/plugins/rebar3_format) with configuration:
===>    {pre_hooks, []}.
===> Running hooks for erlc_compile in app katana_code (/home/jespereskilson/tmp/format_bug/_build/default/plugins/katana_code) with configuration:
===>    {pre_hooks, []}.
===> Running hooks for erlc_compile in app rebar3_format (/home/jespereskilson/tmp/format_bug/_build/default/plugins/rebar3_format) with configuration:
===>    {pre_hooks, []}.
===> Analyzing applications...
===> Compiling katana_code
===> compile options: {erl_opts, [warn_unused_import,warn_export_vars,
                                         warnings_as_errors,verbose,report,
                                         debug_info,
                                         {feature,maybe_expr,enable}]}.
===> files to analyze ["/home/jespereskilson/tmp/format_bug/_build/default/plugins/katana_code/src/ktn_dodger.erl",
                              "/home/jespereskilson/tmp/format_bug/_build/default/plugins/katana_code/src/ktn_io_string.erl",
                              "/home/jespereskilson/tmp/format_bug/_build/default/plugins/katana_code/src/ktn_code.erl"]
===>      Compiled ktn_io_string.erl
===>      Compiled ktn_code.erl
===>      Compiled ktn_dodger.erl
===> Compiling rebar3_format
===> compile options: {erl_opts, [warn_unused_import,warn_export_vars,
                                         verbose,report,debug_info]}.
===> files to analyze ["/home/jespereskilson/tmp/format_bug/_build/default/plugins/rebar3_format/src/rebar3_formatter.erl",
                              "/home/jespereskilson/tmp/format_bug/_build/default/plugins/rebar3_format/src/rebar3_ast_formatter.erl",
                              "/home/jespereskilson/tmp/format_bug/_build/default/plugins/rebar3_format/src/rebar3_format.erl",
                              "/home/jespereskilson/tmp/format_bug/_build/default/plugins/rebar3_format/src/rebar3_format_prv.erl",
                              "/home/jespereskilson/tmp/format_bug/_build/default/plugins/rebar3_format/src/formatters/sr_formatter.erl",
                              "/home/jespereskilson/tmp/format_bug/_build/default/plugins/rebar3_format/src/formatters/otp_formatter.erl",
                              "/home/jespereskilson/tmp/format_bug/_build/default/plugins/rebar3_format/src/formatters/erlfmt_formatter.erl",
                              "/home/jespereskilson/tmp/format_bug/_build/default/plugins/rebar3_format/src/formatters/default_formatter.erl"]
===>      Compiled rebar3_ast_formatter.erl
===>      Compiled rebar3_formatter.erl
===>      Compiled rebar3_format.erl
===>      Compiled sr_formatter.erl
===>      Compiled erlfmt_formatter.erl
===>      Compiled rebar3_format_prv.erl
===>      Compiled otp_formatter.erl
===>      Compiled default_formatter.erl
===> Running hooks for erlc_compile in app katana_code (/home/jespereskilson/tmp/format_bug/_build/default/plugins/katana_code) with configuration:
===>    {post_hooks, []}.
===> Running hooks for erlc_compile in app rebar3_format (/home/jespereskilson/tmp/format_bug/_build/default/plugins/rebar3_format) with configuration:
===>    {post_hooks, []}.
===> Running hooks for app_compile in app katana_code (/home/jespereskilson/tmp/format_bug/_build/default/plugins/katana_code) with configuration:
===>    {pre_hooks, []}.
===> Running hooks for app_compile in app rebar3_format (/home/jespereskilson/tmp/format_bug/_build/default/plugins/rebar3_format) with configuration:
===>    {pre_hooks, []}.
===> Running hooks for app_compile in app katana_code (/home/jespereskilson/tmp/format_bug/_build/default/plugins/katana_code) with configuration:
===>    {post_hooks, []}.
===> Running hooks for app_compile in app rebar3_format (/home/jespereskilson/tmp/format_bug/_build/default/plugins/rebar3_format) with configuration:
===>    {post_hooks, []}.
===> Running hooks for compile in app katana_code (/home/jespereskilson/tmp/format_bug/_build/default/plugins/katana_code) with configuration:
===>    {post_hooks, []}.
===> Running hooks for compile in app rebar3_format (/home/jespereskilson/tmp/format_bug/_build/default/plugins/rebar3_format) with configuration:
===>    {post_hooks, [{compile,"escript priv/scripts/format"}]}.
===> 25.3 satisfies the requirement for minimum OTP version 23
===> 25.3 satisfies the requirement for minimum OTP version 23
===> Compile (apps)
===> Expanded command sequence to be run: [app_discovery,format]
===> Running provider: app_discovery
===> Found top-level apps: [format_bug]
    using config: [{src_dirs,["src"]},{lib_dirs,["apps/*","lib/*","."]}]
===> Running provider: format
===> Formatter options: #{action => format,break_indent => 2,
                                 output_dir => current,paper => 100,
                                 unquote_atoms => false}
===> Found 3 files: ["src/format_bug.erl","src/format_bug.app.src",
                            "rebar.config"]
===> Formatting "src/format_bug.erl" with #{module => default_formatter,
                                                   opts =>
                                                       #{action => format,
                                                         break_indent => 2,
                                                         output_dir => current,
                                                         paper => 100,
                                                         unquote_atoms =>
                                                             false},
                                                   state => nostate}
===> Formatting "src/format_bug.app.src" with #{module =>
                                                           default_formatter,
                                                       opts =>
                                                           #{action => format,
                                                             break_indent => 2,
                                                             output_dir =>
                                                                 current,
                                                             paper => 100,
                                                             unquote_atoms =>
                                                                 false},
                                                       state => nostate}
===> Formatting "rebar.config" with #{module => default_formatter,
                                             opts =>
                                                 #{action => format,
                                                   break_indent => 2,
                                                   output_dir => current,
                                                   paper => 100,
                                                   unquote_atoms => false},
                                             state => nostate}
elbrujohalcon commented 1 year ago

Thanks for reporting this. It's a known issue that is very very hard to fix given the tools that OTP provides to parse and place comments.

elbrujohalcon commented 1 year ago

It's related (if not a duplicate) of #152