1
1
mirror of https://github.com/tweag/ormolu.git synced 2024-09-11 13:16:13 +03:00

Include debugging option in ‘Config’

This commit is contained in:
mrkkrp 2019-02-28 22:16:15 +01:00 committed by Mark Karpov
parent 2f73bae977
commit 6f169f2969
4 changed files with 41 additions and 34 deletions

View File

@ -22,17 +22,11 @@ main :: IO ()
main = withPrettyOrmoluExceptions $ do
Opts {..} <- execParser optsParserInfo
config <- case optConfigFile of
Nothing -> return Config
{ cfgDynOptions = optDynOptions
, cfgUnsafe = optUnsafe
}
Nothing -> return optConfig
Just path -> do
Config {..} <- Yaml.decodeFileThrow path
return Config
{ cfgDynOptions = cfgDynOptions ++ optDynOptions
, cfgUnsafe = optUnsafe || cfgUnsafe
}
r <- ormoluFile config optDebug optInputFile
config <- Yaml.decodeFileThrow path
return (config <> optConfig)
r <- ormoluFile config optInputFile
case optMode of
Stdout ->
TIO.putStr r
@ -50,12 +44,8 @@ data Opts = Opts
-- ^ Mode of operation
, optConfigFile :: !(Maybe FilePath)
-- ^ Location of configuration file (optional)
, optUnsafe :: !Bool
-- ^ Whether to skip sanity checking
, optDynOptions :: ![DynOption]
-- ^ GHC options to set
, optDebug :: !Bool
-- ^ Output information useful for debugging
, optConfig :: !Config
-- ^ Ormolu 'Config'
, optInputFile :: !FilePath
-- ^ Input source file
}
@ -108,26 +98,30 @@ optsParser = Opts
, metavar "CONFIG"
, help "Location of configuration file"
]
<*> (switch . mconcat)
[ long "unsafe"
, short 'u'
, help "Do formatting faster but without automatic detection of defects"
<*> configParser
<*> (strArgument . mconcat)
[ metavar "FILE"
, help "Haskell source file to format"
]
<*> (fmap (fmap DynOption) . many . strOption . mconcat)
configParser :: Parser Config
configParser = Config
<$> (fmap (fmap DynOption) . many . strOption . mconcat)
[ long "ghc-opt"
, short 'o'
, metavar "OPT"
, help "GHC options to enable (e.g. language extensions)"
]
<*> (switch . mconcat)
[ long "unsafe"
, short 'u'
, help "Do formatting faster but without automatic detection of defects"
]
<*> (switch . mconcat)
[ long "debug"
, short 'd'
, help "Output information useful for debugging"
]
<*> (strArgument . mconcat)
[ metavar "FILE"
, help "Haskell source file to format"
]
----------------------------------------------------------------------------
-- Helpers

View File

@ -43,19 +43,18 @@ import qualified Outputable as GHC
ormolu
:: MonadIO m
=> Config -- ^ Ormolu configuration
-> Bool -- ^ Output debugging info
-> FilePath -- ^ Location of source file
-> String -- ^ Input to format
-> m Text
ormolu cfg debugOn path str = do
ormolu cfg path str = do
(ws, (anns0, parsedSrc0)) <-
parseModule' cfg OrmoluParsingFailed path str
when debugOn $ do
when (cfgDebug cfg) $ do
traceM "warnings:\n"
traceM (concatMap showWarn ws)
traceM "anns:\n"
traceM (showOutputable anns0)
let txt = printModule debugOn anns0 parsedSrc0
let txt = printModule (cfgDebug cfg) anns0 parsedSrc0
-- Parse the result of pretty-printing again and make sure that AST is the
-- same as AST of original snippet module span positions.
unless (cfgUnsafe cfg) $ do
@ -74,11 +73,10 @@ ormolu cfg debugOn path str = do
ormoluFile
:: MonadIO m
=> Config -- ^ Ormolu configuration
-> Bool -- ^ Output debugging info
-> FilePath -- ^ Location of source file
-> m Text -- ^ Resulting rendition
ormoluFile cfg debugOn path =
liftIO (readFile path) >>= ormolu cfg debugOn path
ormoluFile cfg path =
liftIO (readFile path) >>= ormolu cfg path
----------------------------------------------------------------------------
-- Helpers

View File

@ -22,12 +22,26 @@ data Config = Config
-- ^ Dynamic options to pass to GHC parser
, cfgUnsafe :: !Bool
-- ^ Do formatting faster but without automatic detection of defects
, cfgDebug :: !Bool
-- ^ Output information useful for debugging
} deriving (Eq, Show)
instance Semigroup Config where
a <> b = Config
{ cfgDynOptions = cfgDynOptions a <> cfgDynOptions b
, cfgUnsafe = cfgUnsafe a || cfgUnsafe b
, cfgDebug = cfgDebug a || cfgDebug b
}
instance Monoid Config where
mempty = defaultConfig
mappend = (<>)
instance FromJSON Config where
parseJSON = withObject "config" $ \o -> do
cfgDynOptions <- o .: "ghc-opts"
cfgUnsafe <- o .: "unsafe"
cfgDebug <- o .: "debug"
return Config {..}
-- | Default 'Config'.
@ -36,6 +50,7 @@ defaultConfig :: Config
defaultConfig = Config
{ cfgDynOptions = []
, cfgUnsafe = False
, cfgDebug = False
}
-- | A wrapper for dynamic options.

View File

@ -29,14 +29,14 @@ checkExample srcPath' = it (fromRelFile srcPath' ++ " works") $ do
-- 2. Parse the result of pretty-printing again and make sure that AST
-- is the same as AST of the original snippet. (This happens in
-- 'ormoluFile' automatically.)
formatted0 <- ormoluFile defaultConfig False (fromRelFile srcPath)
formatted0 <- ormoluFile defaultConfig (fromRelFile srcPath)
-- 3. Check the output against expected output. Thus all tests should
-- include two files: input and expected output.
expected <- (liftIO . T.readFile . fromRelFile) expectedOutputPath
formatted0 `shouldMatch` expected
-- 4. Check that running the formatter on the output produces the same
-- output again (the transformation is idempotent).
formatted1 <- ormolu defaultConfig False "<formatted>" (T.unpack formatted0)
formatted1 <- ormolu defaultConfig "<formatted>" (T.unpack formatted0)
formatted1 `shouldMatch` formatted0
-- | Build list of examples for testing.