module HLint(hlint, Suggestion, suggestionLocation, suggestionSeverity, Severity(..)) where
import Control.Monad
import Data.List
import Data.Maybe
import CmdLine
import Settings
import Report
import Idea
import Apply
import Test
import Util
import Parallel
import HSE.All
newtype Suggestion = Suggestion {fromSuggestion :: Idea}
deriving (Eq,Ord)
instance Show Suggestion where
show = show . fromSuggestion
suggestionLocation :: Suggestion -> SrcLoc
suggestionLocation = loc . fromSuggestion
suggestionSeverity :: Suggestion -> Severity
suggestionSeverity = severity . fromSuggestion
hlint :: [String] -> IO [Suggestion]
hlint args = do
cmd@Cmd{..} <- getCmd args
let flags = parseFlags{cppFlags=cmdCpp, encoding=cmdEncoding, language=cmdLanguage}
if cmdTest then
test (\x -> hlint x >> return ()) cmdDataDir cmdGivenHints >> return []
else if isNothing cmdFiles && notNull cmdFindHints then
mapM_ (\x -> putStrLn . fst =<< findSettings flags x) cmdFindHints >> return []
else if isNothing cmdFiles then
exitWithHelp
else if cmdFiles == Just [] then
error "No files found"
else
runHints cmd flags
runHints :: Cmd -> ParseFlags -> IO [Suggestion]
runHints Cmd{..} flags = do
let outStrLn x = unless cmdQuiet $ putStrLn x
settings1 <- readSettings cmdDataDir cmdHintFiles
settings2 <- concatMapM (fmap snd . findSettings flags) cmdFindHints
settings3 <- return [Classify Ignore x ("","") | x <- cmdIgnore]
let settings = settings1 ++ settings2 ++ settings3
ideas <- fmap concat $ parallel [listM' =<< applyHint flags settings x | x <- fromMaybe [] cmdFiles]
let (showideas,hideideas) = partition (\i -> cmdShowAll || severity i /= Ignore) ideas
showItem <- if cmdColor then showANSI else return show
mapM_ (outStrLn . showItem) showideas
if null showideas then
when (cmdReports /= []) $ outStrLn "Skipping writing reports"
else
forM_ cmdReports $ \x -> do
outStrLn $ "Writing report to " ++ x ++ " ..."
writeReport cmdDataDir x showideas
outStrLn $
(let i = length showideas in if i == 0 then "No suggestions" else show i ++ " suggestion" ++ ['s'|i/=1]) ++
(let i = length hideideas in if i == 0 then "" else " (" ++ show i ++ " ignored)")
return $ map Suggestion showideas