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,27 @@ 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
+ data ConfigSearchStrategy
100
+ = -- | Don't try to search, just use given config file
101
+ UseConfig FilePath
102
+ | -- | Search for @.stylish-haskell.yaml@ starting from given directory.
103
+ -- If not found, try all ancestor directories, @$XDG_CONFIG\/stylish-haskell\/config.yaml@ and @$HOME\/.stylish-haskell.yaml@ in order.
104
+ -- If no config is found, default built-in config will be used.
105
+ SearchFromDirectory FilePath
106
+ | -- | Like SearchFromDirectory, but using current working directory as a starting point
107
+ SearchFromCurrentDirectory
108
+
109
+ configFilePath :: Verbose -> ConfigSearchStrategy -> IO (Maybe FilePath )
110
+ configFilePath _ (UseConfig userSpecified) = return (Just userSpecified)
111
+ configFilePath verbose (SearchFromDirectory dir) = searchFrom verbose dir
112
+ configFilePath verbose SearchFromCurrentDirectory = searchFrom verbose =<< getCurrentDirectory
113
+
114
+ searchFrom :: Verbose -> FilePath -> IO (Maybe FilePath )
115
+ searchFrom verbose startDir = do
102
116
configPath <- getXdgDirectory XdgConfig " stylish-haskell"
103
- home <- getHomeDirectory
117
+ home <- getHomeDirectory
104
118
search verbose $
105
- [d </> configFileName | d <- ancestors current ] ++
119
+ [d </> configFileName | d <- ancestors startDir ] ++
106
120
[configPath </> " config.yaml" , home </> configFileName]
107
121
108
122
search :: Verbose -> [FilePath ] -> IO (Maybe FilePath )
@@ -114,9 +128,9 @@ search verbose (f : fs) = do
114
128
if exists then return (Just f) else search verbose fs
115
129
116
130
--------------------------------------------------------------------------------
117
- loadConfig :: Verbose -> Maybe FilePath -> IO Config
118
- loadConfig verbose userSpecified = do
119
- mbFp <- configFilePath verbose userSpecified
131
+ loadConfig :: Verbose -> ConfigSearchStrategy -> IO Config
132
+ loadConfig verbose configSearchStrategy = do
133
+ mbFp <- configFilePath verbose configSearchStrategy
120
134
verbose $ " Loading configuration at " ++ fromMaybe " <embedded>" mbFp
121
135
bytes <- maybe (return defaultConfigBytes) B. readFile mbFp
122
136
case decode1Strict bytes of
0 commit comments