hrzndhrn / recode

A linter with autocorrection and a refactoring tool.
MIT License
281 stars 15 forks source link

Adds Formatter plugin #46

Closed antedeguemon closed 1 year ago

antedeguemon commented 1 year ago

Hey there! 🖖

Since Elixir 1.13, it is possible to define custom formatter plugins for mix format: https://hexdocs.pm/mix/main/Mix.Tasks.Format.html#module-plugins

This pull request adds a custom formatter that runs Recode tasks. It allows to replace mix recode --autocorrect with mix format on the development workflow.

@NickNeck please feel free to consider this PR a draft or make commits over it as it suits you better 😄

coveralls commented 1 year ago

Pull Request Test Coverage Report for Build e41534721799b537c672a6d862ad0545f71aa1a7-PR-46


Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/recode/config.ex 5 6 83.33%
lib/recode/runner/impl.ex 10 11 90.91%
lib/mix/tasks/recode.ex 4 6 66.67%
lib/recode/formatter_plugin.ex 13 18 72.22%
<!-- Total: 54 63 85.71% -->
Files with Coverage Reduction New Missed Lines %
lib/recode/ast.ex 2 83.91%
lib/recode/formatter.ex 3 89.71%
lib/recode/runner/impl.ex 8 84.85%
<!-- Total: 13 -->
Totals Coverage Status
Change from base Build 408b61877c1707bd7ba45f75553bdc43bca8894c: -0.4%
Covered Lines: 608
Relevant Lines: 681

💛 - Coveralls
NickNeck commented 1 year ago

Thank you. I haven't had a look at the code yet but the idea is great. I never thought about this option.

NickNeck commented 1 year ago

Hi Vicente.

I have added some code.

One change ensures that the options from .formatter.exs will be used (locals_without_parens) and that other formatter plugins will be respected. The support for multiple formatters for an extension comes with the next Elixir release.

Another change solves the problem that the formatter is called by rewrite when we change the AST. Therefore I have added a lookup table in the formatter plugin.

I have also added Recode.Runner.run/3 that works with a string and runs all tasks and returns the changed string.

I think now we just need a few changes to the config stuff.

WDYT?

NickNeck commented 1 year ago

@antedeguemon I have changed my mind several times in this PR but I am thinking now we have a solution. I will make some more tests at the weekend and merge this PR.

One problem was that recode, rewrite and sourceror are calling Mix.Tasks.Format.formatter_for_file and that can cause some problems (https://github.com/doorgan/sourceror/issues/87) and it is useless because we have the info. For now I add the dot_formatter_opts to every source and use this info for the formatting. I add this with Source.put_private(:dot_formatter_opts, opts) but that will change with the next version of rewrite and the implementation of https://github.com/hrzndhrn/rewrite/issues/15 .