ghc-mod/Language/Haskell/GhcMod/Lint.hs

39 lines
1.2 KiB
Haskell
Raw Normal View History

2013-05-17 01:00:01 +00:00
module Language.Haskell.GhcMod.Lint where
2010-05-06 06:29:55 +00:00
2014-03-27 06:56:14 +00:00
import Control.Applicative ((<$>))
2014-03-21 12:40:02 +00:00
import Control.Exception (finally)
2014-03-27 06:56:14 +00:00
import Data.List (intercalate)
2014-03-21 12:40:02 +00:00
import GHC.IO.Handle (hDuplicate, hDuplicateTo)
2013-05-17 01:00:01 +00:00
import Language.Haskell.GhcMod.Types
2014-03-27 06:56:14 +00:00
import Language.Haskell.HLint (hlint)
2014-03-21 12:40:02 +00:00
import System.Directory (getTemporaryDirectory, removeFile)
import System.IO (hClose, openTempFile, stdout)
2010-05-06 06:29:55 +00:00
2013-05-20 05:28:56 +00:00
-- | Checking syntax of a target file using hlint.
-- Warnings and errors are returned.
lintSyntax :: Options
-> FilePath -- ^ A target file.
-> IO String
2014-03-27 01:34:30 +00:00
lintSyntax opt file = pack <$> lint hopts file
2010-05-06 06:29:55 +00:00
where
2013-09-03 05:40:51 +00:00
LineSeparator lsep = lineSeparator opt
pack = unlines . map (intercalate lsep . lines)
2014-03-27 01:34:30 +00:00
hopts = hlintOpts opt
2010-05-06 06:29:55 +00:00
2014-03-27 01:34:30 +00:00
-- | Checking syntax of a target file using hlint.
-- Warnings and errors are returned.
lint :: [String]
2013-05-20 05:28:56 +00:00
-> FilePath -- ^ A target file.
-> IO [String]
2014-03-27 01:34:30 +00:00
lint hopts file = map show <$> suppressStdout (hlint (file : hopts))
2014-03-21 12:40:02 +00:00
suppressStdout :: IO a -> IO a
suppressStdout f = do
tmpdir <- getTemporaryDirectory
(path, handle) <- openTempFile tmpdir "ghc-mod-hlint"
removeFile path
dup <- hDuplicate stdout
hDuplicateTo handle stdout
hClose handle
f `finally` hDuplicateTo dup stdout