• Stars
    star
    687
  • Rank 65,799 (Top 2 %)
  • Language
    Haskell
  • License
    GNU Affero Genera...
  • Created over 8 years ago
  • Updated about 2 years ago

Reviews

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

Repository Details

haskell source code formatter

brittany Hackage version Stackage version Build Status

⚠️ This project is effectively unmaintained! I (@tfausak) would recommend switching to another formatter. At time of writing (2022-11-11), I would suggest Ormolu. Or if you prefer some configuration, I would suggest Fourmolu.

haskell source code formatter

Output sample

(see more examples and comparisons)

This project's goals roughly are to:

  • Always retain the semantics of the source being transformed;
  • Be idempotent;
  • Support the full GHC-haskell syntax including syntactic extensions (but excluding -XCPP which is too hard);
  • Retain newlines and comments unmodified;
  • Be clever about using the available horizontal space while not overflowing the column maximum unless it cannot be avoided;
  • Be clever about aligning things horizontally (this can be turned off completely however);
  • Have linear complexity in the size of the input.

In theory, the core algorithm inside brittany reaches these goals. It is rather clever about making use of horizontal space while still being linear in the size of the input (although the constant factor is not small). See these examples of clever layouting.

But brittany is not finished yet, and there are some open issues that yet require fixing:

  • only the module header (imports/exports), type-signatures and function/value bindings are processed; other module elements (data-decls, classes, instances, etc.) are not transformed in any way; this extends to e.g. bindings inside class instance definitions - they won't be touched (yet).
  • By using ghc-exactprint as the parser, brittany supports full GHC including extensions, but some of the less common syntactic elements (even of 2010 haskell) are not handled.
  • There are some known issues regarding handling of in-source comments. There are cases where comments are not copied to the output (this will be detected and the user will get an error); there are other cases where comments are moved slightly; there are also cases where comments result in wonky newline insertion (although this should be a purely aesthetic issue.)

Try without Installing

You can paste haskell code over here to test how it gets formatted by brittany. (Rg. privacy: the server does log the size of the input, but not the full input/output of requests.)

Other usage notes

  • Supports GHC version 9.0.x.
  • included in stackage with lts>=10.0 (or nightlies dating to >=2017-11-15)
  • config (file) documentation is lacking.
  • some config values can not be configured via commandline yet.
  • uses/creates user config file in ~/.config/brittany/config.yaml; also reads (the first) brittany.yaml found in current or parent directories.

Installation

  • via stack

    stack install brittany # --resolver lts-16.31

    If you use an lts that includes brittany this should just work; otherwise you may want to clone the repo and try again.

  • via cabal

    Due to constant changes to the cabal UI, I have given up on making sure these instructions work before releases. Please do not expect these instructions to be up-to-date; they may produce incomprehensible error messages, they may be broken otherwise, they may work now but break with the next cabal release. Thanks for your understanding, and feel free to open issues for any problems you encounter. -- lennart

    If you are using cabal-3.0, using cabal install brittany --installdir=$HOME/.cabal/bin might work. Keep in mind that cabal merely puts a symlink to the "store" into the installdir, so you have to re-install if you ever clean your store. On cabal-2.4, try cabal v2-install brittany. On cabal-2.2 or earlier you might be succesful using cabal new-build exe:brittany; cp `find dist-newstyle/ -name brittany -type f | xargs -x ls -t | head -n1` $HOME/.cabal/bin/. Alternatively, you can also use the v1-approach with sandboxes as cabal v1-sandbox init; cabal v1-install brittany --bindir=$HOME/.cabal/bin.

    (TODO: These instructions are more confusing than helpful. I am inclined to just remove them.)

  • on ArchLinux:

    pacman -S haskell-brittany

Development tips

Editor Integration

Sublime text

In this gist I have described a haskell setup that includes a shortcut to run brittany formatting.

VSCode

This extension connects commandline brittany to VSCode formatting API. Thanks to @MaxGabriel.

Via HLS

haskell-language-server includes a brittany plugin that directly uses the brittany library. Relevant for any editors that properly support the language-server-protocol.

