Roberto-XY/eunomo

Comments are removed on format

Closed this issue · 4 comments

When using Eunomo as a formatter plugin, comments within an import/alias/require block are removed upon formatting:

Screen.Recording.2023-12-04.at.11.30.19.AM.mov

This can be especially problematic when an alias is commented out. In practice, this will only happen for directives that are not the first or last in a group, since those are the only cases in which in the comment is in the "bounds" of a group:

Screen.Recording.2023-12-04.at.11.18.24.AM.mov

I think the ideal functionality would be to associate comments with their corresponding directive lines and move them along with the directives when sorting.

Thanks for the detailed description. I will look into it on the weekend.

The issue is a bit more complicated than I thought. This bug is also induced by the behavior:

defmodule Foo do
  alias Eunomo.CC
  # alias Eunomo.B
  alias Eunomo.C
end

gets formatted into

defmodule Foo do
  alias Eunomo.C
  # alias Eunomo.B
end

And that's definitely wrong 🤡

I fixed the behavior by moving all comments to the start of a block. That's consistent with sorting by # in the start position. Still need more tests & maybe I can come up with a better solution. Also need to think about multiline comments & not ripping them apart. The curx is that in the general case, the comment does not have to be another valid alias expression, and I would rather avoid introspecting comments for formatting.

Another possibility is to break blocks on comments, so after a line comment, a new block starts. That's simple & should lead to consistent behavior without unexpected comment moves. If a comment in an import block is necessary, it also indicates some form of specialness, so a new block seems reasonable.

I am starting to like the second approach more.

@jaminthorns I finally found some time to approach this issue. If you like, you can test the new version under v3.0.0-preview. It is not released to hex yet, as I would welcome some feedback before. Closing this issue once a working version is released.

Published to https://hex.pm/packages/eunomo

Closing