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

[Request for Feedback] Add ontype formatting support #980

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

onno-vos-dev
Copy link
Contributor

@onno-vos-dev onno-vos-dev commented Apr 12, 2021

[Request for Feedback]

Description

This PR adds on-type formatting support. The way this works is that it is triggered by adding a .. This PR is a DRAFT PR which means we @anha0825 and myself are looking for feedback on this work.

The feature is disabled by default but can be enabled by adding format_on_type: true to your erlang_ls.config.

No tests have been added as of yet as I'm not sure how tests for this sort of feature can be written? Considering that BSP will take care of formatting going forward, this should be more or less an "interim" solution anyway.

Peek 2021-04-12 11-41

@robertoaloi
Copy link
Member

Hi @onno-vos-dev sorry for the late reply. Personally I don't have a problem merging this, especially if it's disabled by default initially. So, if you get a basic test in place and squash, I think we're good!

Unless @gomoripeti, @alanz or others have a different opinion...

@gomoripeti
Copy link
Contributor

very nice!
just minor note that currently there is no folding_range POI created for one-line functions. These are usually short and the formatter wouldn't break them up, but there might be other formattings like missing or extra spaces that the formatter would change. Would it make sense to create a folding_range for every function and filter them out only in els_folding_range_provider?
Or is this change specifically for indenting?

Co-authored-by: Andreas Hasselberg <[email protected]>

Delete iostr.erl

Fix linter complaints

Fix proper error

Fix typespec

Maybe make Dialyzer happy?

Temp fixes to get dialyzer to run

Add spec

Better fix for dialyzer errors

Add config to enable on type formatting, defaulting to false

Skip formatting if . is on a commented out line
@onno-vos-dev
Copy link
Contributor Author

onno-vos-dev commented Feb 5, 2022

Opened: AdRoll/rebar3_format#295 as that's the biggest bug that exists as of now. I haven't forgotten about this PR...

Now let me go back and hide in a corner ashamed that this PR has been rotting away for so long...

@michalmuskala
Copy link
Contributor

FWIW I think much bigger issue with rebar3_format is AdRoll/rebar3_format#216, which can lead to the code changing semantics in a very subtle, and hard to spot way

@onno-vos-dev
Copy link
Contributor Author

FWIW I think much bigger issue with rebar3_format is AdRoll/rebar3_format#216, which can lead to the code changing semantics in a very subtle, and hard to spot way

Yeah I noticed that issue as well when trying to find if my issue was already reported but since the formatter currently doesn't support defines I guess we escape that bug.

@michalmuskala
Copy link
Contributor

michalmuskala commented Feb 7, 2022

The issue is not in defines, but in any usages of ?MACRO. In particular, with no modifications to -define for the following example:

-define(MUL(A, B), A * B).

test() -> ?MUL((1 + 2), (3 + 4)).

the formatter will produce:

-define(MUL(A, B), A * B).

test() -> ?MUL(1 + 2, 3 + 4).

changing the result of test() from 21 to 11.

And while this example is fairly convoluted, there are frequently "guard-safe" macros using andalso and orelse where the order the conditions are applied is important and any violation would be quite hard to notice

@onno-vos-dev
Copy link
Contributor Author

Seems to me that rebar3_format and macros is not ready for production anywhere in that case. These are some serious bugs that as you state, can seriously affect the outcome of running code 😞

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

Successfully merging this pull request may close these issues.

4 participants