{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

module Floskell.Config
    ( Indent(..)
    , LayoutContext(..)
    , Location(..)
    , WsLoc(..)
    , Whitespace(..)
    , Layout(..)
    , ConfigMapKey(..)
    , ConfigMap(..)
    , PenaltyConfig(..)
    , AlignConfig(..)
    , IndentConfig(..)
    , LayoutConfig(..)
    , OpConfig(..)
    , GroupConfig(..)
    , ImportsGroupOrder(..)
    , ImportsGroup(..)
    , SortImportsRule(..)
    , DeclarationConstruct(..)
    , OptionConfig(..)
    , Config(..)
    , defaultConfig
    , safeConfig
    , cfgMapFind
    , cfgOpWs
    , cfgGroupWs
    , inWs
    , wsSpace
    , wsLinebreak
    ) where

import           Data.Aeson
                 ( FromJSON(..), ToJSON(..), genericParseJSON, genericToJSON )
import qualified Data.Aeson        as JSON
import           Data.Aeson.Types  as JSON
                 ( Options(..), camelTo2, typeMismatch )
import           Data.Default      ( Default(..) )
import qualified Data.HashMap.Lazy as HashMap
import           Data.Map.Strict   ( Map )
import qualified Data.Map.Strict   as Map
import           Data.Set          ( Set )
import qualified Data.Set          as Set
import           Data.Text         ( Text )
import qualified Data.Text         as T

import           GHC.Generics

data Indent = Align | IndentBy !Int | AlignOrIndentBy !Int
    deriving ( Indent -> Indent -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Indent -> Indent -> Bool
$c/= :: Indent -> Indent -> Bool
== :: Indent -> Indent -> Bool
$c== :: Indent -> Indent -> Bool
Eq, Eq Indent
Indent -> Indent -> Bool
Indent -> Indent -> Ordering
Indent -> Indent -> Indent
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Indent -> Indent -> Indent
$cmin :: Indent -> Indent -> Indent
max :: Indent -> Indent -> Indent
$cmax :: Indent -> Indent -> Indent
>= :: Indent -> Indent -> Bool
$c>= :: Indent -> Indent -> Bool
> :: Indent -> Indent -> Bool
$c> :: Indent -> Indent -> Bool
<= :: Indent -> Indent -> Bool
$c<= :: Indent -> Indent -> Bool
< :: Indent -> Indent -> Bool
$c< :: Indent -> Indent -> Bool
compare :: Indent -> Indent -> Ordering
$ccompare :: Indent -> Indent -> Ordering
Ord, Int -> Indent -> ShowS
[Indent] -> ShowS
Indent -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Indent] -> ShowS
$cshowList :: [Indent] -> ShowS
show :: Indent -> String
$cshow :: Indent -> String
showsPrec :: Int -> Indent -> ShowS
$cshowsPrec :: Int -> Indent -> ShowS
Show, forall x. Rep Indent x -> Indent
forall x. Indent -> Rep Indent x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Indent x -> Indent
$cfrom :: forall x. Indent -> Rep Indent x
Generic )

data LayoutContext = Declaration | Type | Pattern | Expression | Other
    deriving ( LayoutContext -> LayoutContext -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: LayoutContext -> LayoutContext -> Bool
$c/= :: LayoutContext -> LayoutContext -> Bool
== :: LayoutContext -> LayoutContext -> Bool
$c== :: LayoutContext -> LayoutContext -> Bool
Eq, Eq LayoutContext
LayoutContext -> LayoutContext -> Bool
LayoutContext -> LayoutContext -> Ordering
LayoutContext -> LayoutContext -> LayoutContext
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: LayoutContext -> LayoutContext -> LayoutContext
$cmin :: LayoutContext -> LayoutContext -> LayoutContext
max :: LayoutContext -> LayoutContext -> LayoutContext
$cmax :: LayoutContext -> LayoutContext -> LayoutContext
>= :: LayoutContext -> LayoutContext -> Bool
$c>= :: LayoutContext -> LayoutContext -> Bool
> :: LayoutContext -> LayoutContext -> Bool
$c> :: LayoutContext -> LayoutContext -> Bool
<= :: LayoutContext -> LayoutContext -> Bool
$c<= :: LayoutContext -> LayoutContext -> Bool
< :: LayoutContext -> LayoutContext -> Bool
$c< :: LayoutContext -> LayoutContext -> Bool
compare :: LayoutContext -> LayoutContext -> Ordering
$ccompare :: LayoutContext -> LayoutContext -> Ordering
Ord, LayoutContext
forall a. a -> a -> Bounded a
maxBound :: LayoutContext
$cmaxBound :: LayoutContext
minBound :: LayoutContext
$cminBound :: LayoutContext
Bounded, Int -> LayoutContext
LayoutContext -> Int
LayoutContext -> [LayoutContext]
LayoutContext -> LayoutContext
LayoutContext -> LayoutContext -> [LayoutContext]
LayoutContext -> LayoutContext -> LayoutContext -> [LayoutContext]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: LayoutContext -> LayoutContext -> LayoutContext -> [LayoutContext]
$cenumFromThenTo :: LayoutContext -> LayoutContext -> LayoutContext -> [LayoutContext]
enumFromTo :: LayoutContext -> LayoutContext -> [LayoutContext]
$cenumFromTo :: LayoutContext -> LayoutContext -> [LayoutContext]
enumFromThen :: LayoutContext -> LayoutContext -> [LayoutContext]
$cenumFromThen :: LayoutContext -> LayoutContext -> [LayoutContext]
enumFrom :: LayoutContext -> [LayoutContext]
$cenumFrom :: LayoutContext -> [LayoutContext]
fromEnum :: LayoutContext -> Int
$cfromEnum :: LayoutContext -> Int
toEnum :: Int -> LayoutContext
$ctoEnum :: Int -> LayoutContext
pred :: LayoutContext -> LayoutContext
$cpred :: LayoutContext -> LayoutContext
succ :: LayoutContext -> LayoutContext
$csucc :: LayoutContext -> LayoutContext
Enum, Int -> LayoutContext -> ShowS
[LayoutContext] -> ShowS
LayoutContext -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [LayoutContext] -> ShowS
$cshowList :: [LayoutContext] -> ShowS
show :: LayoutContext -> String
$cshow :: LayoutContext -> String
showsPrec :: Int -> LayoutContext -> ShowS
$cshowsPrec :: Int -> LayoutContext -> ShowS
Show, forall x. Rep LayoutContext x -> LayoutContext
forall x. LayoutContext -> Rep LayoutContext x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep LayoutContext x -> LayoutContext
$cfrom :: forall x. LayoutContext -> Rep LayoutContext x
Generic )

