summaryrefslogtreecommitdiffhomepage
path: root/src/Main.hs
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2021-01-17 11:15:37 -0700
committerSean Whitton <spwhitton@spwhitton.name>2021-01-17 11:15:37 -0700
commit3130faccf7c9a9a7697e246884e2b60fd4b1f9de (patch)
treeab171724845fe928ef05692c27351be933228ec2 /src/Main.hs
parentfd8bfa2853825504c2dbc7678154ac8d56d47035 (diff)
parent84770e33bb6286c163c3b2b10fa98d264f6672b8 (diff)
downloadstylish-haskell-3130faccf7c9a9a7697e246884e2b60fd4b1f9de.tar.gz
Merge tag 'v0.12.2.0'
v0.12.2.0 - 0.12.2.0 (2020-10-08) * align: Add a new option for aligning only adjacent items (by 1Computer1) * align: Add support for aligning MultiWayIf syntax (by 1Computer1) * data: Fix some issues with record field padding * module_header: Add separate_lists option * imports: Respect separate_lists for (..) imports * data: Make sorting deriving list optional (by Maxim Koltsov)
Diffstat (limited to 'src/Main.hs')
-rw-r--r--src/Main.hs38
1 files changed, 31 insertions, 7 deletions
diff --git a/src/Main.hs b/src/Main.hs
index b1ca2d5..a41c1d8 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -1,3 +1,4 @@
+{-# LANGUAGE CPP #-}
--------------------------------------------------------------------------------
module Main
( main
@@ -5,15 +6,18 @@ module Main
--------------------------------------------------------------------------------
-import Control.Monad (forM_, unless)
+import Control.Monad (forM_, unless, when)
import qualified Data.ByteString.Char8 as BC8
-import Data.Monoid ((<>))
import Data.Version (showVersion)
import qualified Options.Applicative as OA
import System.Exit (exitFailure)
import qualified System.IO as IO
import qualified System.IO.Strict as IO.Strict
+--------------------------------------------------------------------------------
+#if __GLASGOW_HASKELL__ < 808
+import Data.Monoid ((<>))
+#endif
--------------------------------------------------------------------------------
import Language.Haskell.Stylish
@@ -112,7 +116,10 @@ stylishHaskell sa = do
forM_ steps $ \s -> verbose' $ "Enabled " ++ stepName s ++ " step"
verbose' $ "Extra language extensions: " ++
show (configLanguageExtensions conf)
- mapM_ (file sa conf) $ files' filesR
+ res <- foldMap (file sa conf) (files' filesR)
+
+ verbose' $ "Exit code behavior: " ++ show (configExitCode conf)
+ when (configExitCode conf == ErrorOnFormatExitBehavior && res == DidFormat) exitFailure
where
verbose' = makeVerbose (saVerbose sa)
files' x = case (saRecursive sa, null x) of
@@ -120,16 +127,33 @@ stylishHaskell sa = do
(_,True) -> [Nothing] -- Involving IO.stdin.
(_,False) -> map Just x -- Process available files.
+data FormattingResult
+ = DidFormat
+ | NoChange
+ deriving (Eq)
+
+instance Semigroup FormattingResult where
+ _ <> DidFormat = DidFormat
+ DidFormat <> _ = DidFormat
+ _ <> _ = NoChange
+
+instance Monoid FormattingResult where
+ mempty = NoChange
--------------------------------------------------------------------------------
-- | Processes a single file, or stdin if no filepath is given
-file :: StylishArgs -> Config -> Maybe FilePath -> IO ()
+file :: StylishArgs -> Config -> Maybe FilePath -> IO FormattingResult
file sa conf mfp = do
contents <- maybe getContents readUTF8File mfp
- let result = runSteps (configLanguageExtensions conf)
- mfp (configSteps conf) $ lines contents
+ let
+ inputLines =
+ lines contents
+ result =
+ runSteps (configLanguageExtensions conf) mfp (configSteps conf) inputLines
case result of
- Right ok -> write contents $ unlines ok
+ Right ok -> do
+ write contents (unlines ok)
+ pure $ if ok /= inputLines then DidFormat else NoChange
Left err -> do
IO.hPutStrLn IO.stderr err
exitFailure