sourceror icon indicating copy to clipboard operation
sourceror copied to clipboard

Sourceror seems to ignore .formatter.exs

Open NickNeck opened this issue 3 years ago • 4 comments

First of all, thanks for your great work.

I have started an experimental project recode to playing around with sourceror. When I reformat one my project time_zone_info with recode I got some unexpected code changes.

The project contains the following .formatter.exs:

[
  inputs: ["{mix,.formatter}.exs", "{config,lib,test,scripts}/**/*.{ex,exs}"],
  import_deps: [:stream_data, :nimble_parsec, :plug, :benchee_dsl],
  locals_without_parens: [prove: 1, prove: 2]
]

Sourcer still changes code lines like this:

-  defparsecp :do_parse, parser
+  defparsecp(:do_parse, parser)

So, it seems sourceror ignores .formatter.exs or I have forgotten to set an option correct.

NickNeck avatar Nov 01 '21 09:11 NickNeck

Sourceror does not read the project's formatter.exs file. It doesn't accept the locals_without_parens option either. This would be a really useful feature for Sourceror, so I'll leave it open as a feature request :)

Two things need to be done:

  1. Support locals_without_parens in Sourceror.to_string/2 (I just pushed a commit to main for this)
  2. Have a way to read and extract these options from formatter.exs files

doorgan avatar Nov 02 '21 00:11 doorgan

Maybe the support in Sourceror.to_string/2 is enough to do for sourceror. You could leave the extracting of the .formatter.exs to the user of your package. I would be nice if Elixir would have a function like Mix.Project.formatter/1 to get the formatter config. WDYT?

For recode I have lend me some code from Mix to extract locals_without_parens.

NickNeck avatar Nov 02 '21 06:11 NickNeck

Sounds reasonable :)

doorgan avatar Nov 05 '21 01:11 doorgan

I was able to get my transformations done with the options provided to to_string, but agreed it'd be much easier if elixir/mix had a tool for getting that information out of a formatter.exs (i manually recreated my list rather than ripping code from Mix)

novaugust avatar Dec 09 '21 16:12 novaugust

Since 1.13 Elixir exposes Mix.Tasks.Format.formatter_for_file/1, which can be used to parse a .formatter.exs, grab the options, and pass them to Sourceror.to_string/2 =)

novaugust avatar Jan 28 '23 00:01 novaugust