Skip to content

Commit 10ce71b

Browse files
committed
ModuleHeader: Add separate_lists option
See #320
1 parent 986cea9 commit 10ce71b

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

data/stylish-haskell.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ steps:
2727
# # Should export lists be sorted? Sorting is only performed within the
2828
# # export section, as delineated by Haddock comments.
2929
# sort: true
30+
#
31+
# # See `separate_lists` for the `imports` step.
32+
# separate_lists: true
3033

3134
# Format record definitions. This is disabled by default.
3235
#

lib/Language/Haskell/Stylish/Config.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,11 @@ parseEnum strs _ (Just k) = case lookup k strs of
195195
--------------------------------------------------------------------------------
196196
parseModuleHeader :: Config -> A.Object -> A.Parser Step
197197
parseModuleHeader _ o = fmap ModuleHeader.step $ ModuleHeader.Config
198-
<$> o A..:? "indent" A..!= (ModuleHeader.indent ModuleHeader.defaultConfig)
199-
<*> o A..:? "sort" A..!= (ModuleHeader.sort ModuleHeader.defaultConfig)
198+
<$> o A..:? "indent" A..!= ModuleHeader.indent def
199+
<*> o A..:? "sort" A..!= ModuleHeader.sort def
200+
<*> o A..:? "separate_lists" A..!= ModuleHeader.separateLists def
201+
where
202+
def = ModuleHeader.defaultConfig
200203

201204
--------------------------------------------------------------------------------
202205
parseSimpleAlign :: Config -> A.Object -> A.Parser Step

lib/Language/Haskell/Stylish/Step/ModuleHeader.hs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ import Language.Haskell.Stylish.Step
4040

4141

4242
data Config = Config
43-
-- TODO(jaspervdj): Use the same sorting as in `Imports`?
44-
-- TODO: make sorting optional?
45-
{ indent :: Int
46-
, sort :: Bool
43+
{ indent :: Int
44+
, sort :: Bool
45+
, separateLists :: Bool
4746
}
4847

4948
defaultConfig :: Config
5049
defaultConfig = Config
51-
{ indent = 4
52-
, sort = True
50+
{ indent = 4
51+
, sort = True
52+
, separateLists = True
5353
}
5454

5555
step :: Config -> Step
@@ -218,21 +218,23 @@ printExportList conf (L srcLoc exports) = do
218218
printExportsGroupTail (x : xs) = printExportsTail [([], x :| xs)]
219219
printExportsGroupTail [] = pure ()
220220

221+
-- NOTE(jaspervdj): This code is almost the same as the import printing
222+
-- in 'Imports' and should be merged.
221223
printExport :: GHC.LIE GhcPs -> P ()
222224
printExport (L _ export) = case export of
223225
IEVar _ name -> putOutputable name
224226
IEThingAbs _ name -> putOutputable name
225227
IEThingAll _ name -> do
226228
putOutputable name
227-
space
229+
when (separateLists conf) space
228230
putText "(..)"
229231
IEModuleContents _ (L _ m) -> do
230232
putText "module"
231233
space
232234
putText (showOutputable m)
233235
IEThingWith _ name _wildcard imps _ -> do
234236
putOutputable name
235-
space
237+
when (separateLists conf) space
236238
putText "("
237239
sep (comma >> space) $
238240
fmap putOutputable $ L.sortBy (compareWrappedName `on` unLoc) imps

tests/Language/Haskell/Stylish/Step/ModuleHeader/Tests.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ tests = testGroup "Language.Haskell.Stylish.Printer.ModuleHeader"
3434
, testCase "Indents with 2 spaces" ex14
3535
, testCase "Group doc with 2 spaces" ex15
3636
, testCase "Does not sort" ex16
37+
, testCase "Repects separate_lists" ex17
3738
]
3839

3940
--------------------------------------------------------------------------------
@@ -299,3 +300,14 @@ ex16 = assertSnippet (step defaultConfig {sort = False}) input input
299300
, " , no"
300301
, " ) where"
301302
]
303+
304+
ex17 :: Assertion
305+
ex17 = assertSnippet (step defaultConfig {separateLists = False})
306+
[ "module Foo"
307+
, " ( Bar (..)"
308+
, " ) where"
309+
]
310+
[ "module Foo"
311+
, " ( Bar(..)"
312+
, " ) where"
313+
]

0 commit comments

Comments
 (0)