diff options
author | Ondřej Janošík <j.ondra14@gmail.com> | 2015-07-09 15:06:32 +0200 |
---|---|---|
committer | Ondřej Janošík <j.ondra14@gmail.com> | 2015-07-09 15:06:32 +0200 |
commit | 184591eda03be2de00613d1ddcc9234f2da95562 (patch) | |
tree | 3427691550cfda01267ad432587022e2ffd4c65f | |
parent | 729c0cf4d315133e92b4e5c5b92bdc5f2fab7ef8 (diff) | |
download | stylish-haskell-184591eda03be2de00613d1ddcc9234f2da95562.tar.gz |
Added option to break line with inline style only when import is too long
-rw-r--r-- | src/Language/Haskell/Stylish/Config.hs | 5 | ||||
-rw-r--r-- | src/Language/Haskell/Stylish/Step/Imports.hs | 18 | ||||
-rw-r--r-- | tests/Language/Haskell/Stylish/Step/Imports/Tests.hs | 29 |
3 files changed, 47 insertions, 5 deletions
diff --git a/src/Language/Haskell/Stylish/Config.hs b/src/Language/Haskell/Stylish/Config.hs index 9dcbb50..72b59ca 100644 --- a/src/Language/Haskell/Stylish/Config.hs +++ b/src/Language/Haskell/Stylish/Config.hs @@ -169,8 +169,8 @@ parseImports config o = Imports.step <*> (o A..:? "list_align" >>= parseEnum listAligns Imports.AfterAlias) <*> (o A..:? "long_list_align" >>= parseEnum longListAligns Imports.Inline) - <*> (o A..:? "list_padding" - >>= (return . maybe 4 (max 1)))) -- Padding have to be at least 1. + <*> (maybe 4 (max 1) <$> o A..:? "list_padding")) + -- ^ Padding have to be at least 1. Default is 4. where aligns = [ ("global", Imports.Global) @@ -187,6 +187,7 @@ parseImports config o = Imports.step longListAligns = [ ("inline", Imports.Inline) + , ("new line", Imports.InlineWithBreak) , ("multiline", Imports.Multiline) ] diff --git a/src/Language/Haskell/Stylish/Step/Imports.hs b/src/Language/Haskell/Stylish/Step/Imports.hs index f8361a8..b1f2bd9 100644 --- a/src/Language/Haskell/Stylish/Step/Imports.hs +++ b/src/Language/Haskell/Stylish/Step/Imports.hs @@ -49,6 +49,7 @@ data ListAlign data LongListAlign = Inline + | InlineWithBreak | Multiline deriving (Eq, Show) @@ -122,10 +123,14 @@ prettyImport :: Int -> Align -> Bool -> Bool -> Int -> H.ImportDecl l prettyImport columns Align{..} padQualified padName longest imp = case longListAlign of Inline -> inlineWrap - Multiline -> if listAlign == NewLine || length inlineWrap > 1 - then multilineWrap - else inlineWrap + InlineWithBreak -> longListWrapper inlineWrap inlineWithBreakWrap + Multiline -> longListWrapper inlineWrap multilineWrap where + longListWrapper shortWrap longWrap = + if listAlign == NewLine || length shortWrap > 1 + then longWrap + else shortWrap + inlineWrap = inlineWrapper $ mapSpecs $ withInit (++ ",") @@ -139,6 +144,13 @@ prettyImport columns Align{..} padQualified padName longest imp = AfterAlias -> withTail (' ' :) . wrap columns paddedBase (afterAliasBaseLength + 1) + inlineWithBreakWrap = paddedNoSpecBase : (wrapRest columns listPadding + $ mapSpecs + $ withInit (++ ",") + . withHead ("(" ++) + . withLast (++ ")")) + + -- 'wrapRest 0' ensures that every item of spec list is on new line. multilineWrap = paddedNoSpecBase : (wrapRest 0 listPadding $ (mapSpecs $ withHead ("( " ++) diff --git a/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs b/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs index 2b35f5a..2062fe0 100644 --- a/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs +++ b/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs @@ -39,6 +39,8 @@ tests = testGroup "Language.Haskell.Stylish.Step.Imports.Tests" , testCase "case 10" case10 , testCase "case 11" case11 , testCase "case 12" case12 + , testCase "case 13" case13 + , testCase "case 14" case14 ] @@ -307,3 +309,30 @@ case12 = expected @=? testStep (step 80 $ Align Group NewLine Inline 2) input' [ "import Data.List" , " (map)" ] + + +-------------------------------------------------------------------------------- +case13 :: Assertion +case13 = expected + @=? testStep (step 80 $ Align None WithAlias InlineWithBreak 4) 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 (step 80 $ Align None WithAlias InlineWithBreak 10) expected + where + expected = unlines + [ "import qualified Data.List as List (concat, map, null, reverse, tail, (++))" + ] |