Browse files

Add a new import alignment mode 'file'.

This mode is like 'global', but doesn't add paddding for non-existent qualified
imports.

For example, with 'global'

import A
import B

is always transformed to

import           A
import           B

even if there are no qualified imports in the whole file.
  • Loading branch information...
1 parent 7c96e69 commit 04982b5bba719d84fdd8dd567af5493abf0dbd60 @23Skidoo 23Skidoo committed Nov 3, 2012
View
3 data/stylish-haskell.yaml
@@ -22,6 +22,9 @@ steps:
# - global: Align the import names and import list throughout the entire
# file.
#
+ # - file: Like global, but don't add padding when there are no qualified
+ # imports in the file.
+ #
# - group: Only align the imports per group (a group is formed by adjacent
# import lines).
#
View
1 src/Language/Haskell/Stylish/Config.hs
@@ -159,6 +159,7 @@ parseImports config o = Imports.step
where
aligns =
[ ("global", Imports.Global)
+ , ("file", Imports.File)
, ("group", Imports.Group)
, ("none", Imports.None)
]
View
14 src/Language/Haskell/Stylish/Step/Imports.hs
@@ -24,6 +24,7 @@ import Language.Haskell.Stylish.Util
--------------------------------------------------------------------------------
data Align
= Global
+ | File
| Group
| None
deriving (Eq, Show)
@@ -144,8 +145,9 @@ prettyImport columns padQualified padName longest imp =
--------------------------------------------------------------------------------
-prettyImportGroup :: Int -> Align -> Int -> [H.ImportDecl LineBlock] -> Lines
-prettyImportGroup columns align longest imps =
+prettyImportGroup :: Int -> Align -> Bool -> Int -> [H.ImportDecl LineBlock]
+ -> Lines
+prettyImportGroup columns align padQual' longest imps =
concatMap (prettyImport columns padQual padName longest') $
sortBy compareImports imps
where
@@ -159,6 +161,7 @@ prettyImportGroup columns align longest imps =
Global -> True
Group -> any H.importQualified imps
None -> False
+ File -> padQual'
--------------------------------------------------------------------------------
@@ -169,10 +172,15 @@ step columns = makeStep "Imports" . step' columns
--------------------------------------------------------------------------------
step' :: Int -> Align -> Lines -> Module -> Lines
step' columns align ls (module', _) = flip applyChanges ls
- [ change block (const $ prettyImportGroup columns align longest importGroup)
+ [ change block (const $ prettyImportGroup columns align padQual
+ longest importGroup)
| (block, importGroup) <- groups
]
where
imps = map sortImportSpecs $ imports $ fmap linesFromSrcSpan module'
longest = longestImport imps
groups = groupAdjacent imps
+
+ padQual = case align of
+ File -> any H.importQualified imps
+ _ -> False
View
14 tests/Language/Haskell/Stylish/Step/Imports/Tests.hs
@@ -23,6 +23,7 @@ tests = testGroup "Language.Haskell.Stylish.Step.Imports.Tests"
, testCase "case 03" case03
, testCase "case 04" case04
, testCase "case 05" case05
+ , testCase "case 06" case06
]
@@ -121,3 +122,16 @@ case05 = input' @=? testStep (step 80 Group) input'
where
input' = "import Distribution.PackageDescription.Configuration " ++
"(finalizePackageDescription)\n"
+
+--------------------------------------------------------------------------------
+case06 :: Assertion
+case06 = expected @=? testStep (step 80 File) input'
+ where
+ input' =
+ "import Data.Aeson.Types (object, typeMismatch, FromJSON(..)," ++
+ "ToJSON(..), Value(..), parseEither, (.!=), (.:), (.:?), (.=))"
+
+ expected = unlines
+ [ "import Data.Aeson.Types (FromJSON (..), ToJSON (..), Value (..), object,"
+ , " parseEither, typeMismatch, (.!=), (.:), (.:?), (.=))"
+ ]

0 comments on commit 04982b5

Please sign in to comment.