diff options
author | Jasper Van der Jeugt <m@jaspervdj.be> | 2012-07-31 10:39:24 +0200 |
---|---|---|
committer | Jasper Van der Jeugt <m@jaspervdj.be> | 2012-07-31 10:39:24 +0200 |
commit | b5f31ec2845ac5ebe3fb1f5b63f8723f072e46b6 (patch) | |
tree | fbc802b9633fb88593501952d57955ec2a46864c | |
parent | 1db6cc5767608e7512d1d0e02807d36c7c2355cb (diff) | |
download | stylish-haskell-b5f31ec2845ac5ebe3fb1f5b63f8723f072e46b6.tar.gz |
Clean up and test case for long imports
-rw-r--r-- | src/StylishHaskell/Step/Imports.hs | 33 | ||||
-rw-r--r-- | tests/StylishHaskell/Step/Imports/Tests.hs | 14 |
2 files changed, 26 insertions, 21 deletions
diff --git a/src/StylishHaskell/Step/Imports.hs b/src/StylishHaskell/Step/Imports.hs index 25be81e..3760e97 100644 --- a/src/StylishHaskell/Step/Imports.hs +++ b/src/StylishHaskell/Step/Imports.hs @@ -6,10 +6,9 @@ module StylishHaskell.Step.Imports -------------------------------------------------------------------------------- -import Control.Applicative ((<$>)) import Control.Arrow ((&&&)) import Data.Char (isAlpha, toLower) -import Data.List (sortBy) +import Data.List (intercalate, sortBy) import Data.Maybe (isJust, maybeToList) import Data.Ord (comparing) import qualified Language.Haskell.Exts.Annotated as H @@ -94,7 +93,14 @@ sortImportSpecs imp = imp {H.importSpecs = fmap sort $ H.importSpecs imp} -------------------------------------------------------------------------------- prettyImport :: Bool -> Bool -> Int -> H.ImportDecl l -> String prettyImport padQualified padName longest imp = - unlines specs + intercalate "\n" $ + wrap 80 base (length base + 1) $ + (if hiding then ("hiding" :) else id) $ + withInit (++ ",") $ + withHead ("(" ++) $ + withLast (++ ")") $ + map H.prettyPrint $ + importSpecs where base = unwords $ concat [ ["import"] @@ -104,23 +110,9 @@ prettyImport padQualified padName longest imp = , ["as " ++ as | H.ModuleName _ as <- maybeToList $ H.importAs imp] ] - - -- specs = unlines . indentSpecs . lines . H.prettyPrint <$> H.importSpecs imp - specs = wrap 80 base (length base + 1) $ - (if hiding then ("hiding" :) else id) $ - withInit (++ ",") $ - withHead ("(" ++) $ - withLast (++ ")") $ - map H.prettyPrint $ - importSpecs - - hiding = case H.importSpecs imp of - Just (H.ImportSpecList _ h _) -> h - _ -> False - - importSpecs = case H.importSpecs imp of - Just (H.ImportSpecList _ _ l) -> l - _ -> [] + (hiding, importSpecs) = case H.importSpecs imp of + Just (H.ImportSpecList _ h l) -> (h, l) + _ -> (False, []) hasExtras = isJust (H.importAs imp) || isJust (H.importSpecs imp) @@ -130,7 +122,6 @@ prettyImport padQualified padName longest imp = | otherwise = [] - -------------------------------------------------------------------------------- prettyImportGroup :: Align -> Int -> [H.ImportDecl LineBlock] -> Lines prettyImportGroup align longest imps = diff --git a/tests/StylishHaskell/Step/Imports/Tests.hs b/tests/StylishHaskell/Step/Imports/Tests.hs index ee55326..d443956 100644 --- a/tests/StylishHaskell/Step/Imports/Tests.hs +++ b/tests/StylishHaskell/Step/Imports/Tests.hs @@ -21,6 +21,7 @@ tests = testGroup "StylishHaskell.Step.Imports.Tests" [ case01 , case02 , case03 + , case04 ] @@ -92,3 +93,16 @@ case03 = testCase "case 03" $ expected @=? testStep (step None) input , "" , "herp = putStrLn \"import Hello world\"" ] + +case04 :: Test +case04 = testCase "case 04" $ expected @=? testStep (step Global) 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, (.!=), (.:), (.:?)," + , " (.=))" + ] |