{-# LANGUAGE PatternGuards, RecordWildCards #-}
module Language.Haskell.HLint3(
hlint, applyHints,
Idea(..), Severity(..), Note(..),
Classify(..),
getHLintDataDir, autoSettings, argsSettings,
findSettings, readSettingsFile,
HintBuiltin(..), HintRule(..),
Hint(..), resolveHints,
Scope, scopeCreate, scopeMatch, scopeMove,
parseModuleEx, defaultParseFlags, parseFlagsAddFixities, ParseError(..), ParseFlags(..), CppFlags(..)
) where
import Config.Type
import Config.Read
import Idea
import Apply
import HLint
import HSE.All
import Hint.All
import CmdLine
import Paths_hlint
import Data.List.Extra
import Data.Maybe
import System.FilePath
import Data.Functor
import Prelude
getHLintDataDir :: IO FilePath
getHLintDataDir = getDataDir
autoSettings :: IO (ParseFlags, [Classify], Hint)
autoSettings = do
(fixities, classify, hints) <- findSettings (readSettingsFile Nothing) Nothing
return (parseFlagsAddFixities fixities defaultParseFlags, classify, resolveHints hints)
argsSettings :: [String] -> IO (ParseFlags, [Classify], Hint)
argsSettings args = do
cmd <- getCmd args
case cmd of
CmdMain{..} -> do
(_,settings) <- readAllSettings args cmd
let (fixities, classify, hints) = splitSettings settings
let flags = parseFlagsSetLanguage (cmdExtensions cmd) $ parseFlagsAddFixities fixities $
defaultParseFlags{cppFlags = cmdCpp cmd}
let ignore = [Classify Ignore x "" "" | x <- cmdIgnore]
return (flags, classify ++ ignore, resolveHints hints)
_ -> error "Can only invoke autoSettingsArgs with the root process"
readSettingsFile :: Maybe FilePath -> String -> IO (FilePath, Maybe String)
readSettingsFile dir x
| takeExtension x `elem` [".yml",".yaml"] = do
dir <- maybe getHLintDataDir return dir
return (dir </> x, Nothing)
| Just x <- "HLint." `stripPrefix` x = do
dir <- maybe getHLintDataDir return dir
return (dir </> x <.> "hs", Nothing)
| otherwise = return (x <.> "hs", Nothing)
findSettings :: (String -> IO (FilePath, Maybe String)) -> Maybe String -> IO ([Fixity], [Classify], [Either HintBuiltin HintRule])
findSettings load start = do
(file,contents) <- load $ fromMaybe "hlint.yaml" start
splitSettings <$> readFilesConfig [(file,contents)]
splitSettings :: [Setting] -> ([Fixity], [Classify], [Either HintBuiltin HintRule])
splitSettings xs =
([x | Infix x <- xs]
,[x | SettingClassify x <- xs]
,[Right x | SettingMatchExp x <- xs] ++ map Left [minBound..maxBound])
_docs :: IO ()
_docs = do
(flags, classify, hint) <- autoSettings
Right m <- parseModuleEx flags "MyFile.hs" Nothing
print $ applyHints classify hint [m]