Skip to content

Commit 2d54b73

Browse files
authored
Add break_only_where option (#325)
* add `break_only_where` option if false, will not drop the `where` in `module A where` when formatting
1 parent a0c4256 commit 2d54b73

File tree

4 files changed

+43
-24
lines changed

4 files changed

+43
-24
lines changed

data/stylish-haskell.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ steps:
3030
#
3131
# # See `separate_lists` for the `imports` step.
3232
# separate_lists: true
33+
#
34+
# # Whether to break the "where" if there are no exports.
35+
# break_only_where: false
3336

3437
# Format record definitions. This is disabled by default.
3538
#

lib/Language/Haskell/Stylish/Config.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,10 @@ parseEnum strs _ (Just k) = case lookup k strs of
197197
--------------------------------------------------------------------------------
198198
parseModuleHeader :: Config -> A.Object -> A.Parser Step
199199
parseModuleHeader _ o = fmap ModuleHeader.step $ ModuleHeader.Config
200-
<$> o A..:? "indent" A..!= ModuleHeader.indent def
201-
<*> o A..:? "sort" A..!= ModuleHeader.sort def
202-
<*> o A..:? "separate_lists" A..!= ModuleHeader.separateLists def
200+
<$> o A..:? "indent" A..!= ModuleHeader.indent def
201+
<*> o A..:? "sort" A..!= ModuleHeader.sort def
202+
<*> o A..:? "separate_lists" A..!= ModuleHeader.separateLists def
203+
<*> o A..:? "break_only_where" A..!= ModuleHeader.breakOnlyWhere def
203204
where
204205
def = ModuleHeader.defaultConfig
205206

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

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

4141

4242
data Config = Config
43-
{ indent :: Int
44-
, sort :: Bool
45-
, separateLists :: Bool
43+
{ indent :: Int
44+
, sort :: Bool
45+
, separateLists :: Bool
46+
, breakOnlyWhere :: Bool
4647
}
4748

4849
defaultConfig :: Config
4950
defaultConfig = Config
50-
{ indent = 4
51-
, sort = True
52-
, separateLists = True
51+
{ indent = 4
52+
, sort = True
53+
, separateLists = True
54+
, breakOnlyWhere = False
5355
}
5456

5557
step :: Config -> Step
@@ -140,10 +142,15 @@ printHeader conf mname mexps _ = do
140142
putText (showOutputable name)
141143
attachEolComment loc
142144

143-
maybe
144-
(when (isJust mname) do newline >> spaces (indent conf) >> putText "where")
145-
(printExportList conf)
146-
mexps
145+
case mexps of
146+
Nothing -> when (isJust mname) do
147+
if breakOnlyWhere conf
148+
then do
149+
newline
150+
spaces (indent conf)
151+
else space
152+
putText "where"
153+
Just exps -> printExportList conf exps
147154

148155
attachEolComment :: SrcSpan -> P ()
149156
attachEolComment = \case

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Language.Haskell.Stylish.Tests.Util
1717
--------------------------------------------------------------------------------
1818
tests :: Test
1919
tests = testGroup "Language.Haskell.Stylish.Printer.ModuleHeader"
20-
[ testCase "Hello world" ex0
20+
[ testCase "Does not indent absent export list" ex0
2121
, testCase "Empty exports list" ex1
2222
, testCase "Single exported variable" ex2
2323
, testCase "Multiple exported variables" ex3
@@ -29,22 +29,22 @@ tests = testGroup "Language.Haskell.Stylish.Printer.ModuleHeader"
2929
, testCase "Exports module" ex9
3030
, testCase "Exports symbol" ex10
3131
, testCase "Respects groups" ex11
32-
, testCase "'where' not repeated in case it isn't part of exports" ex12
33-
, testCase "Indents absent export list with 2 spaces" ex13
32+
, testCase "'where' not repeated when not part of exports with break_only_where" ex12
33+
, testCase "Indents absent export list with 2 spaces with break_only_where" ex13
3434
, testCase "Indents with 2 spaces" ex14
3535
, testCase "Group doc with 2 spaces" ex15
3636
, testCase "Does not sort" ex16
3737
, testCase "Repects separate_lists" ex17
38+
, testCase "Indents absent export list with break_only_where" ex18
3839
]
3940

4041
--------------------------------------------------------------------------------
4142
ex0 :: Assertion
42-
ex0 = assertSnippet (step defaultConfig)
43-
[ "module Foo where"
44-
]
45-
[ "module Foo"
46-
, " where"
47-
]
43+
ex0 = assertSnippet (step defaultConfig) input input
44+
where
45+
input =
46+
[ "module Foo where"
47+
]
4848

4949
ex1 :: Assertion
5050
ex1 = assertSnippet (step defaultConfig)
@@ -240,7 +240,7 @@ ex11 = assertSnippet (step defaultConfig)
240240
]
241241

242242
ex12 :: Assertion
243-
ex12 = assertSnippet (step defaultConfig)
243+
ex12 = assertSnippet (step defaultConfig {breakOnlyWhere = True})
244244
[ "module Foo"
245245
, " where"
246246
, "-- hmm"
@@ -251,7 +251,7 @@ ex12 = assertSnippet (step defaultConfig)
251251
]
252252

253253
ex13 :: Assertion
254-
ex13 = assertSnippet (step defaultConfig {indent = 2})
254+
ex13 = assertSnippet (step defaultConfig {breakOnlyWhere = True, indent = 2})
255255
[ "module Foo where"
256256
]
257257
[ "module Foo"
@@ -311,3 +311,11 @@ ex17 = assertSnippet (step defaultConfig {separateLists = False})
311311
, " ( Bar(..)"
312312
, " ) where"
313313
]
314+
315+
ex18 :: Assertion
316+
ex18 = assertSnippet (step defaultConfig {breakOnlyWhere = True})
317+
[ "module Foo where"
318+
]
319+
[ "module Foo"
320+
, " where"
321+
]

0 commit comments

Comments
 (0)