data Location = Before | After
    deriving ( Location -> Location -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Location -> Location -> Bool
$c/= :: Location -> Location -> Bool
== :: Location -> Location -> Bool
$c== :: Location -> Location -> Bool
Eq, Eq Location
Location -> Location -> Bool
Location -> Location -> Ordering
Location -> Location -> Location
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Location -> Location -> Location
$cmin :: Location -> Location -> Location
max :: Location -> Location -> Location
$cmax :: Location -> Location -> Location
>= :: Location -> Location -> Bool
$c>= :: Location -> Location -> Bool
> :: Location -> Location -> Bool
$c> :: Location -> Location -> Bool
<= :: Location -> Location -> Bool
$c<= :: Location -> Location -> Bool
< :: Location -> Location -> Bool
$c< :: Location -> Location -> Bool
compare :: Location -> Location -> Ordering
$ccompare :: Location -> Location -> Ordering
Ord, Location
forall a. a -> a -> Bounded a
maxBound :: Location
$cmaxBound :: Location
minBound :: Location
$cminBound :: Location
Bounded, Int -> Location
Location -> Int
Location -> [Location]
Location -> Location
Location -> Location -> [Location]
Location -> Location -> Location -> [Location]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: Location -> Location -> Location -> [Location]
$cenumFromThenTo :: Location -> Location -> Location -> [Location]
enumFromTo :: Location -> Location -> [Location]
$cenumFromTo :: Location -> Location -> [Location]
enumFromThen :: Location -> Location -> [Location]
$cenumFromThen :: Location -> Location -> [Location]
enumFrom :: Location -> [Location]
$cenumFrom :: Location -> [Location]
fromEnum :: Location -> Int
$cfromEnum :: Location -> Int
toEnum :: Int -> Location
$ctoEnum :: Int -> Location
pred :: Location -> Location
$cpred :: Location -> Location
succ :: Location -> Location
$csucc :: Location -> Location
Enum, Int -> Location -> ShowS
[Location] -> ShowS
Location -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Location] -> ShowS
$cshowList :: [Location] -> ShowS
show :: Location -> String
$cshow :: Location -> String
showsPrec :: Int -> Location -> ShowS
$cshowsPrec :: Int -> Location -> ShowS
Show, forall x. Rep Location x -> Location
forall x. Location -> Rep Location x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Location x -> Location
$cfrom :: forall x. Location -> Rep Location x
Generic )

data WsLoc = WsNone | WsBefore | WsAfter | WsBoth
    deriving ( WsLoc -> WsLoc -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: WsLoc -> WsLoc -> Bool
$c/= :: WsLoc -> WsLoc -> Bool
== :: WsLoc -> WsLoc -> Bool
$c== :: WsLoc -> WsLoc -> Bool
Eq, Eq WsLoc
WsLoc -> WsLoc -> Bool
WsLoc -> WsLoc -> Ordering
WsLoc -> WsLoc -> WsLoc
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: WsLoc -> WsLoc -> WsLoc
$cmin :: WsLoc -> WsLoc -> WsLoc
max :: WsLoc -> WsLoc -> WsLoc
$cmax :: WsLoc -> WsLoc -> WsLoc
>= :: WsLoc -> WsLoc -> Bool
$c>= :: WsLoc -> WsLoc -> Bool
> :: WsLoc -> WsLoc -> Bool
$c> :: WsLoc -> WsLoc -> Bool
<= :: WsLoc -> WsLoc -> Bool
$c<= :: WsLoc -> WsLoc -> Bool
< :: WsLoc -> WsLoc -> Bool
$c< :: WsLoc -> WsLoc -> Bool
compare :: WsLoc -> WsLoc -> Ordering
$ccompare :: WsLoc -> WsLoc -> Ordering
Ord, WsLoc
forall a. a -> a -> Bounded a
maxBound :: WsLoc
$cmaxBound :: WsLoc
minBound :: WsLoc
$cminBound :: WsLoc
Bounded, Int -> WsLoc
WsLoc -> Int
WsLoc -> [WsLoc]
WsLoc -> WsLoc
WsLoc -> WsLoc -> [WsLoc]
WsLoc -> WsLoc -> WsLoc -> [WsLoc]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: WsLoc -> WsLoc -> WsLoc -> [WsLoc]
$cenumFromThenTo :: WsLoc -> WsLoc -> WsLoc -> [WsLoc]
enumFromTo :: WsLoc -> WsLoc -> [WsLoc]
$cenumFromTo :: WsLoc -> WsLoc -> [WsLoc]
enumFromThen :: WsLoc -> WsLoc -> [WsLoc]
$cenumFromThen :: WsLoc -> WsLoc -> [WsLoc]
enumFrom :: WsLoc -> [WsLoc]
$cenumFrom :: WsLoc -> [WsLoc]
fromEnum :: WsLoc -> Int
$cfromEnum :: WsLoc -> Int
toEnum :: Int -> WsLoc
$ctoEnum :: Int -> WsLoc
pred :: WsLoc -> WsLoc
$cpred :: WsLoc -> WsLoc
succ :: WsLoc -> WsLoc
$csucc :: WsLoc -> WsLoc
Enum, Int -> WsLoc -> ShowS
[WsLoc] -> ShowS
WsLoc -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [WsLoc] -> ShowS
$cshowList :: [WsLoc] -> ShowS
show :: WsLoc -> String
$cshow :: WsLoc -> String
showsPrec :: Int -> WsLoc -> ShowS
$cshowsPrec :: Int -> WsLoc -> ShowS
Show, forall x. Rep WsLoc x -> WsLoc
forall x. WsLoc -> Rep WsLoc x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep WsLoc x -> WsLoc
$cfrom :: forall x. WsLoc -> Rep WsLoc x
Generic )

data Whitespace = Whitespace { Whitespace -> WsLoc
wsSpaces         :: !WsLoc
                             , Whitespace -> WsLoc
wsLinebreaks     :: !WsLoc
                             , Whitespace -> Bool
wsForceLinebreak :: !Bool
                             }
    deriving ( Int -> Whitespace -> ShowS
[Whitespace] -> ShowS
Whitespace -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Whitespace] -> ShowS
$cshowList :: [Whitespace] -> ShowS
show :: Whitespace -> String
$cshow :: Whitespace -> String
showsPrec :: Int -> Whitespace -> ShowS
$cshowsPrec :: Int -> Whitespace -> ShowS
Show, forall x. Rep Whitespace x -> Whitespace
forall x. Whitespace -> Rep Whitespace x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Whitespace x -> Whitespace
$cfrom :: forall x. Whitespace -> Rep Whitespace x
Generic )

data Layout = Flex | Vertical | TryOneline
    deriving ( Layout -> Layout -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Layout -> Layout -> Bool
$c/= :: Layout -> Layout -> Bool
== :: Layout -> Layout -> Bool
$c== :: Layout -> Layout -> Bool
Eq, Eq Layout
Layout -> Layout -> Bool
Layout -> Layout -> Ordering
Layout -> Layout -> Layout
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Layout -> Layout -> Layout
$cmin :: Layout -> Layout -> Layout
max :: Layout -> Layout -> Layout
$cmax :: Layout -> Layout -> Layout
>= :: Layout -> Layout -> Bool
$c>= :: Layout -> Layout -> Bool
> :: Layout -> Layout -> Bool
$c> :: Layout -> Layout -> Bool
<= :: Layout -> Layout -> Bool
$c<= :: Layout -> Layout -> Bool
< :: Layout -> Layout -> Bool
$c< :: Layout -> Layout -> Bool
compare :: Layout -> Layout -> Ordering
$ccompare :: Layout -> Layout -> Ordering
Ord, Layout
forall a. a -> a -> Bounded a
maxBound :: Layout
$cmaxBound :: Layout
minBound :: Layout
$cminBound :: Layout
Bounded, Int -> Layout
Layout -> Int
Layout -> [Layout]
Layout -> Layout
Layout -> Layout -> [Layout]
Layout -> Layout -> Layout -> [Layout]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: Layout -> Layout -> Layout -> [Layout]
$cenumFromThenTo :: Layout -> Layout -> Layout -> [Layout]
enumFromTo :: Layout -> Layout -> [Layout]
$cenumFromTo :: Layout -> Layout -> [Layout]
enumFromThen :: Layout -> Layout -> [Layout]
$cenumFromThen :: Layout -> Layout -> [Layout]
enumFrom :: Layout -> [Layout]
$cenumFrom :: Layout -> [Layout]
fromEnum :: Layout -> Int
$cfromEnum :: Layout -> Int
toEnum :: Int -> Layout
$ctoEnum :: Int -> Layout
pred :: Layout -> Layout
$cpred :: Layout -> Layout
succ :: Layout -> Layout
$csucc :: Layout -> Layout
Enum, Int -> Layout -> ShowS
[Layout] -> ShowS
Layout -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Layout] -> ShowS
$cshowList :: [Layout] -> ShowS
show :: Layout -> String
$cshow :: Layout -> String
showsPrec :: Int -> Layout -> ShowS
$cshowsPrec :: Int -> Layout -> ShowS
Show, forall x. Rep Layout x -> Layout
forall x. Layout -> Rep Layout x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Layout x -> Layout
$cfrom :: forall x. Layout -> Rep Layout x
Generic )

