@@ -17,14 +17,17 @@ import Control.Exception
17import Control.Monad
17import Control.Monad
18import Control.Monad.IO.Class (MonadIO (..))
18import Control.Monad.IO.Class (MonadIO (..))
19import Data.Text (Text)
19import Data.Text (Text)
20import Language.Haskell.GHC.ExactPrint.Types
21import Language.Haskell.GHC.ExactPrint.Types
21import Ormolu.Config
22import Ormolu.Config
22import Ormolu.Diff
23import Ormolu.Diff
23import Ormolu.Exception
24import Ormolu.Exception
24import Ormolu.Parser
25import Ormolu.Parser
25import Ormolu.Printer
26import Ormolu.Printer
27+import qualified CmdLineParser as GHC
26import qualified Data.Text as T
28import qualified Data.Text as T
27import qualified GHC
29import qualified GHC
30+import qualified Outputable as GHC
29-- | Format a 'String', return formatted version as 'Text'.
32-- | Format a 'String', return formatted version as 'Text'.
@@ -40,17 +43,23 @@ import qualified GHC
41 :: MonadIO m
44 :: MonadIO m
42 => Config -- ^ Ormolu configuration
45 => Config -- ^ Ormolu configuration
46+ -> Bool -- ^ Output debugging info
43 -> FilePath -- ^ Location of source file
47 -> FilePath -- ^ Location of source file
44 -> String -- ^ Input to format
48 -> String -- ^ Input to format
46−ormolu cfg path str = do
50+ormolu cfg debugOn path str = do
47− (anns0, parsedSrc0) <-
51+ (ws, (anns0, parsedSrc0)) <-
48 parseModule' cfg OrmoluParsingFailed path str
52 parseModule' cfg OrmoluParsingFailed path str
55+ traceM (concatMap showWarn ws)
57+ traceM (showOutputable anns0)
49− let txt = printModule anns0 parsedSrc0
58+ let txt = printModule debugOn anns0 parsedSrc0
50 -- Parse the result of pretty-printing again and make sure that AST is the
59 -- Parse the result of pretty-printing again and make sure that AST is the
51 -- same as AST of original snippet module span positions.
60 -- same as AST of original snippet module span positions.
52− when (cfgSanityCheck cfg) $ do
61+ unless (cfgUnsafe cfg) $ do
53− (anns1, parsedSrc1) <-
62+ (_, (anns1, parsedSrc1)) <-
54 parseModule' cfg OrmoluOutputParsingFailed "<rendered>" (T.unpack txt)
63 parseModule' cfg OrmoluOutputParsingFailed "<rendered>" (T.unpack txt)
55 when (diff (anns0, parsedSrc0) (anns1, parsedSrc1)) $
64 when (diff (anns0, parsedSrc0) (anns1, parsedSrc1)) $
56 liftIO $ throwIO (OrmoluASTDiffers str txt)
65 liftIO $ throwIO (OrmoluASTDiffers str txt)
@@ -65,10 +74,14 @@ ormolu cfg path str = do
66 :: MonadIO m
75 :: MonadIO m
67 => Config -- ^ Ormolu configuration
76 => Config -- ^ Ormolu configuration
77+ -> Bool -- ^ Output debugging info
68 -> FilePath -- ^ Location of source file
78 -> FilePath -- ^ Location of source file
69 -> m Text -- ^ Resulting rendition
79 -> m Text -- ^ Resulting rendition
70−ormoluFile cfg path =
80+ormoluFile cfg debugOn path =
71− liftIO (readFile path) >>= ormolu cfg path
81+ liftIO (readFile path) >>= ormolu cfg debugOn path
83+----------------------------------------------------------------------------
73-- | A wrapper around 'parseModule'.
86-- | A wrapper around 'parseModule'.
@@ -79,9 +92,20 @@ parseModule'
79 -- ^ How to obtain 'OrmoluException' to throw when parsing fails
92 -- ^ How to obtain 'OrmoluException' to throw when parsing fails
80 -> FilePath -- ^ File name to use in errors
93 -> FilePath -- ^ File name to use in errors
81 -> String -- ^ Actual input for the parser
94 -> String -- ^ Actual input for the parser
82− -> m (Anns, GHC.ParsedSource) -- ^ Annotations and parsed source
95+ -> m ([GHC.Warn], (Anns, GHC.ParsedSource)) -- ^ Annotations and parsed source
83parseModule' Config {..} mkException path str = do
96parseModule' Config {..} mkException path str = do
84− (_, r) <- parseModule cfgDynOptions path str
97+ (ws, r) <- parseModule cfgDynOptions path str
86 Left (spn, err) -> liftIO $ throwIO (mkException spn err)
99 Left (spn, err) -> liftIO $ throwIO (mkException spn err)
87− Right x -> return x
100+ Right x -> return (ws, x)
102+-- | Pretty-print a 'GHC.Warn'.
104+showWarn :: GHC.Warn -> String
105+showWarn (GHC.Warn reason l) =
106+ showOutputable reason ++ "\n" ++ showOutputable l ++ "\n"
108+-- | Pretty-print an 'GHC.Outputable' thing.
110+showOutputable :: GHC.Outputable o => o -> String
111+showOutputable = GHC.showSDocUnsafe . GHC.ppr