1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
|
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
module Main (main) where
import Control.Exception (throwIO)
import Control.Monad
import Data.Bool (bool)
import Data.List (intercalate, sort)
import Data.List.NonEmpty (NonEmpty)
import Data.Map.Strict qualified as Map
import Data.Maybe (fromMaybe, mapMaybe, maybeToList)
import Data.Set qualified as Set
import Data.Text.IO qualified as TIO
import Data.Version (showVersion)
import Distribution.ModuleName (ModuleName)
import Distribution.Types.PackageName (PackageName)
import Language.Haskell.TH.Env (envQ)
import Options.Applicative
import Ormolu
import Ormolu.Diff.Text (diffText, printTextDiff)
import Ormolu.Fixity
import Ormolu.Parser (manualExts)
import Ormolu.Terminal
import Ormolu.Utils (showOutputable)
import Ormolu.Utils.Fixity
import Ormolu.Utils.IO
import Paths_ormolu (version)
import System.Directory
import System.Exit (ExitCode (..), exitWith)
import System.FilePath qualified as FP
import System.IO (hPutStrLn, stderr)
-- | Entry point of the program.
main :: IO ()
main = do
Opts {..} <- execParser optsParserInfo
let formatOne' =
formatOne
optConfigFileOpts
optMode
optSourceType
optConfig
exitCode <- case optInputFiles of
[] -> formatOne' Nothing
["-"] -> formatOne' Nothing
[x] -> formatOne' (Just x)
xs -> do
let selectFailure = \case
ExitSuccess -> Nothing
ExitFailure n -> Just n
errorCodes <-
mapMaybe selectFailure <$> mapM (formatOne' . Just) (sort xs)
return $
if null errorCodes
then ExitSuccess
else
ExitFailure $
if all (== 100) errorCodes
then 100
else 102
exitWith exitCode
-- | Format a single input.
formatOne ::
-- | How to use .cabal files
ConfigFileOpts ->
-- | Mode of operation
Mode ->
-- | The 'SourceType' requested by the user
Maybe SourceType ->
-- | Configuration
Config RegionIndices ->
-- | File to format or stdin as 'Nothing'
Maybe FilePath ->
IO ExitCode
formatOne ConfigFileOpts {..} mode reqSourceType rawConfig mpath =
withPrettyOrmoluExceptions (cfgColorMode rawConfig) $ do
let getCabalInfoForSourceFile' sourceFile = do
cabalSearchResult <- getCabalInfoForSourceFile sourceFile
let debugEnabled = cfgDebug rawConfig
case cabalSearchResult of
CabalNotFound -> do
when debugEnabled $
hPutStrLn stderr $
"Could not find a .cabal file for " <> sourceFile
return Nothing
CabalDidNotMention cabalInfo -> do
when debugEnabled $ do
relativeCabalFile <-
makeRelativeToCurrentDirectory (ciCabalFilePath cabalInfo)
hPutStrLn stderr $
"Found .cabal file "
<> relativeCabalFile
<> ", but it did not mention "
<> sourceFile
return (Just cabalInfo)
CabalFound cabalInfo -> return (Just cabalInfo)
getDotOrmoluForSourceFile' sourceFile = do
if optDoNotUseDotOrmolu
then return Nothing
else Just <$> getDotOrmoluForSourceFile sourceFile
case FP.normalise <$> mpath of
-- input source = STDIN
Nothing -> do
mcabalInfo <- case (optStdinInputFile, optDoNotUseCabal) of
(_, True) -> return Nothing
(Nothing, False) -> throwIO OrmoluMissingStdinInputFile
(Just inputFile, False) -> getCabalInfoForSourceFile' inputFile
mdotOrmolu <- case optStdinInputFile of
Nothing -> return Nothing
Just inputFile -> getDotOrmoluForSourceFile' inputFile
config <- patchConfig Nothing mcabalInfo mdotOrmolu
case mode of
Stdout -> do
ormoluStdin config >>= TIO.putStr
return ExitSuccess
InPlace -> do
hPutStrLn
stderr
"In place editing is not supported when input comes from stdin."
-- 101 is different from all the other exit codes we already use.
return (ExitFailure 101)
Check -> do
-- ormoluStdin is not used because we need the originalInput
originalInput <- getContentsUtf8
let stdinRepr = "<stdin>"
formattedInput <-
ormolu config stdinRepr originalInput
handleDiff originalInput formattedInput stdinRepr
-- input source = a file
Just inputFile -> do
mcabalInfo <-
if optDoNotUseCabal
then return Nothing
else getCabalInfoForSourceFile' inputFile
mdotOrmolu <- getDotOrmoluForSourceFile' inputFile
config <-
patchConfig
(Just (detectSourceType inputFile))
mcabalInfo
mdotOrmolu
case mode of
Stdout -> do
ormoluFile config inputFile >>= TIO.putStr
return ExitSuccess
InPlace -> do
-- ormoluFile is not used because we need originalInput
originalInput <- readFileUtf8 inputFile
formattedInput <-
ormolu config inputFile originalInput
when (formattedInput /= originalInput) $
writeFileUtf8 inputFile formattedInput
return ExitSuccess
Check -> do
-- ormoluFile is not used because we need originalInput
originalInput <- readFileUtf8 inputFile
formattedInput <-
ormolu config inputFile originalInput
handleDiff originalInput formattedInput inputFile
where
patchConfig mdetectedSourceType mcabalInfo mdotOrmolu = do
let sourceType =
fromMaybe
ModuleSource
(reqSourceType <|> mdetectedSourceType)
let mfixityOverrides = fst <$> mdotOrmolu
mmoduleReexports = snd <$> mdotOrmolu
return $
refineConfig
sourceType
mcabalInfo
mfixityOverrides
mmoduleReexports
rawConfig
handleDiff originalInput formattedInput fileRepr =
case diffText originalInput formattedInput fileRepr of
Nothing -> return ExitSuccess
Just diff -> do
runTerm (printTextDiff diff) (cfgColorMode rawConfig) stderr
-- 100 is different to all the other exit code that are emitted
-- either from an 'OrmoluException' or from 'error' and
-- 'notImplemented'.
return (ExitFailure 100)
----------------------------------------------------------------------------
-- Command line options parsing
-- | All command line options.
data Opts = Opts
{ -- | Mode of operation
optMode :: !Mode,
-- | Ormolu 'Config'
optConfig :: !(Config RegionIndices),
-- | Options related to info extracted from files
optConfigFileOpts :: ConfigFileOpts,
-- | Source type option, where 'Nothing' means autodetection
optSourceType :: !(Maybe SourceType),
-- | Haskell source files to format or stdin (when the list is empty)
optInputFiles :: ![FilePath]
}
-- | Mode of operation.
data Mode
= -- | Output formatted source code to stdout
Stdout
| -- | Overwrite original file
InPlace
| -- | Exit with non-zero status code if
-- source is not already formatted
Check
deriving (Eq, Show)
-- | Options related to configuration stored in the file system.
data ConfigFileOpts = ConfigFileOpts
{ -- | DO NOT extract default-extensions and dependencies from .cabal files
optDoNotUseCabal :: Bool,
-- | DO NOT look for @.ormolu@ files
optDoNotUseDotOrmolu :: Bool,
-- | Optional path to a file which will be used to find a .cabal file
-- when using input from stdin
optStdinInputFile :: Maybe FilePath
}
deriving (Show)
optsParserInfo :: ParserInfo Opts
optsParserInfo =
info (helper <*> ver <*> exts <*> optsParser) . mconcat $
[fullDesc]
where
ver :: Parser (a -> a)
ver =
infoOption verStr . mconcat $
[ long "version",
short 'v',
help "Print version of the program"
]
verStr =
intercalate
"\n"
[ unwords $
["ormolu", showVersion version]
<> maybeToList $$(envQ @String "ORMOLU_REV"),
"using ghc-lib-parser " ++ VERSION_ghc_lib_parser
]
exts :: Parser (a -> a)
exts =
infoOption displayExts . mconcat $
[ long "manual-exts",
help "Display extensions that need to be enabled manually"
]
displayExts = unlines $ sort (showOutputable <$> manualExts)
optsParser :: Parser Opts
optsParser =
Opts
<$> ( (fmap (bool Stdout InPlace) . switch . mconcat)
[ short 'i',
help "A shortcut for --mode inplace"
]
<|> (option parseMode . mconcat)
[ long "mode",
short 'm',
metavar "MODE",
value Stdout,
help "Mode of operation: 'stdout' (the default), 'inplace', or 'check'"
]
)
<*> configParser
<*> configFileOptsParser
<*> sourceTypeParser
<*> (many . strArgument . mconcat)
[ metavar "FILE",
help "Haskell source files to format or stdin (the default)"
]
configFileOptsParser :: Parser ConfigFileOpts
configFileOptsParser =
ConfigFileOpts
<$> (switch . mconcat)
[ long "no-cabal",
help "Do not extract default-extensions and dependencies from .cabal files"
]
<*> (switch . mconcat)
[ long "no-dot-ormolu",
help "Do not look for .ormolu files"
]
<*> (optional . strOption . mconcat)
[ long "stdin-input-file",
help "Path which will be used to find the .cabal file when using input from stdin"
]
configParser :: Parser (Config RegionIndices)
configParser =
Config
<$> (fmap (fmap DynOption) . many . strOption . mconcat)
[ long "ghc-opt",
short 'o',
metavar "OPT",
help "GHC options to enable (e.g. language extensions)"
]
<*> ( fmap (FixityOverrides . Map.fromList . mconcat)
. many
. option parseFixityDeclaration
. mconcat
)
[ long "fixity",
short 'f',
metavar "FIXITY",
help "Fixity declaration to use (an override)"
]
<*> ( fmap (ModuleReexports . Map.fromListWith (<>) . mconcat . pure)
. many
. option parseModuleReexportDeclaration
. mconcat
)
[ long "reexport",
short 'r',
metavar "REEXPORT",
help "Module re-export that Ormolu should know about"
]
<*> (fmap Set.fromList . many . strOption . mconcat)
[ long "package",
short 'p',
metavar "PACKAGE",
help "Explicitly specified dependency (for operator fixity/precedence only)"
]
<*> (switch . mconcat)
[ long "unsafe",
short 'u',
help "Do formatting faster but without automatic detection of defects"
]
<*> (switch . mconcat)
[ long "debug",
short 'd',
help "Output information useful for debugging"
]
<*> (switch . mconcat)
[ long "check-idempotence",
short 'c',
help "Fail if formatting is not idempotent"
]
-- We cannot parse the source type here, because we might need to do
-- autodection based on the input file extension (not available here)
-- before storing the resolved value in the config struct.
<*> pure ModuleSource
<*> (option parseColorMode . mconcat)
[ long "color",
metavar "WHEN",
value Auto,
help "Colorize the output; WHEN can be 'never', 'always', or 'auto' (the default)"
]
<*> ( RegionIndices
<$> (optional . option auto . mconcat)
[ long "start-line",
metavar "START",
help "Start line of the region to format (starts from 1)"
]
<*> (optional . option auto . mconcat)
[ long "end-line",
metavar "END",
help "End line of the region to format (inclusive)"
]
)
sourceTypeParser :: Parser (Maybe SourceType)
sourceTypeParser =
(option parseSourceType . mconcat)
[ long "source-type",
short 't',
metavar "TYPE",
value Nothing,
help "Set the type of source; TYPE can be 'module', 'sig', or 'auto' (the default)"
]
----------------------------------------------------------------------------
-- Helpers
-- | Parse 'Mode'.
parseMode :: ReadM Mode
parseMode = eitherReader $ \case
"stdout" -> Right Stdout
"inplace" -> Right InPlace
"check" -> Right Check
s -> Left $ "unknown mode: " ++ s
-- | Parse a fixity declaration.
parseFixityDeclaration :: ReadM [(OpName, FixityInfo)]
parseFixityDeclaration = eitherReader parseFixityDeclarationStr
-- | Parse a module reexport declaration.
parseModuleReexportDeclaration ::
ReadM (ModuleName, NonEmpty (Maybe PackageName, ModuleName))
parseModuleReexportDeclaration = eitherReader parseModuleReexportDeclarationStr
-- | Parse 'ColorMode'.
parseColorMode :: ReadM ColorMode
parseColorMode = eitherReader $ \case
"never" -> Right Never
"always" -> Right Always
"auto" -> Right Auto
s -> Left $ "unknown color mode: " ++ s
-- | Parse the 'SourceType'. 'Nothing' means that autodetection based on
-- file extension is requested.
parseSourceType :: ReadM (Maybe SourceType)
parseSourceType = eitherReader $ \case
"module" -> Right (Just ModuleSource)
"sig" -> Right (Just SignatureSource)
"auto" -> Right Nothing
s -> Left $ "unknown source type: " ++ s
|