data ConfigMapKey = ConfigMapKey !(Maybe Text) !(Maybe LayoutContext)
    deriving ( ConfigMapKey -> ConfigMapKey -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ConfigMapKey -> ConfigMapKey -> Bool
$c/= :: ConfigMapKey -> ConfigMapKey -> Bool
== :: ConfigMapKey -> ConfigMapKey -> Bool
$c== :: ConfigMapKey -> ConfigMapKey -> Bool
Eq, Eq ConfigMapKey
ConfigMapKey -> ConfigMapKey -> Bool
ConfigMapKey -> ConfigMapKey -> Ordering
ConfigMapKey -> ConfigMapKey -> ConfigMapKey
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ConfigMapKey -> ConfigMapKey -> ConfigMapKey
$cmin :: ConfigMapKey -> ConfigMapKey -> ConfigMapKey
max :: ConfigMapKey -> ConfigMapKey -> ConfigMapKey
$cmax :: ConfigMapKey -> ConfigMapKey -> ConfigMapKey
>= :: ConfigMapKey -> ConfigMapKey -> Bool
$c>= :: ConfigMapKey -> ConfigMapKey -> Bool
> :: ConfigMapKey -> ConfigMapKey -> Bool
$c> :: ConfigMapKey -> ConfigMapKey -> Bool
<= :: ConfigMapKey -> ConfigMapKey -> Bool
$c<= :: ConfigMapKey -> ConfigMapKey -> Bool
< :: ConfigMapKey -> ConfigMapKey -> Bool
$c< :: ConfigMapKey -> ConfigMapKey -> Bool
compare :: ConfigMapKey -> ConfigMapKey -> Ordering
$ccompare :: ConfigMapKey -> ConfigMapKey -> Ordering
Ord, Int -> ConfigMapKey -> ShowS
[ConfigMapKey] -> ShowS
ConfigMapKey -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ConfigMapKey] -> ShowS
$cshowList :: [ConfigMapKey] -> ShowS
show :: ConfigMapKey -> String
$cshow :: ConfigMapKey -> String
showsPrec :: Int -> ConfigMapKey -> ShowS
$cshowsPrec :: Int -> ConfigMapKey -> ShowS
Show )

data ConfigMap a =
    ConfigMap { forall a. ConfigMap a -> a
cfgMapDefault :: !a, forall a. ConfigMap a -> Map ConfigMapKey a
cfgMapOverrides :: !(Map ConfigMapKey a) }
    deriving ( forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (ConfigMap a) x -> ConfigMap a
forall a x. ConfigMap a -> Rep (ConfigMap a) x
$cto :: forall a x. Rep (ConfigMap a) x -> ConfigMap a
$cfrom :: forall a x. ConfigMap a -> Rep (ConfigMap a) x
Generic )

data PenaltyConfig = PenaltyConfig { PenaltyConfig -> Int
penaltyMaxLineLength :: !Int
                                   , PenaltyConfig -> Int
penaltyLinebreak     :: !Int
                                   , PenaltyConfig -> Int
penaltyIndent        :: !Int
                                   , PenaltyConfig -> Int
penaltyOverfull      :: !Int
                                   , PenaltyConfig -> Int
penaltyOverfullOnce  :: !Int
                                   }
    deriving ( forall x. Rep PenaltyConfig x -> PenaltyConfig
forall x. PenaltyConfig -> Rep PenaltyConfig x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PenaltyConfig x -> PenaltyConfig
$cfrom :: forall x. PenaltyConfig -> Rep PenaltyConfig x
Generic )

instance Default PenaltyConfig where
    def :: PenaltyConfig
def = PenaltyConfig { penaltyMaxLineLength :: Int
penaltyMaxLineLength = Int
80
                        , penaltyLinebreak :: Int
penaltyLinebreak     = Int
100
                        , penaltyIndent :: Int
penaltyIndent        = Int
1
                        , penaltyOverfull :: Int
penaltyOverfull      = Int
10
                        , penaltyOverfullOnce :: Int
penaltyOverfullOnce  = Int
200
                        }

data AlignConfig =
    AlignConfig { AlignConfig -> (Int, Int)
cfgAlignLimits       :: !(Int, Int)
                , AlignConfig -> Bool
cfgAlignCase         :: !Bool
                , AlignConfig -> Bool
cfgAlignClass        :: !Bool
                , AlignConfig -> Bool
cfgAlignImportModule :: !Bool
                , AlignConfig -> Bool
cfgAlignImportSpec   :: !Bool
                , AlignConfig -> Bool
cfgAlignLetBinds     :: !Bool
                , AlignConfig -> Bool
cfgAlignMatches      :: !Bool
                , AlignConfig -> Bool
cfgAlignRecordFields :: !Bool
                , AlignConfig -> Bool
cfgAlignWhere        :: !Bool
                }
    deriving ( forall x. Rep AlignConfig x -> AlignConfig
forall x. AlignConfig -> Rep AlignConfig x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep AlignConfig x -> AlignConfig
$cfrom :: forall x. AlignConfig -> Rep AlignConfig x
Generic )

instance Default AlignConfig where
    def :: AlignConfig
def = AlignConfig { cfgAlignLimits :: (Int, Int)
cfgAlignLimits       = (Int
10, Int
25)
                      , cfgAlignCase :: Bool
cfgAlignCase         = Bool
False
                      , cfgAlignClass :: Bool
cfgAlignClass        = Bool
False
                      , cfgAlignImportModule :: Bool
cfgAlignImportModule = Bool
False
                      , cfgAlignImportSpec :: Bool
cfgAlignImportSpec   = Bool
False
                      , cfgAlignLetBinds :: Bool
cfgAlignLetBinds     = Bool
False
                      , cfgAlignMatches :: Bool
cfgAlignMatches      = Bool
False
                      , cfgAlignRecordFields :: Bool
cfgAlignRecordFields = Bool
False
                      , cfgAlignWhere :: Bool
cfgAlignWhere        = Bool
False
                      }

data IndentConfig =
    IndentConfig { IndentConfig -> Int
cfgIndentOnside         :: !Int
                 , IndentConfig -> Int
cfgIndentDeriving       :: !Int
                 , IndentConfig -> Int
cfgIndentWhere          :: !Int
                 , IndentConfig -> Indent
cfgIndentApp            :: !Indent
                 , IndentConfig -> Indent
cfgIndentCase           :: !Indent
                 , IndentConfig -> Indent
cfgIndentClass          :: !Indent
                 , IndentConfig -> Indent
cfgIndentDo             :: !Indent
                 , IndentConfig -> Indent
cfgIndentExportSpecList :: !Indent
                 , IndentConfig -> Indent
cfgIndentIf             :: !Indent
                 , IndentConfig -> Indent
cfgIndentImportSpecList :: !Indent
                 , IndentConfig -> Indent
cfgIndentLet            :: !Indent
                 , IndentConfig -> Indent
cfgIndentLetBinds       :: !Indent
                 , IndentConfig -> Indent
cfgIndentLetIn          :: !Indent
                 , IndentConfig -> Indent
cfgIndentMultiIf        :: !Indent
                 , IndentConfig -> Indent
cfgIndentTypesig        :: !Indent
                 , IndentConfig -> Indent
cfgIndentWhereBinds     :: !Indent
                 }
    deriving ( forall x. Rep IndentConfig x -> IndentConfig
forall x. IndentConfig -> Rep IndentConfig x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep IndentConfig x -> IndentConfig
$cfrom :: forall x. IndentConfig -> Rep IndentConfig x
Generic )