Neovim / Vim 8

The Neoformat plugin comes with support for brittany built in.

Atom

Atom Beautify supports brittany as a formatter for Haskell. Since the default formatter is set to hindent, you will need to change this setting to brittany, after installing the extension.

Emacs

format-all support brittany as the default formatter for Haskell.

Usage

  • Default mode of operation: Transform a single module, from stdin to stdout. Can pass one or multiple files as input, and there is a flag to override them in place instead of using stdout (since 0.9.0.0). So:

    brittany                           # stdin -> stdout
    brittany mysource.hs               # ./mysource.hs -> stdout
    brittany --write-mode=inplace *.hs # apply formatting to all ./*.hs inplace
  • For stdin/stdout usage it makes sense to enable certain syntactic extensions by default, i.e. to add something like this to your ~/.config/brittany/config.yaml (execute brittany once to create default):

    conf_forward:
      options_ghc:
      - -XLambdaCase
      - -XMultiWayIf
      - -XGADTs
      - -XPatternGuards
      - -XViewPatterns
      - -XRecursiveDo
      - -XTupleSections
      - -XExplicitForAll
      - -XImplicitParams
      - -XQuasiQuotes
      - -XTemplateHaskell
      - -XBangPatterns
    

Feature Requests, Contribution, Documentation

For a long time this project has had a single maintainer, and as a consequence there have been some mildly large delays for reacting to feature requests and even PRs.

Sorry about that.

The good news is that this project is getting sponsored by PRODA LTD, and two previous contributors, Evan Borden and Taylor Fausak, have agreed on helping with organisational aspects. Thanks!

Still, this project has a long queue of very sensible feature requests, so it may take some time until new ones get our attention. But with the help of the co-maintainers, at least the reaction-times on PRs and the frequency of releases should improve significantly.

If you are interested in making your own contributions, there is a good amount of high-level documentation at

the documentation index

License

Copyright (C) 2016-2019 Lennart Spitzner
Copyright (C) 2019 PRODA LTD

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License, version 3, as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see http://www.gnu.org/licenses/.

More Repositories

1

exference

Haskell tool to generate expressions from types
Haskell
101
star
2

butcher

haskell library to parse commandline args - an alternative to optparse-applicative
Haskell
19
star
3

multistate

haskell library for monad transformers, like mtl's ReaderT/StateT, but more than one contained value/type
Haskell
17
star
4

pqueue

Haskell priority queue package
Haskell
15
star
5

iridium

Automated Testing and Package Uploading
Haskell
12
star
6

pkgdbgc

ABANDONED garbage collection for haskell new-style global databases ("store")
Haskell
7
star
7

bricki-reflex

Reflex FRP interface for the `bricki` shallow fork of the `brick` commandline UI library
Haskell
6
star
8

data-tree-print

Haskell
4
star
9

cont-io-playground

playground for abstractions around the `ContT () IO` monad
Haskell
4
star
10

inotify-watch

little script for executing a command whenever inotify event CLOSE_WRITE fires (i.e. when you save file in editor)
Shell
4
star
11

bindynamic

Haskell library: A variation of Data.Dynamic.Dynamic with a Binary instance
Haskell
3
star
12

memorypool

Haskell
2
star
13

qualified-prelude

A Haskell Prelude providing qualified imports by using CPP
Haskell
2
star
14

czipwith

haskell library for CZipWith class
Haskell
2
star
15

frpcli

Haskell
2
star
16

cabal-exec-install

script for (automized) installation of haskell executables with cabal using sandboxes
Shell
2
star
17

exference-paper

2014 paper/report for the exference tool
Makefile
1
star
18

MIN_VERSION-testcase

temporary repository
Haskell
1
star
19

hasynt

Haskell
1
star
20

haskell-boa

A program to check and impose constraints on imports in Haskell modules.
Haskell
1
star
21

bricki-banana

reactive-banana FRP interface for the `bricki` shallow fork of the `brick` commandline UI library
Haskell
1
star