66module Language.Haskell.Stylish.Config
77 ( Extensions
88 , Config (.. )
9+ , ConfigSearchStrategy (.. )
910 , ExitCodeBehavior (.. )
1011 , defaultConfigBytes
1112 , configFilePath
@@ -95,14 +96,17 @@ 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+ 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
102106 configPath <- getXdgDirectory XdgConfig " stylish-haskell"
103- home <- getHomeDirectory
107+ home <- getHomeDirectory
104108 search verbose $
105- [d </> configFileName | d <- ancestors current ] ++
109+ [d </> configFileName | d <- ancestors startDir ] ++
106110 [configPath </> " config.yaml" , home </> configFileName]
107111
108112search :: Verbose -> [FilePath ] -> IO (Maybe FilePath )
@@ -114,16 +118,16 @@ search verbose (f : fs) = do
114118 if exists then return (Just f) else search verbose fs
115119
116120--------------------------------------------------------------------------------
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
120124 verbose $ " Loading configuration at " ++ fromMaybe " <embedded>" mbFp
121125 bytes <- maybe (return defaultConfigBytes) B. readFile mbFp
122126 case decode1Strict bytes of
123127 Left (pos, err) -> error $ prettyPosWithSource pos (fromStrict bytes) (" Language.Haskell.Stylish.Config.loadConfig: " ++ err)
124128 Right config -> do
125129 cabalLanguageExtensions <- if configCabal config
126- then map toStr <$> Cabal. findLanguageExtensions verbose
130+ then map toStr <$> Cabal. findLanguageExtensions verbose configSearchStrategy
127131 else pure []
128132
129133 return $ config
0 commit comments