diff options
author | Jasper Van der Jeugt <m@jaspervdj.be> | 2012-06-06 09:54:10 +0200 |
---|---|---|
committer | Jasper Van der Jeugt <m@jaspervdj.be> | 2012-06-06 09:54:10 +0200 |
commit | 6d815f4b4d3aeec8ad5b352eb9a29c124a47846c (patch) | |
tree | 5030b18a3129cd48ec2d35634b562a9ab4b568b3 | |
parent | 306b889e7e4bf808e7f2c8acd77ff35951344fed (diff) | |
download | stylish-haskell-6d815f4b4d3aeec8ad5b352eb9a29c124a47846c.tar.gz |
Different import alignment styles
-rw-r--r-- | .stylish-haskell.yaml | 15 | ||||
-rw-r--r-- | src/StylishHaskell/Config.hs | 10 | ||||
-rw-r--r-- | src/StylishHaskell/Step/Imports.hs | 40 | ||||
-rw-r--r-- | tests/StylishHaskell/Step/Imports/Tests.hs | 28 |
4 files changed, 74 insertions, 19 deletions
diff --git a/.stylish-haskell.yaml b/.stylish-haskell.yaml index e86b2bf..1dd89bf 100644 --- a/.stylish-haskell.yaml +++ b/.stylish-haskell.yaml @@ -4,14 +4,25 @@ steps: # Import cleanup - imports: - # Align import names and import lists. Default: true. - align: true + # There are different ways we can align names and lists. + # + # - global: Align the import names and import list throughout the entire + # file. + # + # - group: Only align the imports per group (a group is formed by adjacent + # import lines). + # + # - none: Do not perform any alignment. + # + # Default: global. + align: global # Language pragmas - language_pragmas: # We can generate different styles of language pragma lists. # # - vertical: Vertical-spaced language pragmas, one per line. + # # - compact: A more compact style. # # Default: vertical. diff --git a/src/StylishHaskell/Config.hs b/src/StylishHaskell/Config.hs index a4da1aa..9b533c6 100644 --- a/src/StylishHaskell/Config.hs +++ b/src/StylishHaskell/Config.hs @@ -46,7 +46,7 @@ instance FromJSON Config where -------------------------------------------------------------------------------- defaultConfig :: Config defaultConfig = Config $ - [ Imports.step True + [ Imports.step Imports.Global , LanguagePragmas.step LanguagePragmas.Vertical True , TrailingWhitespace.step ] @@ -132,7 +132,13 @@ parseEnum strs _ (Just k) = case lookup k strs of -------------------------------------------------------------------------------- parseImports :: A.Object -> A.Parser Step parseImports o = Imports.step - <$> o A..:? "align" A..!= True + <$> (o A..:? "align" >>= parseEnum aligns Imports.Global) + where + aligns = + [ ("global", Imports.Global) + , ("group", Imports.Group) + , ("none", Imports.None) + ] -------------------------------------------------------------------------------- diff --git a/src/StylishHaskell/Step/Imports.hs b/src/StylishHaskell/Step/Imports.hs index 6312aae..fc77c55 100644 --- a/src/StylishHaskell/Step/Imports.hs +++ b/src/StylishHaskell/Step/Imports.hs @@ -1,6 +1,7 @@ -------------------------------------------------------------------------------- module StylishHaskell.Step.Imports - ( step + ( Align (..) + , step ) where @@ -21,6 +22,14 @@ import StylishHaskell.Util -------------------------------------------------------------------------------- +data Align + = Global + | Group + | None + deriving (Eq, Show) + + +-------------------------------------------------------------------------------- imports :: H.Module l -> [H.ImportDecl l] imports (H.Module _ _ _ is _) = is imports _ = [] @@ -82,11 +91,11 @@ sortImportSpecs imp = imp {H.importSpecs = fmap sort $ H.importSpecs imp} -------------------------------------------------------------------------------- -prettyImport :: Bool -> Int -> H.ImportDecl l -> String -prettyImport align longest imp = unwords $ concat +prettyImport :: Bool -> Bool -> Int -> H.ImportDecl l -> String +prettyImport padQualified padName longest imp = unwords $ concat [ ["import"] , qualified - , [(if hasExtras && align then padRight longest else id) (importName imp)] + , [(if hasExtras && padName then padRight longest else id) (importName imp)] , ["as " ++ as | H.ModuleName _ as <- maybeToList $ H.importAs imp] , [H.prettyPrint specs | specs <- maybeToList $ H.importSpecs imp] ] @@ -95,23 +104,34 @@ prettyImport align longest imp = unwords $ concat qualified | H.importQualified imp = ["qualified"] - | align = [" "] + | padQualified = [" "] | otherwise = [] -------------------------------------------------------------------------------- -prettyImportGroup :: Bool -> Int -> [H.ImportDecl LineBlock] -> Lines -prettyImportGroup align longest = - map (prettyImport align longest) . sortBy compareImports +prettyImportGroup :: Align -> Int -> [H.ImportDecl LineBlock] -> Lines +prettyImportGroup align longest imps = + map (prettyImport padQual padName longest') $ sortBy compareImports imps + where + longest' = case align of + Group -> longestImport imps + _ -> longest + + padName = align /= None + + padQual = case align of + Global -> True + Group -> any H.importQualified imps + None -> False -------------------------------------------------------------------------------- -step :: Bool -> Step +step :: Align -> Step step = makeStep "Imports" . step' -------------------------------------------------------------------------------- -step' :: Bool -> Lines -> Module -> Lines +step' :: Align -> Lines -> Module -> Lines step' align ls (module', _) = flip applyChanges ls [ change block (prettyImportGroup align longest importGroup) | (block, importGroup) <- groups diff --git a/tests/StylishHaskell/Step/Imports/Tests.hs b/tests/StylishHaskell/Step/Imports/Tests.hs index e86282f..45a821d 100644 --- a/tests/StylishHaskell/Step/Imports/Tests.hs +++ b/tests/StylishHaskell/Step/Imports/Tests.hs @@ -20,6 +20,7 @@ tests :: Test tests = testGroup "StylishHaskell.Step.Imports.Tests" [ case01 , case02 + , case03 ] @@ -32,7 +33,7 @@ input = unlines , "import Control.Monad" , "import Data.Map (lookup, (!), insert, Map)" , "" - , "import Herp.Derp.Internals" + , "import Herp.Derp.Internals hiding (foo)" , "" , "herp = putStrLn \"import Hello world\"" ] @@ -40,7 +41,7 @@ input = unlines -------------------------------------------------------------------------------- case01 :: Test -case01 = testCase "case 01" $ expected @=? testStep (step True) input +case01 = testCase "case 01" $ expected @=? testStep (step Global) input where expected = unlines [ "module Herp where" @@ -49,7 +50,7 @@ case01 = testCase "case 01" $ expected @=? testStep (step True) input , "import Data.Map (Map, insert, lookup, (!))" , "import qualified Data.Map as M" , "" - , "import Herp.Derp.Internals" + , "import Herp.Derp.Internals hiding (foo)" , "" , "herp = putStrLn \"import Hello world\"" ] @@ -57,7 +58,24 @@ case01 = testCase "case 01" $ expected @=? testStep (step True) input -------------------------------------------------------------------------------- case02 :: Test -case02 = testCase "case 02" $ expected @=? testStep (step False) input +case02 = testCase "case 02" $ expected @=? testStep (step Groups) input + where + expected = unlines + [ "module Herp where" + , "" + , "import Control.Monad" + , "import Data.Map (Map, insert, lookup, (!))" + , "import qualified Data.Map as M" + , "" + , "import Herp.Derp.Internals hiding (foo)" + , "" + , "herp = putStrLn \"import Hello world\"" + ] + + +-------------------------------------------------------------------------------- +case03 :: Test +case03 = testCase "case 03" $ expected @=? testStep (step None) input where expected = unlines [ "module Herp where" @@ -66,7 +84,7 @@ case02 = testCase "case 02" $ expected @=? testStep (step False) input , "import Data.Map (Map, insert, lookup, (!))" , "import qualified Data.Map as M" , "" - , "import Herp.Derp.Internals" + , "import Herp.Derp.Internals hiding (foo)" , "" , "herp = putStrLn \"import Hello world\"" ] |