summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJasper Van der Jeugt <jaspervdj@gmail.com>2012-11-05 01:59:12 -0800
committerJasper Van der Jeugt <jaspervdj@gmail.com>2012-11-05 01:59:12 -0800
commitf9c412f27e5c839d2ddfddfee4d381e7ea75e057 (patch)
tree02a2aafbe8454dbc5bac6c8286ec78828d6946c4
parent7c96e69a4494aa0a1c41b1dcb5f65ff19bff64b6 (diff)
parent04982b5bba719d84fdd8dd567af5493abf0dbd60 (diff)
downloadstylish-haskell-f9c412f27e5c839d2ddfddfee4d381e7ea75e057.tar.gz
Merge pull request #20 from 23Skidoo/spurious-padding
Don't add paddding for non-existent qualified imports.
-rw-r--r--data/stylish-haskell.yaml3
-rw-r--r--src/Language/Haskell/Stylish/Config.hs1
-rw-r--r--src/Language/Haskell/Stylish/Step/Imports.hs14
-rw-r--r--tests/Language/Haskell/Stylish/Step/Imports/Tests.hs14
4 files changed, 29 insertions, 3 deletions
diff --git a/data/stylish-haskell.yaml b/data/stylish-haskell.yaml
index c128830..080c2f9 100644
--- a/data/stylish-haskell.yaml
+++ b/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).
#
diff --git a/src/Language/Haskell/Stylish/Config.hs b/src/Language/Haskell/Stylish/Config.hs
index 346fcad..2593004 100644
--- a/src/Language/Haskell/Stylish/Config.hs
+++ b/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)
]
diff --git a/src/Language/Haskell/Stylish/Step/Imports.hs b/src/Language/Haskell/Stylish/Step/Imports.hs
index 9d91995..476e6ec 100644
--- a/src/Language/Haskell/Stylish/Step/Imports.hs
+++ b/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
diff --git a/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs b/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs
index 78083f9..d3b205a 100644
--- a/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs
+++ b/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, (.!=), (.:), (.:?), (.=))"
+ ]