• Stars
    star
    958
  • Rank 47,718 (Top 1.0 %)
  • Language
    Haskell
  • License
    Other
  • Created almost 6 years ago
  • Updated 30 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A formatter for Haskell source code

Ormolu

License BSD3 Hackage Stackage Nightly Stackage LTS CI

Ormolu is a formatter for Haskell source code. The project was created with the following goals in mind:

  • Using GHC's own parser to avoid parsing problems caused by haskell-src-exts.
  • Let some whitespace be programmable. The layout of the input influences the layout choices in the output. This means that the choices between single-line/multi-line layouts in certain situations are made by the user, not by an algorithm. This makes the implementation simpler and leaves some control to the user while still guaranteeing that the formatted code is stylistically consistent.
  • Writing code in such a way so it's easy to modify and maintain.
  • Implementing one “true” formatting style which admits no configuration.
  • The formatting style aims to result in minimal diffs.
  • Choose a style compatible with modern dialects of Haskell. As new Haskell extensions enter broad use, we may change the style to accommodate them.
  • Idempotence: formatting already formatted code doesn't change it.
  • Be well-tested and robust so that the formatter can be used in large projects.

Try it out in your browser at https://ormolu-live.tweag.io! See Ormolu Live for more info.

Installation

The release page has binaries for Linux, macOS and Windows.

You can also install using cabal or stack:

$ cabal install ormolu
$ stack install ormolu

Ormolu is also included in several package repositories. E.g., on Arch Linux, one can use the package on AUR:

$ yay -S ormolu

Building from source

The easiest way to build the project is with Nix:

$ nix build

Make sure to accept the offered Nix caches (in particular the IOG cache), otherwise building may take a very long time.

Alternatively, stack could be used as follows:

$ stack build # to build
$ stack install # to install

To use Ormolu directly from GitHub with Nix flakes, this snippet may come in handy:

{
  inputs.ormolu.url = "github:tweag/ormolu";
  outputs = { ormolu, ... }: {
    # use ormolu.packages.${system}.default here
  };
}

Usage

The following will print the formatted output to the standard output.

$ ormolu Module.hs

Add --mode inplace to replace the contents of the input file with the formatted output.

$ ormolu --mode inplace Module.hs

Use find to format a tree recursively:

$ ormolu --mode inplace $(find . -name '*.hs')

Or find all files in a project with git ls-files:

$ ormolu --mode inplace $(git ls-files '*.hs')

To check if files are are already formatted (useful on CI):

$ ormolu --mode check $(find . -name '*.hs')

Beware git's core.autocrlf on Windows

Ormolu's output always uses LF line endings. In particular, ormolu --mode check will fail if its input is correctly formatted except that it has CRLF line endings. This situation can happen on Windows when checking out a git repository without having set core.autocrlf to false.

Ormolu Live

On every new commit to master, Ormolu Live is deployed to https://ormolu-live.tweag.io. Older versions are available at https://COMMITHASH--ormolu-live.netlify.app.

Editor integration

We know of the following editor integrations:

Haskell Language Server

Haskell Language Server has built-in support for using Ormolu as a formatter.

GitHub actions

run-ormolu is the recommended way to ensure that a project is formatted with Ormolu.

Language extensions, dependencies, and fixities

Ormolu automatically locates the Cabal file that corresponds to a given source code file. Cabal files are used to extract both default extensions and dependencies. Default extensions directly affect behavior of the GHC parser, while dependencies are used to figure out fixities of operators that appear in the source code. Fixities can also be overridden via an .ormolu file which should be located at a higher level in the file system hierarchy than the source file that is being formatted. When the input comes from stdin, one can pass --stdin-input-file which will give Ormolu the location that should be used as the starting point for searching for .cabal and .ormolu files.

Here is an example of .ormolu file:

infixr 9  .
infixr 5  ++
infixl 4  <$
infixl 1  >>, >>=
infixr 1  =<<
infixr 0  $, $!
infixl 4 <*>, <*, *>, <**>

It uses exactly the same syntax as usual Haskell fixity declarations to make it easier for Haskellers to edit and maintain.

