MyNixOS website logo
Description

Haskell source code formatter.

The Haskell Formatter formats Haskell source code. It is strict in that it fundamentally rearranges code.

Haskell Formatter

Build License Package

This is deprecated.

The Haskell Formatter formats Haskell source code. It is strict in that it fundamentally rearranges code.

Deprecation notice

The Haskell Formatter is deprecated. If you think it should still be maintained, please let me know.

Take a look at the following projects, which aim at formatting Haskell code, too.

Personally, I like hindent, to which you can migrate as follows.

Use caseHaskell Formatterhindent
Format file in-placehaskell-formatter --force --input a.hs --output a.hshindent a.hs
Format multiple files in-placeNot supported out-of-the-boxhindent a.hs b.hs
Format stdin to stdouthaskell-formatterhindent
Order importsDone by defaulthindent --sort-imports …
Get helphaskell-formatter --helphindent --help

Installation

Install it by running

stack install haskell-formatter

or

cabal new-install haskell-formatter

You are ready when

haskell-formatter --help

works.

Usage

Basics

Read source code from Input.hs, format it, and write it to Output.hs by

haskell-formatter --input Input.hs --output Output.hs

If the input or output file is not given, it defaults to the corresponding standard stream. This allows commands like

haskell-formatter < Input.hs

To format a file in-place, use the --force option as in

# Warning: this overwrites the file `Code.hs`.
haskell-formatter --force --input Code.hs --output Code.hs

For more help about the usage, call

haskell-formatter --help

Formatting Many Files

For a diff of how code in the current folder would be formatted, without actually changing anything, run

find . -name '*.hs' -type f -print0 \
  | xargs -0 -n 1 bash -c 'haskell-formatter < "$@" | diff -u "$@" -' --

The returned exit status is nonzero if there are unformatted files. This may be useful for continuous integration.

To format any *.hs files in a folder code/ or (recursively) in its subfolders, run

# Warning: this overwrites files, so better back them up first.
find code/ -name '*.hs' -type f -print0 \
  | xargs -0 -I {} -n 1 haskell-formatter --force --input {} --output {}

Style Configuration

The formatting style can be configured with a file referred by the --style option. For instance, the call

haskell-formatter --style my_style.yaml --input Input.hs --output Output.hs

uses my_style.yaml as a style file. Such files are in the YAML format. The following is an example style file, which at the same time shows the available keys with their default values.

# Lines should be no longer than this length in characters.
line_length_limit: 80

# How much to spread code over multiple lines instead of trying to fill a single
# line. More precisely, this guides the ratio of "line_length_limit" to the
# ribbon length (the number of characters on a line without leading and trailing
# whitespace). Only the lowest value of 1 forces "line_length_limit" to be
# applied strictly.
# Reference: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.38.8777
ribbons_per_line: 1

# More than this number of empty lines in succession are merged.
successive_empty_lines_limit: 1

# Indentation lengths in characters.
indentations:
  class: 8 # "class" and "instance" declarations.
  do: 3 # "do" notation.
  case: 4 # Body of "case" expressions.
  let: 4 # Declarations in "let" expressions.
  where: 6 # Declarations in "where" clauses.
  onside: 2 # Continuation lines which would otherwise be offside.

# Decides which parts of the code to sort.
order:
  # Sequence of import declarations.
  import_declarations: true

  # Entities of import lists.
  import_entities: true
Metadata

Version

2.0.3

License

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows