on 2dfd181 help sign in
69 lines 2.2 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 GHC
14import Ormolu.Imports
15import Ormolu.Printer.Combinators
16import Ormolu.Printer.Comments
17import Ormolu.Printer.Meat.Common
18import Ormolu.Printer.Meat.Declaration
19import Ormolu.Printer.Meat.ImportExport
20import Ormolu.Utils
21import SrcLoc (combineSrcSpans)
23p_hsModule :: ParsedSource -> R ()
24p_hsModule loc@(L moduleSpan hsModule) = do
25 -- NOTE If span of exports in multiline, the whole thing is multiline.
26 -- This is especially important because span of module itself always seems
27 -- to have length zero, so it's not reliable for layout selection.
28 let spn =
29 case hsmodExports hsModule of
30 Nothing -> moduleSpan
31 Just (L exportsSpan _) -> combineSrcSpans moduleSpan exportsSpan
32 locatedVia (Just spn) loc $ \HsModule {..} -> do
33 case hsmodName of
34 Nothing -> pure ()
35 Just hsmodName' -> line $ do
36 located hsmodName' p_hsmodName
37 case hsmodExports of
38 Nothing -> return ()
39 Just hsmodExports' -> do
40 breakpoint
41 inci (locatedVia Nothing hsmodExports' p_hsmodExports)
42 breakpoint
43 txt "where"
44 when (not (null hsmodImports) || not (null hsmodDecls)) newline
45 forM_ (sortImports hsmodImports) (located' p_hsmodImport)
46 when (not (null hsmodImports) && not (null hsmodDecls)) newline
48 forM_ (zip hsmodDecls ((Just <$> drop 1 hsmodDecls) ++ [Nothing])) $
49 \(d, md) -> do
50 case md of
51 Nothing -> located d p_hsDecl
52 Just d' ->
53 if separatedDecls (unL d) (unL d')
54 then line (located d p_hsDecl)
55 else located d p_hsDecl
57 trailingComments <- hasMoreComments
58 when (trailingComments && isJust hsmodName) newline
59 spitRemainingComments
61-- | Determine if these declarations should be separated by a blank line.
63separatedDecls
64 :: HsDecl GhcPs
65 -> HsDecl GhcPs
66 -> Bool
67separatedDecls (SigD NoExt (TypeSig NoExt (n:_) _)) (ValD NoExt (FunBind NoExt n' _ _ _)) =
68 unL n /= unL n'
69separatedDecls _ _ = True