As of Ormolu 0.7.0.0, .ormolu files can also contain instructions about module re-exports that Ormolu should be aware of. This might be desirable because at the moment Ormolu cannot know about all possible module re-exports in the ecosystem and only few of them are actually important when it comes to fixity deduction. In 99% of cases the user won't have to do anything, especially since most common re-exports are already programmed into Ormolu. (You are welcome to open PRs to make Ormolu aware of more re-exports by default.) However, when the fixity of an operator is not inferred correctly, making Ormolu aware of a re-export may come in handy. Here is an example:

module Control.Lens exports Control.Lens.At
module Control.Lens exports "lens" Control.Lens.Lens

Module re-export declarations can be mixed freely with fixity overrides, as long as each declaration is on its own line. As of Ormolu 0.7.1.0 explicit package names are allowed in re-export declarations (see the example above).

Finally, all of the above-mentioned parameters can be controlled from the command line:

  • Language extensions can be specified with the -o or --ghc-opt flag.
  • Dependencies can be specified with the -p or --package flag.
  • Fixities can be specified with the -f or --fixity flag.
  • Re-exports can be specified with the -r or --reexport flag.

Searching for .cabal and .ormolu files can be disabled by passing --no-cabal and --no-dot-ormolu respectively.

Magic comments

Ormolu understands two magic comments:

{- ORMOLU_DISABLE -}

and

{- ORMOLU_ENABLE -}

This allows us to disable formatting selectively for code between these markers or disable it for the entire file. To achieve the latter, just put {- ORMOLU_DISABLE -} at the very top. Note that for Ormolu to work the fragments where Ormolu is enabled must be parseable on their own. Because of that the magic comments cannot be placed arbitrarily, but rather must enclose independent top-level definitions.

Regions

One can ask Ormolu to format a region of input and leave the rest unformatted. This is accomplished by passing the --start-line and --end-line command line options. --start-line defaults to the beginning of the file, while --end-line defaults to the end.

Exit codes

Exit code Meaning
0 Success
1 General problem
2 CPP used (deprecated)
3 Parsing of original input failed
4 Parsing of formatted code failed
5 AST of original and formatted code differs
6 Formatting is not idempotent
7 Unrecognized GHC options
8 Cabal file parsing failed
9 Missing input file path when using stdin input and accounting for .cabal files
10 Parse error while parsing fixity overrides
100 In checking mode: unformatted files
101 Inplace mode does not work with stdin
102 Other issue (with multiple input files)

Using as a library

The ormolu package can also be depended upon from other Haskell programs. For these purposes only the top Ormolu module should be considered stable. It follows PVP starting from the version 0.5.3.0. Rely on other modules at your own risk.

Limitations

  • CPP support is experimental. CPP is virtually impossible to handle correctly, so we process them as a sort of unchangeable snippets. This works only in simple cases when CPP conditionals surround top-level declarations. See the CPP section in the design notes for a discussion of the dangers.
  • Input modules should be parsable by Haddock, which is a bit stricter criterion than just being valid Haskell modules.

Running on Hackage

It's possible to try Ormolu on arbitrary packages from Hackage. For that execute (from the root of the cloned repo):

$ nix build .#hackage.<package>

Then inspect result/log.txt for possible problems. The derivation will also contain formatted .hs files for inspection and original inputs with .hs-original extension (those are with CPP dropped, exactly what is fed into Ormolu).

Forks and modifications

We know of the following actively maintained forks:

  • Fourmolu, which uses 4-space indentation and allows arbitrary configuration.

Contributing

See CONTRIBUTING.md.

License

See LICENSE.md.

Copyright © 2018–present Tweag I/O

More Repositories

1

nickel

Better configuration for less
Rust
2,275
star
2

asterius

DEPRECATED in favor of ghc wasm backend, see https://www.tweag.io/blog/2022-11-22-wasm-backend-merged-in-ghc
Haskell
1,978
star
3

jupyenv

Declarative and reproducible Jupyter environments - powered by Nix
Nix
660
star
4

HaskellR

The full power of R in Haskell.
Haskell
583
star
5

topiary

Rust
486
star
6

