lspitzner / brittany

haskell source code formatter
GNU Affero General Public License v3.0
690 stars 72 forks source link

Sort imports #292

Closed lspitzner closed 3 years ago

lspitzner commented 4 years ago

See #155

This implements the basic functionality, including some trickery to handle comments sensibly.

Terminology:

import Foo ( MyData(MyExposedConstructor), myFunction )
                    \----constructor----/
             \----------item------------/  \--item--/
\----------------------import-------------------------/

import A                       \ import block
-- comment on why we import B  |
import qualified B             |
import C                       /

-- comment in the middle       | independent comment (not connected to import D)

import D ( a                   \ import block
         , b                   |
         , b                   |
         )                     | 
import E                       /

-- that is, blocks are series of import statements that don't contain any empty lines.

-- Merging (for example on constructors):
import A ( MyType(Foo), MyType(Bar) )
-- -->
import A ( MyType (Foo, Bar) )

-- question left for the reader: How can these be merged?
import qualified A (foo, bar)
import           A hiding (bar, baz)

Implemented features

Issues that must be fixed before merging

all other are non-blockers

Features yet to be implemented

expipiplus1 commented 4 years ago

@lspitzner I've got a large codebase which I've been deferring neatening imports on until brittany had support! I'll give this branch a test run this weekend.

expipiplus1 commented 4 years ago

@lspitzner works very well! https://github.com/expipiplus1/vulkan/commit/dcca2969c301c0a062c489150983a5f767f336f8

I like how it preserves groups of imports and doesn't barf on {-# SOURCE #-} imports.

If possible: when you live code next could it be friendly to Singapore time?

tfausak commented 4 years ago

I don't have a public codebase to point to, but this worked great on our ~80 KLOC project :+1:

expipiplus1 commented 4 years ago

@lspitzner This is a great improvement already, would it be possible to get this merged and move the exising TODO items to issues?

lspitzner commented 4 years ago

sorry I am so unresponsive atm. I have been / am still struggling to move all my maintenance and potentially CI over to nix. It has been a bit frustrating but I think it will be worth it in the long run.

There are two minor fixups I wanted to add on this branch, both simple "if this special case occurs, just fall back on not modifying the thing in question". These should indeed make this a useful addition without any downsides even for users that might have curious comments on their imports. Any other improvements can indeed be moved to separate issues.

So I am not taking a summer break, but the priorities will be nix > ghc810 > sorting imports.

expipiplus1 commented 4 years ago

@lspitzner Is there any specific help you could use on the nix side of things? FWIW this very simple github action (using cachix for caching) has been working well for me: https://github.com/expipiplus1/vulkan/blob/master/.github/workflows/ci.yml#L170-L186

expipiplus1 commented 3 years ago

I've been using this for quite some time, and think it's certainly stable enough to be merged as is, perhaps conditional on a config setting as it's considered incomplete.

tfausak commented 3 years ago

Closing this in favor of #330.