Skip to content
This repository has been archived by the owner on Jun 22, 2020. It is now read-only.

Commit

Permalink
Self-format, update format.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
cwgoes committed Oct 13, 2019
1 parent 6772b61 commit fa91918
Show file tree
Hide file tree
Showing 43 changed files with 1,562 additions and 1,549 deletions.
7 changes: 3 additions & 4 deletions format.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/usr/bin/env nix-shell
#!nix-shell -p "(import ./default.nix {}).ormolu" -i bash --pure
#
# Format Ormolu using current version of Ormolu.

set -e
set -xe

stack install

export LANG="C.UTF-8"

Expand Down
3 changes: 3 additions & 0 deletions ormolu.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ library
else
ghc-options: -O2 -Wall
default-language: Haskell2010
default-extensions: UnicodeSyntax

test-suite tests
main-is: Spec.hs
Expand All @@ -143,6 +144,7 @@ test-suite tests
else
ghc-options: -O2 -Wall
default-language: Haskell2010
default-extensions: UnicodeSyntax

executable ormolu
main-is: Main.hs
Expand All @@ -163,3 +165,4 @@ executable ormolu
else
ghc-options: -O2 -Wall
default-language: Haskell2010
default-extensions: UnicodeSyntax
56 changes: 28 additions & 28 deletions src/Ormolu.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ import System.IO (hGetContents, stdin)
-- side-effects though.
-- * Takes file name just to use it in parse error messages.
-- * Throws 'OrmoluException'.
ormolu ::
MonadIO m =>
ormolu
MonadIO m
-- | Ormolu configuration
Config ->
Config
-- | Location of source file
FilePath ->
FilePath
-- | Input to format
String ->
String
m Text
ormolu cfg path str = do
(ws, result0) <-
(ws, result0)
parseModule' cfg OrmoluParsingFailed path str
when (cfgDebug cfg) $ do
traceM "warnings:\n"
Expand All @@ -67,23 +67,23 @@ ormolu cfg path str = do
let pathRendered = path ++ "<rendered>"
-- Parse the result of pretty-printing again and make sure that AST
-- is the same as AST of original snippet module span positions.
(_, result1) <-
(_, result1)
parseModule'
cfg
OrmoluOutputParsingFailed
pathRendered
(T.unpack txt)
unless (cfgUnsafe cfg) $
case diffParseResult result0 result1 of
Same -> return ()
Different ss -> liftIO $ throwIO (OrmoluASTDiffers path ss)
Same return ()
Different ss liftIO $ throwIO (OrmoluASTDiffers path ss)
-- Try re-formatting the formatted result to check if we get exactly
-- the same output.
when (cfgCheckIdempotency cfg) $
let txt2 = printModule result1
in case diffText txt txt2 pathRendered of
Nothing -> return ()
Just (loc, l, r) ->
Nothing return ()
Just (loc, l, r)
liftIO $
throwIO (OrmoluNonIdempotentOutput loc l r)
return txt
Expand All @@ -93,12 +93,12 @@ ormolu cfg path str = do
--
-- > ormoluFile cfg path =
-- > liftIO (readFile path) >>= ormolu cfg path
ormoluFile ::
MonadIO m =>
ormoluFile
MonadIO m
-- | Ormolu configuration
Config ->
Config
-- | Location of source file
FilePath ->
FilePath
-- | Resulting rendition
m Text
ormoluFile cfg path =
Expand All @@ -108,10 +108,10 @@ ormoluFile cfg path =
--
-- > ormoluStdin cfg =
-- > liftIO (hGetContents stdin) >>= ormolu cfg "<stdin>"
ormoluStdin ::
MonadIO m =>
ormoluStdin
MonadIO m
-- | Ormolu configuration
Config ->
Config
-- | Resulting rendition
m Text
ormoluStdin cfg =
Expand All @@ -121,25 +121,25 @@ ormoluStdin cfg =
-- Helpers

-- | A wrapper around 'parseModule'.
parseModule' ::
MonadIO m =>
parseModule'
MonadIO m
-- | Ormolu configuration
Config ->
Config
-- | How to obtain 'OrmoluException' to throw when parsing fails
(GHC.SrcSpan -> String -> OrmoluException) ->
(GHC.SrcSpan String OrmoluException)
-- | File name to use in errors
FilePath ->
FilePath
-- | Actual input for the parser
String ->
String
m ([GHC.Warn], ParseResult)
parseModule' cfg mkException path str = do
(ws, r) <- parseModule cfg path str
(ws, r) parseModule cfg path str
case r of
Left (spn, err) -> liftIO $ throwIO (mkException spn err)
Right x -> return (ws, x)
Left (spn, err) liftIO $ throwIO (mkException spn err)
Right x return (ws, x)

