packages feed

hfmt 0.0.1.0 → 0.0.2.0

raw patch · 5 files changed

+41/−24 lines, 5 files

Files

app/Actions.hs view
@@ -10,15 +10,18 @@ import Data.Algorithm.DiffOutput
 import Text.PrettyPrint
 
-act :: Options -> ReformatResult -> IO ()
-act options (InvalidReformat input errorString) =
+act :: Options -> ReformatResult -> IO ReformatResult
+act options r@(InvalidReformat input errorString) = do
   putStrLn ("Error reformatting " ++ show input ++ ": " ++ errorString)
-act options (Reformat input source result) = act' (optAction options)
+  return r
+act options r@(Reformat input source result) = act' (optAction options)
   where
-    act' PrintDiffs = when wasReformatted (printDiff input source result)
+    act' PrintDiffs = when wasReformatted (printDiff input source result) >> return r
     act' PrintSources = undefined
-    act' PrintFilePaths = when wasReformatted (print input)
-    act' WriteSources = when wasReformatted (writeSource input (reformattedSource result))
+    act' PrintFilePaths = when wasReformatted (print input) >> return r
+    act' WriteSources = do
+      when wasReformatted (writeSource input (reformattedSource result))
+      return (Reformat input (reformattedSource result) result)
     wasReformatted = sourceChangedOrHasSuggestions source result
 
 sourceChangedOrHasSuggestions :: HaskellSource -> Reformatted -> Bool
app/Main.hs view
@@ -15,28 +15,39 @@ import           Options.Applicative.Extra          as OptApp
 import           Pipes
 import qualified Pipes.Prelude                      as P
+import           System.Exit
 
 main :: IO ()
 main = do
   options <- execParser Options.parser
   formatter <- defaultFormatter
-  runEffect $ inputFiles options >-> P.map (reformat formatter) >-> P.mapM_ (Actions.act options)
+  reformatNeeded <- runFormatter formatter options
+  if reformatNeeded
+    then exitFailure
+    else exitSuccess
 
-inputFiles :: Options -> Producer InputFileWithSource IO ()
-inputFiles options = determineInputFilePaths (optPaths options) >-> P.mapM readInputFile
+runFormatter :: Formatter -> Options -> IO Bool
+runFormatter formatter options =
+  P.any sourceChangedOrHasSuggestions (inputFiles >-> P.map reformat >-> P.mapM writeOutput)
   where
-    determineInputFilePaths :: [FilePath] -> Producer InputFile IO ()
-    determineInputFilePaths [] = enumeratePath "." >-> P.map InputFilePath
-    determineInputFilePaths ["-"] = yield InputFromStdIn
-    determineInputFilePaths paths = for (each paths) enumeratePath >-> P.map InputFilePath
+    inputFiles = readInputFiles options
+    reformat = applyFormatter formatter
+    writeOutput = Actions.act options
 
-    readInputFile :: InputFile -> IO InputFileWithSource
-    readInputFile (InputFilePath path) = InputFileWithSource (InputFilePath path) <$> readSource
-                                                                                        path
-    readInputFile (InputFromStdIn) = InputFileWithSource InputFromStdIn <$> readStdin
+readInputFiles :: Options -> Producer InputFileWithSource IO ()
+readInputFiles options = determineInputFilePaths (optPaths options) >-> P.mapM readInputFile
 
-reformat :: Formatter -> InputFileWithSource -> ReformatResult
-reformat (Formatter format) (InputFileWithSource input source) =
+determineInputFilePaths :: [FilePath] -> Producer InputFile IO ()
+determineInputFilePaths [] = enumeratePath "." >-> P.map InputFilePath
+determineInputFilePaths ["-"] = yield InputFromStdIn
+determineInputFilePaths paths = for (each paths) enumeratePath >-> P.map InputFilePath
+
+readInputFile :: InputFile -> IO InputFileWithSource
+readInputFile (InputFilePath path) = InputFileWithSource (InputFilePath path) <$> readSource path
+readInputFile (InputFromStdIn) = InputFileWithSource InputFromStdIn <$> readStdin
+
+applyFormatter :: Formatter -> InputFileWithSource -> ReformatResult
+applyFormatter (Formatter format) (InputFileWithSource input source) =
   case format source of
     Left error        -> InvalidReformat input error
     Right reformatted -> Reformat input source reformatted
@@ -46,3 +57,7 @@ 
 readStdin :: IO HaskellSource
 readStdin = HaskellSource <$> getContents
+
+sourceChangedOrHasSuggestions :: ReformatResult -> Bool
+sourceChangedOrHasSuggestions (Reformat input source reformatted) =
+  not (null (suggestions reformatted)) || source /= reformattedSource reformatted
app/OptionsParser.hs view
@@ -57,7 +57,7 @@            , paragraph
                ".cabal files will be parsed and all specified source directories and files processed."
            , paragraph
-               "If no paths are given, the current directory will be searched for .cabal files to process, if none are found the currend directory will be recursively searched for source files to process."
+               "If no paths are given, the current directory will be searched for .cabal files to process, if none are found the current directory will be recursively searched for source files to process."
            ]
 
 paragraph :: String -> Doc
@@ -69,9 +69,9 @@ optionParserInfo :: ParserInfo Options
 optionParserInfo = info (helper <*> optionParser)
                      (fullDesc
-                      <> header "hfmt - formats Haskell programs"
+                      <> header "hfmt - format Haskell programs"
                       <> progDesc
-                           "Operates on Haskell source files, reformatting them by applying suggestions from hlint, hindent, and stylish-haskell. Inspired by the gofmt utility.")
+                           "Operates on Haskell source files, reformatting them by applying suggestions from HLint, hindent, and stylish-haskell. Inspired by the gofmt utility.")
 
 parser :: ParserInfo Options
 parser = optionParserInfo
hfmt.cabal view
@@ -1,5 +1,5 @@ name:                hfmt
-version:             0.0.1.0
+version:             0.0.2.0
 synopsis:            Haskell source code formatter
 description:         Inspired by gofmt. Built using hlint, hindent, and stylish-haskell.
 license:             MIT
src/Language/Haskell/Format.hs view
@@ -19,7 +19,6 @@ 
 import           Control.Applicative
 import           Data.Maybe
-import           Language.Haskell.HLint3             (Idea)
 
 data Settings = Settings