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
Sorting language pragmas should be more intelligent #404
Comments
An almost sorted list is worse than an unsorted list, IMO. It's going to be very nonobvious to anyone reading formatted code that it's not a bug/accident. |
What matters here I guess is that the order will be deterministic, thus it'll prevent diffs from shuffling. |
I'm not sure there's a unique most general almost sorted list, so I don't think it's obvious to choose a sorting algorithm. For instance, if you have pragmas
They're both equally far from being sorted. I'm not saying it shouldn't be done, just that the sorting won't be obvious. |
Almost sorted won't work. At the end of the day, Ormolu is a programmable formatter. You sketch out the format you want, and it does the rest. So the semantics of the source file, resulting from sorting the language pragmas, could well be the semantics the programmer intends, even if it's sometimes different from the input. The much simpler option is to allow pragmas to be grouped using intervening blank lines, and then to sort each group independently but not reorder the groups. |
I'd like to have a single section because it's more normalized that way. It's not a problem to invent an ordering. Returning to @teofr 's example, we could say that if X < Z, then we choose:
Or we could pick all extensions that disable things that not enabled by default and put them all after everything else. |
But you're ignoring @neongreen's concern. A very valid one. |
I think it's not a big deal because in practice in 99% of cases extensions will be simply alphabetically sorted. It's not common to enable an extension X and then add some more extensions to disable something that was enabled by X as side-effect. |
Even if a user decides that it is a bug, he/she will report it here to discover that it is not. |
Just to note, here is the relevant part of the GHC source code. Unfortunately it isn't exported from |
@utdemir if you make a patch to export it in the API next week, and ask for it to be in 8.10 will be available in the next release. |
The linked table has only two
I think we can ignore {-# LANGUAGE RebindableSyntax #-}
{-# LANGUAGE ImplicitPrelude #-}
main = if True then print "hi" else print "bye"
This leaves only the case where the user says Yes, we can no longer say "if it compiled before reformatting, it will compile after reformatting", but on the other hand we can prevent people from having to debug "why doesn't it compile even though I said NoX?" errors. Plus, I find myself somewhat persuaded by @mboes's comment:
|
Update: I changed my mind, one block of pragmas seems better than two blocks. Then we can have "one block = one type of pragmas" (LANGUAGE, OPTIONS_GHC, etc). |
This is not true. It is true that I regularly build student code with |
Oops, right. Then we can't dismiss this case. Ok, what about this? -- all extensions
-- all NoExtensions
-- CUSKs
-- ImplicitPrelude And in the meantime someone has to make a GHC patch so that eventually we'd be able to ask GHC for extension interactions instead of hard-coding the logic. |
It's worth asking: if someone asks for |
This was mentioned in #216, but it is a separate issue.
IOW, we should take into account that some pragmas that disable extensions enabled as side-effect of enabling other extensions should be put after those extensions because sometimes the order of extension pragmas matters.
The text was updated successfully, but these errors were encountered: