@@ -9,19 +9,11 @@ module Ormolu.Printer
13−import Data.Char (isAlphaNum)
14import Data.Text (Text)
12import Data.Text (Text)
15−import GHC hiding (GhcPs, IE)
13+import GHC (ParsedSource)
16−import HsImpExp (IE (..))
17import Language.Haskell.GHC.ExactPrint.Types
14import Language.Haskell.GHC.ExactPrint.Types
18−import Module (Module (..))
19−import OccName (OccName (..))
20−import Ormolu.Imports (sortImports)
21import Ormolu.Printer.Combinators
15import Ormolu.Printer.Combinators
22−import Outputable (Outputable (..), showSDocUnsafe)
16+import Ormolu.Printer.Meat.Module
23−import RdrName (RdrName (..), rdrNameOcc)
24−import SrcLoc (combineSrcSpans)
26-- | Render a module.
18-- | Render a module.
@@ -31,133 +23,4 @@ printModule
31 -> ParsedSource -- ^ Parsed source
23 -> ParsedSource -- ^ Parsed source
32 -> Text -- ^ Resulting rendition
24 -> Text -- ^ Resulting rendition
33printModule debugOn anns src =
25printModule debugOn anns src =
34− runR debugOn (p_HsModule src) anns
26+ runR debugOn (p_hsModule src) anns
36−p_HsModule :: ParsedSource -> R ()
37−p_HsModule loc@(L moduleSpan hsModule) = do
38− -- NOTE If span of exports in multiline, the whole thing is multiline.
39− -- This is especially important because span of module itself always seems
40− -- to have length zero, so it's not reliable for layout selection.
42− case hsmodExports hsModule of
43− Nothing -> moduleSpan
44− Just (L exportsSpan _) -> combineSrcSpans moduleSpan exportsSpan
45− locatedVia (Just spn) loc $ \HsModule {..} -> do
48− Just hsmodName' -> do
50− [ located hsmodName' p_hsmodName ] ++
51− (case hsmodExports of
53− Just hsmodExports' ->
54− [ inci (locatedVia Nothing hsmodExports' p_hsmodExports)
58− unless (null hsmodImports) newline
59− forM_ (sortImports hsmodImports) (located' p_hsmodImports)
61−p_hsmodName :: ModuleName -> R ()
62−p_hsmodName mname = do
66−p_hsmodExports :: [LIE GhcPs] -> R ()
67−p_hsmodExports xs = do
68− parens . velt $ withSep comma (located' p_lie) xs
70−p_lie :: IE GhcPs -> R ()
72− IEVar l1 -> located l1 p_ieWrappedName
73− IEThingAbs l1 -> located l1 p_ieWrappedName
75− located l1 p_ieWrappedName
77− IEThingWith l1 w xs _ -> velt'
78− [ located l1 p_ieWrappedName
81− parens . velt $ withSep comma (located' p_ieWrappedName) xs
83− -- XXX I have no idea what field labels are in this context.
84− -- parens . velt $ withSep comma (located' p_FieldLbl) fls
85− IEModuleContents l1 -> located l1 p_hsmodName
86− -- XXX I have no idea what these things are for.
87− IEGroup _ _ -> return ()
89− IEDocNamed _ -> return ()
91−p_hsmodImports :: ImportDecl GhcPs -> R ()
92−p_hsmodImports ImportDecl {..} = line . velt' $
95− txt "{-# SOURCE #-} "
98− when ideclQualified $
100− case ideclPkgQual of
101− Nothing -> return ()
105− located ideclName atom
107− Nothing -> return ()
112− Nothing -> return ()
116− ] ++ (case ideclHiding of
119− [ inci . locatedVia Nothing l $ parens . velt . withSep comma (located' p_lie)
122−p_ieWrappedName :: IEWrappedName RdrName -> R ()
123−p_ieWrappedName = \case
124− IEName l2 -> located l2 p_rdrName
125− IEPattern l2 -> located l2 $ \x -> do
128− IEType l2 -> located l2 $ \x -> do
132−p_rdrName :: RdrName -> R ()
133−p_rdrName x = opParens (rdrNameOcc x) $ case x of
134− Unqual occName -> atom occName
135− Qual mname occName -> p_qualName mname occName
136− Orig (Module _ mname) occName -> p_qualName mname occName
137− Exact name -> atom name
139−-- p_FieldLbl :: FieldLbl RdrName -> R ()
140−-- p_FieldLbl (FieldLabel x _ _) = atom x
142−p_qualName :: ModuleName -> OccName -> R ()
143−p_qualName mname occName = do
148−p_ieWildcard :: IEWildcard -> R ()
150− NoIEWildcard -> return ()
151− IEWildcard n -> parens (atom n)
153−----------------------------------------------------------------------------
156−-- | Put parentheses around the second argument if the 'Outputable' thing
157−-- consists only of punctuation characters.
159−opParens :: Outputable a => a -> R () -> R ()
161− if all (not . isAlphaNum) (showSDocUnsafe (ppr x))
162− then txt "(" >> m >> txt ")"