on master help sign in
Include debugging option in ‘Config’
commit6f169f2969fdacebde19c93b196ada68bc6799e7 authormrkkrp <mark.karpov@tweag.io> dateThu 28 Feb 2019, 22:16 (8 years ago) parent2f73bae
4 files changed+41−34
app/Main.hs+19−25 src/Ormolu.hs+5−7 src/Ormolu/Config.hs+15 tests/Ormolu/PrinterSpec.hs+2−2
app/Main.hs+19−25
@@ -22,17 +22,11 @@ main :: IO ()
2222main = withPrettyOrmoluExceptions $ do
2323 Opts {..} <- execParser optsParserInfo
2424 config <- case optConfigFile of
25− Nothing -> return Config
26− { cfgDynOptions = optDynOptions
27− , cfgUnsafe = optUnsafe
28− }
25+ Nothing -> return optConfig
2926 Just path -> do
30− Config {..} <- Yaml.decodeFileThrow path
31− return Config
32− { cfgDynOptions = cfgDynOptions ++ optDynOptions
33− , cfgUnsafe = optUnsafe || cfgUnsafe
34− }
35− r <- ormoluFile config optDebug optInputFile
27+ config <- Yaml.decodeFileThrow path
28+ return (config <> optConfig)
29+ r <- ormoluFile config optInputFile
3630 case optMode of
3731 Stdout ->
3832 TIO.putStr r
@@ -50,12 +44,8 @@ data Opts = Opts
5044 -- ^ Mode of operation
5145 , optConfigFile :: !(Maybe FilePath)
5246 -- ^ Location of configuration file (optional)
53− , optUnsafe :: !Bool
54− -- ^ Whether to skip sanity checking
55− , optDynOptions :: ![DynOption]
56− -- ^ GHC options to set
57− , optDebug :: !Bool
58− -- ^ Output information useful for debugging
47+ , optConfig :: !Config
48+ -- ^ Ormolu 'Config'
5949 , optInputFile :: !FilePath
6050 -- ^ Input source file
6151 }
@@ -108,26 +98,30 @@ optsParser = Opts
10898 , metavar "CONFIG"
10999 , help "Location of configuration file"
110100 ]
111− <*> (switch . mconcat)
112− [ long "unsafe"
113− , short 'u'
114− , help "Do formatting faster but without automatic detection of defects"
101+ <*> configParser
102+ <*> (strArgument . mconcat)
103+ [ metavar "FILE"
104+ , help "Haskell source file to format"
115105 ]
116− <*> (fmap (fmap DynOption) . many . strOption . mconcat)
106+
107+configParser :: Parser Config
108+configParser = Config
109+ <$> (fmap (fmap DynOption) . many . strOption . mconcat)
117110 [ long "ghc-opt"
118111 , short 'o'
119112 , metavar "OPT"
120113 , help "GHC options to enable (e.g. language extensions)"
121114 ]
115+ <*> (switch . mconcat)
116+ [ long "unsafe"
117+ , short 'u'
118+ , help "Do formatting faster but without automatic detection of defects"
119+ ]
122120 <*> (switch . mconcat)
123121 [ long "debug"
124122 , short 'd'
125123 , help "Output information useful for debugging"
126124 ]
127− <*> (strArgument . mconcat)
128− [ metavar "FILE"
129− , help "Haskell source file to format"
130− ]
131125
132126----------------------------------------------------------------------------
133127-- Helpers
src/Ormolu.hs+5−7
@@ -43,19 +43,18 @@ import qualified Outputable as GHC
4343ormolu
4444 :: MonadIO m
4545 => Config -- ^ Ormolu configuration
46− -> Bool -- ^ Output debugging info
4746 -> FilePath -- ^ Location of source file
4847 -> String -- ^ Input to format
4948 -> m Text
50−ormolu cfg debugOn path str = do
49+ormolu cfg path str = do
5150 (ws, (anns0, parsedSrc0)) <-
5251 parseModule' cfg OrmoluParsingFailed path str
53− when debugOn $ do
52+ when (cfgDebug cfg) $ do
5453 traceM "warnings:\n"
5554 traceM (concatMap showWarn ws)
5655 traceM "anns:\n"
5756 traceM (showOutputable anns0)
58− let txt = printModule debugOn anns0 parsedSrc0
57+ let txt = printModule (cfgDebug cfg) anns0 parsedSrc0
5958 -- Parse the result of pretty-printing again and make sure that AST is the
6059 -- same as AST of original snippet module span positions.
6160 unless (cfgUnsafe cfg) $ do
@@ -74,11 +73,10 @@ ormolu cfg debugOn path str = do
7473ormoluFile
7574 :: MonadIO m
7675 => Config -- ^ Ormolu configuration
77− -> Bool -- ^ Output debugging info
7876 -> FilePath -- ^ Location of source file
7977 -> m Text -- ^ Resulting rendition
80−ormoluFile cfg debugOn path =
81− liftIO (readFile path) >>= ormolu cfg debugOn path
78+ormoluFile cfg path =
79+ liftIO (readFile path) >>= ormolu cfg path
8280
8381----------------------------------------------------------------------------
8482-- Helpers
src/Ormolu/Config.hs+15
@@ -22,12 +22,26 @@ data Config = Config
2222 -- ^ Dynamic options to pass to GHC parser
2323 , cfgUnsafe :: !Bool
2424 -- ^ Do formatting faster but without automatic detection of defects
25+ , cfgDebug :: !Bool
26+ -- ^ Output information useful for debugging
2527 } deriving (Eq, Show)
2628
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+
2740instance FromJSON Config where
2841 parseJSON = withObject "config" $ \o -> do
2942 cfgDynOptions <- o .: "ghc-opts"
3043 cfgUnsafe <- o .: "unsafe"
44+ cfgDebug <- o .: "debug"
3145 return Config {..}
3246
3347-- | Default 'Config'.
@@ -36,6 +50,7 @@ defaultConfig :: Config
3650defaultConfig = Config
3751 { cfgDynOptions = []
3852 , cfgUnsafe = False
53+ , cfgDebug = False
3954 }
4055
4156-- | A wrapper for dynamic options.
tests/Ormolu/PrinterSpec.hs+2−2
@@ -29,14 +29,14 @@ checkExample srcPath' = it (fromRelFile srcPath' ++ " works") $ do
2929 -- 2. Parse the result of pretty-printing again and make sure that AST
3030 -- is the same as AST of the original snippet. (This happens in
3131 -- 'ormoluFile' automatically.)
32− formatted0 <- ormoluFile defaultConfig False (fromRelFile srcPath)
32+ formatted0 <- ormoluFile defaultConfig (fromRelFile srcPath)
3333 -- 3. Check the output against expected output. Thus all tests should
3434 -- include two files: input and expected output.
3535 expected <- (liftIO . T.readFile . fromRelFile) expectedOutputPath
3636 formatted0 `shouldMatch` expected
3737 -- 4. Check that running the formatter on the output produces the same
3838 -- output again (the transformation is idempotent).
39− formatted1 <- ormolu defaultConfig False "<formatted>" (T.unpack formatted0)
39+ formatted1 <- ormolu defaultConfig "<formatted>" (T.unpack formatted0)
4040 formatted1 `shouldMatch` formatted0
4141
4242-- | Build list of examples for testing.