on master help sign in
Include debugging option in ‘Config’
6f169f2mrkkrp
app/Main.hs+19−25
@@ -22,17 +22,11 @@ main :: IO ()
22main = withPrettyOrmoluExceptions $ do
22main = withPrettyOrmoluExceptions $ do
23 Opts {..} <- execParser optsParserInfo
23 Opts {..} <- execParser optsParserInfo
24 config <- case optConfigFile of
24 config <- case optConfigFile of
25− Nothing -> return Config
25+ Nothing -> return optConfig
26− { cfgDynOptions = optDynOptions
27− , cfgUnsafe = optUnsafe
28− }
29 Just path -> do
26 Just path -> do
30− Config {..} <- Yaml.decodeFileThrow path
27+ config <- Yaml.decodeFileThrow path
31− return Config
28+ return (config <> optConfig)
32− { cfgDynOptions = cfgDynOptions ++ optDynOptions
33− , cfgUnsafe = optUnsafe || cfgUnsafe
34− }
35− r <- ormoluFile config optDebug optInputFile
29+ r <- ormoluFile config optInputFile
36 case optMode of
30 case optMode of
37 Stdout ->
31 Stdout ->
38 TIO.putStr r
32 TIO.putStr r
@@ -50,12 +44,8 @@ data Opts = Opts
50 -- ^ Mode of operation
44 -- ^ Mode of operation
51 , optConfigFile :: !(Maybe FilePath)
45 , optConfigFile :: !(Maybe FilePath)
52 -- ^ Location of configuration file (optional)
46 -- ^ Location of configuration file (optional)
53− , optUnsafe :: !Bool
47+ , optConfig :: !Config
54− -- ^ Whether to skip sanity checking
48+ -- ^ Ormolu 'Config'
55− , optDynOptions :: ![DynOption]
56− -- ^ GHC options to set
57− , optDebug :: !Bool
58− -- ^ Output information useful for debugging
59 , optInputFile :: !FilePath
49 , optInputFile :: !FilePath
60 -- ^ Input source file
50 -- ^ Input source file
61 }
51 }
@@ -108,26 +98,30 @@ optsParser = Opts
108 , metavar "CONFIG"
98 , metavar "CONFIG"
109 , help "Location of configuration file"
99 , help "Location of configuration file"
110 ]
100 ]
101+ <*> configParser
111− <*> (switch . mconcat)
102+ <*> (strArgument . mconcat)
112− [ long "unsafe"
103+ [ metavar "FILE"
113− , short 'u'
104+ , help "Haskell source file to format"
114− , help "Do formatting faster but without automatic detection of defects"
115 ]
105 ]
106+
107+configParser :: Parser Config
108+configParser = Config
116− <*> (fmap (fmap DynOption) . many . strOption . mconcat)
109+ <$> (fmap (fmap DynOption) . many . strOption . mconcat)
117 [ long "ghc-opt"
110 [ long "ghc-opt"
118 , short 'o'
111 , short 'o'
119 , metavar "OPT"
112 , metavar "OPT"
120 , help "GHC options to enable (e.g. language extensions)"
113 , help "GHC options to enable (e.g. language extensions)"
121 ]
114 ]
115+ <*> (switch . mconcat)
116+ [ long "unsafe"
117+ , short 'u'
118+ , help "Do formatting faster but without automatic detection of defects"
119+ ]
122 <*> (switch . mconcat)
120 <*> (switch . mconcat)
123 [ long "debug"
121 [ long "debug"
124 , short 'd'
122 , short 'd'
125 , help "Output information useful for debugging"
123 , help "Output information useful for debugging"
126 ]
124 ]
127− <*> (strArgument . mconcat)
128− [ metavar "FILE"
129− , help "Haskell source file to format"
130− ]
131
125
132----------------------------------------------------------------------------
126----------------------------------------------------------------------------
133-- Helpers
127-- Helpers
src/Ormolu.hs+5−7
@@ -43,19 +43,18 @@ import qualified Outputable as GHC
43ormolu
43ormolu
44 :: MonadIO m
44 :: MonadIO m
45 => Config -- ^ Ormolu configuration
45 => Config -- ^ Ormolu configuration
46− -> Bool -- ^ Output debugging info
47 -> FilePath -- ^ Location of source file
46 -> FilePath -- ^ Location of source file
48 -> String -- ^ Input to format
47 -> String -- ^ Input to format
49 -> m Text
48 -> m Text
50−ormolu cfg debugOn path str = do
49+ormolu cfg path str = do
51 (ws, (anns0, parsedSrc0)) <-
50 (ws, (anns0, parsedSrc0)) <-
52 parseModule' cfg OrmoluParsingFailed path str
51 parseModule' cfg OrmoluParsingFailed path str
53− when debugOn $ do
52+ when (cfgDebug cfg) $ do
54 traceM "warnings:\n"
53 traceM "warnings:\n"
55 traceM (concatMap showWarn ws)
54 traceM (concatMap showWarn ws)
56 traceM "anns:\n"
55 traceM "anns:\n"
57 traceM (showOutputable anns0)
56 traceM (showOutputable anns0)
58− let txt = printModule debugOn anns0 parsedSrc0
57+ let txt = printModule (cfgDebug cfg) anns0 parsedSrc0
59 -- Parse the result of pretty-printing again and make sure that AST is the
58 -- Parse the result of pretty-printing again and make sure that AST is the
60 -- same as AST of original snippet module span positions.
59 -- same as AST of original snippet module span positions.
61 unless (cfgUnsafe cfg) $ do
60 unless (cfgUnsafe cfg) $ do
@@ -74,11 +73,10 @@ ormolu cfg debugOn path str = do
74ormoluFile
73ormoluFile
75 :: MonadIO m
74 :: MonadIO m
76 => Config -- ^ Ormolu configuration
75 => Config -- ^ Ormolu configuration
77− -> Bool -- ^ Output debugging info
78 -> FilePath -- ^ Location of source file
76 -> FilePath -- ^ Location of source file
79 -> m Text -- ^ Resulting rendition
77 -> m Text -- ^ Resulting rendition
80−ormoluFile cfg debugOn path =
78+ormoluFile cfg path =
81− liftIO (readFile path) >>= ormolu cfg debugOn path
79+ liftIO (readFile path) >>= ormolu cfg path
82
80
83----------------------------------------------------------------------------
81----------------------------------------------------------------------------
84-- Helpers
82-- Helpers
src/Ormolu/Config.hs+15
@@ -22,12 +22,26 @@ data Config = Config
22 -- ^ Dynamic options to pass to GHC parser
22 -- ^ Dynamic options to pass to GHC parser
23 , cfgUnsafe :: !Bool
23 , cfgUnsafe :: !Bool
24 -- ^ Do formatting faster but without automatic detection of defects
24 -- ^ Do formatting faster but without automatic detection of defects
25+ , cfgDebug :: !Bool
26+ -- ^ Output information useful for debugging
25 } deriving (Eq, Show)
27 } deriving (Eq, Show)
26
28
29+instance Semigroup Config where
30+ a <> b = Config
31+ { cfgDynOptions = cfgDynOptions a <> cfgDynOptions b
32+ , cfgUnsafe = cfgUnsafe a || cfgUnsafe b
33+ , cfgDebug = cfgDebug a || cfgDebug b
34+ }
35+
36+instance Monoid Config where
37+ mempty = defaultConfig
38+ mappend = (<>)
39+
27instance FromJSON Config where
40instance FromJSON Config where
28 parseJSON = withObject "config" $ \o -> do
41 parseJSON = withObject "config" $ \o -> do
29 cfgDynOptions <- o .: "ghc-opts"
42 cfgDynOptions <- o .: "ghc-opts"
30 cfgUnsafe <- o .: "unsafe"
43 cfgUnsafe <- o .: "unsafe"
44+ cfgDebug <- o .: "debug"
31 return Config {..}
45 return Config {..}
32
46
33-- | Default 'Config'.
47-- | Default 'Config'.
@@ -36,6 +50,7 @@ defaultConfig :: Config
36defaultConfig = Config
50defaultConfig = Config
37 { cfgDynOptions = []
51 { cfgDynOptions = []
38 , cfgUnsafe = False
52 , cfgUnsafe = False
53+ , cfgDebug = False
39 }
54 }
40
55
41-- | A wrapper for dynamic options.
56-- | A wrapper for dynamic options.
tests/Ormolu/PrinterSpec.hs+2−2
@@ -29,14 +29,14 @@ checkExample srcPath' = it (fromRelFile srcPath' ++ " works") $ do
29 -- 2. Parse the result of pretty-printing again and make sure that AST
29 -- 2. Parse the result of pretty-printing again and make sure that AST
30 -- is the same as AST of the original snippet. (This happens in
30 -- is the same as AST of the original snippet. (This happens in
31 -- 'ormoluFile' automatically.)
31 -- 'ormoluFile' automatically.)
32− formatted0 <- ormoluFile defaultConfig False (fromRelFile srcPath)
32+ formatted0 <- ormoluFile defaultConfig (fromRelFile srcPath)
33 -- 3. Check the output against expected output. Thus all tests should
33 -- 3. Check the output against expected output. Thus all tests should
34 -- include two files: input and expected output.
34 -- include two files: input and expected output.
35 expected <- (liftIO . T.readFile . fromRelFile) expectedOutputPath
35 expected <- (liftIO . T.readFile . fromRelFile) expectedOutputPath
36 formatted0 `shouldMatch` expected
36 formatted0 `shouldMatch` expected
37 -- 4. Check that running the formatter on the output produces the same
37 -- 4. Check that running the formatter on the output produces the same
38 -- output again (the transformation is idempotent).
38 -- output again (the transformation is idempotent).
39− formatted1 <- ormolu defaultConfig False "<formatted>" (T.unpack formatted0)
39+ formatted1 <- ormolu defaultConfig "<formatted>" (T.unpack formatted0)
40 formatted1 `shouldMatch` formatted0
40 formatted1 `shouldMatch` formatted0
41
41
42-- | Build list of examples for testing.
42-- | Build list of examples for testing.