instance Default IndentConfig where
    def :: IndentConfig
def = IndentConfig { cfgIndentOnside :: Int
cfgIndentOnside         = Int
4
                       , cfgIndentDeriving :: Int
cfgIndentDeriving       = Int
4
                       , cfgIndentWhere :: Int
cfgIndentWhere          = Int
2
                       , cfgIndentApp :: Indent
cfgIndentApp            = Int -> Indent
IndentBy Int
4
                       , cfgIndentCase :: Indent
cfgIndentCase           = Int -> Indent
IndentBy Int
4
                       , cfgIndentClass :: Indent
cfgIndentClass          = Int -> Indent
IndentBy Int
4
                       , cfgIndentDo :: Indent
cfgIndentDo             = Int -> Indent
IndentBy Int
4
                       , cfgIndentExportSpecList :: Indent
cfgIndentExportSpecList = Int -> Indent
IndentBy Int
4
                       , cfgIndentIf :: Indent
cfgIndentIf             = Int -> Indent
IndentBy Int
4
                       , cfgIndentImportSpecList :: Indent
cfgIndentImportSpecList = Int -> Indent
IndentBy Int
4
                       , cfgIndentLet :: Indent
cfgIndentLet            = Int -> Indent
IndentBy Int
4
                       , cfgIndentLetBinds :: Indent
cfgIndentLetBinds       = Int -> Indent
IndentBy Int
4
                       , cfgIndentLetIn :: Indent
cfgIndentLetIn          = Int -> Indent
IndentBy Int
4
                       , cfgIndentMultiIf :: Indent
cfgIndentMultiIf        = Int -> Indent
IndentBy Int
4
                       , cfgIndentTypesig :: Indent
cfgIndentTypesig        = Int -> Indent
IndentBy Int
4
                       , cfgIndentWhereBinds :: Indent
cfgIndentWhereBinds     = Int -> Indent
IndentBy Int
2
                       }

data LayoutConfig =
    LayoutConfig { LayoutConfig -> Layout
cfgLayoutApp            :: !Layout
                 , LayoutConfig -> Layout
cfgLayoutConDecls       :: !Layout
                 , LayoutConfig -> Layout
cfgLayoutDeclaration    :: !Layout
                 , LayoutConfig -> Layout
cfgLayoutExportSpecList :: !Layout
                 , LayoutConfig -> Layout
cfgLayoutIf             :: !Layout
                 , LayoutConfig -> Layout
cfgLayoutImportSpecList :: !Layout
                 , LayoutConfig -> Layout
cfgLayoutInfixApp       :: !Layout
                 , LayoutConfig -> Layout
cfgLayoutLet            :: !Layout
                 , LayoutConfig -> Layout
cfgLayoutListComp       :: !Layout
                 , LayoutConfig -> Layout
cfgLayoutRecord         :: !Layout
                 , LayoutConfig -> Layout
cfgLayoutType           :: !Layout
                 }
    deriving ( forall x. Rep LayoutConfig x -> LayoutConfig
forall x. LayoutConfig -> Rep LayoutConfig x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep LayoutConfig x -> LayoutConfig
$cfrom :: forall x. LayoutConfig -> Rep LayoutConfig x
Generic )

instance Default LayoutConfig where
    def :: LayoutConfig
def = LayoutConfig { cfgLayoutApp :: Layout
cfgLayoutApp            = Layout
Flex
                       , cfgLayoutConDecls :: Layout
cfgLayoutConDecls       = Layout
Flex
                       , cfgLayoutDeclaration :: Layout
cfgLayoutDeclaration    = Layout
Flex
                       , cfgLayoutExportSpecList :: Layout
cfgLayoutExportSpecList = Layout
Flex
                       , cfgLayoutIf :: Layout
cfgLayoutIf             = Layout
Flex
                       , cfgLayoutImportSpecList :: Layout
cfgLayoutImportSpecList = Layout
Flex
                       , cfgLayoutInfixApp :: Layout
cfgLayoutInfixApp       = Layout
Flex
                       , cfgLayoutLet :: Layout
cfgLayoutLet            = Layout
Flex
                       , cfgLayoutListComp :: Layout
cfgLayoutListComp       = Layout
Flex
                       , cfgLayoutRecord :: Layout
cfgLayoutRecord         = Layout
Flex
                       , cfgLayoutType :: Layout
cfgLayoutType           = Layout
Flex
                       }

newtype OpConfig = OpConfig { OpConfig -> ConfigMap Whitespace
unOpConfig :: ConfigMap Whitespace }
    deriving ( forall x. Rep OpConfig x -> OpConfig
forall x. OpConfig -> Rep OpConfig x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep OpConfig x -> OpConfig
$cfrom :: forall x. OpConfig -> Rep OpConfig x
Generic )

instance Default OpConfig where
    def :: OpConfig
def =
        ConfigMap Whitespace -> OpConfig
OpConfig ConfigMap { cfgMapDefault :: Whitespace
cfgMapDefault   = WsLoc -> WsLoc -> Bool -> Whitespace
Whitespace WsLoc
WsBoth WsLoc
WsBefore Bool
False
                           , cfgMapOverrides :: Map ConfigMapKey Whitespace
cfgMapOverrides = forall k a. Map k a
Map.empty
                           }

newtype GroupConfig = GroupConfig { GroupConfig -> ConfigMap Whitespace
unGroupConfig :: ConfigMap Whitespace }
    deriving ( forall x. Rep GroupConfig x -> GroupConfig
forall x. GroupConfig -> Rep GroupConfig x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep GroupConfig x -> GroupConfig
$cfrom :: forall x. GroupConfig -> Rep GroupConfig x
Generic )

instance Default GroupConfig where
    def :: GroupConfig
def = ConfigMap Whitespace -> GroupConfig
GroupConfig ConfigMap { cfgMapDefault :: Whitespace
cfgMapDefault   =
                                      WsLoc -> WsLoc -> Bool -> Whitespace
Whitespace WsLoc
WsBoth WsLoc
WsAfter Bool
False
                                , cfgMapOverrides :: Map ConfigMapKey Whitespace
cfgMapOverrides = forall k a. Map k a
Map.empty
                                }

data ImportsGroupOrder =
    ImportsGroupKeep | ImportsGroupSorted | ImportsGroupGrouped
    deriving ( forall x. Rep ImportsGroupOrder x -> ImportsGroupOrder
forall x. ImportsGroupOrder -> Rep ImportsGroupOrder x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ImportsGroupOrder x -> ImportsGroupOrder
$cfrom :: forall x. ImportsGroupOrder -> Rep ImportsGroupOrder x
Generic )

data ImportsGroup = ImportsGroup { ImportsGroup -> [String]
importsPrefixes :: ![String]
                                 , ImportsGroup -> ImportsGroupOrder
importsOrder    :: !ImportsGroupOrder
                                 }
    deriving ( forall x. Rep ImportsGroup x -> ImportsGroup
forall x. ImportsGroup -> Rep ImportsGroup x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ImportsGroup x -> ImportsGroup
$cfrom :: forall x. ImportsGroup -> Rep ImportsGroup x
Generic )

data SortImportsRule =
    NoImportSort | SortImportsByPrefix | SortImportsByGroups ![ImportsGroup]

data DeclarationConstruct = DeclModule | DeclClass | DeclInstance | DeclWhere
    deriving ( DeclarationConstruct -> DeclarationConstruct -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DeclarationConstruct -> DeclarationConstruct -> Bool
$c/= :: DeclarationConstruct -> DeclarationConstruct -> Bool
== :: DeclarationConstruct -> DeclarationConstruct -> Bool
$c== :: DeclarationConstruct -> DeclarationConstruct -> Bool
Eq, Eq DeclarationConstruct
DeclarationConstruct -> DeclarationConstruct -> Bool
DeclarationConstruct -> DeclarationConstruct -> Ordering
DeclarationConstruct
-> DeclarationConstruct -> DeclarationConstruct
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: DeclarationConstruct
-> DeclarationConstruct -> DeclarationConstruct
$cmin :: DeclarationConstruct
-> DeclarationConstruct -> DeclarationConstruct
max :: DeclarationConstruct
-> DeclarationConstruct -> DeclarationConstruct
$cmax :: DeclarationConstruct
-> DeclarationConstruct -> DeclarationConstruct
>= :: DeclarationConstruct -> DeclarationConstruct -> Bool
$c>= :: DeclarationConstruct -> DeclarationConstruct -> Bool
> :: DeclarationConstruct -> DeclarationConstruct -> Bool
$c> :: DeclarationConstruct -> DeclarationConstruct -> Bool
<= :: DeclarationConstruct -> DeclarationConstruct -> Bool
$c<= :: DeclarationConstruct -> DeclarationConstruct -> Bool
< :: DeclarationConstruct -> DeclarationConstruct -> Bool
$c< :: DeclarationConstruct -> DeclarationConstruct -> Bool
compare :: DeclarationConstruct -> DeclarationConstruct -> Ordering
$ccompare :: DeclarationConstruct -> DeclarationConstruct -> Ordering
Ord, forall x. Rep DeclarationConstruct x -> DeclarationConstruct
forall x. DeclarationConstruct -> Rep DeclarationConstruct x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep DeclarationConstruct x -> DeclarationConstruct
$cfrom :: forall x. DeclarationConstruct -> Rep DeclarationConstruct x
Generic )

data OptionConfig =
    OptionConfig { OptionConfig -> Bool
cfgOptionSortPragmas            :: !Bool
                 , OptionConfig -> Bool
cfgOptionSplitLanguagePragmas   :: !Bool
                 , OptionConfig -> SortImportsRule
cfgOptionSortImports            :: !SortImportsRule
                 , OptionConfig -> Bool
cfgOptionSortImportLists        :: !Bool
                 , OptionConfig -> Bool
cfgOptionAlignSumTypeDecl       :: !Bool
                 , OptionConfig -> Bool
cfgOptionFlexibleOneline        :: !Bool
                 , OptionConfig -> Bool
cfgOptionPreserveVerticalSpace  :: !Bool
                 , OptionConfig -> Set DeclarationConstruct
cfgOptionDeclNoBlankLines
                       :: !(Set DeclarationConstruct)
                 , OptionConfig -> Bool
cfgOptionAlignLetBindsAndInExpr :: !Bool
                 }
    deriving ( forall x. Rep OptionConfig x -> OptionConfig
forall x. OptionConfig -> Rep OptionConfig x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep OptionConfig x -> OptionConfig
$cfrom :: forall x. OptionConfig -> Rep OptionConfig x
Generic )

instance Default OptionConfig where
    def :: OptionConfig
def = OptionConfig { cfgOptionSortPragmas :: Bool
cfgOptionSortPragmas            = Bool
False
                       , cfgOptionSplitLanguagePragmas :: Bool
cfgOptionSplitLanguagePragmas   = Bool
False
                       , cfgOptionSortImports :: SortImportsRule
cfgOptionSortImports            = SortImportsRule
NoImportSort
                       , cfgOptionSortImportLists :: Bool
cfgOptionSortImportLists        = Bool
False
                       , cfgOptionAlignSumTypeDecl :: Bool
cfgOptionAlignSumTypeDecl       = Bool
False
                       , cfgOptionFlexibleOneline :: Bool
cfgOptionFlexibleOneline        = Bool
False
                       , cfgOptionPreserveVerticalSpace :: Bool
cfgOptionPreserveVerticalSpace  = Bool
False
                       , cfgOptionDeclNoBlankLines :: Set DeclarationConstruct
cfgOptionDeclNoBlankLines       = forall a. Set a
Set.empty
                       , cfgOptionAlignLetBindsAndInExpr :: Bool
cfgOptionAlignLetBindsAndInExpr = Bool
False
                       }

data Config = Config { Config -> PenaltyConfig
cfgPenalty :: !PenaltyConfig
                     , Config -> AlignConfig
cfgAlign   :: !AlignConfig
                     , Config -> IndentConfig
cfgIndent  :: !IndentConfig
                     , Config -> LayoutConfig
cfgLayout  :: !LayoutConfig
                     , Config -> OpConfig
cfgOp      :: !OpConfig
                     , Config -> GroupConfig
cfgGroup   :: !GroupConfig
                     , Config -> OptionConfig
cfgOptions :: !OptionConfig
                     }
    deriving ( forall x. Rep Config x -> Config
forall x. Config -> Rep Config x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Config x -> Config
$cfrom :: forall x. Config -> Rep Config x
Generic )

instance Default Config where
    def :: Config
def = Config { cfgPenalty :: PenaltyConfig
cfgPenalty = forall a. Default a => a
def
                 , cfgAlign :: AlignConfig
cfgAlign   = forall a. Default a => a
def
                 , cfgIndent :: IndentConfig
cfgIndent  = forall a. Default a => a
def
                 , cfgLayout :: LayoutConfig
cfgLayout  = forall a. Default a => a
def
                 , cfgOp :: OpConfig
cfgOp      = forall a. Default a => a
def
                 , cfgGroup :: GroupConfig
cfgGroup   = forall a. Default a => a
def
                 , cfgOptions :: OptionConfig
cfgOptions = forall a. Default a => a
def
                 }

defaultConfig :: Config
defaultConfig :: Config
defaultConfig =
    forall a. Default a => a
def { cfgOp :: OpConfig
cfgOp    = ConfigMap Whitespace -> OpConfig
OpConfig ((OpConfig -> ConfigMap Whitespace
unOpConfig forall a. Default a => a
def) { cfgMapOverrides :: Map ConfigMapKey Whitespace
cfgMapOverrides =
                                                      forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(ConfigMapKey, Whitespace)]
opWsOverrides
                                                })
        , cfgGroup :: GroupConfig
cfgGroup = ConfigMap Whitespace -> GroupConfig
GroupConfig ((GroupConfig -> ConfigMap Whitespace
unGroupConfig forall a. Default a => a
def) { cfgMapOverrides :: Map ConfigMapKey Whitespace
cfgMapOverrides = forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(ConfigMapKey, Whitespace)]
groupWsOverrides
                                                      })
        }
  where
    opWsOverrides :: [(ConfigMapKey, Whitespace)]
