Import lines not sorted #155
Labels
No Label
blocked: dependency
blocked: info-needed
bug
duplicate
enhancement
fixed in HEAD
help wanted
hs:arrows
hs:brackets
hs:classes
hs:comments
hs:do-notation
hs:guards
hs:lists
hs:operators
hs:patterns
hs:records
hs:types
invalid
language extension support
layouting
needs confirmation
priority: high
priority: low
question
revisit before next release
wontfix
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: hexagoxel/brittany#155
Loading…
Reference in New Issue
There is no content yet.
Delete Branch "%!s(<nil>)"
Deleting a branch is permanent. Although the deleted branch may exist for a short time before cleaning up, in most cases it CANNOT be undone. Continue?
Thought this would be an easy fix?
In no particular order, some thoughts:
uniq
after sorting (?)import MyMod(unordered, mess, here)
,import MyMod hiding (don't, want, these)
)It is an important feature that brittany preserves user formatting (blank lines and comments) and sorting at least seems to have the potential to clash with that. But I also agree that unsorted imports tend to get messy over time.
clang-format
sorts the equivalent of import lines (#include
andusing namespace
). It only sorts contiguous blocks. I think this is a good default behaviour that people may have come to expect.I agree; I think people will expect their groups to be preserved. The group itself should be sorted, but the groups and the order of the groups with respect to each other should be preserved.
I currently use
haskell-mode
in emacs to sort the imports - it uses fairly simple rules like above mentioned i.e., sort the imports in continuous block of text not separated by whitespace.Translating haskell-mode's sort function into haskell seems like a good idea to start with.
Although not exactly related, this is similar to #201.
The problem currently is that brittany breaks a single import across multiple lines, so you cannot select all imports and hit "sort". If there was an option to keep imports on a single line, this would be an ok workaround.
I have been working on this, but handling comments is complex as usual. :( Any input on the following example case:
The annoying part here is that the first comment is connected to the
CCC
import by exactprint right now, so even if we respect the "contiguous blocks" aspect and keep XXX at the top, the most straight-forward implementation still turns this into..But that can't be right. However I don't know how to decide when we want a comment to "belong" to an import statement next to it. I suppose in this case we would want this output:
but the general case logic is hard to capture. Also, this will most likely require completely reinterpreting the output from ghc-exactprint - we need to cleverly attribute each comment to the most fitting neighbor (which exactprint gets wrong) while also marking "free-standing" comments as "independent"/"first-class" so they don't get affected by sorting at all.
Also, I just notice that writing an algorithm on the plain textual data for imports would a) be relatively easy and b) make handling comments much less prone to error. Of course imports are simply a flat structure, from syntax-tree perspective, so it is somewhat expectable that you don't need a full parser here, but it is sad how ghc-exactprint turns this into a really complex exercise.
And this approach - working on the textual data with very superficial parsing - is probably exactly what
stylish-haskell
does. Hmm.Done as part of #330 and released in version 0.13.1.0.