@@ -9,14 +9,15 @@ module Ormolu.Printer
12import Data.Char (isAlphaNum)
13import Data.Char (isAlphaNum)
13import Data.Text (Text)
14import Data.Text (Text)
14−import FieldLabel (FieldLbl (..))
15import GHC hiding (GhcPs, IE)
15import GHC hiding (GhcPs, IE)
16import HsImpExp (IE (..))
16import HsImpExp (IE (..))
17import Language.Haskell.GHC.ExactPrint.Types
17import Language.Haskell.GHC.ExactPrint.Types
18import Module (Module (..))
18import Module (Module (..))
19import OccName (OccName (..))
19import OccName (OccName (..))
20+import Ormolu.Imports (sortImports)
20import Ormolu.Printer.Combinators
21import Ormolu.Printer.Combinators
21import Outputable (Outputable (..), showSDocUnsafe)
22import Outputable (Outputable (..), showSDocUnsafe)
22import RdrName (RdrName (..), rdrNameOcc)
23import RdrName (RdrName (..), rdrNameOcc)
@@ -41,18 +42,21 @@ p_HsModule loc@(L moduleSpan hsModule) = do
41 case hsmodExports hsModule of
42 case hsmodExports hsModule of
42 Nothing -> moduleSpan
43 Nothing -> moduleSpan
43 Just (L exportsSpan _) -> combineSrcSpans moduleSpan exportsSpan
44 Just (L exportsSpan _) -> combineSrcSpans moduleSpan exportsSpan
44− locatedVia spn loc $ \HsModule {..} ->
45+ locatedVia (Just spn) loc $ \HsModule {..} -> do
45 case hsmodName of
46 case hsmodName of
46 Nothing -> pure ()
47 Nothing -> pure ()
47− Just hsmodName' -> line . velt' $
48+ Just hsmodName' -> do
48− [ located hsmodName' p_hsmodName ] ++
50+ [ located hsmodName' p_hsmodName ] ++
49− (case hsmodExports of
51+ (case hsmodExports of
50− Nothing -> []
52+ Nothing -> []
51− Just hsmodExports' ->
53+ Just hsmodExports' ->
52− [ inci (locatedVia spn hsmodExports' p_hsmodExports)
54+ [ inci (locatedVia Nothing hsmodExports' p_hsmodExports)
54− ++ [ txt "where"
56+ ++ [ txt "where"
58+ unless (null hsmodImports) newline
59+ forM_ (sortImports hsmodImports) (located' p_hsmodImports)
57p_hsmodName :: ModuleName -> R ()
61p_hsmodName :: ModuleName -> R ()
58p_hsmodName mname = do
62p_hsmodName mname = do
@@ -70,18 +74,51 @@ p_lie = \case
70 IEThingAll l1 -> do
74 IEThingAll l1 -> do
71 located l1 p_ieWrappedName
75 located l1 p_ieWrappedName
72 txt " (..)"
76 txt " (..)"
73− IEThingWith l1 w xs fls -> do
77+ IEThingWith l1 w xs _ -> velt'
74− located l1 p_ieWrappedName
78+ [ located l1 p_ieWrappedName
76− p_ieWildcard w
80+ p_ieWildcard w
77− parens . velt $ withSep comma (located' p_ieWrappedName) xs
81+ parens . velt $ withSep comma (located' p_ieWrappedName) xs
83+ -- XXX I have no idea what field labels are in this context.
78− parens . velt $ withSep comma (located' p_FieldLbl) fls
84+ -- parens . velt $ withSep comma (located' p_FieldLbl) fls
79 IEModuleContents l1 -> located l1 p_hsmodName
85 IEModuleContents l1 -> located l1 p_hsmodName
80 -- XXX I have no idea what these things are for.
86 -- XXX I have no idea what these things are for.
81 IEGroup _ _ -> return ()
87 IEGroup _ _ -> return ()
82 IEDoc _ -> return ()
88 IEDoc _ -> return ()
83 IEDocNamed _ -> 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)
85p_ieWrappedName :: IEWrappedName RdrName -> R ()
122p_ieWrappedName :: IEWrappedName RdrName -> R ()
86p_ieWrappedName = \case
123p_ieWrappedName = \case
87 IEName l2 -> located l2 p_rdrName
124 IEName l2 -> located l2 p_rdrName
@@ -99,8 +136,8 @@ p_rdrName x = opParens (rdrNameOcc x) $ case x of
99 Orig (Module _ mname) occName -> p_qualName mname occName
136 Orig (Module _ mname) occName -> p_qualName mname occName
100 Exact name -> atom name
137 Exact name -> atom name
102−p_FieldLbl :: FieldLbl RdrName -> R ()
139+-- p_FieldLbl :: FieldLbl RdrName -> R ()
103−p_FieldLbl (FieldLabel x _ _) = atom x
140+-- p_FieldLbl (FieldLabel x _ _) = atom x
105p_qualName :: ModuleName -> OccName -> R ()
142p_qualName :: ModuleName -> OccName -> R ()
106p_qualName mname occName = do
143p_qualName mname occName = do