Skip to content

Commit

Permalink
Merge pull request #380 from adamse/pretty-imports
Browse files Browse the repository at this point in the history
Pretty print imports
  • Loading branch information
sighingnow authored Oct 15, 2017
2 parents 4556ff9 + 3c2db4e commit e7068cf
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
35 changes: 30 additions & 5 deletions TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,35 @@ import qualified MegaModule as M ((>>>), MonadBaseControl, void, MaybeT(..), joi

```haskell expect
import qualified MegaModule as M
(Either, Maybe(Just, Nothing), MaybeT(..),
Monad((>>), (>>=), return), MonadBaseControl, (<<<), (>>>), join,
liftIO, void)
( Either
, Maybe(Just, Nothing)
, MaybeT(..)
, Monad((>>), (>>=), return)
, MonadBaseControl
, (<<<)
, (>>>)
, join
, liftIO
, void
)
```

Pretty import specification

```haskell
import A hiding
( foobarbazqux
, foobarbazqux
, foobarbazqux
, foobarbazqux
, foobarbazqux
, foobarbazqux
, foobarbazqux
)

import Name hiding ()

import {-# SOURCE #-} safe qualified Module as M hiding (a, b, c, d, e, f)
```

# Declarations
Expand Down Expand Up @@ -940,8 +966,7 @@ import B
```

```haskell expect
import ATooLongList
(alpha, beta, delta, epsilon, eta, gamma, theta, zeta)
import ATooLongList (alpha, beta, delta, epsilon, eta, gamma, theta, zeta)
import B
```

Expand Down
33 changes: 31 additions & 2 deletions src/HIndent/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1538,14 +1538,43 @@ instance Pretty ModulePragma where
prettyInternal = pretty'

instance Pretty ImportDecl where
prettyInternal = pretty'
prettyInternal (ImportDecl _ name qualified source safe mpkg mas mspec) = do
write "import"
when source $ write " {-# SOURCE #-}"
when safe $ write " safe"
when qualified $ write " qualified"
case mpkg of
Nothing -> return ()
Just pkg -> space >> write pkg
space
pretty name
case mas of
Nothing -> return ()
Just asName -> do
space
write "as "
pretty asName
case mspec of
Nothing -> return ()
Just spec -> pretty spec

instance Pretty ModuleName where
prettyInternal (ModuleName _ name) =
write name

instance Pretty ImportSpecList where
prettyInternal = pretty'
prettyInternal (ImportSpecList _ hiding spec) = do
when hiding $ write " hiding"
let verVar = do
space
parens (commas (map pretty spec))
let horVar = do
newline
indentedBlock
(do depend (write "( ") (prefixedLined ", " (map pretty spec))
newline
write ")")
verVar `ifFitsOnOneLineOrElse` horVar

instance Pretty ImportSpec where
prettyInternal = pretty'
Expand Down

0 comments on commit e7068cf

Please sign in to comment.