@@ -12,7 +12,9 @@ import Control.Exception
12import Ormolu.Utils (showOutputable)
12import Ormolu.Utils (showOutputable)
13import System.Exit (ExitCode (..), exitWith)
13import System.Exit (ExitCode (..), exitWith)
14import System.IO
14import System.IO
15+import Data.Text (Text)
15import qualified GHC
16import qualified GHC
17+import qualified Outputable as GHC
17-- | Ormolu exception representing all cases when Ormolu can fail.
19-- | Ormolu exception representing all cases when Ormolu can fail.
@@ -25,6 +27,8 @@ data OrmoluException
25 -- ^ Parsing of formatted source code failed
27 -- ^ Parsing of formatted source code failed
26 | OrmoluASTDiffers FilePath [GHC.SrcSpan]
28 | OrmoluASTDiffers FilePath [GHC.SrcSpan]
27 -- ^ Original and resulting ASTs differ
29 -- ^ Original and resulting ASTs differ
30+ | OrmoluNonIdempotentOutput GHC.RealSrcLoc Text Text
31+ -- ^ Formatted source code is not idempotent
28 deriving (Eq, Show)
32 deriving (Eq, Show)
30instance Exception OrmoluException where
34instance Exception OrmoluException where
@@ -34,9 +38,9 @@ instance Exception OrmoluException where
34 , withIndent path
38 , withIndent path
36 OrmoluParsingFailed s e ->
40 OrmoluParsingFailed s e ->
37− showParsingErr "Parsing of source code failed:" s e
41+ showParsingErr "Parsing of source code failed:" s [e]
38 OrmoluOutputParsingFailed s e ->
42 OrmoluOutputParsingFailed s e ->
39− showParsingErr "Parsing of formatted code failed:" s e ++
43+ showParsingErr "Parsing of formatted code failed:" s [e] ++
40 "Please, consider reporting the bug.\n"
44 "Please, consider reporting the bug.\n"
41 OrmoluASTDiffers path ss -> unlines $
45 OrmoluASTDiffers path ss -> unlines $
42 [ "AST of input and AST of formatted code differ."
46 [ "AST of input and AST of formatted code differ."
@@ -44,8 +48,12 @@ instance Exception OrmoluException where
44 ++ (fmap withIndent $ case fmap (\s -> "at " ++ showOutputable s) ss of
48 ++ (fmap withIndent $ case fmap (\s -> "at " ++ showOutputable s) ss of
45 [] -> ["in " ++ path]
49 [] -> ["in " ++ path]
46 xs -> xs) ++
50 xs -> xs) ++
47− [ "Please, consider reporting the bug."
51+ [ "Please, consider reporting the bug." ]
48− ]
52+ OrmoluNonIdempotentOutput loc left right ->
53+ showParsingErr "Formatting is not idempotent:" loc
54+ [ "before: " ++ show left , "after: " ++ show right ]
55+ ++ "Please, consider reporting the bug.\n"
50-- | Inside this wrapper 'OrmoluException' will be caught and displayed
58-- | Inside this wrapper 'OrmoluException' will be caught and displayed
51-- nicely using 'displayException'.
59-- nicely using 'displayException'.
@@ -65,18 +73,18 @@ withPrettyOrmoluExceptions m = m `catch` h
65 OrmoluParsingFailed _ _ -> 3
73 OrmoluParsingFailed _ _ -> 3
66 OrmoluOutputParsingFailed _ _ -> 4
74 OrmoluOutputParsingFailed _ _ -> 4
67 OrmoluASTDiffers _ _ -> 5
75 OrmoluASTDiffers _ _ -> 5
76+ OrmoluNonIdempotentOutput _ _ _ -> 6
69----------------------------------------------------------------------------
78----------------------------------------------------------------------------
72-- | Show a parse error.
81-- | Show a parse error.
74−showParsingErr :: String -> GHC.SrcSpan -> String -> String
83+showParsingErr :: GHC.Outputable a => String -> a -> [String] -> String
75−showParsingErr msg spn err = unlines
84+showParsingErr msg spn err = unlines $
77 , withIndent (showOutputable spn)
86 , withIndent (showOutputable spn)
78− , withIndent err
87+ ] ++ map withIndent err
81-- | Indent with 2 spaces for readability.
89-- | Indent with 2 spaces for readability.