33module 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+
5057data BreakWhere
5158 = Exports
5259 | Single
5360 | Inline
5461 | Always
62+ deriving (Eq , Show )
5563
5664defaultConfig :: Config
5765defaultConfig = Config
5866 { indent = 4
5967 , sort = True
6068 , separateLists = True
6169 , breakWhere = Exports
70+ , openBracket = NextLine
6271 }
6372
6473step :: Maybe Int -> Config -> Step
@@ -142,36 +151,64 @@ printHeader
142151 -> Maybe GHC. LHsDocString
143152 -> P ()
144153printHeader 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+
175212attachEolComment :: SrcSpan -> P ()
176213attachEolComment = \ case
177214 UnhelpfulSpan _ -> pure ()
@@ -202,8 +239,7 @@ printMultiLineExportList
202239 -> P ()
203240printMultiLineExportList 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