Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow alternate formatter #144

Open
rlipscombe opened this issue Jan 15, 2023 · 5 comments
Open

Allow alternate formatter #144

rlipscombe opened this issue Jan 15, 2023 · 5 comments

Comments

@rlipscombe
Copy link

I prefer erlfmt to rebar3_fmt. It would be great if the extension allowed me to choose. Currently I'm using szTheory.erlang-formatter, but it would be nice if ErlangLS allowed me to choose instead.

@robertoaloi
Copy link
Member

Hi @rlipscombe, the reason we opted for rebar3_fmt is that it abstracts away the actual formatter used, so we should still be able to use erlfmt via rebar3_fmt as described here: https://github.com/AdRoll/rebar3_format#erlfmt

The default formatter is hard-coded here, but it should be trivial to read this from config.

@rlipscombe
Copy link
Author

rlipscombe commented Jan 15, 2023

Thanks for the response.

I was going to reply with this:

The problem with that is that we're using erlang.mk, rather than rebar3. Needing a working rebar3 installation is an extra hurdle to having nicely-formatted code.

I've figured out the source of my confusion. It turns out you need a working rebar3 installation anyway.

szTheory.erlang-formatter looks like it supports a variety of different formatters. But, in reality, it's just assuming that you've got rebar3 installed and the rebar3_format, erlfmt, or steamroller plugin installed. Then it just works out which rebar3 command to run to trigger the configured plugin: rebar3 format vs rebar3 fmt vs rebar3 steamroll.

Whereas ErlangLS just pushes that further back, so it's now the responsibility of rebar3_fmt to either use the default formatter or erlfmt or steamroller.

But: either I'm not reading it right, or the documentation doesn't explicitly say how to choose an alternate formatter. Could it be made more prominent somehow?

@rlipscombe
Copy link
Author

I think the other source of my confusion is that (a while ago) I looked at the source for erlang-ls and noticed that the formatter was hard-coded as rebar3_formatter (which I dislike, because it messes up various simple bits of code), and assumed that I'd need to use another VS Code extension to choose.

It never occurred to me that rebar3_formatter would allow itself to be usurped :)

@rlipscombe
Copy link
Author

rlipscombe commented May 16, 2023

I've been digging into this some more, and it looks like erlang-ls invokes rebar3_formatter:new, passing default_formatter. See https://github.com/erlang-ls/erlang_ls/blob/main/apps/els_lsp/src/els_formatting_provider.erl#L102

This bypasses the configuration setting in rebar3.config, as documented at https://github.com/AdRoll/rebar3_format#erlfmt (which you linked to).

rebar3 format applies the configuration setting here: https://github.com/AdRoll/rebar3_format/blob/main/src/rebar3_format_prv.erl#L135

@rlipscombe
Copy link
Author

(Sheepish grin) I got confused and assumed that you were telling me that erlang-ls used rebar3_format, so that it would use its configuration. You're actually saying that erlang-ls uses it because it abstracts the differences away, but that erlang-ls isn't making use of that to allow a different formatter.

it should be trivial to read this from config

means: "hey, Roger, if you've got some time at the weekend, could you (a) make erlang-ls read it from configuration, and (b) add the relevant configuration drop-down to the VS Code plugin"...? :)

I might have some time at the weekend...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants