erlang-ls / vscode

The Visual Studio Code Extension for the Erlang Language Server
https://erlang-ls.github.io/
Apache License 2.0
78 stars 26 forks source link

Allow alternate formatter #144

Open rlipscombe opened 1 year ago

rlipscombe commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

(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...