Skip to content

Commit 3d53480

Browse files
authored
Add new configuration to ModuleHeader step (#357)
Add a new configuration to ModuleHeader step Previously the formatter of module header would always add open the bracket of the export list on the next line after the module name. With this change, we give the user the ability to customize this behavior. The open bracket ends up either on the same line as module name or on the next line. For backward compatibility, we keep the 'next_line' a default behavior.
1 parent a996292 commit 3d53480

File tree

4 files changed

+485
-10
lines changed

4 files changed

+485
-10
lines changed

data/stylish-haskell.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ steps:
4040
# # list contains comments as newlines will be required.
4141
# # - always: always break before the "where".
4242
# break_where: exports
43+
#
44+
# # Where to put open bracket
45+
# # Possible values:
46+
# # - same_line: put open bracket on the same line as the module name, before the
47+
# # comment of the module
48+
# # - next_line: put open bracket on the next line, after module comment
49+
# open_bracket: next_line
4350

4451
# Format record definitions. This is disabled by default.
4552
#

lib/Language/Haskell/Stylish/Config.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ parseModuleHeader config o = fmap (ModuleHeader.step columns) $ ModuleHeader.Con
201201
<*> (o A..:? "sort" A..!= ModuleHeader.sort def)
202202
<*> (o A..:? "separate_lists" A..!= ModuleHeader.separateLists def)
203203
<*> (o A..:? "break_where" >>= parseEnum breakWhere (ModuleHeader.breakWhere def))
204+
<*> (o A..:? "open_bracket" >>= parseEnum openBracket (ModuleHeader.openBracket def))
204205
where
205206
def = ModuleHeader.defaultConfig
206207

@@ -213,6 +214,11 @@ parseModuleHeader config o = fmap (ModuleHeader.step columns) $ ModuleHeader.Con
213214
, ("always", ModuleHeader.Always)
214215
]
215216

217+
openBracket =
218+
[ ("same_line", ModuleHeader.SameLine)
219+
, ("next_line", ModuleHeader.NextLine)
220+
]
221+
216222
--------------------------------------------------------------------------------
217223
parseSimpleAlign :: Config -> A.Object -> A.Parser Step
218224
parseSimpleAlign c o = SimpleAlign.step

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

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module Language.Haskell.Stylish.Step.ModuleHeader
44
( Config (..)
55
, BreakWhere (..)
6+
, OpenBracket (..)
67
, defaultConfig
78
, step
89
) where
@@ -45,20 +46,28 @@ data Config = Config
4546
, sort :: Bool
4647
, separateLists :: Bool
4748
, breakWhere :: BreakWhere
49+
, openBracket :: OpenBracket
4850
}
4951

52+
data OpenBracket
53+
= SameLine
54+
| NextLine
55+
deriving (Eq, Show)
56+
5057
data BreakWhere
5158
= Exports
5259
| Single
5360
| Inline
5461
| Always
62+
deriving (Eq, Show)
5563

5664
defaultConfig :: Config
5765
defaultConfig = Config
5866
{ indent = 4
5967
, sort = True
6068
, separateLists = True
6169
, breakWhere = Exports
70+
, openBracket = NextLine
6271
}
6372

6473
step :: Maybe Int -> Config -> Step
@@ -142,36 +151,64 @@ printHeader
142151
-> Maybe GHC.LHsDocString
143152
-> P ()
144153
printHeader conf mname mexps _ = do
145-
forM_ mname \(L loc name) -> do
154+
forM_ mname \(L _ name) -> do
146155
putText "module"
147156
space
148157
putText (showOutputable name)
149-
attachEolComment loc
150158

151159
case mexps of
152160
Nothing -> when (isJust mname) do
161+
forM_ mname \(L nloc _) -> attachEolComment nloc
153162
case breakWhere conf of
154163
Always -> do
155164
newline
156165
spaces (indent conf)
157166
_ -> space
158167
putText "where"
159168
Just (L loc exps) -> do
169+
moduleComment <- getModuleComment
160170
exportsWithComments <- fmap (second doSort) <$> groupAttachedComments exps
161171
case breakWhere conf of
162172
Single
163173
| Just exportsWithoutComments <- groupWithoutComments exportsWithComments
164174
, length exportsWithoutComments <= 1
165-
-> printSingleLineExportList conf (L loc exportsWithoutComments)
175+
-> do
176+
attachModuleComment moduleComment
177+
printSingleLineExportList conf (L loc exportsWithoutComments)
166178
Inline
167179
| Just exportsWithoutComments <- groupWithoutComments exportsWithComments
168-
-> wrapping
169-
(printSingleLineExportList conf (L loc exportsWithoutComments))
170-
(printMultiLineExportList conf (L loc exportsWithComments))
171-
_ -> printMultiLineExportList conf (L loc exportsWithComments)
180+
-> do
181+
wrapping
182+
( attachModuleComment moduleComment
183+
>> printSingleLineExportList conf (L loc exportsWithoutComments))
184+
( attachOpenBracket
185+
>> attachModuleComment moduleComment
186+
>> printMultiLineExportList conf (L loc exportsWithComments))
187+
_ -> do
188+
attachOpenBracket
189+
attachModuleComment moduleComment
190+
printMultiLineExportList conf (L loc exportsWithComments)
172191
where
192+
193+
getModuleComment = do
194+
maybemaybeComment <- traverse (\(L nloc _) -> removeModuleComment nloc) mname
195+
pure $ join maybemaybeComment
196+
197+
attachModuleComment moduleComment =
198+
mapM_ (\c -> space >> putComment c) moduleComment
199+
173200
doSort = if sort conf then NonEmpty.sortBy compareLIE else id
174201

202+
attachOpenBracket
203+
| openBracket conf == SameLine = putText " ("
204+
| otherwise = pure ()
205+
206+
removeModuleComment :: SrcSpan -> P (Maybe AnnotationComment)
207+
removeModuleComment = \case
208+
UnhelpfulSpan _ -> pure Nothing
209+
RealSrcSpan rspan ->
210+
removeLineComment (srcSpanStartLine rspan)
211+
175212
attachEolComment :: SrcSpan -> P ()
176213
attachEolComment = \case
177214
UnhelpfulSpan _ -> pure ()
@@ -202,8 +239,7 @@ printMultiLineExportList
202239
-> P ()
203240
printMultiLineExportList conf (L srcLoc exportsWithComments) = do
204241
newline
205-
doIndent >> putText "(" >> when (notNull exportsWithComments) space
206-
242+
doIndent >> putText firstChar >> when (notNull exportsWithComments) space
207243
printExports exportsWithComments
208244

209245
putText ")" >> space >> putText "where" >> attachEolCommentEnd srcLoc
@@ -221,6 +257,12 @@ printMultiLineExportList conf (L srcLoc exportsWithComments) = do
221257
-- > xxxx( -- Some comment
222258
-- > xxxxyyfoo
223259
-- > xxxx) where
260+
261+
firstChar =
262+
case openBracket conf of
263+
SameLine -> " "
264+
NextLine -> "("
265+
224266
doIndent = spaces (indent conf)
225267
doHang = pad (indent conf + 2)
226268

0 commit comments

Comments
 (0)