summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJasper Van der Jeugt <jaspervdj@gmail.com>2016-08-02 11:16:23 +0200
committerGitHub <noreply@github.com>2016-08-02 11:16:23 +0200
commitab625813a8ebf69defe83745a71c7c085632ec23 (patch)
treeb0d0bfc207266a248bb2f629b0ba1bc4d8955b77
parentf344a0fbbe2431b138bec61eaa2d2280b962ee0e (diff)
parent85e582b7fdfcb257c26594cf4ec08fdef2dbfbdb (diff)
downloadstylish-haskell-ab625813a8ebf69defe83745a71c7c085632ec23.tar.gz
Merge pull request #123 from phadej/empty-list-align
Empty list align
-rw-r--r--data/stylish-haskell.yaml13
-rw-r--r--lib/Language/Haskell/Stylish/Config.hs7
-rw-r--r--lib/Language/Haskell/Stylish/Step/Imports.hs29
-rw-r--r--tests/Language/Haskell/Stylish/Step/Imports/Tests.hs48
4 files changed, 78 insertions, 19 deletions
diff --git a/data/stylish-haskell.yaml b/data/stylish-haskell.yaml
index 2398e6b..b8e569d 100644
--- a/data/stylish-haskell.yaml
+++ b/data/stylish-haskell.yaml
@@ -87,6 +87,19 @@ steps:
# Default: inline
long_list_align: inline
+ # Align empty list (importing instances)
+ #
+ # Empty list align has following options
+ #
+ # - inherit: inherit list_align setting
+ #
+ # - right_after: () is right after the module name:
+ #
+ # > import Vector.Instances ()
+ #
+ # Default: inherit
+ empty_list_align: inherit
+
# List padding determines indentation of import list on lines after import.
# This option affects 'list_align' and 'long_list_align'.
list_padding: 4
diff --git a/lib/Language/Haskell/Stylish/Config.hs b/lib/Language/Haskell/Stylish/Config.hs
index d14e1be..e551d71 100644
--- a/lib/Language/Haskell/Stylish/Config.hs
+++ b/lib/Language/Haskell/Stylish/Config.hs
@@ -187,6 +187,8 @@ parseImports config o = Imports.step
<*> (o A..:? "long_list_align"
>>= parseEnum longListAligns Imports.Inline)
-- Note that padding has to be at least 1. Default is 4.
+ <*> (o A..:? "empty_list_align"
+ >>= parseEnum emptyListAligns Imports.Inherit)
<*> (maybe 4 (max 1) <$> o A..:? "list_padding")
<*> o A..:? "separate_lists" A..!= True)
where
@@ -210,6 +212,11 @@ parseImports config o = Imports.step
, ("multiline", Imports.Multiline)
]
+ emptyListAligns =
+ [ ("inherit", Imports.Inherit)
+ , ("right_after", Imports.RightAfter)
+ ]
+
--------------------------------------------------------------------------------
parseLanguagePragmas :: Config -> A.Object -> A.Parser Step
diff --git a/lib/Language/Haskell/Stylish/Step/Imports.hs b/lib/Language/Haskell/Stylish/Step/Imports.hs
index ac84933..00ca9b7 100644
--- a/lib/Language/Haskell/Stylish/Step/Imports.hs
+++ b/lib/Language/Haskell/Stylish/Step/Imports.hs
@@ -5,12 +5,14 @@ module Language.Haskell.Stylish.Step.Imports
, ImportAlign (..)
, ListAlign (..)
, LongListAlign (..)
+ , EmptyListAlign (..)
, step
) where
--------------------------------------------------------------------------------
import Control.Arrow ((&&&))
+import Control.Monad (void)
import Data.Char (toLower)
import Data.List (intercalate, sortBy)
import Data.Maybe (isJust, maybeToList)
@@ -26,11 +28,12 @@ import Language.Haskell.Stylish.Util
--------------------------------------------------------------------------------
data Align = Align
- { importAlign :: ImportAlign
- , listAlign :: ListAlign
- , longListAlign :: LongListAlign
- , listPadding :: Int
- , separateLists :: Bool
+ { importAlign :: ImportAlign
+ , listAlign :: ListAlign
+ , longListAlign :: LongListAlign
+ , emptyListAlign :: EmptyListAlign
+ , listPadding :: Int
+ , separateLists :: Bool
}
deriving (Eq, Show)
@@ -47,6 +50,11 @@ data ListAlign
| AfterAlias
deriving (Eq, Show)
+data EmptyListAlign
+ = Inherit
+ | RightAfter
+ deriving (Eq, Show)
+
data LongListAlign
= Inline
| InlineWithBreak
@@ -136,13 +144,16 @@ prettyImportSpec separate = prettyImportSpec'
--------------------------------------------------------------------------------
prettyImport :: (Ord l, Show l) =>
Int -> Align -> Bool -> Bool -> Int -> H.ImportDecl l -> [String]
-prettyImport columns Align{..} padQualified padName longest imp =
- case longListAlign of
+prettyImport columns Align{..} padQualified padName longest imp
+ | (void `fmap` H.importSpecs imp) == emptyImportSpec = emptyWrap
+ | otherwise = case longListAlign of
Inline -> inlineWrap
InlineWithBreak -> longListWrapper inlineWrap inlineWithBreakWrap
InlineToMultiline -> longListWrapper inlineWrap inlineToMultilineWrap
Multiline -> longListWrapper inlineWrap multilineWrap
where
+ emptyImportSpec = Just (H.ImportSpecList () False [])
+
longListWrapper shortWrap longWrap
| listAlign == NewLine
|| length shortWrap > 1
@@ -150,6 +161,10 @@ prettyImport columns Align{..} padQualified padName longest imp =
= longWrap
| otherwise = shortWrap
+ emptyWrap = case emptyListAlign of
+ Inherit -> inlineWrap
+ RightAfter -> [paddedNoSpecBase ++ " ()"]
+
inlineWrap = inlineWrapper
$ mapSpecs
$ withInit (++ ",")
diff --git a/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs b/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs
index 4ed0bd6..4ebc050 100644
--- a/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs
+++ b/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs
@@ -17,7 +17,7 @@ import Language.Haskell.Stylish.Tests.Util
--------------------------------------------------------------------------------
defaultAlign :: Align
-defaultAlign = Align Global AfterAlias Inline 4 True
+defaultAlign = Align Global AfterAlias Inline Inherit 4 True
--------------------------------------------------------------------------------
@@ -45,6 +45,7 @@ tests = testGroup "Language.Haskell.Stylish.Step.Imports.Tests"
, testCase "case 16" case16
, testCase "case 17" case17
, testCase "case 18" case18
+ , testCase "case 19" case19
]
@@ -184,7 +185,7 @@ case07 = expected @=? testStep (step 80 $ fromImportAlign File) input'
--------------------------------------------------------------------------------
case08 :: Assertion
case08 = expected
- @=? testStep (step 80 $ Align Global WithAlias Inline 4 True) input
+ @=? testStep (step 80 $ Align Global WithAlias Inline Inherit 4 True) input
where
expected = unlines
[ "module Herp where"
@@ -207,7 +208,7 @@ case08 = expected
--------------------------------------------------------------------------------
case09 :: Assertion
case09 = expected
- @=? testStep (step 80 $ Align Global WithAlias Multiline 4 True) input
+ @=? testStep (step 80 $ Align Global WithAlias Multiline Inherit 4 True) input
where
expected = unlines
[ "module Herp where"
@@ -241,7 +242,7 @@ case09 = expected
--------------------------------------------------------------------------------
case10 :: Assertion
case10 = expected
- @=? testStep (step 40 $ Align Group WithAlias Multiline 4 True) input
+ @=? testStep (step 40 $ Align Group WithAlias Multiline Inherit 4 True) input
where
expected = unlines
[ "module Herp where"
@@ -280,7 +281,7 @@ case10 = expected
--------------------------------------------------------------------------------
case11 :: Assertion
case11 = expected
- @=? testStep (step 80 $ Align Group NewLine Inline 4 True) input
+ @=? testStep (step 80 $ Align Group NewLine Inline Inherit 4 True) input
where
expected = unlines
[ "module Herp where"
@@ -308,7 +309,7 @@ case11 = expected
--------------------------------------------------------------------------------
case12 :: Assertion
case12 = expected
- @=? testStep (step 80 $ Align Group NewLine Inline 2 True) input'
+ @=? testStep (step 80 $ Align Group NewLine Inline Inherit 2 True) input'
where
input' = unlines
[ "import Data.List (map)"
@@ -323,7 +324,7 @@ case12 = expected
--------------------------------------------------------------------------------
case13 :: Assertion
case13 = expected
- @=? testStep (step 80 $ Align None WithAlias InlineWithBreak 4 True) input'
+ @=? testStep (step 80 $ Align None WithAlias InlineWithBreak Inherit 4 True) input'
where
input' = unlines
[ "import qualified Data.List as List (concat, foldl, foldr, head, init,"
@@ -341,7 +342,7 @@ case13 = expected
case14 :: Assertion
case14 = expected
@=? testStep
- (step 80 $ Align None WithAlias InlineWithBreak 10 True) expected
+ (step 80 $ Align None WithAlias InlineWithBreak Inherit 10 True) expected
where
expected = unlines
[ "import qualified Data.List as List (concat, map, null, reverse, tail, (++))"
@@ -351,7 +352,7 @@ case14 = expected
--------------------------------------------------------------------------------
case15 :: Assertion
case15 = expected
- @=? testStep (step 80 $ Align None AfterAlias Multiline 4 True) input'
+ @=? testStep (step 80 $ Align None AfterAlias Multiline Inherit 4 True) input'
where
expected = unlines
[ "import Data.Acid (AcidState)"
@@ -377,7 +378,7 @@ case15 = expected
--------------------------------------------------------------------------------
case16 :: Assertion
case16 = expected
- @=? testStep (step 80 $ Align None AfterAlias Multiline 4 False) input'
+ @=? testStep (step 80 $ Align None AfterAlias Multiline Inherit 4 False) input'
where
expected = unlines
[ "import Data.Acid (AcidState)"
@@ -401,7 +402,7 @@ case16 = expected
--------------------------------------------------------------------------------
case17 :: Assertion
case17 = expected
- @=? testStep (step 80 $ Align None AfterAlias Multiline 4 True) input'
+ @=? testStep (step 80 $ Align None AfterAlias Multiline Inherit 4 True) input'
where
expected = unlines
[ "import Control.Applicative (Applicative (pure, (<*>)))"
@@ -419,7 +420,7 @@ case17 = expected
--------------------------------------------------------------------------------
case18 :: Assertion
case18 = expected @=? testStep
- (step 40 $ Align None AfterAlias InlineToMultiline 4 True) input'
+ (step 40 $ Align None AfterAlias InlineToMultiline Inherit 4 True) input'
where
expected = unlines
----------------------------------------
@@ -442,3 +443,26 @@ case18 = expected @=? testStep
, ""
, "import Data.Acid as Acid (closeAcidState, createCheckpoint, openLocalStateFrom)"
]
+
+--------------------------------------------------------------------------------
+case19 :: Assertion
+case19 = expected @=? testStep
+ (step 40 $ Align Global NewLine InlineWithBreak RightAfter 17 True) input'
+ where
+ expected = unlines
+ ----------------------------------------
+ [ "import Prelude ()"
+ , "import Prelude.Compat hiding"
+ , " (foldMap)"
+ , ""
+ , "import Data.List"
+ , " (foldl', intercalate,"
+ , " intersperse)"
+ ]
+
+ input' = unlines
+ [ "import Prelude.Compat hiding (foldMap)"
+ , "import Prelude ()"
+ , ""
+ , "import Data.List (foldl', intercalate, intersperse)"
+ ]