summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorOndřej Janošík <j.ondra14@gmail.com>2015-09-21 01:12:29 +0200
committerOndřej Janošík <j.ondra14@gmail.com>2015-09-21 01:12:29 +0200
commit82ec3e1c458f01206c0230d4db1855c4fb6c64d8 (patch)
treefe13b64ab5ab40695df4ef86cb5dc19b486d46f5
parent1637b47a9c773b870ac39c773c72d9dd6e01412b (diff)
downloadstylish-haskell-82ec3e1c458f01206c0230d4db1855c4fb6c64d8.tar.gz
New import list option
new line-multiline: Import list will start on new line when it's short enough to fit to single line. Otherwise it'll be multiline.
-rw-r--r--data/stylish-haskell.yaml3
-rw-r--r--src/Language/Haskell/Stylish/Config.hs7
-rw-r--r--src/Language/Haskell/Stylish/Step/Imports.hs8
-rw-r--r--tests/Language/Haskell/Stylish/Step/Imports/Tests.hs31
4 files changed, 46 insertions, 3 deletions
diff --git a/data/stylish-haskell.yaml b/data/stylish-haskell.yaml
index 28b994e..86baae3 100644
--- a/data/stylish-haskell.yaml
+++ b/data/stylish-haskell.yaml
@@ -63,6 +63,9 @@ steps:
#
# - new line: Import list will start on new line.
#
+ # - new line-multiline: Import list will start on new line when it's
+ # short enough to fit to single line. Otherwise it'll be multiline.
+ #
# - multiline: One line per import list entry.
# Type with contructor list acts like single import.
#
diff --git a/src/Language/Haskell/Stylish/Config.hs b/src/Language/Haskell/Stylish/Config.hs
index b8ae588..271a461 100644
--- a/src/Language/Haskell/Stylish/Config.hs
+++ b/src/Language/Haskell/Stylish/Config.hs
@@ -187,9 +187,10 @@ parseImports config o = Imports.step
]
longListAligns =
- [ ("inline", Imports.Inline)
- , ("new line", Imports.InlineWithBreak)
- , ("multiline", Imports.Multiline)
+ [ ("inline", Imports.Inline)
+ , ("new line", Imports.InlineWithBreak)
+ , ("new line-multiline", Imports.InlineToMultiline)
+ , ("multiline", Imports.Multiline)
]
diff --git a/src/Language/Haskell/Stylish/Step/Imports.hs b/src/Language/Haskell/Stylish/Step/Imports.hs
index db4e63e..2cd8e6c 100644
--- a/src/Language/Haskell/Stylish/Step/Imports.hs
+++ b/src/Language/Haskell/Stylish/Step/Imports.hs
@@ -52,6 +52,7 @@ data ListAlign
data LongListAlign
= Inline
| InlineWithBreak
+ | InlineToMultiline
| Multiline
deriving (Eq, Show)
@@ -141,6 +142,7 @@ prettyImport columns Align{..} padQualified padName longest imp =
case longListAlign of
Inline -> inlineWrap
InlineWithBreak -> longListWrapper inlineWrap inlineWithBreakWrap
+ InlineToMultiline -> longListWrapper inlineWrap inlineToMultilineWrap
Multiline -> longListWrapper inlineWrap multilineWrap
where
longListWrapper shortWrap longWrap
@@ -169,6 +171,12 @@ prettyImport columns Align{..} padQualified padName longest imp =
. withHead ("(" ++)
. withLast (++ ")"))
+ inlineToMultilineWrap
+ | length inlineWithBreakWrap > 2
+ || any ((> columns) . length) (tail inlineWithBreakWrap)
+ = multilineWrap
+ | otherwise = inlineWithBreakWrap
+
-- 'wrapRest 0' ensures that every item of spec list is on new line.
multilineWrap = paddedNoSpecBase : wrapRest 0 listPadding
( mapSpecs
diff --git a/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs b/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs
index 36e54d9..4ed0bd6 100644
--- a/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs
+++ b/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs
@@ -44,6 +44,7 @@ tests = testGroup "Language.Haskell.Stylish.Step.Imports.Tests"
, testCase "case 15" case15
, testCase "case 16" case16
, testCase "case 17" case17
+ , testCase "case 18" case18
]
@@ -346,6 +347,7 @@ case14 = expected
[ "import qualified Data.List as List (concat, map, null, reverse, tail, (++))"
]
+
--------------------------------------------------------------------------------
case15 :: Assertion
case15 = expected
@@ -395,6 +397,7 @@ case16 = expected
, "import Data.Foo (Foo (Foo,Bar), Goo(Goo))"
]
+
--------------------------------------------------------------------------------
case17 :: Assertion
case17 = expected
@@ -411,3 +414,31 @@ case17 = expected
, ""
, "import Data.Identity (Identity (runIdentity,Identity))"
]
+
+
+--------------------------------------------------------------------------------
+case18 :: Assertion
+case18 = expected @=? testStep
+ (step 40 $ Align None AfterAlias InlineToMultiline 4 True) input'
+ where
+ expected = unlines
+ ----------------------------------------
+ [ "import Data.Foo as Foo (Bar, Baz, Foo)"
+ , ""
+ , "import Data.Identity"
+ , " (Identity (Identity, runIdentity))"
+ , ""
+ , "import Data.Acid as Acid"
+ , " ( closeAcidState"
+ , " , createCheckpoint"
+ , " , openLocalStateFrom"
+ , " )"
+ ]
+
+ input' = unlines
+ [ "import Data.Foo as Foo (Bar, Baz, Foo)"
+ , ""
+ , "import Data.Identity (Identity (Identity, runIdentity))"
+ , ""
+ , "import Data.Acid as Acid (closeAcidState, createCheckpoint, openLocalStateFrom)"
+ ]