-- | Pretty-print a 'GHC.Warn'.
showWarn :: GHC.Warn -> String
showWarn GHC.Warn String
showWarn (GHC.Warn reason l) =
unlines
[ showOutputable reason,
Expand Down
16 changes: 8 additions & 8 deletions src/Ormolu/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ import qualified SrcLoc as GHC
data Config
= Config
{ -- | Dynamic options to pass to GHC parser
cfgDynOptions :: ![DynOption],
cfgDynOptions ![DynOption],
-- | Do formatting faster but without automatic detection of defects
cfgUnsafe :: !Bool,
cfgUnsafe !Bool,
-- | Output information useful for debugging
cfgDebug :: !Bool,
cfgDebug !Bool,
-- | Do not fail if CPP pragma is present (still doesn't handle CPP but
-- useful for formatting of files that enable the extension without
-- actually containing CPP macros)
cfgTolerateCpp :: !Bool,
cfgTolerateCpp !Bool,
-- | Checks if re-formatting the result is idempotent.
cfgCheckIdempotency :: !Bool
cfgCheckIdempotency !Bool
}
deriving (Eq, Show)

-- | Default 'Config'.
defaultConfig :: Config
defaultConfig Config
defaultConfig = Config
{ cfgDynOptions = [],
cfgUnsafe = False,
Expand All @@ -44,10 +44,10 @@ defaultConfig = Config
-- | A wrapper for dynamic options.
newtype DynOption
= DynOption
{ unDynOption :: String
{ unDynOption String
}
deriving (Eq, Ord, Show)

-- | Convert 'DynOption' to @'GHC.Located' 'String'@.
dynOptionToLocatedStr :: DynOption -> GHC.Located String
dynOptionToLocatedStr DynOption GHC.Located String
dynOptionToLocatedStr (DynOption o) = GHC.L GHC.noSrcSpan o
58 changes: 29 additions & 29 deletions src/Ormolu/Diff.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ instance Monoid Diff where
mempty = Same

-- | Return 'Diff' of two 'ParseResult's.
diffParseResult :: ParseResult -> ParseResult -> Diff
diffParseResult ParseResult ParseResult Diff
diffParseResult
ParseResult
{ prCommentStream = cstream0,
Expand All @@ -53,16 +53,16 @@ diffParseResult

-- | Compare two values for equality disregarding differences in 'SrcSpan's
-- and the ordering of import lists.
matchIgnoringSrcSpans :: Data a => a -> a -> Diff
matchIgnoringSrcSpans Data a a a Diff
matchIgnoringSrcSpans = genericQuery
where
genericQuery :: GenericQ (GenericQ Diff)
genericQuery GenericQ (GenericQ Diff)
genericQuery x y
-- NOTE 'ByteString' implement 'Data' instance manually and does not
-- implement 'toConstr', so we have to deal with it in a special way.
| Just x' <- cast x,
Just y' <- cast y =
if x' == (y' :: ByteString)
| Just x' cast x,
Just y' cast y =
if x' == (y' ByteString)
then Same
else Different []
| typeOf x == typeOf y,
Expand All @@ -79,33 +79,33 @@ matchIgnoringSrcSpans = genericQuery
x
y
| otherwise = Different []
srcSpanEq :: SrcSpan -> GenericQ Diff
srcSpanEq SrcSpan GenericQ Diff
srcSpanEq _ _ = Same
hsModuleEq :: HsModule GhcPs -> GenericQ Diff
hsModuleEq HsModule GhcPs GenericQ Diff
hsModuleEq hs0 hs1' =
case cast hs1' :: Maybe (HsModule GhcPs) of
Nothing -> Different []
Just hs1 ->
case cast hs1' Maybe (HsModule GhcPs) of
Nothing Different []
Just hs1
matchIgnoringSrcSpans
hs0 {hsmodImports = sortImports (hsmodImports hs0)}
hs1 {hsmodImports = sortImports (hsmodImports hs1)}
sourceTextEq :: SourceText -> GenericQ Diff
sourceTextEq SourceText GenericQ Diff
sourceTextEq _ _ = Same
hsDocStringEq :: HsDocString -> GenericQ Diff
hsDocStringEq HsDocString GenericQ Diff
hsDocStringEq str0 str1' =
case cast str1' :: Maybe HsDocString of
Nothing -> Different []
Just str1 ->
case cast str1' Maybe HsDocString of
Nothing Different []
Just str1
if splitDocString str0 == splitDocString str1
then Same
else Different []
forLocated ::
(Data e0, Data e1) =>
GenLocated e0 e1 ->
forLocated
(Data e0, Data e1)
GenLocated e0 e1
GenericQ Diff
forLocated x@(L mspn _) y =
maybe id appendSpan (cast mspn) (genericQuery x y)
appendSpan :: SrcSpan -> Diff -> Diff
appendSpan SrcSpan Diff Diff
appendSpan s (Different ss) | fresh && helpful = Different (s : ss)
where
fresh = not $ any (flip isSubspanOf s) ss
Expand All @@ -114,18 +114,18 @@ matchIgnoringSrcSpans = genericQuery

-- | Diff two texts and return the location they start to differ, alongside
-- with excerpts around that location.
diffText ::
diffText
-- | Text before
Text ->
Text
-- | Text after
Text ->
Text
-- | Path to use to construct 'GHC.RealSrcLoc'
FilePath ->
FilePath
Maybe (GHC.RealSrcLoc, Text, Text)
diffText left right fp =
case go (0, 0, 0) left right of
Nothing -> Nothing
Just (row, col, loc) ->
Nothing Nothing
Just (row, col, loc)
Just
( GHC.mkRealSrcLoc (GHC.mkFastString fp) row col,
getSpan loc left,
Expand All @@ -135,17 +135,17 @@ diffText left right fp =
go (row, col, loc) t1 t2 =
case (T.uncons t1, T.uncons t2) of
-- both text empty, all good
(Nothing, Nothing) ->
(Nothing, Nothing)
Nothing
-- first chars are the same, adjust position and recurse
(Just (c1, r1), Just (c2, r2))
| c1 == c2 ->
| c1 == c2
let (row', col', loc') =
if c1 == '\n'
then (row + 1, 0, loc + 1)
else (row, col + 1, loc + 1)
in go (row', col', loc') r1 r2
-- something is different, return the position
_ ->
_
Just (row, col, loc)
getSpan loc = T.take 20 . T.drop (loc - 10)
Loading

0 comments on commit fa91918

Please sign in to comment.