on d82d607 help sign in
61 lines 2.1 kB history raw
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE RecordWildCards #-}
4-- | Rendering of modules.
6module Ormolu.Printer.Meat.Module
7 ( p_hsModule
8 )
9where
11import Control.Monad
12import Data.Maybe (isJust)
13import Data.Set (Set)
14import GHC
15import Ormolu.Imports
16import Ormolu.Printer.Combinators
17import Ormolu.Printer.Comments
18import Ormolu.Printer.Meat.Common
19import Ormolu.Printer.Meat.Declaration
20import Ormolu.Printer.Meat.Declaration.Warning
21import Ormolu.Printer.Meat.ImportExport
22import Ormolu.Printer.Meat.LanguagePragma
23import SrcLoc (combineSrcSpans)
25p_hsModule :: Set String -> ParsedSource -> R ()
26p_hsModule exts loc@(L moduleSpan hsModule) = do
27 -- NOTE If span of exports in multiline, the whole thing is multiline.
28 -- This is especially important because span of module itself always seems
29 -- to have length zero, so it's not reliable for layout selection.
30 let spn =
31 case hsmodExports hsModule of
32 Nothing -> moduleSpan
33 Just (L exportsSpan _) -> combineSrcSpans moduleSpan exportsSpan
34 locatedVia (Just spn) loc $ \HsModule {..} -> do
35 let hasLangPragmas = not (null exts)
36 hasModuleHeader = isJust hsmodName
37 hasImports = not (null hsmodImports)
38 hasDecls = not (null hsmodDecls)
39 p_langPragmas exts
40 when (hasLangPragmas &&
41 (hasModuleHeader || hasImports || hasDecls)) $
42 newline
43 case hsmodName of
44 Nothing -> pure ()
45 Just hsmodName' -> line $ do
46 located hsmodName' p_hsmodName
47 forM_ hsmodDeprecMessage (located' p_moduleWarning)
48 case hsmodExports of
49 Nothing -> return ()
50 Just hsmodExports' -> do
51 breakpoint
52 inci (locatedVia Nothing hsmodExports' p_hsmodExports)
53 breakpoint
54 txt "where"
55 when (hasImports || hasDecls) newline
56 forM_ (sortImports hsmodImports) (located' p_hsmodImport)
57 when (hasImports && hasDecls) newline
58 p_hsDecls Free hsmodDecls
59 trailingComments <- hasMoreComments
60 when (trailingComments && hasModuleHeader) newline
61 spitRemainingComments