diff options
author | Jasper Van der Jeugt <m@jaspervdj.be> | 2016-08-23 12:33:29 +0200 |
---|---|---|
committer | Jasper Van der Jeugt <m@jaspervdj.be> | 2016-08-23 12:33:29 +0200 |
commit | d5d03ca67c26960b53dd625493004c2df1a84125 (patch) | |
tree | 9174e9a41112be4159f0947207878ec40f7ce6b4 | |
parent | a6cb91f45079e9a96d5ed81dbc941ba8de6d3949 (diff) | |
download | stylish-haskell-d5d03ca67c26960b53dd625493004c2df1a84125.tar.gz |
Export Import options & add default
-rw-r--r-- | lib/Language/Haskell/Stylish.hs | 5 | ||||
-rw-r--r-- | lib/Language/Haskell/Stylish/Config.hs | 16 | ||||
-rw-r--r-- | lib/Language/Haskell/Stylish/Step/Imports.hs | 26 | ||||
-rw-r--r-- | stylish-haskell.cabal | 16 | ||||
-rw-r--r-- | tests/Language/Haskell/Stylish/Step/Imports/Tests.hs | 39 |
5 files changed, 55 insertions, 47 deletions
diff --git a/lib/Language/Haskell/Stylish.hs b/lib/Language/Haskell/Stylish.hs index 5b1e918..46543ec 100644 --- a/lib/Language/Haskell/Stylish.hs +++ b/lib/Language/Haskell/Stylish.hs @@ -9,9 +9,6 @@ module Language.Haskell.Stylish , tabs , trailingWhitespace , unicodeSyntax - -- ** Data types - , Imports.Align (..) - , LanguagePragmas.Style (..) -- ** Helpers , stepName -- * Config @@ -51,7 +48,7 @@ simpleAlign = SimpleAlign.step -------------------------------------------------------------------------------- imports :: Int -- ^ columns - -> Imports.Align + -> Imports.Options -> Step imports = Imports.step diff --git a/lib/Language/Haskell/Stylish/Config.hs b/lib/Language/Haskell/Stylish/Config.hs index 5092ed9..b83cf3a 100644 --- a/lib/Language/Haskell/Stylish/Config.hs +++ b/lib/Language/Haskell/Stylish/Config.hs @@ -181,17 +181,19 @@ parseSimpleAlign c o = SimpleAlign.step parseImports :: Config -> A.Object -> A.Parser Step parseImports config o = Imports.step <$> pure (configColumns config) - <*> (Imports.Align - <$> (o A..:? "align" >>= parseEnum aligns Imports.Global) - <*> (o A..:? "list_align" >>= parseEnum listAligns Imports.AfterAlias) + <*> (Imports.Options + <$> (o A..:? "align" >>= parseEnum aligns (def Imports.importAlign)) + <*> (o A..:? "list_align" >>= parseEnum listAligns (def Imports.listAlign)) <*> (o A..:? "long_list_align" - >>= parseEnum longListAligns Imports.Inline) + >>= parseEnum longListAligns (def Imports.longListAlign)) -- Note that padding has to be at least 1. Default is 4. <*> (o A..:? "empty_list_align" - >>= parseEnum emptyListAligns Imports.Inherit) - <*> o A..:? "list_padding" A..!= Imports.LPConstant 4 - <*> o A..:? "separate_lists" A..!= True) + >>= parseEnum emptyListAligns (def Imports.emptyListAlign)) + <*> o A..:? "list_padding" A..!= (def Imports.listPadding) + <*> o A..:? "separate_lists" A..!= (def Imports.separateLists)) where + def f = f Imports.defaultOptions + aligns = [ ("global", Imports.Global) , ("file", Imports.File) diff --git a/lib/Language/Haskell/Stylish/Step/Imports.hs b/lib/Language/Haskell/Stylish/Step/Imports.hs index d0519ef..334fa15 100644 --- a/lib/Language/Haskell/Stylish/Step/Imports.hs +++ b/lib/Language/Haskell/Stylish/Step/Imports.hs @@ -2,7 +2,8 @@ {-# LANGUAGE OverloadedStrings #-} -------------------------------------------------------------------------------- module Language.Haskell.Stylish.Step.Imports - ( Align (..) + ( Options (..) + , defaultOptions , ImportAlign (..) , ListAlign (..) , LongListAlign (..) @@ -31,15 +32,24 @@ import Language.Haskell.Stylish.Step import Language.Haskell.Stylish.Util -------------------------------------------------------------------------------- -data Align = Align +data Options = Options { importAlign :: ImportAlign , listAlign :: ListAlign , longListAlign :: LongListAlign , emptyListAlign :: EmptyListAlign , listPadding :: ListPadding , separateLists :: Bool + } deriving (Eq, Show) + +defaultOptions :: Options +defaultOptions = Options + { importAlign = Global + , listAlign = AfterAlias + , longListAlign = Inline + , emptyListAlign = Inherit + , listPadding = LPConstant 4 + , separateLists = True } - deriving (Eq, Show) data ListPadding = LPConstant Int @@ -152,8 +162,8 @@ prettyImportSpec separate = prettyImportSpec' -------------------------------------------------------------------------------- prettyImport :: (Ord l, Show l) => - Int -> Align -> Bool -> Bool -> Int -> H.ImportDecl l -> [String] -prettyImport columns Align{..} padQualified padName longest imp + Int -> Options -> Bool -> Bool -> Int -> H.ImportDecl l -> [String] +prettyImport columns Options{..} padQualified padName longest imp | (void `fmap` H.importSpecs imp) == emptyImportSpec = emptyWrap | otherwise = case longListAlign of Inline -> inlineWrap @@ -259,7 +269,7 @@ prettyImport columns Align{..} padQualified padName longest imp -------------------------------------------------------------------------------- -prettyImportGroup :: Int -> Align -> Bool -> Int +prettyImportGroup :: Int -> Options -> Bool -> Int -> [H.ImportDecl LineBlock] -> Lines prettyImportGroup columns align fileAlign longest imps = @@ -282,12 +292,12 @@ prettyImportGroup columns align fileAlign longest imps = -------------------------------------------------------------------------------- -step :: Int -> Align -> Step +step :: Int -> Options -> Step step columns = makeStep "Imports" . step' columns -------------------------------------------------------------------------------- -step' :: Int -> Align -> Lines -> Module -> Lines +step' :: Int -> Options -> Lines -> Module -> Lines step' columns align ls (module', _) = applyChanges [ change block $ const $ prettyImportGroup columns align fileAlign longest importGroup diff --git a/stylish-haskell.cabal b/stylish-haskell.cabal index 74bb4c4..e51114f 100644 --- a/stylish-haskell.cabal +++ b/stylish-haskell.cabal @@ -25,10 +25,18 @@ Extra-source-files: CHANGELOG Library - Exposed-modules: Language.Haskell.Stylish Hs-source-dirs: lib Ghc-options: -Wall + Exposed-modules: + Language.Haskell.Stylish + Language.Haskell.Stylish.Step.Imports + Language.Haskell.Stylish.Step.LanguagePragmas + Language.Haskell.Stylish.Step.SimpleAlign + Language.Haskell.Stylish.Step.Tabs + Language.Haskell.Stylish.Step.TrailingWhitespace + Language.Haskell.Stylish.Step.UnicodeSyntax + Other-modules: Language.Haskell.Stylish.Align Language.Haskell.Stylish.Block @@ -36,12 +44,6 @@ Library Language.Haskell.Stylish.Editor Language.Haskell.Stylish.Parse Language.Haskell.Stylish.Step - Language.Haskell.Stylish.Step.SimpleAlign - Language.Haskell.Stylish.Step.Imports - Language.Haskell.Stylish.Step.LanguagePragmas - Language.Haskell.Stylish.Step.Tabs - Language.Haskell.Stylish.Step.TrailingWhitespace - Language.Haskell.Stylish.Step.UnicodeSyntax Language.Haskell.Stylish.Util Language.Haskell.Stylish.Verbose Paths_stylish_haskell diff --git a/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs b/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs index 3a839cb..5ca60de 100644 --- a/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs +++ b/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs @@ -15,14 +15,11 @@ import Language.Haskell.Stylish.Step.Imports import Language.Haskell.Stylish.Tests.Util --------------------------------------------------------------------------------- -defaultAlign :: Align -defaultAlign = Align Global AfterAlias Inline Inherit (LPConstant 4) True - -------------------------------------------------------------------------------- -fromImportAlign :: ImportAlign -> Align -fromImportAlign align = defaultAlign { importAlign = align } +fromImportAlign :: ImportAlign -> Options +fromImportAlign align = defaultOptions { importAlign = align } + -------------------------------------------------------------------------------- tests :: Test @@ -188,7 +185,7 @@ case07 = expected @=? testStep (step 80 $ fromImportAlign File) input' -------------------------------------------------------------------------------- case08 :: Assertion case08 = expected - @=? testStep (step 80 $ Align Global WithAlias Inline Inherit (LPConstant 4) True) input + @=? testStep (step 80 $ Options Global WithAlias Inline Inherit (LPConstant 4) True) input where expected = unlines [ "module Herp where" @@ -211,7 +208,7 @@ case08 = expected -------------------------------------------------------------------------------- case09 :: Assertion case09 = expected - @=? testStep (step 80 $ Align Global WithAlias Multiline Inherit (LPConstant 4) True) input + @=? testStep (step 80 $ Options Global WithAlias Multiline Inherit (LPConstant 4) True) input where expected = unlines [ "module Herp where" @@ -245,7 +242,7 @@ case09 = expected -------------------------------------------------------------------------------- case10 :: Assertion case10 = expected - @=? testStep (step 40 $ Align Group WithAlias Multiline Inherit (LPConstant 4) True) input + @=? testStep (step 40 $ Options Group WithAlias Multiline Inherit (LPConstant 4) True) input where expected = unlines [ "module Herp where" @@ -284,7 +281,7 @@ case10 = expected -------------------------------------------------------------------------------- case11 :: Assertion case11 = expected - @=? testStep (step 80 $ Align Group NewLine Inline Inherit (LPConstant 4) True) input + @=? testStep (step 80 $ Options Group NewLine Inline Inherit (LPConstant 4) True) input where expected = unlines [ "module Herp where" @@ -312,7 +309,7 @@ case11 = expected -------------------------------------------------------------------------------- case12 :: Assertion case12 = expected - @=? testStep (step 80 $ Align Group NewLine Inline Inherit (LPConstant 2) True) input' + @=? testStep (step 80 $ Options Group NewLine Inline Inherit (LPConstant 2) True) input' where input' = unlines [ "import Data.List (map)" @@ -327,7 +324,7 @@ case12 = expected -------------------------------------------------------------------------------- case13 :: Assertion case13 = expected - @=? testStep (step 80 $ Align None WithAlias InlineWithBreak Inherit (LPConstant 4) True) input' + @=? testStep (step 80 $ Options None WithAlias InlineWithBreak Inherit (LPConstant 4) True) input' where input' = unlines [ "import qualified Data.List as List (concat, foldl, foldr, head, init," @@ -345,7 +342,7 @@ case13 = expected case14 :: Assertion case14 = expected @=? testStep - (step 80 $ Align None WithAlias InlineWithBreak Inherit (LPConstant 10) True) expected + (step 80 $ Options None WithAlias InlineWithBreak Inherit (LPConstant 10) True) expected where expected = unlines [ "import qualified Data.List as List (concat, map, null, reverse, tail, (++))" @@ -355,7 +352,7 @@ case14 = expected -------------------------------------------------------------------------------- case15 :: Assertion case15 = expected - @=? testStep (step 80 $ Align None AfterAlias Multiline Inherit (LPConstant 4) True) input' + @=? testStep (step 80 $ Options None AfterAlias Multiline Inherit (LPConstant 4) True) input' where expected = unlines [ "import Data.Acid (AcidState)" @@ -381,7 +378,7 @@ case15 = expected -------------------------------------------------------------------------------- case16 :: Assertion case16 = expected - @=? testStep (step 80 $ Align None AfterAlias Multiline Inherit (LPConstant 4) False) input' + @=? testStep (step 80 $ Options None AfterAlias Multiline Inherit (LPConstant 4) False) input' where expected = unlines [ "import Data.Acid (AcidState)" @@ -405,7 +402,7 @@ case16 = expected -------------------------------------------------------------------------------- case17 :: Assertion case17 = expected - @=? testStep (step 80 $ Align None AfterAlias Multiline Inherit (LPConstant 4) True) input' + @=? testStep (step 80 $ Options None AfterAlias Multiline Inherit (LPConstant 4) True) input' where expected = unlines [ "import Control.Applicative (Applicative (pure, (<*>)))" @@ -423,7 +420,7 @@ case17 = expected -------------------------------------------------------------------------------- case18 :: Assertion case18 = expected @=? testStep - (step 40 $ Align None AfterAlias InlineToMultiline Inherit (LPConstant 4) True) input' + (step 40 $ Options None AfterAlias InlineToMultiline Inherit (LPConstant 4) True) input' where expected = unlines ---------------------------------------- @@ -450,7 +447,7 @@ case18 = expected @=? testStep -------------------------------------------------------------------------------- case19 :: Assertion case19 = expected @=? testStep - (step 40 $ Align Global NewLine InlineWithBreak RightAfter (LPConstant 17) True) case19input + (step 40 $ Options Global NewLine InlineWithBreak RightAfter (LPConstant 17) True) case19input where expected = unlines ---------------------------------------- @@ -465,7 +462,7 @@ case19 = expected @=? testStep case19b :: Assertion case19b = expected @=? testStep - (step 40 $ Align File NewLine InlineWithBreak RightAfter (LPConstant 17) True) case19input + (step 40 $ Options File NewLine InlineWithBreak RightAfter (LPConstant 17) True) case19input where expected = unlines ---------------------------------------- @@ -480,7 +477,7 @@ case19b = expected @=? testStep case19c :: Assertion case19c = expected @=? testStep - (step 40 $ Align File NewLine InlineWithBreak RightAfter LPModuleName True) case19input + (step 40 $ Options File NewLine InlineWithBreak RightAfter LPModuleName True) case19input where expected = unlines ---------------------------------------- @@ -495,7 +492,7 @@ case19c = expected @=? testStep case19d :: Assertion case19d = expected @=? testStep - (step 40 $ Align Global NewLine InlineWithBreak RightAfter LPModuleName True) case19input + (step 40 $ Options Global NewLine InlineWithBreak RightAfter LPModuleName True) case19input where expected = unlines ---------------------------------------- |