on d82d607 help sign in
ormolu/src/Ormolu/Config.hs
65 lines 1.6 kB history raw
1{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE RecordWildCards #-}
5-- | Configuration options used by the tool.
7module Ormolu.Config
8 ( Config (..)
9 , defaultConfig
10 , DynOption (..)
11 , dynOptionToLocatedStr
12 )
13where
15import Data.Yaml
16import qualified SrcLoc as GHC
18-- | Ormolu configuration.
20data Config = Config
21 { cfgDynOptions :: ![DynOption]
22 -- ^ Dynamic options to pass to GHC parser
23 , cfgUnsafe :: !Bool
24 -- ^ Do formatting faster but without automatic detection of defects
25 , cfgDebug :: !Bool
26 -- ^ Output information useful for debugging
27 } deriving (Eq, Show)
29instance 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 }
36instance Monoid Config where
37 mempty = defaultConfig
38 mappend = (<>)
40instance FromJSON Config where
41 parseJSON = withObject "config" $ \o -> do
42 cfgDynOptions <- o .: "ghc-opts"
43 cfgUnsafe <- o .: "unsafe"
44 cfgDebug <- o .: "debug"
45 return Config {..}
47-- | Default 'Config'.
49defaultConfig :: Config
50defaultConfig = Config
51 { cfgDynOptions = []
52 , cfgUnsafe = False
53 , cfgDebug = False
54 }
56-- | A wrapper for dynamic options.
58newtype DynOption = DynOption
59 { unDynOption :: String
60 } deriving (Eq, Ord, Show, FromJSON)
62-- | Convert 'DynOption' to @'GHC.Located' 'String'@.
64dynOptionToLocatedStr :: DynOption -> GHC.Located String
65dynOptionToLocatedStr (DynOption o) = GHC.L GHC.noSrcSpan o