elixir-lang/elixir

Possible code formatter improvement when last function param is a function

Closed this issue · 3 comments

dkuku commented

Elixir and Erlang/OTP versions

% elixir -v
Erlang/OTP 27 [erts-15.2.7.1] [source] [64-bit] [smp:14:14] [ds:14:14:10] [async-threads:1] [jit]

Elixir 1.18.3 (compiled with Erlang/OTP 27)

Operating system

macos

Current behavior

I see this pattern very often when using mock libraries, probably due to that it fits on a single line with the function and first param.
I wonder if this could be better formatted by default, currently it is pushing everything to the right ?

      Mimic.expect(Events.MyEvent, :emit_enabled_event, fn :my_event,
                                                           true,
                                                           false,
                                                           [
                                                             :param1,
                                                             :param2
                                                           ] ->
        [Factory.build(:outbox_event), Factory.build(:outbox_event)]
      end)

the solution is to put an enter after the ( and reformat then manually join the function because then it all fits in a single line but is not reformatted because it was already formatted with multiple lines.

Expected behavior

Ideally it would end up like this:

      Mimic.expect(
        Events.MyEvent,
        :emit_enabled_event,
        fn :my_event, true, false, [:param1, :param2] ->
          [Factory.build(:outbox_event), Factory.build(:outbox_event)]
        end
      )

If you can figure out a way to make it happen, I would love to see a pull request, but I personally couldn't crack this puzzle. The way it work is that it breaks on the last possibility, which ends up being the parameters. In such cases, I most often end up refatoring, but uising a different approach than yours:

      Mimic.expect(Events.MyEvent, :emit_enabled_event, fn
        :my_event, true, false, [:param1, :param2] ->
          [Factory.build(:outbox_event), Factory.build(:outbox_event)]
      end)

The optoin is to be more aggressive and break earlier, but then it leads to other problems.

dkuku commented

I understand that it's not trivial, and there are many edge cases. It's a good opportunity to at least learn how it works now.

I will ahead and close this one, as we prepare for the next RC, as it is not a task the core team will tackle, but feel free to ask questions or submit a PR! Happy learning!