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

Sort identifiers in deriving clauses? #968

Closed
tfausak opened this issue Jan 12, 2023 · 4 comments · Fixed by fourmolu/fourmolu#434
Closed

Sort identifiers in deriving clauses? #968

tfausak opened this issue Jan 12, 2023 · 4 comments · Fixed by fourmolu/fourmolu#434
Labels
style Nitpicking and things related to purely visual aspect for formatting.

Comments

@tfausak
Copy link

tfausak commented Jan 12, 2023

Describe the solution you'd like
I would like it if Ormolu sorted identifiers in deriving clauses. For example:

-- input
data T = C deriving (Show, Eq)
-- output
data T = C deriving (Eq, Show)

As far as I know, different orderings of the identifiers in a deriving clause are equivalent, so this should be a safe transformation to make. It would reduce noise in diffs, especially with multi-line deriving clauses.

I don't have a strong opinion about how exactly things should be sorted. Alphabetical is a natural choice. Qualified identifiers make things a bit trickier; it's not clear if A.C should come before or after B.

Describe alternatives you've considered
Currently I make a best effort to sort things manually. It's tedious and error prone.

Additional context
I think this is similar in spirit to how Ormolu sorts imports.

@amesgen amesgen added the style Nitpicking and things related to purely visual aspect for formatting. label Jan 12, 2023
@mrkkrp
Copy link
Member

mrkkrp commented Jan 16, 2023

Sorting of this kind of thing is doable and perhaps desirable. The problem is that Ormolu relies on source span information in order to both control the layout (single line vs multiline) and to insert comments. The latter logic is quite complicated and it doesn't like things to move around and be out of order compared to the original input. One example where we went ahead and implemented sorting is explicit module export/import lists. This was a good change, but there are still some unsolved problems with that to this day (see, e.g. #469). Not sure we should do more of this.

@tfausak
Copy link
Author

tfausak commented Jan 18, 2023

What if the identifiers were only sorted when there were no comments at all in the list of derived type classes? That way the common case of a flat list of names would be sorted. But if there was a comment anywhere in there, the order would be left alone.

-- would be sorted
data T = C deriving (Show, Eq)
-- would not be sorted
data T = C deriving (Show, {- x -} Eq)

That also allows for some fancier logic to come later, if it's desired. For example comments could move with their associated identifiers. Or comments could set off separate blocks, which are sorted independently.

@mrkkrp
Copy link
Member

mrkkrp commented Jan 18, 2023

To me this doesn't seem satisfactory enough—complex logic and more inconsistent behavior for a questionable gain.

@tfausak
Copy link
Author

tfausak commented Jan 19, 2023

Gotcha. Sounds like this won't be supported in Ormolu then. Thanks for considering it!

@tfausak tfausak closed this as not planned Won't fix, can't repro, duplicate, stale Jan 19, 2023
michaelpj added a commit to michaelpj/fourmolu that referenced this issue Oct 6, 2024
This adds two configuration options for sorting a) types in deriving
clauses, and b) deriving clauses themselves.

Fixes tweag/ormolu#968
michaelpj added a commit to michaelpj/fourmolu that referenced this issue Oct 6, 2024
This adds two configuration options for sorting a) types in deriving
clauses, and b) deriving clauses themselves.

Fixes tweag/ormolu#968
michaelpj added a commit to michaelpj/fourmolu that referenced this issue Oct 6, 2024
This adds two configuration options for sorting a) types in deriving
clauses, and b) deriving clauses themselves.

Fixes tweag/ormolu#968
michaelpj added a commit to michaelpj/fourmolu that referenced this issue Oct 7, 2024
This adds two configuration options for sorting a) types in deriving
clauses, and b) deriving clauses themselves.

Fixes tweag/ormolu#968
michaelpj added a commit to michaelpj/fourmolu that referenced this issue Oct 13, 2024
This adds two configuration options for sorting a) types in deriving
clauses, and b) deriving clauses themselves.

Fixes tweag/ormolu#968
michaelpj added a commit to michaelpj/fourmolu that referenced this issue Oct 13, 2024
This adds two configuration options for sorting a) types in deriving
clauses, and b) deriving clauses themselves.

Fixes tweag/ormolu#968
michaelpj added a commit to michaelpj/fourmolu that referenced this issue Oct 14, 2024
This adds two configuration options for sorting a) types in deriving
clauses, and b) deriving clauses themselves.

Fixes tweag/ormolu#968
michaelpj added a commit to michaelpj/fourmolu that referenced this issue Oct 16, 2024
This adds two configuration options for sorting a) types in deriving
clauses, and b) deriving clauses themselves.

Fixes tweag/ormolu#968
michaelpj added a commit to michaelpj/fourmolu that referenced this issue Oct 16, 2024
This adds two configuration options for sorting a) types in deriving
clauses, and b) deriving clauses themselves.

Fixes tweag/ormolu#968
michaelpj added a commit to michaelpj/fourmolu that referenced this issue Nov 2, 2024
This adds two configuration options for sorting a) types in deriving
clauses, and b) deriving clauses themselves.

Fixes tweag/ormolu#968
michaelpj added a commit to michaelpj/fourmolu that referenced this issue Nov 4, 2024
This adds two configuration options for sorting a) types in deriving
clauses, and b) deriving clauses themselves.

Fixes tweag/ormolu#968
brandonchinn178 pushed a commit to fourmolu/fourmolu that referenced this issue Nov 4, 2024
This adds two configuration options for sorting a) types in deriving
clauses, and b) deriving clauses themselves.

Fixes tweag/ormolu#968
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
style Nitpicking and things related to purely visual aspect for formatting.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants