module Language.Haskell.Format.Stylish
  ( autoSettings
  , formatter
  ) where

import Language.Haskell.Stylish         as Stylish

import Language.Haskell.Format.Internal
import Language.Haskell.Format.Types

newtype Settings =
  Settings Stylish.Config

autoSettings :: IO Settings
autoSettings :: IO Settings
autoSettings = do
  Maybe FilePath
path <- Verbose -> Maybe FilePath -> IO (Maybe FilePath)
Stylish.configFilePath Verbose
verbose Maybe FilePath
forall a. Maybe a
Nothing
  Config
config <- Verbose -> Maybe FilePath -> IO Config
Stylish.loadConfig Verbose
verbose Maybe FilePath
path
  Settings -> IO Settings
forall (m :: * -> *) a. Monad m => a -> m a
return (Config -> Settings
Settings Config
config)
  where
    verbose :: Verbose
verbose = Bool -> Verbose
Stylish.makeVerbose Bool
False

formatter :: Settings -> Formatter
formatter :: Settings -> Formatter
formatter = (HaskellSource -> Either FilePath HaskellSource) -> Formatter
mkFormatter ((HaskellSource -> Either FilePath HaskellSource) -> Formatter)
-> (Settings -> HaskellSource -> Either FilePath HaskellSource)
-> Settings
-> Formatter
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Settings -> HaskellSource -> Either FilePath HaskellSource
stylish

stylish :: Settings -> HaskellSource -> Either String HaskellSource
stylish :: Settings -> HaskellSource -> Either FilePath HaskellSource
stylish (Settings Config
config) (HaskellSource FilePath
filepath FilePath
source) =
  FilePath -> FilePath -> HaskellSource
HaskellSource FilePath
filepath (FilePath -> HaskellSource)
-> ([FilePath] -> FilePath) -> [FilePath] -> HaskellSource
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [FilePath] -> FilePath
unlines ([FilePath] -> HaskellSource)
-> Either FilePath [FilePath] -> Either FilePath HaskellSource
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
  [FilePath]
-> Maybe FilePath
-> [Step]
-> [FilePath]
-> Either FilePath [FilePath]
Stylish.runSteps [FilePath]
extensions (FilePath -> Maybe FilePath
forall a. a -> Maybe a
Just FilePath
filepath) [Step]
steps [FilePath]
sourceLines
  where
    sourceLines :: [FilePath]
sourceLines = FilePath -> [FilePath]
lines FilePath
source
    extensions :: [FilePath]
extensions = Config -> [FilePath]
Stylish.configLanguageExtensions Config
config
    steps :: [Step]
steps = Config -> [Step]
Stylish.configSteps Config
config