opWsOverrides =
        [ (Maybe Text -> Maybe LayoutContext -> ConfigMapKey
ConfigMapKey (forall a. a -> Maybe a
Just Text
",") forall a. Maybe a
Nothing, WsLoc -> WsLoc -> Bool -> Whitespace
Whitespace WsLoc
WsAfter WsLoc
WsBefore Bool
False)
        , ( Maybe Text -> Maybe LayoutContext -> ConfigMapKey
ConfigMapKey (forall a. a -> Maybe a
Just Text
"record") forall a. Maybe a
Nothing
          , WsLoc -> WsLoc -> Bool -> Whitespace
Whitespace WsLoc
WsAfter WsLoc
WsAfter Bool
False
          )
        , ( Maybe Text -> Maybe LayoutContext -> ConfigMapKey
ConfigMapKey (forall a. a -> Maybe a
Just Text
".") (forall a. a -> Maybe a
Just LayoutContext
Type)
          , WsLoc -> WsLoc -> Bool -> Whitespace
Whitespace WsLoc
WsAfter WsLoc
WsAfter Bool
False
          )
        ]

    groupWsOverrides :: [(ConfigMapKey, Whitespace)]
groupWsOverrides =
        [ (Maybe Text -> Maybe LayoutContext -> ConfigMapKey
ConfigMapKey (forall a. a -> Maybe a
Just Text
"[") (forall a. a -> Maybe a
Just LayoutContext
Type), WsLoc -> WsLoc -> Bool -> Whitespace
Whitespace WsLoc
WsBoth WsLoc
WsNone Bool
False)
        ]

safeConfig :: Config -> Config
safeConfig :: Config -> Config
safeConfig Config
cfg = Config
cfg { cfgGroup :: GroupConfig
cfgGroup = GroupConfig
group, cfgOp :: OpConfig
cfgOp = OpConfig
op }
  where
    group :: GroupConfig
group = ConfigMap Whitespace -> GroupConfig
GroupConfig forall a b. (a -> b) -> a -> b
$
        forall {t :: * -> *}.
Foldable t =>
ConfigMap Whitespace
-> t (Text, LayoutContext, WsLoc) -> ConfigMap Whitespace
updateOverrides (GroupConfig -> ConfigMap Whitespace
unGroupConfig forall a b. (a -> b) -> a -> b
$ Config -> GroupConfig
cfgGroup Config
cfg)
                        [ (Text
"(#", LayoutContext
Expression, WsLoc
WsBoth), (Text
"(#", LayoutContext
Pattern, WsLoc
WsBoth) ]

    op :: OpConfig
op = ConfigMap Whitespace -> OpConfig
OpConfig forall a b. (a -> b) -> a -> b
$
        forall {t :: * -> *}.
Foldable t =>
ConfigMap Whitespace
-> t (Text, LayoutContext, WsLoc) -> ConfigMap Whitespace
updateOverrides (OpConfig -> ConfigMap Whitespace
unOpConfig forall a b. (a -> b) -> a -> b
$ Config -> OpConfig
cfgOp Config
cfg)
                        [ (Text
".", LayoutContext
Expression, WsLoc
WsBoth), (Text
"@", LayoutContext
Pattern, WsLoc
WsNone) ]

    updateOverrides :: ConfigMap Whitespace
-> t (Text, LayoutContext, WsLoc) -> ConfigMap Whitespace
updateOverrides ConfigMap Whitespace
config t (Text, LayoutContext, WsLoc)
overrides =
        ConfigMap Whitespace
config { cfgMapOverrides :: Map ConfigMapKey Whitespace
cfgMapOverrides =
                     forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (ConfigMap Whitespace
-> Map ConfigMapKey Whitespace
-> (Text, LayoutContext, WsLoc)
-> Map ConfigMapKey Whitespace
updateWs ConfigMap Whitespace
config) (forall a. ConfigMap a -> Map ConfigMapKey a
cfgMapOverrides ConfigMap Whitespace
config) t (Text, LayoutContext, WsLoc)
overrides
               }

    updateWs :: ConfigMap Whitespace
-> Map ConfigMapKey Whitespace
-> (Text, LayoutContext, WsLoc)
-> Map ConfigMapKey Whitespace
updateWs ConfigMap Whitespace
config Map ConfigMapKey Whitespace
m (Text
key, LayoutContext
ctx, WsLoc
ws) =
        forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
Map.insertWith (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b. a -> b -> a
const)
                       (Maybe Text -> Maybe LayoutContext -> ConfigMapKey
ConfigMapKey (forall a. a -> Maybe a
Just Text
key) (forall a. a -> Maybe a
Just LayoutContext
ctx))
                       (forall a. LayoutContext -> Text -> ConfigMap a -> a
cfgMapFind LayoutContext
ctx Text
key ConfigMap Whitespace
config) { wsSpaces :: WsLoc
wsSpaces = WsLoc
ws }
                       Map ConfigMapKey Whitespace
m

cfgMapFind :: LayoutContext -> Text -> ConfigMap a -> a
cfgMapFind :: forall a. LayoutContext -> Text -> ConfigMap a -> a
cfgMapFind LayoutContext
ctx Text
key ConfigMap{a
Map ConfigMapKey a
cfgMapOverrides :: Map ConfigMapKey a
cfgMapDefault :: a
cfgMapOverrides :: forall a. ConfigMap a -> Map ConfigMapKey a
cfgMapDefault :: forall a. ConfigMap a -> a
..} =
    let value :: a
value = a
cfgMapDefault
        value' :: a
value' = forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault a
value
                                     (Maybe Text -> Maybe LayoutContext -> ConfigMapKey
ConfigMapKey forall a. Maybe a
Nothing (forall a. a -> Maybe a
Just LayoutContext
ctx))
                                     Map ConfigMapKey a
cfgMapOverrides
        value'' :: a
value'' = forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault a
value'
                                      (Maybe Text -> Maybe LayoutContext -> ConfigMapKey
ConfigMapKey (forall a. a -> Maybe a
Just Text
key) forall a. Maybe a
Nothing)
                                      Map ConfigMapKey a
cfgMapOverrides
        value''' :: a
value''' = forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault a
value''
                                       (Maybe Text -> Maybe LayoutContext -> ConfigMapKey
ConfigMapKey (forall a. a -> Maybe a
Just Text
key) (forall a. a -> Maybe a
Just LayoutContext
ctx))
                                       Map ConfigMapKey a
cfgMapOverrides
    in
        a
