summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRupert Horlick <ruhatch@me.com>2019-06-12 11:25:56 +0100
committerJasper Van der Jeugt <m@jaspervdj.be>2019-06-12 12:25:56 +0200
commit660b0c894461a780c7bda8f2231a5101f5f6f164 (patch)
tree11c27558df2db8c9e9906b5c65d9c093c3b33e11
parentf253f37362be840bafe2c82a8d7fb284046fda48 (diff)
downloadstylish-haskell-660b0c894461a780c7bda8f2231a5101f5f6f164.tar.gz
Add with_module_name option to list_align
-rw-r--r--data/stylish-haskell.yaml12
-rw-r--r--lib/Language/Haskell/Stylish/Config.hs7
-rw-r--r--lib/Language/Haskell/Stylish/Step/Imports.hs10
-rw-r--r--tests/Language/Haskell/Stylish/Step/Imports/Tests.hs114
4 files changed, 137 insertions, 6 deletions
diff --git a/data/stylish-haskell.yaml b/data/stylish-haskell.yaml
index 6d43499..4677b4a 100644
--- a/data/stylish-haskell.yaml
+++ b/data/stylish-haskell.yaml
@@ -56,6 +56,18 @@ steps:
# > import qualified Data.List as List (concat, foldl, foldr, head,
# > init, last, length)
#
+ # - with_module_name: Import list is aligned `list_padding` spaces after
+ # the module name.
+ #
+ # > import qualified Data.List as List (concat, foldl, foldr, head,
+ # init, last, length)
+ #
+ # This is mainly intended for use with `pad_module_names: false`.
+ #
+ # > import qualified Data.List as List (concat, foldl, foldr, head,
+ # init, last, length, scanl, scanr, take, drop,
+ # sort, nub)
+ #
# - new_line: Import list starts always on new line.
#
# > import qualified Data.List as List
diff --git a/lib/Language/Haskell/Stylish/Config.hs b/lib/Language/Haskell/Stylish/Config.hs
index ad30498..6513ff8 100644
--- a/lib/Language/Haskell/Stylish/Config.hs
+++ b/lib/Language/Haskell/Stylish/Config.hs
@@ -209,9 +209,10 @@ parseImports config o = Imports.step
]
listAligns =
- [ ("new_line", Imports.NewLine)
- , ("with_alias", Imports.WithAlias)
- , ("after_alias", Imports.AfterAlias)
+ [ ("new_line", Imports.NewLine)
+ , ("with_module_name", Imports.WithModuleName)
+ , ("with_alias", Imports.WithAlias)
+ , ("after_alias", Imports.AfterAlias)
]
longListAligns =
diff --git a/lib/Language/Haskell/Stylish/Step/Imports.hs b/lib/Language/Haskell/Stylish/Step/Imports.hs
index fc035a2..4ceb802 100644
--- a/lib/Language/Haskell/Stylish/Step/Imports.hs
+++ b/lib/Language/Haskell/Stylish/Step/Imports.hs
@@ -72,6 +72,7 @@ data ImportAlign
data ListAlign
= NewLine
+ | WithModuleName
| WithAlias
| AfterAlias
deriving (Eq, Show)
@@ -291,10 +292,11 @@ prettyImport columns Options{..} padQualified padName longest imp
. withLast (++ (maybeSpace ++ ")"))
inlineWrapper = case listAlign of
- NewLine -> (paddedNoSpecBase :) . wrapRest columns listPadding'
- WithAlias -> wrap columns paddedBase (inlineBaseLength + 1)
+ NewLine -> (paddedNoSpecBase :) . wrapRest columns listPadding'
+ WithModuleName -> wrap columns paddedBase (withModuleNameBaseLength + 4)
+ WithAlias -> wrap columns paddedBase (inlineBaseLength + 1)
-- Add 1 extra space to ensure same padding as in original code.
- AfterAlias -> withTail ((' ' : maybeSpace) ++)
+ AfterAlias -> withTail ((' ' : maybeSpace) ++)
. wrap columns paddedBase (afterAliasBaseLength + 1)
inlineWithBreakWrap = paddedNoSpecBase : wrapRest columns listPadding'
@@ -349,6 +351,8 @@ prettyImport columns Options{..} padQualified padName longest imp
inlineBaseLength = length $
base' (padImport $ compoundImportName imp) [] []
+ withModuleNameBaseLength = length $ base' "" [] []
+
afterAliasBaseLength = length $ base' (padImport $ compoundImportName imp)
["as " ++ as | H.ModuleName _ as <- maybeToList $ H.importAs imp] []
diff --git a/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs b/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs
index 67c7c5a..760018a 100644
--- a/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs
+++ b/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs
@@ -32,11 +32,15 @@ tests = testGroup "Language.Haskell.Stylish.Step.Imports.Tests"
, testCase "case 06" case06
, testCase "case 07" case07
, testCase "case 08" case08
+ , testCase "case 08b" case08b
, testCase "case 09" case09
, testCase "case 10" case10
, testCase "case 11" case11
+ , testCase "case 11b" case11b
, testCase "case 12" case12
+ , testCase "case 12b" case12b
, testCase "case 13" case13
+ , testCase "case 13b" case13b
, testCase "case 14" case14
, testCase "case 15" case15
, testCase "case 16" case16
@@ -50,6 +54,7 @@ tests = testGroup "Language.Haskell.Stylish.Step.Imports.Tests"
, testCase "case 21" case21
, testCase "case 22" case22
, testCase "case 23" case23
+ , testCase "case 23b" case23b
, testCase "case 24" case24
, testCase "case 25" case25
, testCase "case 26 (issue 185)" case26
@@ -213,6 +218,28 @@ case08 = expected
--------------------------------------------------------------------------------
+case08b :: Assertion
+case08b = expected
+ @=? testStep (step 80 $ Options Global WithModuleName True Inline Inherit (LPConstant 4) True False) input
+ where
+ expected = unlines
+ ["module Herp where"
+ , ""
+ , "import Control.Monad"
+ , "import Data.List as List (concat, foldl, foldr, head, init,"
+ , " last, length, map, null, reverse, tail, (++))"
+ , "import Data.Map (Map, insert, lookup, (!))"
+ , "import qualified Data.Map as M"
+ , "import Only.Instances ()"
+ , ""
+ , "import Foo (Bar (..))"
+ , "import Herp.Derp.Internals hiding (foo)"
+ , ""
+ , "herp = putStrLn \"import Hello world\""
+ ]
+
+
+--------------------------------------------------------------------------------
case09 :: Assertion
case09 = expected
@=? testStep (step 80 $ Options Global WithAlias True Multiline Inherit (LPConstant 4) True False) input
@@ -313,6 +340,27 @@ case11 = expected
]
+case11b :: Assertion
+case11b = expected
+ @=? testStep (step 80 $ Options Group WithModuleName True Inline Inherit (LPConstant 4) True False) input
+ where
+ expected = unlines
+ [ "module Herp where"
+ , ""
+ , "import Control.Monad"
+ , "import Data.List as List (concat, foldl, foldr, head, init, last,"
+ , " length, map, null, reverse, tail, (++))"
+ , "import Data.Map (Map, insert, lookup, (!))"
+ , "import qualified Data.Map as M"
+ , "import Only.Instances ()"
+ , ""
+ , "import Foo (Bar (..))"
+ , "import Herp.Derp.Internals hiding (foo)"
+ , ""
+ , "herp = putStrLn \"import Hello world\""
+ ]
+
+
--------------------------------------------------------------------------------
case12 :: Assertion
case12 = expected
@@ -329,6 +377,18 @@ case12 = expected
--------------------------------------------------------------------------------
+case12b :: Assertion
+case12b = expected
+ @=? testStep (step 80 $ Options Group WithModuleName True Inline Inherit (LPConstant 2) True False) input'
+ where
+ input' = unlines
+ [ "import Data.List (map)"
+ ]
+
+ expected = input'
+
+
+--------------------------------------------------------------------------------
case13 :: Assertion
case13 = expected
@=? testStep (step 80 $ Options None WithAlias True InlineWithBreak Inherit (LPConstant 4) True False) input'
@@ -346,6 +406,23 @@ case13 = expected
--------------------------------------------------------------------------------
+case13b :: Assertion
+case13b = expected
+ @=? testStep (step 80 $ Options None WithModuleName True InlineWithBreak Inherit (LPConstant 4) True False) input'
+ where
+ input' = unlines
+ [ "import qualified Data.List as List (concat, foldl, foldr, head, init,"
+ , " last, length, map, null, reverse, tail, (++))"
+ ]
+
+ expected = unlines
+ [ "import qualified Data.List as List"
+ , " (concat, foldl, foldr, head, init, last, length, map, null, reverse, tail,"
+ , " (++))"
+ ]
+
+
+--------------------------------------------------------------------------------
case14 :: Assertion
case14 = expected
@=? testStep
@@ -451,6 +528,7 @@ case18 = expected @=? testStep
, "import Data.Acid as Acid (closeAcidState, createCheckpoint, openLocalStateFrom)"
]
+
--------------------------------------------------------------------------------
case19 :: Assertion
case19 = expected @=? testStep
@@ -467,6 +545,7 @@ case19 = expected @=? testStep
, " intersperse)"
]
+
case19b :: Assertion
case19b = expected @=? testStep
(step 40 $ Options File NewLine True InlineWithBreak RightAfter (LPConstant 17) True False) case19input
@@ -482,6 +561,7 @@ case19b = expected @=? testStep
, " intersperse)"
]
+
case19c :: Assertion
case19c = expected @=? testStep
(step 40 $ Options File NewLine True InlineWithBreak RightAfter LPModuleName True False) case19input
@@ -497,6 +577,7 @@ case19c = expected @=? testStep
, " intersperse)"
]
+
case19d :: Assertion
case19d = expected @=? testStep
(step 40 $ Options Global NewLine True InlineWithBreak RightAfter LPModuleName True False) case19input
@@ -512,6 +593,7 @@ case19d = expected @=? testStep
, " intersperse)"
]
+
case19input :: String
case19input = unlines
[ "import Prelude.Compat hiding (foldMap)"
@@ -520,6 +602,7 @@ case19input = unlines
, "import Data.List (foldl', intercalate, intersperse)"
]
+
--------------------------------------------------------------------------------
case20 :: Assertion
case20 = expected
@@ -538,6 +621,7 @@ case20 = expected
, "import Data.Set (empty)"
]
+
--------------------------------------------------------------------------------
case21 :: Assertion
case21 = expected
@@ -568,6 +652,7 @@ case21 = expected
, "import X9 hiding (x, y, z, x)"
]
+
--------------------------------------------------------------------------------
case22 :: Assertion
case22 = expected
@@ -594,6 +679,7 @@ case22 = expected
"theLongestNameYet, shortName)"
]
+
--------------------------------------------------------------------------------
case23 :: Assertion
case23 = expected
@@ -618,6 +704,33 @@ case23 = expected
, "import Data.ALongName.Foo (Foo, Goo, Boo)"
]
+
+--------------------------------------------------------------------------------
+case23b :: Assertion
+case23b = expected
+ @=? testStep (step 40 $ Options None WithModuleName False Inline Inherit (LPConstant 4) True True) input'
+ where
+ expected = unlines
+ [ "import Data.Acid ( AcidState )"
+ , "import Data.Default.Class"
+ , " ( Default (def) )"
+ , ""
+ , "import Data.Monoid ( (<>) )"
+ , ""
+ , "import Data.ALongName.Foo ( Boo, Foo,"
+ , " Goo )"
+ ]
+
+ input' = unlines
+ [ "import Data.Acid (AcidState)"
+ , "import Data.Default.Class (Default(def))"
+ , ""
+ , "import Data.Monoid ((<>) )"
+ , ""
+ , "import Data.ALongName.Foo (Foo, Goo, Boo)"
+ ]
+
+
--------------------------------------------------------------------------------
case24 :: Assertion
case24 = expected
@@ -641,6 +754,7 @@ case24 = expected
"GooReallyLong, BooReallyLong)"
]
+
--------------------------------------------------------------------------------
case25 :: Assertion
case25 = expected