Add support for sorting and aligning LANGUAGE pragmas #201
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#201
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?
I'd like to see similar behaviour to
stylish-haskell
where the pragmas are sorted,nub
ed, and aligned. I started looking into this a bit and the pragmas are stored as comments, so this might be a little complicated. @lspitzner I'd love some guidance on how to get started on this.thanks for the interest!
my first thought was: i bet there is a quick and dirty approach that works for 90% of cases that just works on the un-parsed lines and uses some variation of
span ("{-# LANGUAGE" `isPrefixOf`)
but after thinking a bit i think it might break quickly, see point 4) below.Before talking any implementation, I would like to nail down the desired behaviour a bit more. Even for such a simple thing there are some corner-cases:
I guess we want per-block sorting (and uniq). I.e.
turns into
We could treat "commented-out" ones like regular ones with respect to
sorting:
turns into
and also:
collapse commented/not commented pragmas:
turns into
transpose pragma-of-list into list-of-pragma
turns into
btw this is a good case against any line-based approach: the input might
be
Apart from commented-out-pragmas, other comments are treated like
whitespace (and consequently break up blocks).
This includes any comments before at file start that precede pragmas.
I have somewhat intentionally omitted the alignment aspect again here, because it is another instance of "context sensitivity": Do we really want to touch all pragmas because we add
-XGeneralizedNewtypeDeriving
? (Or whatever is the longest pragma :p)Assuming we are roughly in agreement on this behaviour, I think the implementation is just a plain recursion over the list of comments in the annotation. Must gather elements of a "blocks" (as discussed above); blocks are identified by DPs; for each completed block: stash the DP of the first comment and replace it with
DP (1,0)
, sort, uniq, then restore the DP of the (new) first comment to the stashed one; continue on remaining comments.The task of taking apart the pragma-of-list almost is sufficient reason to use some proper parser, but plain List functions will work too. No strong opinion - e.g. the existing
extractCommentConfigs
implementation is not pretty, but works without any additional dependencies *shrugs*, see621e00bf3f/src/Language/Haskell/Brittany/Internal.hs (L97-L114)
Thanks @lspitzner - I'm also working on a PR for sorting import lists and it uses a similar stash, sort, uniq, restore approach. I'll give it a go and get back to you!
Any news on the progress of this?
Just adding an note that this should only be in effect for appropriate
ColumnAlignMode
s 😉