6
6
module Language.Haskell.Stylish.Config
7
7
( Extensions
8
8
, Config (.. )
9
+ , ConfigSearchStrategy (.. )
9
10
, ExitCodeBehavior (.. )
10
11
, defaultConfigBytes
11
12
, configFilePath
@@ -95,14 +96,17 @@ defaultConfigBytes = $(FileEmbed.embedFile "data/stylish-haskell.yaml")
95
96
96
97
97
98
--------------------------------------------------------------------------------
98
- configFilePath :: Verbose -> Maybe FilePath -> IO (Maybe FilePath )
99
- configFilePath _ (Just userSpecified) = return (Just userSpecified)
100
- configFilePath verbose Nothing = do
101
- current <- getCurrentDirectory
99
+ configFilePath :: Verbose -> ConfigSearchStrategy -> IO (Maybe FilePath )
100
+ configFilePath _ (UseConfig userSpecified) = return (Just userSpecified)
101
+ configFilePath verbose (SearchFromDirectory dir) = searchFrom verbose dir
102
+ configFilePath verbose SearchFromCurrentDirectory = searchFrom verbose =<< getCurrentDirectory
103
+
104
+ searchFrom :: Verbose -> FilePath -> IO (Maybe FilePath )
105
+ searchFrom verbose startDir = do
102
106
configPath <- getXdgDirectory XdgConfig " stylish-haskell"
103
- home <- getHomeDirectory
107
+ home <- getHomeDirectory
104
108
search verbose $
105
- [d </> configFileName | d <- ancestors current ] ++
109
+ [d </> configFileName | d <- ancestors startDir ] ++
106
110
[configPath </> " config.yaml" , home </> configFileName]
107
111
108
112
search :: Verbose -> [FilePath ] -> IO (Maybe FilePath )
@@ -114,16 +118,16 @@ search verbose (f : fs) = do
114
118
if exists then return (Just f) else search verbose fs
115
119
116
120
--------------------------------------------------------------------------------
117
- loadConfig :: Verbose -> Maybe FilePath -> IO Config
118
- loadConfig verbose userSpecified = do
119
- mbFp <- configFilePath verbose userSpecified
121
+ loadConfig :: Verbose -> ConfigSearchStrategy -> IO Config
122
+ loadConfig verbose configSearchStrategy = do
123
+ mbFp <- configFilePath verbose configSearchStrategy
120
124
verbose $ " Loading configuration at " ++ fromMaybe " <embedded>" mbFp
121
125
bytes <- maybe (return defaultConfigBytes) B. readFile mbFp
122
126
case decode1Strict bytes of
123
127
Left (pos, err) -> error $ prettyPosWithSource pos (fromStrict bytes) (" Language.Haskell.Stylish.Config.loadConfig: " ++ err)
124
128
Right config -> do
125
129
cabalLanguageExtensions <- if configCabal config
126
- then map toStr <$> Cabal. findLanguageExtensions verbose
130
+ then map toStr <$> Cabal. findLanguageExtensions verbose configSearchStrategy
127
131
else pure []
128
132
129
133
return $ config
0 commit comments