sparkle

Haskell on Apache Spark.
Haskell
447
star
7

monad-bayes

A library for probabilistic programming in Haskell.
Jupyter Notebook
407
star
8

awesome-learning-haskell

A collection of resources which were useful to Tweagers for learning Haskell and its various aspects
399
star
9

funflow

Functional workflows
Haskell
361
star
10

linear-base

Standard library for linear types in Haskell.
Haskell
335
star
11

rules_nixpkgs

Rules for importing Nixpkgs packages into Bazel.
Starlark
285
star
12

rules_haskell

Haskell rules for Bazel.
Starlark
265
star
13

inline-java

Haskell/Java interop via inline Java code in Haskell modules.
Haskell
230
star
14

capability

Extensional capabilities and deriving combinators
Haskell
214
star
15

FawltyDeps

Python dependency checker
Python
192
star
16

clodl

Turn dynamically linked ELF binaries and libraries into self-contained closures.
Starlark
170
star
17

inline-js

Call JavaScript from Haskell, and vice versa!
Haskell
128
star
18

opam-nix

Turn opam-based OCaml projects into Nix derivations
Nix
111
star
19

nixtract

A CLI tool to extract the graph of derivations from a Nix flake.
Rust
81
star
20

nix-hour

Questions for the weekly Nix Hour
Nix
77
star
21

linear-types

Drafts, notes and resources for adding linear typing to GHC.
TeX
75
star
22

python-monorepo-example

Example of a python monorepo using pip, the poetry backend, and Pants
Python
75
star
23

terraform-provider-nixos

Terraform provider for NixOS and NixOps
Go
70
star
24

tf-ncl

Terraform Configurations with Nickel
Rust
65
star
25

distributed-closure

Serializable closures for distributed programming.
Haskell
63
star
26

terraform-provider-secret

Terraform secret provider
Shell
62
star
27

guides

Designing, programming and deploying, in style.
58
star
28

servant-template

A modern template for a Servant
Haskell
51
star
29

nix_bazel_codelab

Nix+Bazel Codelab
Starlark
49
star
30

kernmantle

Braiding extensible effects together in a pipeline/workflow of tasks
Haskell
47
star
31

pirouette

Language-generic workbench for building static analysis
Haskell
47
star
32

python-nix

Python-Nix FFI library using the new C API
Python
45
star
33

skyscope

A tool for visualising and exploring Bazel Skyframe graphs.
Haskell
45
star
34

rules_sh

Shell rules for Bazel
Starlark
42
star
35

nix-ux

Nix UX improvements
Nix
36
star
36

genealogos

Genealogos, a Nix sbom generator
Rust
36
star
37

blog-resources

Extra resources for Tweag's blog posts.
Jupyter Notebook
35
star
38

cooked-validators

Haskell
35
star
39

lagoon

Data centralization tool
Haskell
35
star
40

webauthn

A library for parsing and validating webauthn/fido2 credentials
Haskell
34
star
41

haskell-training

Material for Haskell training
Haskell
31
star
42

hyperion

A lab for future Criterion features.
Haskell
29
star
43

ghc-wasm-miso-examples

Haskell
24
star
44

rust-alpine-mimalloc

Shell
23
star
45

network-transport-zeromq

ZeroMQ transport for distributed-process (aka Cloud Haskell)
Haskell
22
star
46

nix-remote-rust

Rust
21
star
47

haskell-stack-nix-example

Examples of valid and invalid Stack + Nix integration
Nix
20
star
48

timestats

A library to profile time in a Haskell program
Haskell
17
star
49

ssh-participation

An ssh server that creates new users on-the-fly, great for letting users participate in a demo
Nix
16
star
50

nix_gazelle_extension

Gazelle language extension for nix files
Go
15
star
51

epcb

Nix RFC draft on evaluation purity and caching builtins
15
star
52

nixpkgs-graph-explorer

Explore the nixpkgs dependency graph
Python
14
star
53

nixpkgs-graph

Generate a graph from nixpkgs
Python
14
star
54

haskell-binaryen

Haskell bindings to binaryen.
WebAssembly
14
star
55

