Skip to content

Commit

Permalink
Thread FileState through calls
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspervdj committed Oct 30, 2011
1 parent fb57a84 commit b55471d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
54 changes: 35 additions & 19 deletions src/HStyle.hs
@@ -1,11 +1,11 @@
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
module HStyle module HStyle
( checkStyle ( FileState (..)
, checkStyle
, fixStyle , fixStyle
) where ) where


import Control.Applicative ((<$>)) import Control.Monad (foldM, forM_, unless)
import Control.Monad (forM, forM_, unless)
import Data.Char (isSpace) import Data.Char (isSpace)
import Data.List (isPrefixOf) import Data.List (isPrefixOf)
import Data.Maybe (fromMaybe) import Data.Maybe (fromMaybe)
Expand All @@ -23,26 +23,41 @@ import HStyle.Fixer
-- | A selector and a check... -- | A selector and a check...
type Rule = (Selector, Checker, Fixer) type Rule = (Selector, Checker, Fixer)


runRule :: Bool -> FilePath -> Block -> H.Module H.SrcSpanInfo -> Rule data FileState = FileState
-> IO Bool { fileBlock :: Block
runRule quiet file block md (selector, checker, fixer) = fmap and $ , fileUpdated :: Bool
forM (selector md block) $ \block' -> do , fileOk :: Bool
let problems = checker block' } deriving (Show)
let fixed = fixer block'
runRule :: Bool -> FilePath -> H.Module H.SrcSpanInfo
-> FileState -> Rule -> IO FileState
runRule quiet file md fileState (selector, checker, fixer) =
foldM checkBlock fileState (selector md $ fileBlock fileState)
where
checkBlock :: FileState -> Block -> IO FileState
checkBlock fs block = do
let problems = checker block
let (fixed, block') = case fixer block of
Nothing -> (False, block)
Just b -> (True, b)
forM_ problems $ \(i, problem) -> do forM_ problems $ \(i, problem) -> do
let line = absoluteLineNumber i block' let line = absoluteLineNumber i (fileBlock fs)
T.putStrLn $ T.pack file `T.append` ":" `T.append` T.putStrLn $ T.pack file `T.append` ":" `T.append`
T.pack (show line) `T.append` ": " `T.append` problem T.pack (show line) `T.append` ": " `T.append` problem
unless quiet $ do unless quiet $ do
T.putStrLn " Found:" T.putStrLn " Found:"
T.putStr $ prettyBlock 4 block' T.putStr $ prettyBlock 4 block
case fixed of if not fixed
Nothing -> T.putStrLn " (Couldn't automatically fix)" then T.putStrLn " (Couldn't automatically fix)"
Just f -> do else do
T.putStrLn " Fixed to:" T.putStrLn " Fixed to:"
T.putStr $ prettyBlock 4 f T.putStr $ prettyBlock 4 block'
T.putStrLn "" T.putStrLn ""
return $ null problems return fs
{ fileBlock = updateSubBlock block block' (fileBlock fs)
, fileUpdated = fileUpdated fs || fixed
, fileOk = fileOk fs && null problems
}


fromSrcSpanInfo :: H.SrcSpanInfo -> Block -> Block fromSrcSpanInfo :: H.SrcSpanInfo -> Block -> Block
fromSrcSpanInfo ssi = subBlock start end fromSrcSpanInfo ssi = subBlock start end
Expand Down Expand Up @@ -91,7 +106,7 @@ unCPP = unlines . map unCpp' . lines
| "#" `isPrefixOf` x = "" | "#" `isPrefixOf` x = ""
| otherwise = x | otherwise = x


checkStyle :: Bool -> FilePath -> IO Bool checkStyle :: Bool -> FilePath -> IO FileState
checkStyle quiet file = do checkStyle quiet file = do
contents <- readFile file contents <- readFile file
let block = fromText $ T.pack contents let block = fromText $ T.pack contents
Expand All @@ -101,8 +116,9 @@ checkStyle quiet file = do
mode = H.defaultParseMode {H.extensions = exts} mode = H.defaultParseMode {H.extensions = exts}
-- Special handling for CPP, haskell-src-exts can't deal with it -- Special handling for CPP, haskell-src-exts can't deal with it
contents' = if H.CPP `elem` exts then unCPP contents else contents contents' = if H.CPP `elem` exts then unCPP contents else contents
fs = FileState block False True
case H.parseModuleWithMode mode contents' of case H.parseModuleWithMode mode contents' of
H.ParseOk md -> and <$> mapM (runRule quiet file block md) H.ParseOk md -> foldM (runRule quiet file md) fs
[ (typeSigSelector, typeSigCheck, fixNothing) [ (typeSigSelector, typeSigCheck, fixNothing)
, (selectLines, tabsCheck, fixNothing) , (selectLines, tabsCheck, fixNothing)
, (selectLines, lineLengthCheck 78, fixNothing) , (selectLines, lineLengthCheck 78, fixNothing)
Expand All @@ -111,7 +127,7 @@ checkStyle quiet file = do
err -> do err -> do
putStrLn $ "HStyle.checkStyle: could not parse " ++ putStrLn $ "HStyle.checkStyle: could not parse " ++
file ++ ": " ++ show err file ++ ": " ++ show err
return False return fs


fixStyle :: FilePath -> IO () fixStyle :: FilePath -> IO ()
fixStyle = error "HStyle.fixStyle: Not implemented" fixStyle = error "HStyle.fixStyle: Not implemented"
2 changes: 1 addition & 1 deletion src/Main.hs
Expand Up @@ -33,5 +33,5 @@ hstyle = HStyle
main :: IO () main :: IO ()
main = do main = do
config <- cmdArgs hstyle config <- cmdArgs hstyle
ok <- and <$> mapM (checkStyle $ quiet config) (files config) ok <- all fileOk <$> mapM (checkStyle $ quiet config) (files config)
exitWith $ if ok then ExitSuccess else ExitFailure 1 exitWith $ if ok then ExitSuccess else ExitFailure 1

0 comments on commit b55471d

Please sign in to comment.