I often will get reminded by credo to sort my alias calls like below.
Is there a way I could configure the Elixir formatter (that is kicked by VS Code) to auto-sort these for me?
┃ [R] ↘ The alias `RetroTaxi.JoinBoard` is not alphabetically ordered among its group.
┃ lib/retro_taxi/board_creation.ex:12:9 #(RetroTaxi.BoardCreation)
It looks like formatter Plugins are a recent/upcoming (1.13) thing. Anyone ever try to do this?
For now, formatter plugins only support sigils and custom file extensions. For what you want, you’d need to find a way to run code before or after the formatter, with Code.string_to_quoted_with_comments and Code.quoted_to_algebra + Inspect.Algebra.format. The challenge is in syncing the changes you did to the AST with the comments.
I created Sourceror specifically for this. Here is an example to sort dependencies in mix.exs:
Although auto-sorting aliases on save is still not a thing the formatter has (at the time of this writing), I have found this VSCode extension to do the job just fine:
Ah, that extension looks helpful! Although one downside is that (if I understand it correctly) is that you need to run it on each list of aliases you have.
There’s an elixir project that was started after this thread (and is based on Sourceror) that makes this even easier:
Recode has a Task.AliasOrder that if configured will auto-sort your aliases when you run mix recode