gazelle_cabal

A gazelle extension to produce Haskell rules from cabal files
Haskell
13
star
56

smtlib-backends

A Haskell library providing low-level functions for SMTLIB-based interaction with SMT solvers.
Haskell
13
star
57

chainsail

Replica Exchange sampling as-a-service
Python
11
star
58

rust-wasm-threads

Examples of Web Workers using rust and WASM
Rust
11
star
59

random-quality

Framework for testing quality of random number generators
Nix
10
star
60

rules_haskell_examples

Examples of using Bazel's Haskell rules.
9
star
61

funflow2

Compose and run computational workflows
Haskell
9
star
62

rust-wasm-nix

Nix
9
star
63

store-graph

simple haskell code that builds a graph from the nix store
Shell
9
star
64

purescript-unlift

MonadBase, MonadUnliftEffect, MonadUnliftAff, and MonadUnlift
Nix
9
star
65

stackage-head

Stackage builds based on GHC HEAD
Haskell
9
star
66

nix-installer-generator

Nix installer generator
Nix
8
star
67

functionless

CLI tool for packaging Haskell executables for AWS Lambda
Java
8
star
68

formik-apollo

A little bit of for using Formik with Apollo
TypeScript
8
star
69

ch-nixops-example

Example deployment of Cloud Haskell app using NixOps.
Haskell
8
star
70

nix-store-gcs-proxy

A HTTP nix store that proxies requests to Google Storage
Nix
8
star
71

ghc-wasm32-wasi

DEPRECATED, new home https://gitlab.haskell.org/ghc/ghc-wasm-meta
Nix
7
star
72

python-nix-flake-template

Bootstrap a reproducible yet flexible Python development environment using Nix
Nix
7
star
73

ghc-wasm-bindists

Stable links for various GHC WASM bindists
Haskell
7
star
74

terraform-gcp-cdn-bucket

A Google Storage Bucket + CDN configuration
HCL
7
star
75

servant-oauth2

A modern servant wrapper around the wai-middleware-auth OAuth2 provider implementations.
Haskell
7
star
76

chainsail-resources

Examples, documentation and other additional resources related to Chainsail
Python
6
star
77

hello-plutarch

Template project for smart-contracts in Plutarch
Nix
6
star
78

summer-of-nix-modules

Incremental module system buildup for Summer of Nix
Nix
6
star
79

tendermint-bazel

Building Go with Bazel
Go
6
star
80

linear-constraints

TeX
6
star
81

ghc-asterius

DEPRECATED, new home https://gitlab.haskell.org/ghc/ghc
Haskell
6
star
82

remote-execution-nix

nix to bazel-re proxy
Rust
6
star
83

work-daigest

Create a digest of your work week using a LLM
Python
6
star
84

tf-ncl-examples

Examples of Terraform configuration with Nickel
NCL
5
star
85

nickel-lang.org

The website of the Nickel language
JavaScript
5
star
86

nix-marp

Run Marp tools via Nix
Nix
5
star
87

nickel-kubernetes

Typecheck, template and modularize your Kubernetes definitions with Nickel
Rust
5
star
88

pyfunflow

Declarative composable typed workflows in Python
Python
5
star
89

duckling

a Haskell library that parses text into structured data
Haskell
4
star
90

graft

Haskell
4
star
91

nix-unit-testing

A showcase of different unit testing frameworks for Nix.
Python
4
star
92

rules_purescript

Python
4
star
93

toronto_reproducibility_workshop

Slides and toy project for the talk at the Toronto Workshop on Reproducibility
Python
4
star
94

nixos-specialisation-dual-boot

Nix
4
star
95

organist-example

Python
4
star
96

gh-migration-scripts

Repository to house scripts to help with GH migration projects
JavaScript
4
star
97

gazelle_haskell_modules

A gazelle extension to generate haskell_module rules
Haskell
4
star
98

inputs

Utilities for building forms with React
TypeScript
3
star
99

pthread

Bindings for the pthread library
Haskell
3
star
100

cooked-smart-contracts

Smart contracts for the Cardano blockchain written with Cooked-validators
Haskell
3
star