<commit_before>module CabalDev (modifyOptions) where

{-
  If the directory 'cabal-dev/packages-X.X.X.conf' exists, add it to the
  options ghc-mod uses to check the source.  Otherwise just pass it on.
-}

import Control.Applicative    ((<$>))
import Data.List              (find)
import System.FilePath        (splitPath,joinPath,(</>))
import System.Directory
import Text.Regex.Posix       ((=~))

import Types

modifyOptions :: Options -> IO Options
modifyOptions opts =
    fmap (has_cdev opts) findCabalDev
  where
    has_cdev :: Options -> Maybe String -> Options
    has_cdev op Nothing = op
    has_cdev op (Just path) = addPath op path

findCabalDev :: IO (Maybe String)
findCabalDev =
    getCurrentDirectory >>= searchIt . splitPath

addPath :: Options -> String -> Options
addPath orig_opts path = do
    let orig_ghcopt = ghcOpts orig_opts
    orig_opts { ghcOpts = orig_ghcopt ++ ["-package-conf", path] }

searchIt :: [FilePath] -> IO (Maybe FilePath)
searchIt [] = return Nothing
searchIt path = do
    a <- doesDirectoryExist (mpath path)
    if a then
        findConf (mpath path)
      else
        searchIt $ init path
  where
    mpath a = joinPath a </> "cabal-dev/"

findConf :: FilePath -> IO (Maybe FilePath)
findConf path = do
    f <- find (=~ "packages.*\\.conf") <$> getDirectoryContents path
    return ((path </>) <$> f)<commit_msg>Removing the anti-pattern: IO (Maybe a).<commit_after>module CabalDev (modifyOptions) where

{-
  If the directory 'cabal-dev/packages-X.X.X.conf' exists, add it to the
  options ghc-mod uses to check the source.  Otherwise just pass it on.
-}

import Control.Applicative    ((<$>))
import Control.Exception      (SomeException, throwIO)
import Data.List              (find)
import GHC                    (gcatch)
import System.Directory
import System.FilePath        (splitPath,joinPath,(</>))
import Text.Regex.Posix       ((=~))
import Types

modifyOptions :: Options -> IO Options
modifyOptions opts = found `gcatch` notFound
  where
    found = addPath opts <$> findCabalDev
    notFound :: SomeException -> IO Options
    notFound _ = return opts

findCabalDev :: IO String
findCabalDev = getCurrentDirectory >>= searchIt . splitPath

addPath :: Options -> String -> Options
addPath orig_opts path = do
    let orig_ghcopt = ghcOpts orig_opts
    orig_opts { ghcOpts = orig_ghcopt ++ ["-package-conf", path] }

searchIt :: [FilePath] -> IO FilePath
searchIt [] = throwIO $ userError "Not found"
searchIt path = do
    a <- doesDirectoryExist (mpath path)
    if a then
        findConf (mpath path)
      else
        searchIt $ init path
  where
    mpath a = joinPath a </> "cabal-dev/"

findConf :: FilePath -> IO FilePath
findConf path = do
    Just f <- find (=~ "packages.*\\.conf") <$> getDirectoryContents path
    return $ path </> f
{'fraction_non_alphanumeric': 0.1119800924280128, 'fraction_numerical': 0.0, 'mean_word_length': 3.066473988439306, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '853489', 'n_tokens_mistral': 892, 'n_tokens_neox': 887, 'n_words': 281}