Skip to content

Commit 39a8c54

Browse files
committed
Respect ConfigSearchStrategy in findCabalFiles
1 parent 73e6bc0 commit 39a8c54

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

lib/Language/Haskell/Stylish/Config.hs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,6 @@ defaultConfigBytes = $(FileEmbed.embedFile "data/stylish-haskell.yaml")
9696

9797

9898
--------------------------------------------------------------------------------
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-
10999
configFilePath :: Verbose -> ConfigSearchStrategy -> IO (Maybe FilePath)
110100
configFilePath _ (UseConfig userSpecified) = return (Just userSpecified)
111101
configFilePath verbose (SearchFromDirectory dir) = searchFrom verbose dir
@@ -137,7 +127,7 @@ loadConfig verbose configSearchStrategy = do
137127
Left (pos, err) -> error $ prettyPosWithSource pos (fromStrict bytes) ("Language.Haskell.Stylish.Config.loadConfig: " ++ err)
138128
Right config -> do
139129
cabalLanguageExtensions <- if configCabal config
140-
then map toStr <$> Cabal.findLanguageExtensions verbose
130+
then map toStr <$> Cabal.findLanguageExtensions verbose configSearchStrategy
141131
else pure []
142132

143133
return $ config

lib/Language/Haskell/Stylish/Config/Cabal.hs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,32 @@ import qualified Distribution.Parsec as Cabal
1616
import qualified Distribution.Simple.Utils as Cabal
1717
import qualified Distribution.Utils.Path as Cabal
1818
import qualified Distribution.Verbosity as Cabal
19+
import GHC.Data.Maybe (mapMaybe)
1920
import qualified Language.Haskell.Extension as Language
21+
import Language.Haskell.Stylish.Config.Internal
2022
import Language.Haskell.Stylish.Verbose
2123
import System.Directory (doesFileExist,
2224
getCurrentDirectory)
2325

2426

2527
--------------------------------------------------------------------------------
26-
import GHC.Data.Maybe (mapMaybe)
27-
import Language.Haskell.Stylish.Config.Internal
28-
29-
30-
--------------------------------------------------------------------------------
31-
findLanguageExtensions :: Verbose -> IO [(Language.KnownExtension, Bool)]
32-
findLanguageExtensions verbose =
33-
findCabalFile verbose >>=
28+
findLanguageExtensions
29+
:: Verbose -> ConfigSearchStrategy -> IO [(Language.KnownExtension, Bool)]
30+
findLanguageExtensions verbose configSearchStrategy =
31+
findCabalFile verbose configSearchStrategy >>=
3432
maybe (pure []) (readDefaultLanguageExtensions verbose)
3533

3634

3735
--------------------------------------------------------------------------------
3836
-- | Find the closest .cabal file, possibly going up the directory structure.
39-
-- TODO: use ConfigSearchStrategy here, too
40-
findCabalFile :: Verbose -> IO (Maybe FilePath)
41-
findCabalFile verbose = do
42-
cwd <- getCurrentDirectory
43-
go [] $ ancestors cwd
37+
findCabalFile :: Verbose -> ConfigSearchStrategy -> IO (Maybe FilePath)
38+
findCabalFile verbose configSearchStrategy = case configSearchStrategy of
39+
-- If the invocation pointed us to a specific config file, it doesn't make
40+
-- much sense to search for cabal files manually (the config file could be
41+
-- somewhere like /etc, not necessarily a Haskell project).
42+
UseConfig _ -> pure Nothing
43+
SearchFromDirectory path -> go [] $ ancestors path
44+
SearchFromCurrentDirectory -> getCurrentDirectory >>= go [] . ancestors
4445
where
4546
go :: [FilePath] -> [FilePath] -> IO (Maybe FilePath)
4647
go searched [] = do

lib/Language/Haskell/Stylish/Config/Internal.hs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
--------------------------------------------------------------------------------
22
module Language.Haskell.Stylish.Config.Internal
3-
( ancestors
3+
( ConfigSearchStrategy (..)
4+
, ancestors
45
) where
56

67

@@ -13,3 +14,15 @@ import System.FilePath (joinPath, splitPath)
1314
-- All ancestors of a dir (including that dir)
1415
ancestors :: FilePath -> [FilePath]
1516
ancestors = map joinPath . reverse . dropWhile null . inits . splitPath
17+
18+
19+
--------------------------------------------------------------------------------
20+
data ConfigSearchStrategy
21+
= -- | Don't try to search, just use given config file
22+
UseConfig FilePath
23+
| -- | Search for @.stylish-haskell.yaml@ starting from given directory.
24+
-- If not found, try all ancestor directories, @$XDG_CONFIG\/stylish-haskell\/config.yaml@ and @$HOME\/.stylish-haskell.yaml@ in order.
25+
-- If no config is found, default built-in config will be used.
26+
SearchFromDirectory FilePath
27+
| -- | Like SearchFromDirectory, but using current working directory as a starting point
28+
SearchFromCurrentDirectory

0 commit comments

Comments
 (0)