• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

_cabal_deps/H03-May-2022-332,249197,880

app/H09-Sep-2001-337277

data/H03-May-2022-9,6915,982

src/H03-May-2022-7,7366,002

tests/H03-May-2022-301233

CHANGELOG.mdH A D09-Sep-200117.6 KiB442315

CONTRIBUTING.mdH A D09-Sep-20012.4 KiB6847

DESIGN.mdH A D09-Sep-200115 KiB408324

LICENSE.mdH A D09-Sep-20011.4 KiB2922

README.mdH A D09-Sep-20018.6 KiB260196

Setup.hsH A D09-Sep-200187 74

cabal.projectH A D03-May-2022532 1615

ormolu.cabalH A D09-Sep-20014.7 KiB163148

README.md

1# Ormolu
2
3[![License BSD3](https://img.shields.io/badge/license-BSD3-brightgreen.svg)](http://opensource.org/licenses/BSD-3-Clause)
4[![Hackage](https://img.shields.io/hackage/v/ormolu.svg?style=flat)](https://hackage.haskell.org/package/ormolu)
5[![Stackage Nightly](http://stackage.org/package/ormolu/badge/nightly)](http://stackage.org/nightly/package/ormolu)
6[![Stackage LTS](http://stackage.org/package/ormolu/badge/lts)](http://stackage.org/lts/package/ormolu)
7[![Build status](https://badge.buildkite.com/8e3b0951f3652b77e1c422b361904136a539b0522029156354.svg?branch=master)](https://buildkite.com/tweag-1/ormolu)
8
9* [Installation](#installation)
10* [Building from source](#building-from-source)
11* [Usage](#usage)
12    * [Ormolu Live](#ormolu-live)
13    * [Editor integration](#editor-integration)
14    * [Haskell Language Server](#haskell-language-server)
15    * [GitHub actions](#github-actions)
16    * [Magic comments](#magic-comments)
17    * [Account for .cabal files](#account-for-cabal-files)
18    * [Exit codes](#exit-codes)
19* [Limitations](#limitations)
20* [Running on Hackage](#running-on-hackage)
21* [Contributing](#contributing)
22* [License](#license)
23
24Ormolu is a formatter for Haskell source code. The project was created with
25the following goals in mind:
26
27* Using GHC's own parser to avoid parsing problems caused by
28  [`haskell-src-exts`][haskell-src-exts].
29* Let some whitespace be programmable. The layout of the input influences
30  the layout choices in the output. This means that the choices between
31  single-line/multi-line layouts in certain situations are made by the user,
32  not by an algorithm. This makes the implementation simpler and leaves some
33  control to the user while still guaranteeing that the formatted code is
34  stylistically consistent.
35* Writing code in such a way so it's easy to modify and maintain.
36* Implementing one “true” formatting style which admits no configuration.
37* The formatting style aims to result in minimal diffs.
38* Choose a style compatible with modern dialects of Haskell. As new Haskell
39  extensions enter broad use, we may change the style to accommodate them.
40* Idempotence: formatting already formatted code doesn't change it.
41* Be well-tested and robust so that the formatter can be used in large
42  projects.
43
44Try it out in your browser at <https://ormolu-live.tweag.io>!
45See [Ormolu Live](#ormolu-live) for more info.
46
47## Installation
48
49The [release page][releases] has binaries for Linux, macOS and Windows.
50
51You can also install using `cabal` or `stack`:
52
53```console
54$ cabal install ormolu
55$ stack install ormolu
56```
57
58Ormolu is also included in several package repositories. E.g., on Arch Linux,
59one can use [the package on AUR][aur]:
60
61```console
62$ yay -S ormolu
63```
64
65## Building from source
66
67The easiest way to build the project is with Nix:
68
69```console
70$ nix-build -A ormolu
71```
72
73Note that you will need to add [IOHK Hydra binary
74cache][iohk-hydra-binary-cache], otherwise building may take a very long
75time.
76
77Alternatively, `stack` could be used as follows:
78
79```console
80$ stack build # to build
81$ stack install # to install
82```
83
84To use Ormolu directly from GitHub with Nix, this snippet may come in handy:
85
86```nix
87let
88  pkgs = import <nixpkgs> { };
89  source = pkgs.fetchFromGitHub {
90      owner = "tweag";
91      repo = "ormolu";
92      rev = "c1d8a8083cf1492545b8deed342c6399fe9873ea"; # update as necessary
93      # do not forget to update the hash:
94      sha256 = "sha256-3XxKuWqZnFa9s3mY7OBD+uEn/fGxPmC8jdevx7exy9o=";
95    };
96in (import source {  }).ormoluExe # this is e.g. the executable derivation
97```
98
99## Usage
100
101The following will print the formatted output to the standard output.
102
103```console
104$ ormolu Module.hs
105```
106
107Add `--mode inplace` to replace the contents of the input file with the
108formatted output.
109
110```console
111$ ormolu --mode inplace Module.hs
112```
113
114Use `find` to format a tree recursively:
115
116```console
117$ ormolu --mode inplace $(find . -name '*.hs')
118```
119
120Or find all files in a project with `git ls-files`:
121
122```console
123$ ormolu --mode inplace $(git ls-files '*.hs')
124```
125
126To check if files are are already formatted (useful on CI):
127
128```console
129$ ormolu --mode check $(find . -name '*.hs')
130```
131
132#### :zap: Beware git's `core.autocrlf` on Windows :zap:
133Ormolu's output always uses LF line endings. In particular,
134`ormolu --mode check` will fail if its input is correctly formatted
135*except* that it has CRLF line endings. This situation can happen on Windows
136when checking out a git repository without having set [`core.autocrlf`](
137https://www.git-scm.com/docs/git-config#Documentation/git-config.txt-coreautocrlf)
138to `false`.
139
140### Ormolu Live
141
142On every new commit to `master`, [Ormolu Live](./ormolu-live) is deployed to
143https://ormolu-live.tweag.io. Older versions are available at
144https://COMMITHASH--ormolu-live.netlify.app.
145
146### Editor integration
147
148We know of the following editor integrations:
149
150* [Emacs][emacs-package]
151* [VS Code][vs-code-plugin]
152* Vim: [neoformat][neoformat], [vim-ormolu][vim-ormolu]
153
154### Haskell Language Server
155
156[Haskell Language Server](https://haskell-language-server.readthedocs.io)
157has built-in support for using Ormolu as a formatter.
158
159### GitHub actions
160
161[`ormolu-action`][ormolu-action] is the recommended way to ensure that a
162project is formatted with Ormolu.
163
164### Magic comments
165
166Ormolu understands two magic comments:
167
168```haskell
169{- ORMOLU_DISABLE -}
170```
171
172and
173
174```haskell
175{- ORMOLU_ENABLE -}
176```
177
178This allows us to disable formatting selectively for code between these
179markers or disable it for the entire file. To achieve the latter, just put
180`{- ORMOLU_DISABLE -}` at the very top. Note that for Ormolu to work the
181fragments where Ormolu is enabled must be parseable on their own. Because of
182that the magic comments cannot be placed arbitrarily, but rather must
183enclose independent top-level definitions.
184
185### Account for .cabal files
186
187Many cabal and stack projects use `default-extensions` to enable GHC
188language extensions in all source files. With the
189`--cabal-default-extensions` flag, Ormolu will take them into consideration
190during formatting.
191
192When you format input from stdin, you can pass `--stdin-input-file` which
193will give Ormolu the location of the Haskell source file that should be used
194as the starting point for searching for a suitable .cabal file.
195
196### Exit codes
197
198Exit code | Meaning
199----------|-----------------------------------------------
2000         | Success
2011         | General problem
2022         | CPP used (deprecated)
2033         | Parsing of original input failed
2044         | Parsing of formatted code failed
2055         | AST of original and formatted code differs
2066         | Formatting is not idempotent
2077         | Unrecognized GHC options
2088         | Cabal file parsing failed
2099         | Missing input file path when using stdin input and accounting for .cabal files
210100       | In checking mode: unformatted files
211101       | Inplace mode does not work with stdin
212102       | Other issue (with multiple input files)
213
214## Limitations
215
216* CPP support is experimental. CPP is virtually impossible to handle
217  correctly, so we process them as a sort of unchangeable snippets. This
218  works only in simple cases when CPP conditionals surround top-level
219  declarations. See the [CPP][design-cpp] section in the design notes for a
220  discussion of the dangers.
221* Input modules should be parsable by Haddock, which is a bit stricter
222  criterion than just being valid Haskell modules.
223
224## Running on Hackage
225
226It's possible to try Ormolu on arbitrary packages from Hackage. For that
227execute (from the root of the cloned repo):
228
229```console
230$ nix-build -A hackage.<package>
231```
232
233Then inspect `result/log.txt` for possible problems. The derivation will
234also contain formatted `.hs` files for inspection and original inputs with
235`.hs-original` extension (those are with CPP dropped, exactly what is fed
236into Ormolu).
237
238## Contributing
239
240See [CONTRIBUTING.md][contributing].
241
242## License
243
244See [LICENSE.md][license].
245
246Copyright © 2018–present Tweag I/O
247
248[aur]: https://aur.archlinux.org/packages/ormolu
249[contributing]: https://github.com/tweag/ormolu/blob/master/CONTRIBUTING.md
250[design-cpp]: https://github.com/tweag/ormolu/blob/master/DESIGN.md#cpp
251[emacs-package]: https://github.com/vyorkin/ormolu.el
252[haskell-src-exts]: https://hackage.haskell.org/package/haskell-src-exts
253[iohk-hydra-binary-cache]: https://input-output-hk.github.io/haskell.nix/tutorials/getting-started/#setting-up-the-binary-cache
254[license]: https://github.com/tweag/ormolu/blob/master/LICENSE.md
255[neoformat]: https://github.com/sbdchd/neoformat
256[releases]: https://github.com/tweag/ormolu/releases
257[ormolu-action]: https://github.com/marketplace/actions/ormolu-action
258[vim-ormolu]: https://github.com/sdiehl/vim-ormolu
259[vs-code-plugin]: https://marketplace.visualstudio.com/items?itemName=sjurmillidahl.ormolu-vscode
260