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

Comments: Field comment moved outside declaration #134

Closed
richcarl opened this issue Sep 9, 2020 · 13 comments
Closed

Comments: Field comment moved outside declaration #134

richcarl opened this issue Sep 9, 2020 · 13 comments
Labels
duplicate This issue or pull request already exists

Comments

@richcarl
Copy link
Contributor

richcarl commented Sep 9, 2020

Original:

-record(state, {callback,    % callback module
                pass = 0,
                fail = 0,
                skip = 0,
                cancel = 0,
                state        % substate
               }).

Formatted:

% callback module
-record(state, {
    callback,
    pass = 0,
    fail = 0,
    skip = 0,
    cancel = 0,
    % substate
    state
}).
@michalmuskala
Copy link
Member

michalmuskala commented Sep 9, 2020

I understand this is not expected in here, but the behaviour is consistent with moving the trailing comment to the line before.

There are places, I've seen in the wild, where this is the desired behaviour. For example:

apply(Module, Fun,     % dynamic call
      build_arguments(State)).

Formatting this as:

% dynamic call
apply(
    Module, 
    Fun,     
    build_arguments(State)
).

seems more correct than

apply(
    Module, 
    % dynamic call
    Fun,
    build_arguments(State)
).

even if we did floating comments, the issue wouldn't be solved since:

apply(
    Module, 
    Fun, % dynamic call
    build_arguments(State)
).

would be equally incorrect.

It seems it's impossible to differentiate.

@richcarl
Copy link
Contributor Author

richcarl commented Sep 9, 2020

It's certainly impossible to know (for a machine) whether the comment pertains to the nearest item or to the item at the start of the line (or any inbetween). Personally, I'd prefer to punish people who use the latter style (so erl_prettypr always attaches to the nearest item).

@elbrujohalcon
Copy link
Contributor

You can always punish nobody :trollface:

Sorry, @michalmuskala … I couldn't resist. 🙄

Jokes aside, that's exactly why we wrote rebar3_format in the way we wrote it and why we allow users to use erlfmt as a formatter there… Nobody needs to be punished and everyone can read the same code but with the comments placed wherever they like them the most.

@michalmuskala
Copy link
Member

michalmuskala commented Sep 9, 2020

It's not about preference, though. In both cases one format is correct and the other is incorrect, the choice only changes which case is correct.

Additionally the formatting roundtrip though a different formatter wouldn't result in getting the same code back, e.g. original -> erlfmt -> rebar3_format default would produce something like:

% callback module
-record(state, {callback,
                pass = 0,
                fail = 0,
                skip = 0,
                cancel = 0,
                % substate
                state
}).

I'm not sure how the choice of formatter helps here.

@elbrujohalcon
Copy link
Contributor

Oh, right! Sorry, I jumped over too quickly.
Since this is related to comments (i.e. outside the scope of parsing and rendering the AST), you're entirely correct: once a comment is moved by a formatter, it won't come back to its original location.
Disregard my previous comment.

@alanz
Copy link
Member

alanz commented Sep 9, 2020

Given the line is expanded while formatting anyway, would it not make sense to turn it into

-record(state, {
    % callback module
    callback,
    pass = 0,
    fail = 0,
    skip = 0,
    cancel = 0,
    % substate
    state
}).

@michalmuskala
Copy link
Member

michalmuskala commented Sep 9, 2020

It would make sense in this case, yes. It would unfortunately be worse than current formatting for other cases that I shown in #134 (comment)

@awalterschulze awalterschulze added the discussion Issue requires further discussion before solving label Sep 18, 2020
@awalterschulze
Copy link
Contributor

Just adding a data point. I was looking at @rvirding 's luerl and found that the style that richard referenced in this issue is quite popular there https://github.com/rvirding/luerl/pull/133/files?file-filters%5B%5D=.hrl#diff-526cd0f79036f27ed96d792ce69e68f8L24

@michalmuskala
Copy link
Member

Some more examples from other issues I've closed in favour of the discussion happening here:


From #160

f(1,
    2). % comment

formatted today as:

f(
    1,
    % comment
    2
).

Where the issue author would prefer

% comment
f(
    1,
    2
).

or

f(
    1,
    2
). % comment

From #165

[x % x
, y % y
].

formatted today as:

% x
[
    x,
    % y
    y
].

Where the issue author would prefer

[
    % x
    x,
    % y
    y
].

@awalterschulze awalterschulze changed the title Field comment moved outside declaration Comments: Field comment moved outside declaration Sep 22, 2020
@awalterschulze
Copy link
Contributor

After doing more analyses on comments, we have decided that adding support for trailing comments is a good idea #219

Doing it will require a lot of work, so no promises on when this will be delivered, but I believe we can consider this issue a duplicate, because if we support trailing comments, then this wouldn't have been an issue.

I hope that makes sense.

@awalterschulze awalterschulze added duplicate This issue or pull request already exists and removed discussion Issue requires further discussion before solving labels Nov 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

5 participants