66module Language.Haskell.Stylish.Config
77 ( Extensions
88 , Config (.. )
9+ , ConfigSearchStrategy (.. )
910 , ExitCodeBehavior (.. )
1011 , defaultConfigBytes
1112 , configFilePath
@@ -95,14 +96,27 @@ defaultConfigBytes = $(FileEmbed.embedFile "data/stylish-haskell.yaml")
9596
9697
9798--------------------------------------------------------------------------------
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
102116 configPath <- getXdgDirectory XdgConfig " stylish-haskell"
103- home <- getHomeDirectory
117+ home <- getHomeDirectory
104118 search verbose $
105- [d </> configFileName | d <- ancestors current ] ++
119+ [d </> configFileName | d <- ancestors startDir ] ++
106120 [configPath </> " config.yaml" , home </> configFileName]
107121
108122search :: Verbose -> [FilePath ] -> IO (Maybe FilePath )
@@ -114,9 +128,9 @@ search verbose (f : fs) = do
114128 if exists then return (Just f) else search verbose fs
115129
116130--------------------------------------------------------------------------------
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
120134 verbose $ " Loading configuration at " ++ fromMaybe " <embedded>" mbFp
121135 bytes <- maybe (return defaultConfigBytes) B. readFile mbFp
122136 case decode1Strict bytes of
0 commit comments