haskell source code formatter
 
 
Go to file
Lennart Spitzner 7fd2bef440
Merge pull request #273 from lspitzner/error-handling
Error handling
2020-01-23 01:17:07 +01:00
doc Add doc chapter on exactprinting, plus minor doc fixups 2019-11-18 11:26:18 +01:00
doc-svg-gen Fix cabal file for doc project 2018-11-30 22:13:02 +01:00
src/Language/Haskell Merge branch 'master' into error-handling 2020-01-22 22:58:40 +01:00
src-brittany Make unknown syntax errors non-fatal/Fall back on exactprint 2020-01-03 11:58:53 +01:00
src-idemtests Refactor all modules: +Internal; Add public Brittany module 2017-05-22 21:25:08 +02:00
src-libinterfacetests Refactor exposed function (now parsePrintModule) 2017-06-03 18:23:03 +02:00
src-literatetests Fix newline issue on comments before where 2020-01-11 23:19:04 +01:00
src-unittests Comment the single-line record decl config flag out 2019-12-09 11:22:43 -07:00
srcinc Remove dependency on either package 2017-11-27 18:11:49 +01:00
.gitignore Add a Makefile for easy testing many version 2019-12-09 11:17:31 -07:00
.hlint.yaml Add .hlint.yaml 2018-04-02 22:47:07 +01:00
.travis.yml Try fixing CI, ghc-8.8 seems to use more heap 2019-09-29 23:24:05 +02:00
ChangeLog.md Bump to 0.12.1.1; Update changelog 2019-12-09 13:38:26 +01:00
LICENSE Add license (AGPLv3) 2017-06-03 18:23:03 +02:00
Makefile Add a Makefile for easy testing many version 2019-12-09 11:17:31 -07:00
README.md Update cabal instructions 2019-09-02 13:58:35 +02:00
Setup.hs Squash previous history 2016-07-30 15:22:07 +02:00
brittany-sample.gif Add sample gif to README.md 2016-08-12 15:01:15 +02:00
brittany.cabal Merge pull request #259 from eborden/datadecl 2019-12-09 22:46:03 +01:00
default.nix Refactor nix expressions 2019-01-29 14:07:08 -08:00
pkgs.nix Add build instructions for nix 2019-01-26 10:55:29 -08:00
release.nix Refactor nix expressions 2019-01-29 14:07:08 -08:00
shell.nix Refactor nix expressions 2019-01-29 14:07:08 -08:00
stack-8.0.2.yaml Fix stack yamls for ghc-exactprint bounds 2018-09-13 22:47:25 +02:00
stack-8.0.2.yaml.lock Add stack lock file for 8.0.2 2019-12-09 11:17:31 -07:00
stack-8.2.2.yaml Fix stack yamls for ghc-exactprint bounds 2018-09-13 22:47:25 +02:00
stack-8.2.2.yaml.lock Include stack lock files 2019-12-09 11:17:31 -07:00
stack-8.4.3.yaml Add instance formatting for simple case 2018-10-10 14:32:58 -04:00
stack-8.4.3.yaml.lock Include stack lock files 2019-12-09 11:17:31 -07:00
stack-8.6.5.yaml Add stack configuration for GHC 8.6 2019-05-31 17:00:38 -04:00
stack-8.6.5.yaml.lock Include stack lock files 2019-12-09 11:17:31 -07:00
stack.yaml Update stack.yaml to allow compilation with ghc-8.8 2019-09-29 23:24:29 +02:00
stack.yaml.lock Include stack lock files 2019-12-09 11:17:31 -07:00

README.md

brittany Hackage version Stackage version Build Status

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 versions 8.0, 8.2, 8.4, 8.6.
  • 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-10.0
    

    If you use an lts that includes brittany this should just work; otherwise you may want to clone the repo and try again (there are several stack.yamls included).

  • via nix:

    nix build -f release.nix     # or 'nix-build -f release.nix'
    nix-env -i ./result
    
  • 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 via the brittany AUR package using aura:

    aura -A brittany
    

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 HIE

haskell-ide-engine 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/.