@@ -9,14 +9,15 @@ module Ormolu.Printer
99 )
1010where
1111
12+import Control.Monad
1213import Data.Char (isAlphaNum)
1314import Data.Text (Text)
14−import FieldLabel (FieldLbl (..))
1515import GHC hiding (GhcPs, IE)
1616import HsImpExp (IE (..))
1717import Language.Haskell.GHC.ExactPrint.Types
1818import Module (Module (..))
1919import OccName (OccName (..))
20+import Ormolu.Imports (sortImports)
2021import Ormolu.Printer.Combinators
2122import Outputable (Outputable (..), showSDocUnsafe)
2223import RdrName (RdrName (..), rdrNameOcc)
@@ -41,18 +42,21 @@ p_HsModule loc@(L moduleSpan hsModule) = do
4142 case hsmodExports hsModule of
4243 Nothing -> moduleSpan
4344 Just (L exportsSpan _) -> combineSrcSpans moduleSpan exportsSpan
44− locatedVia spn loc $ \HsModule {..} ->
45+ locatedVia (Just spn) loc $ \HsModule {..} -> do
4546 case hsmodName of
4647 Nothing -> pure ()
47− Just hsmodName' -> line . velt' $
48− [ located hsmodName' p_hsmodName ] ++
49− (case hsmodExports of
50− Nothing -> []
51− Just hsmodExports' ->
52− [ inci (locatedVia spn hsmodExports' p_hsmodExports)
53− ])
54− ++ [ txt "where"
55− ]
48+ Just hsmodName' -> do
49+ line . velt' $
50+ [ located hsmodName' p_hsmodName ] ++
51+ (case hsmodExports of
52+ Nothing -> []
53+ Just hsmodExports' ->
54+ [ inci (locatedVia Nothing hsmodExports' p_hsmodExports)
55+ ])
56+ ++ [ txt "where"
57+ ]
58+ unless (null hsmodImports) newline
59+ forM_ (sortImports hsmodImports) (located' p_hsmodImports)
5660
5761p_hsmodName :: ModuleName -> R ()
5862p_hsmodName mname = do
@@ -70,18 +74,51 @@ p_lie = \case
7074 IEThingAll l1 -> do
7175 located l1 p_ieWrappedName
7276 txt " (..)"
73− IEThingWith l1 w xs fls -> do
74− located l1 p_ieWrappedName
75− space
76− p_ieWildcard w
77− parens . velt $ withSep comma (located' p_ieWrappedName) xs
78− parens . velt $ withSep comma (located' p_FieldLbl) fls
77+ IEThingWith l1 w xs _ -> velt'
78+ [ located l1 p_ieWrappedName
79+ , inci $ do
80+ p_ieWildcard w
81+ parens . velt $ withSep comma (located' p_ieWrappedName) xs
82+ ]
83+ -- XXX I have no idea what field labels are in this context.
84+ -- parens . velt $ withSep comma (located' p_FieldLbl) fls
7985 IEModuleContents l1 -> located l1 p_hsmodName
8086 -- XXX I have no idea what these things are for.
8187 IEGroup _ _ -> return ()
8288 IEDoc _ -> return ()
8389 IEDocNamed _ -> return ()
8490
91+p_hsmodImports :: ImportDecl GhcPs -> R ()
92+p_hsmodImports ImportDecl {..} = line . velt' $
93+ [ do txt "import "
94+ when ideclSource $
95+ txt "{-# SOURCE #-} "
96+ when ideclSafe $
97+ txt "safe "
98+ when ideclQualified $
99+ txt "qualified "
100+ case ideclPkgQual of
101+ Nothing -> return ()
102+ Just slit -> do
103+ atom slit
104+ space
105+ located ideclName atom
106+ case ideclAs of
107+ Nothing -> return ()
108+ Just l -> do
109+ txt " as "
110+ located l atom
111+ case ideclHiding of
112+ Nothing -> return ()
113+ Just (hiding, _) ->
114+ when hiding $
115+ txt " hiding"
116+ ] ++ (case ideclHiding of
117+ Nothing -> []
118+ Just (_, l) ->
119+ [ inci . locatedVia Nothing l $ parens . velt . withSep comma (located' p_lie)
120+ ])
121+
85122p_ieWrappedName :: IEWrappedName RdrName -> R ()
86123p_ieWrappedName = \case
87124 IEName l2 -> located l2 p_rdrName
@@ -99,8 +136,8 @@ p_rdrName x = opParens (rdrNameOcc x) $ case x of
99136 Orig (Module _ mname) occName -> p_qualName mname occName
100137 Exact name -> atom name
101138
102−p_FieldLbl :: FieldLbl RdrName -> R ()
103−p_FieldLbl (FieldLabel x _ _) = atom x
139+-- p_FieldLbl :: FieldLbl RdrName -> R ()
140+-- p_FieldLbl (FieldLabel x _ _) = atom x
104141
105142p_qualName :: ModuleName -> OccName -> R ()
106143p_qualName mname occName = do