value'''

cfgOpWs :: LayoutContext -> Text -> OpConfig -> Whitespace
cfgOpWs :: LayoutContext -> Text -> OpConfig -> Whitespace
cfgOpWs LayoutContext
ctx Text
op = forall a. LayoutContext -> Text -> ConfigMap a -> a
cfgMapFind LayoutContext
ctx Text
op forall b c a. (b -> c) -> (a -> b) -> a -> c
. OpConfig -> ConfigMap Whitespace
unOpConfig

cfgGroupWs :: LayoutContext -> Text -> GroupConfig -> Whitespace
cfgGroupWs :: LayoutContext -> Text -> GroupConfig -> Whitespace
cfgGroupWs LayoutContext
ctx Text
op = forall a. LayoutContext -> Text -> ConfigMap a -> a
cfgMapFind LayoutContext
ctx Text
op forall b c a. (b -> c) -> (a -> b) -> a -> c
. GroupConfig -> ConfigMap Whitespace
unGroupConfig

inWs :: Location -> WsLoc -> Bool
inWs :: Location -> WsLoc -> Bool
inWs Location
_ WsLoc
WsBoth = Bool
True
inWs Location
Before WsLoc
WsBefore = Bool
True
inWs Location
After WsLoc
WsAfter = Bool
True
inWs Location
_ WsLoc
_ = Bool
False

wsSpace :: Location -> Whitespace -> Bool
wsSpace :: Location -> Whitespace -> Bool
wsSpace Location
loc Whitespace
ws = Location
loc Location -> WsLoc -> Bool
`inWs` Whitespace -> WsLoc
wsSpaces Whitespace
ws

wsLinebreak :: Location -> Whitespace -> Bool
wsLinebreak :: Location -> Whitespace -> Bool
wsLinebreak Location
loc Whitespace
ws = Location
loc Location -> WsLoc -> Bool
`inWs` Whitespace -> WsLoc
wsLinebreaks Whitespace
ws

------------------------------------------------------------------------
readMaybe :: Read a => String -> Maybe a
readMaybe :: forall a. Read a => String -> Maybe a
readMaybe String
str = case forall a. Read a => ReadS a
reads String
str of
    [ (a
x, String
"") ] -> forall a. a -> Maybe a
Just a
x
    [(a, String)]
_ -> forall a. Maybe a
Nothing

enumOptions :: Int -> Options
enumOptions :: Int -> Options
enumOptions Int
n =
    Options
JSON.defaultOptions { constructorTagModifier :: ShowS
constructorTagModifier = Char -> ShowS
JSON.camelTo2 Char
'-' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> [a] -> [a]
drop Int
n }

recordOptions :: Int -> Options
recordOptions :: Int -> Options
recordOptions Int
n =
    Options
JSON.defaultOptions { fieldLabelModifier :: ShowS
fieldLabelModifier = Char -> ShowS
JSON.camelTo2 Char
'-' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> [a] -> [a]
drop Int
n
                        , unwrapUnaryRecords :: Bool
unwrapUnaryRecords = Bool
True
                        }

instance ToJSON Indent where
    toJSON :: Indent -> Value
toJSON Indent
i = Text -> Value
JSON.String forall a b. (a -> b) -> a -> b
$ case Indent
i of
        Indent
Align -> Text
"align"
        IndentBy Int
x -> Text
"indent-by " Text -> Text -> Text
`T.append` String -> Text
T.pack (forall a. Show a => a -> String
show Int
x)
        AlignOrIndentBy Int
x -> Text
"align-or-indent-by " Text -> Text -> Text
`T.append` String -> Text
T.pack (forall a. Show a => a -> String
show Int
x)

instance FromJSON Indent where
    parseJSON :: Value -> Parser Indent
parseJSON v :: Value
v@(JSON.String Text
t) = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. String -> Value -> Parser a
JSON.typeMismatch String
"Indent" Value
v) forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
        if Text
t forall a. Eq a => a -> a -> Bool
== Text
"align"
        then forall a. a -> Maybe a
Just Indent
Align
        else if Text
"indent-by " Text -> Text -> Bool
`T.isPrefixOf` Text
t
             then Int -> Indent
IndentBy forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Read a => String -> Maybe a
readMaybe (Text -> String
T.unpack forall a b. (a -> b) -> a -> b
$ Int -> Text -> Text
T.drop Int
10 Text
t)
             else if Text
"align-or-indent-by " Text -> Text -> Bool
`T.isPrefixOf` Text
t
                  then Int -> Indent
AlignOrIndentBy forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Read a => String -> Maybe a
readMaybe (Text -> String
T.unpack forall a b. (a -> b) -> a -> b
$ Int -> Text -> Text
T.drop Int
19 Text
t)
                  else forall a. Maybe a
Nothing

    parseJSON Value
v = forall a. String -> Value -> Parser a
JSON.typeMismatch String
"Indent" Value
v

instance ToJSON LayoutContext where
    toJSON :: LayoutContext -> Value
toJSON = forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON (Int -> Options
enumOptions Int
0)

instance FromJSON LayoutContext where
    parseJSON :: Value -> Parser LayoutContext
parseJSON = forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON (Int -> Options
enumOptions Int
0)

instance ToJSON WsLoc where
    toJSON :: WsLoc -> Value
toJSON = forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON (Int -> Options
enumOptions Int
2)

instance FromJSON WsLoc where
    parseJSON :: Value -> Parser WsLoc
parseJSON = forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON (Int -> Options
enumOptions Int
2)

instance ToJSON Whitespace where
    toJSON :: Whitespace -> Value
toJSON = forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON (Int -> Options
recordOptions Int
2)

instance FromJSON Whitespace where
    parseJSON :: Value -> Parser Whitespace
parseJSON = forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON (Int -> Options
recordOptions Int
2)

instance ToJSON Layout where
    toJSON :: Layout -> Value
toJSON = forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON (Int -> Options
enumOptions Int
0)

instance FromJSON Layout where
    parseJSON :: Value -> Parser Layout
parseJSON = forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON (Int -> Options
enumOptions Int
0)

layoutToText :: LayoutContext -> T.Text
layoutToText :: LayoutContext -> Text
layoutToText LayoutContext
Declaration = Text
"declaration"
layoutToText LayoutContext
Type = Text
"type"
layoutToText LayoutContext
Pattern = Text
"pattern"
layoutToText LayoutContext
Expression = Text
"expression"
layoutToText LayoutContext
Other = Text
"other"

textToLayout :: T.Text -> Maybe LayoutContext
textToLayout :: Text -> Maybe LayoutContext
textToLayout Text
"declaration" = forall a. a -> Maybe a
Just LayoutContext
Declaration
textToLayout Text
"type" = forall a. a -> Maybe a
Just LayoutContext
Type
textToLayout Text
"pattern" = forall a. a -> Maybe a
Just LayoutContext
Pattern
textToLayout Text
"expression" = forall a. a -> Maybe a
Just LayoutContext
Expression
textToLayout Text
"other" = forall a. a -> Maybe a
Just LayoutContext
Other
textToLayout Text
_ = forall a. Maybe a
Nothing

keyToText :: ConfigMapKey -> T.Text
keyToText :: ConfigMapKey -> Text
keyToText (ConfigMapKey Maybe Text
Nothing Maybe LayoutContext
Nothing) = Text
"default"
keyToText (ConfigMapKey (Just Text
n) Maybe LayoutContext
Nothing) = Text
n
keyToText (ConfigMapKey Maybe Text
Nothing (Just LayoutContext
l)) = Text
"* in " Text -> Text -> Text
`T.append` LayoutContext -> Text
layoutToText LayoutContext
l
keyToText (ConfigMapKey (Just Text
n) (Just LayoutContext
l)) =
    Text
n Text -> Text -> Text
`T.append` Text
" in " Text -> Text -> Text
`T.append` LayoutContext -> Text
layoutToText LayoutContext
l

textToKey :: T.Text -> Maybe ConfigMapKey
textToKey :: Text -> Maybe ConfigMapKey
textToKey Text
t = case HasCallStack => Text -> Text -> [Text]
T.splitOn Text
" in " Text
t of
    [ Text
"default" ] -> forall a. a -> Maybe a
Just (Maybe Text -> Maybe LayoutContext -> ConfigMapKey
ConfigMapKey forall a. Maybe a
Nothing forall a. Maybe a
Nothing)
    [ Text
"*", Text
"*" ] -> forall a. a -> Maybe a
Just (Maybe Text -> Maybe LayoutContext -> ConfigMapKey
ConfigMapKey forall a. Maybe a
Nothing forall a. Maybe a
Nothing)
    [ Text
name ] -> forall a. a -> Maybe a
Just (Maybe Text -> Maybe LayoutContext -> ConfigMapKey
ConfigMapKey (forall a. a -> Maybe a
Just Text
name) forall a. Maybe a
Nothing)
    [ Text
name, Text
"*" ] -> forall a. a -> Maybe a
Just (Maybe Text -> Maybe LayoutContext -> ConfigMapKey
ConfigMapKey (forall a. a -> Maybe a
Just Text
name) forall a. Maybe a
Nothing)
    [ Text
"*", Text
layout ] -> Maybe Text -> Maybe LayoutContext -> ConfigMapKey
ConfigMapKey forall a. Maybe a
Nothing forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe LayoutContext
textToLayout Text
layout
    [ Text
name, Text
layout ] -> Maybe Text -> Maybe LayoutContext -> ConfigMapKey
ConfigMapKey (forall a. a -> Maybe a
Just Text
name) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe LayoutContext
textToLayout Text
layout
    [Text]
_ -> forall a. Maybe a
Nothing

instance ToJSON a => ToJSON (ConfigMap a) where
    toJSON :: ConfigMap a -> Value
toJSON ConfigMap{a
Map ConfigMapKey a
cfgMapOverrides :: Map ConfigMapKey a
cfgMapDefault :: a
cfgMapOverrides :: forall a. ConfigMap a -> Map ConfigMapKey a
cfgMapDefault :: forall a. ConfigMap a -> a
..} = forall a. ToJSON a => a -> Value
toJSON forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert Text
"default" a
cfgMapDefault forall a b. (a -> b) -> a -> b
$
        forall k2 k1 a. Ord k2 => (k1 -> k2) -> Map k1 a -> Map k2 a
Map.mapKeys ConfigMapKey -> Text
keyToText Map ConfigMapKey a
cfgMapOverrides

instance FromJSON a => FromJSON (ConfigMap a) where
    parseJSON :: Value -> Parser (ConfigMap a)
parseJSON Value
value = do
        HashMap Text a
o <- forall a. FromJSON a => Value -> Parser a
parseJSON Value
value
        a
cfgMapDefault <- forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Missing key: default") forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
            forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup Text
"default" HashMap Text a
o
        Map ConfigMapKey a
cfgMapOverrides <- forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall (m :: * -> *) a. MonadFail m => String -> m a
fail (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList) forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall {b}. (Text, b) -> Either String (ConfigMapKey, b)
toKey forall a b. (a -> b) -> a -> b
$
            forall k v. HashMap k v -> [(k, v)]
HashMap.toList forall a b. (a -> b) -> a -> b
$ forall k v. (Eq k, Hashable k) => k -> HashMap k v -> HashMap k v
HashMap.delete Text
"default" HashMap Text a
o
        forall (m :: * -> *) a. Monad m => a -> m a
return ConfigMap { a
Map ConfigMapKey a
cfgMapOverrides :: Map ConfigMapKey a
cfgMapDefault :: a
cfgMapOverrides :: Map ConfigMapKey a
cfgMapDefault :: a
.. }
      where
        toKey :: (Text, b) -> Either String (ConfigMapKey, b)
toKey (Text
k, b
v) = case Text -> Maybe ConfigMapKey
textToKey Text
k of
            Just ConfigMapKey
k' -> forall a b. b -> Either a b
Right (ConfigMapKey
k', b
v)
            Maybe ConfigMapKey
Nothing -> forall a b. a -> Either a b
Left (String
"Invalid key: " forall a. [a] -> [a] -> [a]
++ Text -> String
T.unpack Text
k)

instance ToJSON PenaltyConfig where
    toJSON :: PenaltyConfig -> Value
toJSON = forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON (Int -> Options
recordOptions Int
7)

instance FromJSON PenaltyConfig where
    parseJSON :: Value -> Parser PenaltyConfig
parseJSON = forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON (Int -> Options
recordOptions Int
7)

instance ToJSON AlignConfig where
    toJSON :: AlignConfig -> Value
toJSON = forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON (Int -> Options
recordOptions Int
8)

instance FromJSON AlignConfig where
    parseJSON :: Value -> Parser AlignConfig
parseJSON = forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON (Int -> Options
recordOptions Int
8)

instance ToJSON IndentConfig where
    toJSON :: IndentConfig -> Value
toJSON = forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON (Int -> Options
recordOptions Int
9)

instance FromJSON IndentConfig where
    parseJSON :: Value -> Parser IndentConfig
parseJSON = forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON (Int -> Options
recordOptions Int
9)

instance ToJSON LayoutConfig where
    toJSON :: LayoutConfig -> Value
toJSON = forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON (Int -> Options
recordOptions Int
9)

instance FromJSON LayoutConfig where
    parseJSON :: Value -> Parser LayoutConfig
parseJSON = forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON (Int -> Options
recordOptions Int
9)

instance ToJSON OpConfig where
    toJSON :: OpConfig -> Value
toJSON = forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON (Int -> Options
recordOptions Int
0)

instance FromJSON OpConfig where
    parseJSON :: Value -> Parser OpConfig
parseJSON = forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON (Int -> Options
recordOptions Int
0)

instance ToJSON GroupConfig where
    toJSON :: GroupConfig -> Value
toJSON = forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON (Int -> Options
recordOptions Int
0)

instance FromJSON GroupConfig where
    parseJSON :: Value -> Parser GroupConfig
parseJSON = forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON (Int -> Options
recordOptions Int
0)

instance ToJSON ImportsGroupOrder where
    toJSON :: ImportsGroupOrder -> Value
toJSON = forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON (Int -> Options
enumOptions Int
12)

instance FromJSON ImportsGroupOrder where
    parseJSON :: Value -> Parser ImportsGroupOrder
parseJSON = forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON (Int -> Options
enumOptions Int
12)

instance ToJSON ImportsGroup where
    toJSON :: ImportsGroup -> Value
toJSON = forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON (Int -> Options
recordOptions Int
7)

instance FromJSON ImportsGroup where
    parseJSON :: Value -> Parser ImportsGroup
parseJSON x :: Value
x@JSON.Array{} =
        [String] -> ImportsGroupOrder -> ImportsGroup
ImportsGroup forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromJSON a => Value -> Parser a
parseJSON Value
x forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a. Applicative f => a -> f a
pure ImportsGroupOrder
ImportsGroupKeep
    parseJSON Value
x = forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON (Int -> Options
recordOptions Int
7) Value
x

instance ToJSON SortImportsRule where
    toJSON :: SortImportsRule -> Value
toJSON SortImportsRule
NoImportSort = forall a. ToJSON a => a -> Value
toJSON Bool
False
    toJSON SortImportsRule
SortImportsByPrefix = forall a. ToJSON a => a -> Value
toJSON Bool
True
    toJSON (SortImportsByGroups [ImportsGroup]
xs) = forall a. ToJSON a => a -> Value
toJSON [ImportsGroup]
xs

instance FromJSON SortImportsRule where
    parseJSON :: Value -> Parser SortImportsRule
parseJSON (JSON.Bool Bool
False) = forall (m :: * -> *) a. Monad m => a -> m a
return SortImportsRule
NoImportSort
    parseJSON (JSON.Bool Bool
True) = forall (m :: * -> *) a. Monad m => a -> m a
return SortImportsRule
SortImportsByPrefix
    parseJSON Value
v = [ImportsGroup] -> SortImportsRule
SortImportsByGroups forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromJSON a => Value -> Parser a
parseJSON Value
v

instance ToJSON DeclarationConstruct where
    toJSON :: DeclarationConstruct -> Value
toJSON = forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON (Int -> Options
enumOptions Int
4)

instance FromJSON DeclarationConstruct where
    parseJSON :: Value -> Parser DeclarationConstruct
parseJSON = forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON (Int -> Options
enumOptions Int
4)

instance ToJSON OptionConfig where
    toJSON :: OptionConfig -> Value
toJSON = forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON (Int -> Options
recordOptions Int
9)

instance FromJSON OptionConfig where
    parseJSON :: Value -> Parser OptionConfig
parseJSON = forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON (Int -> Options
recordOptions Int
9)

instance ToJSON Config where
    toJSON :: Config -> Value
toJSON = forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON (Int -> Options
recordOptions Int
3)

instance FromJSON Config where
    parseJSON :: Value -> Parser Config
parseJSON = forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON (Int -> Options
recordOptions Int
3)