From 8dfa649696c6321b34bdd124020d8afc03c8c9bc Mon Sep 17 00:00:00 2001 From: Yuriy Syrovetskiy Date: Mon, 19 Jun 2017 14:54:06 +0300 Subject: Add `imports.list_same_align` option --- data/stylish-haskell.yaml | 19 +++++++ lib/Language/Haskell/Stylish/Config.hs | 1 + lib/Language/Haskell/Stylish/Step/Imports.hs | 5 +- .../Language/Haskell/Stylish/Step/Imports/Tests.hs | 59 +++++++++++++++------- 4 files changed, 66 insertions(+), 18 deletions(-) diff --git a/data/stylish-haskell.yaml b/data/stylish-haskell.yaml index 8c57f51..a5789d1 100644 --- a/data/stylish-haskell.yaml +++ b/data/stylish-haskell.yaml @@ -64,6 +64,25 @@ steps: # Default: after_alias list_align: after_alias + # Keep the same alignment between imports in a group: + # + # - true: a little more readable + # + # > import qualified Data.List as List (concat, foldl, foldr, + # > init, last, length) + # > import qualified Data.List.Extra as List (concat, foldl, foldr, + # > init, last, length) + # + # - false: diff-safe + # + # > import qualified Data.List as List (concat, foldl, foldr, init, + # > last, length) + # > import qualified Data.List.Extra as List (concat, foldl, foldr, + # > init, last, length) + # + # Default: true + list_same_align: true + # Long list align style takes effect when import is too long. This is # determined by 'columns' setting. # diff --git a/lib/Language/Haskell/Stylish/Config.hs b/lib/Language/Haskell/Stylish/Config.hs index 667a6eb..3f9efc5 100644 --- a/lib/Language/Haskell/Stylish/Config.hs +++ b/lib/Language/Haskell/Stylish/Config.hs @@ -184,6 +184,7 @@ parseImports config o = Imports.step <*> (Imports.Options <$> (o A..:? "align" >>= parseEnum aligns (def Imports.importAlign)) <*> (o A..:? "list_align" >>= parseEnum listAligns (def Imports.listAlign)) + <*> (o A..:? "list_same_align" A..!= def Imports.listSameAlign) <*> (o A..:? "long_list_align" >>= parseEnum longListAligns (def Imports.longListAlign)) -- Note that padding has to be at least 1. Default is 4. diff --git a/lib/Language/Haskell/Stylish/Step/Imports.hs b/lib/Language/Haskell/Stylish/Step/Imports.hs index 38ced99..2580502 100644 --- a/lib/Language/Haskell/Stylish/Step/Imports.hs +++ b/lib/Language/Haskell/Stylish/Step/Imports.hs @@ -38,6 +38,7 @@ import Language.Haskell.Stylish.Util data Options = Options { importAlign :: ImportAlign , listAlign :: ListAlign + , listSameAlign :: Bool , longListAlign :: LongListAlign , emptyListAlign :: EmptyListAlign , listPadding :: ListPadding @@ -49,6 +50,7 @@ defaultOptions :: Options defaultOptions = Options { importAlign = Global , listAlign = AfterAlias + , listSameAlign = True , longListAlign = Inline , emptyListAlign = Inherit , listPadding = LPConstant 4 @@ -386,12 +388,13 @@ prettyImportGroup columns align fileAlign longest imps = sortBy compareImports imps where align' = importAlign align + sameAlign = listSameAlign align longest' = case align' of Group -> longestImport imps _ -> longest - padName = align' /= None + padName = align' /= None && sameAlign padQual = case align' of Global -> True diff --git a/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs b/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs index 8f9abb7..bc6772c 100644 --- a/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs +++ b/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs @@ -51,6 +51,7 @@ tests = testGroup "Language.Haskell.Stylish.Step.Imports.Tests" , testCase "case 22" case22 , testCase "case 23" case23 , testCase "case 24" case24 + , testCase "case 25" case25 ] @@ -190,7 +191,7 @@ case07 = expected @=? testStep (step 80 $ fromImportAlign File) input' -------------------------------------------------------------------------------- case08 :: Assertion case08 = expected - @=? testStep (step 80 $ Options Global WithAlias Inline Inherit (LPConstant 4) True False) input + @=? testStep (step 80 $ Options Global WithAlias True Inline Inherit (LPConstant 4) True False) input where expected = unlines [ "module Herp where" @@ -213,7 +214,7 @@ case08 = expected -------------------------------------------------------------------------------- case09 :: Assertion case09 = expected - @=? testStep (step 80 $ Options Global WithAlias Multiline Inherit (LPConstant 4) True False) input + @=? testStep (step 80 $ Options Global WithAlias True Multiline Inherit (LPConstant 4) True False) input where expected = unlines [ "module Herp where" @@ -247,7 +248,7 @@ case09 = expected -------------------------------------------------------------------------------- case10 :: Assertion case10 = expected - @=? testStep (step 40 $ Options Group WithAlias Multiline Inherit (LPConstant 4) True False) input + @=? testStep (step 40 $ Options Group WithAlias True Multiline Inherit (LPConstant 4) True False) input where expected = unlines [ "module Herp where" @@ -286,7 +287,7 @@ case10 = expected -------------------------------------------------------------------------------- case11 :: Assertion case11 = expected - @=? testStep (step 80 $ Options Group NewLine Inline Inherit (LPConstant 4) True False) input + @=? testStep (step 80 $ Options Group NewLine True Inline Inherit (LPConstant 4) True False) input where expected = unlines [ "module Herp where" @@ -314,7 +315,7 @@ case11 = expected -------------------------------------------------------------------------------- case12 :: Assertion case12 = expected - @=? testStep (step 80 $ Options Group NewLine Inline Inherit (LPConstant 2) True False) input' + @=? testStep (step 80 $ Options Group NewLine True Inline Inherit (LPConstant 2) True False) input' where input' = unlines [ "import Data.List (map)" @@ -329,7 +330,7 @@ case12 = expected -------------------------------------------------------------------------------- case13 :: Assertion case13 = expected - @=? testStep (step 80 $ Options None WithAlias InlineWithBreak Inherit (LPConstant 4) True False) input' + @=? testStep (step 80 $ Options None WithAlias True InlineWithBreak Inherit (LPConstant 4) True False) input' where input' = unlines [ "import qualified Data.List as List (concat, foldl, foldr, head, init," @@ -347,7 +348,7 @@ case13 = expected case14 :: Assertion case14 = expected @=? testStep - (step 80 $ Options None WithAlias InlineWithBreak Inherit (LPConstant 10) True False) expected + (step 80 $ Options None WithAlias True InlineWithBreak Inherit (LPConstant 10) True False) expected where expected = unlines [ "import qualified Data.List as List (concat, map, null, reverse, tail, (++))" @@ -357,7 +358,7 @@ case14 = expected -------------------------------------------------------------------------------- case15 :: Assertion case15 = expected - @=? testStep (step 80 $ Options None AfterAlias Multiline Inherit (LPConstant 4) True False) input' + @=? testStep (step 80 $ Options None AfterAlias True Multiline Inherit (LPConstant 4) True False) input' where expected = unlines [ "import Data.Acid (AcidState)" @@ -383,7 +384,7 @@ case15 = expected -------------------------------------------------------------------------------- case16 :: Assertion case16 = expected - @=? testStep (step 80 $ Options None AfterAlias Multiline Inherit (LPConstant 4) False False) input' + @=? testStep (step 80 $ Options None AfterAlias True Multiline Inherit (LPConstant 4) False False) input' where expected = unlines [ "import Data.Acid (AcidState)" @@ -407,7 +408,7 @@ case16 = expected -------------------------------------------------------------------------------- case17 :: Assertion case17 = expected - @=? testStep (step 80 $ Options None AfterAlias Multiline Inherit (LPConstant 4) True False) input' + @=? testStep (step 80 $ Options None AfterAlias True Multiline Inherit (LPConstant 4) True False) input' where expected = unlines [ "import Control.Applicative (Applicative (pure, (<*>)))" @@ -425,7 +426,7 @@ case17 = expected -------------------------------------------------------------------------------- case18 :: Assertion case18 = expected @=? testStep - (step 40 $ Options None AfterAlias InlineToMultiline Inherit (LPConstant 4) True False) input' + (step 40 $ Options None AfterAlias True InlineToMultiline Inherit (LPConstant 4) True False) input' where expected = unlines ---------------------------------------- @@ -452,7 +453,7 @@ case18 = expected @=? testStep -------------------------------------------------------------------------------- case19 :: Assertion case19 = expected @=? testStep - (step 40 $ Options Global NewLine InlineWithBreak RightAfter (LPConstant 17) True False) case19input + (step 40 $ Options Global NewLine True InlineWithBreak RightAfter (LPConstant 17) True False) case19input where expected = unlines ---------------------------------------- @@ -467,7 +468,7 @@ case19 = expected @=? testStep case19b :: Assertion case19b = expected @=? testStep - (step 40 $ Options File NewLine InlineWithBreak RightAfter (LPConstant 17) True False) case19input + (step 40 $ Options File NewLine True InlineWithBreak RightAfter (LPConstant 17) True False) case19input where expected = unlines ---------------------------------------- @@ -482,7 +483,7 @@ case19b = expected @=? testStep case19c :: Assertion case19c = expected @=? testStep - (step 40 $ Options File NewLine InlineWithBreak RightAfter LPModuleName True False) case19input + (step 40 $ Options File NewLine True InlineWithBreak RightAfter LPModuleName True False) case19input where expected = unlines ---------------------------------------- @@ -497,7 +498,7 @@ case19c = expected @=? testStep case19d :: Assertion case19d = expected @=? testStep - (step 40 $ Options Global NewLine InlineWithBreak RightAfter LPModuleName True False) case19input + (step 40 $ Options Global NewLine True InlineWithBreak RightAfter LPModuleName True False) case19input where expected = unlines ---------------------------------------- @@ -595,7 +596,7 @@ case22 = expected -------------------------------------------------------------------------------- case23 :: Assertion case23 = expected - @=? testStep (step 40 $ Options None AfterAlias Inline Inherit (LPConstant 4) True True) input' + @=? testStep (step 40 $ Options None AfterAlias False Inline Inherit (LPConstant 4) True True) input' where expected = unlines [ "import Data.Acid ( AcidState )" @@ -619,7 +620,7 @@ case23 = expected -------------------------------------------------------------------------------- case24 :: Assertion case24 = expected - @=? testStep (step 40 $ Options None AfterAlias InlineWithBreak Inherit (LPConstant 4) True True) input' + @=? testStep (step 40 $ Options None AfterAlias False InlineWithBreak Inherit (LPConstant 4) True True) input' where expected = unlines [ "import Data.Acid ( AcidState )" @@ -638,3 +639,27 @@ case24 = expected , "import Data.ALongName.Foo (FooReallyLong, " ++ "GooReallyLong, BooReallyLong)" ] + +-------------------------------------------------------------------------------- +case25 :: Assertion +case25 = expected + @=? testStep (step 80 $ Options Group AfterAlias False Multiline Inherit (LPConstant 4) False False) input' + where + expected = unlines + [ "import Data.Acid (AcidState)" + , "import Data.Default.Class (Default(def))" + , "" + , "import Data.Maybe (Maybe(Just, Nothing))" + , "import qualified Data.Maybe.Extra (Maybe(Just, Nothing))" + , "" + , "import Data.Foo (Foo(Bar, Foo), Goo(Goo))" + ] + input' = unlines + [ "import Data.Acid (AcidState)" + , "import Data.Default.Class (Default(def))" + , "" + , "import Data.Maybe (Maybe (Just, Nothing))" + , "import qualified Data.Maybe.Extra (Maybe(Just, Nothing))" + , "" + , "import Data.Foo (Foo (Foo,Bar), Goo(Goo))" + ] -- cgit v1.2.3