summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorJasper Van der Jeugt <m@jaspervdj.be>2020-10-02 13:08:39 +0200
committerJasper Van der Jeugt <m@jaspervdj.be>2020-10-02 13:08:39 +0200
commit250e7091edd93ce5a476706ddd968ef3ec1ef336 (patch)
tree98c1a37f8f7adf031b317f820428184c084b9b49 /src
parentce3feb1db9a0e7998a66c9dfdc7aebd9bae79477 (diff)
downloadstylish-haskell-250e7091edd93ce5a476706ddd968ef3ec1ef336.tar.gz
Use ghc-lib-parser rather than haskell-src-exts
This patch swaps out the parsing library from `haskell-src-exts` to `ghc-lib-parser`, which gives us better compatibility with GHC. Because almost every module heavily used the Haskell AST provided by `haskell-src-exts`, this was a huge effort and it would not have been possible without Felix Mulder doing an initial port, GSoC student Beatrice Vergani porting several other steps, and Łukasz Gołębiewski and Paweł Szulc who helped me finish up things in the home stretch. I've generally tried to keep styling 100% compatible with what was there before, but some issues may have unintentionally slipped in so please report those. This introduces one new import styling contributed by Felix: when wrapping import lists over multiple lines, you can repeat the module name, e.g.: import Control.Monad.Except as X (ExceptT (..), MonadError (..), liftEither) import Control.Monad.Except as X (runExceptT, withExceptT) This is activated by using `import_align: repeat`. Secondly, a new Step was added, `module_header`, which formats the export list of a module, including the trailing `where` clause. Details for this new step can be found in the `data/stylish-haskell.yaml`. Co-Authored-By: Beatrice Vergani <beatrice.vergani11@gmail.com> Co-Authored-By: Paweł Szulc <paul.szulc@gmail.com> Co-Authored-By: Łukasz Gołębiewski <lukasz.golebiewski@gmail.com> Co-Authored-By: Felix Mulder <felix.mulder@klarna.com>
Diffstat (limited to 'src')
-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