ormolu-0.7.7.0: A formatter for Haskell source code
Safe HaskellNone
LanguageGHC2021

Ormolu

Description

A formatter for Haskell source code. This module exposes the official stable API, other modules may be not as reliable.

Synopsis

Top-level formatting functions

ormolu #

Arguments

:: MonadIO m 
=> Config RegionIndices

Ormolu configuration

-> FilePath

Location of source file

-> Text

Input to format

-> m Text 

Format a Text.

The function

  • Needs IO because some functions from GHC that are necessary to setup parsing context require IO. There should be no visible side-effects though.
  • Takes file name just to use it in parse error messages.
  • Throws OrmoluException.

NOTE: The caller is responsible for setting the appropriate value in the cfgSourceType field. Autodetection of source type won't happen here, see detectSourceType.

ormoluFile #

Arguments

:: MonadIO m 
=> Config RegionIndices

Ormolu configuration

-> FilePath

Location of source file

-> m Text

Resulting rendition

Load a file and format it. The file stays intact and the rendered version is returned as Text.

NOTE: The caller is responsible for setting the appropriate value in the cfgSourceType field. Autodetection of source type won't happen here, see detectSourceType.

ormoluStdin #

Arguments

:: MonadIO m 
=> Config RegionIndices

Ormolu configuration

-> m Text

Resulting rendition

Read input from stdin and format it.

NOTE: The caller is responsible for setting the appropriate value in the cfgSourceType field. Autodetection of source type won't happen here, see detectSourceType.

Configuration

data Config region #

Ormolu configuration.

Constructors

Config 

Fields

Instances

Instances details
Functor Config # 
Instance details

Defined in Ormolu.Config

Methods

fmap :: (a -> b) -> Config a -> Config b #

(<$) :: a -> Config b -> Config a #

Generic (Config region) # 
Instance details

Defined in Ormolu.Config

Associated Types

type Rep (Config region) 
Instance details

Defined in Ormolu.Config

Methods

from :: Config region -> Rep (Config region) x #

to :: Rep (Config region) x -> Config region #

Show region => Show (Config region) # 
Instance details

Defined in Ormolu.Config

Methods

showsPrec :: Int -> Config region -> ShowS #

show :: Config region -> String #

showList :: [Config region] -> ShowS #

Eq region => Eq (Config region) # 
Instance details

Defined in Ormolu.Config

Methods

(==) :: Config region -> Config region -> Bool #

(/=) :: Config region -> Config region -> Bool #

type Rep (Config region) # 
Instance details

Defined in Ormolu.Config

data ColorMode #

Whether to use colors and other features of ANSI terminals.

Constructors

Never 
Always 
Auto 

Instances

Instances details
Show ColorMode # 
Instance details

Defined in Ormolu.Terminal

Eq ColorMode # 
Instance details

Defined in Ormolu.Terminal

data RegionIndices #

Region selection as the combination of start and end line numbers.

Constructors

RegionIndices 

Fields

Instances

Instances details
Show RegionIndices # 
Instance details

Defined in Ormolu.Config

Eq RegionIndices # 
Instance details

Defined in Ormolu.Config

data SourceType #

Type of sources that can be formatted by Ormolu.

Constructors

ModuleSource

Consider the input as a regular Haskell module

SignatureSource

Consider the input as a Backpack module signature

Instances

Instances details
Show SourceType # 
Instance details

Defined in Ormolu.Config

Eq SourceType # 
Instance details

Defined in Ormolu.Config

detectSourceType :: FilePath -> SourceType #

Detect SourceType based on the file extension.

refineConfig #

Arguments

:: SourceType

Source type to use

-> Maybe CabalInfo

Cabal info for the file, if available

-> Maybe FixityOverrides

Fixity overrides, if available

-> Maybe ModuleReexports

Module re-exports, if available

-> Config region

Config to refine

-> Config region

Refined Config

Refine a Config by incorporating given SourceType, CabalInfo, and fixity overrides FixityMap. You can use detectSourceType to deduce SourceType based on the file extension, getCabalInfoForSourceFile to obtain CabalInfo and getFixityOverridesForSourceFile for FixityMap.

Since: 0.5.3.0

newtype DynOption #

A wrapper for dynamic options.

Constructors

DynOption 

Fields

Instances

Instances details
Show DynOption # 
Instance details

Defined in Ormolu.Config

Eq DynOption # 
Instance details

Defined in Ormolu.Config

Ord DynOption # 
Instance details

Defined in Ormolu.Config

Cabal info

data CabalSearchResult #

The result of searching for a .cabal file.

Since: 0.5.3.0

Constructors

CabalNotFound

Cabal file could not be found

CabalDidNotMention CabalInfo

Cabal file was found, but it did not mention the source file in question

CabalFound CabalInfo

Cabal file was found and it mentions the source file in question

data CabalInfo #

Cabal information of interest to Ormolu.

Constructors

CabalInfo 

Fields

Instances

Instances details
Show CabalInfo # 
Instance details

Defined in Ormolu.Utils.Cabal

Eq CabalInfo # 
Instance details

Defined in Ormolu.Utils.Cabal

getCabalInfoForSourceFile #

Arguments

:: MonadIO m 
=> FilePath

Haskell source file

-> m CabalSearchResult

Extracted cabal info, if any

Locate a .cabal file corresponding to the given Haskell source file and obtain CabalInfo from it.

Fixity overrides and module re-exports

data FixityOverrides #

Map from the operator name to its FixityInfo.

defaultFixityOverrides :: FixityOverrides #

Fixity overrides to use by default.

data ModuleReexports #

Module re-exports

defaultModuleReexports :: ModuleReexports #

Module re-exports to apply by default.

getDotOrmoluForSourceFile #

Arguments

:: MonadIO m 
=> FilePath

CabalInfo already obtained for this source file

-> m (FixityOverrides, ModuleReexports) 

Attempt to locate and parse an .ormolu file. If it does not exist, default fixity map and module reexports are returned. This function maintains a cache of fixity overrides and module re-exports where cabal file paths act as keys.

Working with exceptions

data OrmoluException #

Ormolu exception representing all cases when Ormolu can fail.

Constructors

OrmoluParsingFailed SrcSpan String

Parsing of original source code failed

OrmoluOutputParsingFailed SrcSpan String

Parsing of formatted source code failed

OrmoluASTDiffers TextDiff [RealSrcSpan]

Original and resulting ASTs differ

OrmoluNonIdempotentOutput TextDiff

Formatted source code is not idempotent

OrmoluUnrecognizedOpts (NonEmpty String)

Some GHC options were not recognized

OrmoluCabalFileParsingFailed FilePath (NonEmpty PError)

Cabal file parsing failed

OrmoluMissingStdinInputFile

Missing input file path when using stdin input and accounting for .cabal files

OrmoluFixityOverridesParseError (ParseErrorBundle Text Void)

A parse error in a fixity overrides file

withPrettyOrmoluExceptions #

Arguments

:: ColorMode

Color mode

-> IO ExitCode

Action that may throw an exception

-> IO ExitCode 

Inside this wrapper OrmoluException will be caught and displayed nicely.