stylish-haskell

Build Status

Introduction

A simple Haskell code prettifier. The goal is not to format all of the code in a file, since I find those kind of tools often "get in the way". However, manually cleaning up import statements etc. gets tedious very quickly.

This tool tries to help where necessary without getting in the way.

Installation

You can install it using stack install stylish-haskell or cabal install stylish-haskell.

You can also install it using your package manager: Debian 9 or later: apt-get install stylish-haskell Ubuntu 16.10 or later: apt-get install stylish-haskell * Arch Linux: pacman -S stylish-haskell

Features

  • Aligns and sorts import statements
  • Groups and wraps {-# LANGUAGE #-} pragmas, can remove (some) redundant pragmas
  • Removes trailing whitespace
  • Aligns branches in case and fields in records
  • Converts line endings (customizable)
  • Replaces tabs by four spaces (turned off by default)
  • Replaces some ASCII sequences by their Unicode equivalents (turned off by default)

Feature requests are welcome! Use the issue tracker for that.

Example

Turns:

{-# LANGUAGE ViewPatterns, TemplateHaskell #-}
{-# LANGUAGE GeneralizedNewtypeDeriving,
            ViewPatterns,
    ScopedTypeVariables #-}

module Bad where

import Control.Applicative ((<$>))
import System.Directory (doesFileExist)

import qualified Data.Map as M
import      Data.Map    ((!), keys, Map)

data Point = Point
    { pointX, pointY :: Double
    , pointName :: String
    } deriving (Show)

into:

{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE ScopedTypeVariables        #-}
{-# LANGUAGE TemplateHaskell            #-}

module Bad where

import           Control.Applicative ((<$>))
import           System.Directory    (doesFileExist)

import           Data.Map            (Map, keys, (!))
import qualified Data.Map            as M

data Point = Point
    { pointX, pointY :: Double
    , pointName      :: String
    } deriving (Show)

Configuration

The tool is customizable to some extent. It tries to find a config file in the following order:

  1. A file passed to the tool using the -c/--config argument
  2. .stylish-haskell.yaml in the current directory (useful for per-directory settings)
  3. .stylish-haskell.yaml in the nearest ancestor directory (useful for per-project settings)
  4. stylish-haskell/config.yaml in the platform’s configuration directory (on Windows, it is %APPDATA%, elsewhere it defaults to ~/.config and can be overridden by the XDG_CONFIG_HOME environment variable; useful for user-wide settings)
  5. .stylish-haskell.yaml in your home directory (useful for user-wide settings)
  6. The default settings.

Use stylish-haskell --defaults > .stylish-haskell.yaml to dump a well-documented default configuration to a file, this way you can get started quickly.

VIM integration

Since it works as a filter it is pretty easy to integrate this with VIM.

You can call

:%!stylish-haskell

and add a keybinding for it.

Or you can define formatprg

:set formatprg=stylish-haskell

and then use gq.

There is also the vim-stylish-haskell plugin, which runs stylish-haskell automatically when you save a Haskell file.

Emacs integration

haskell-mode for Emacs supports stylish-haskell. For configuration, see the “Using external formatters” section of the haskell-mode manual.

Atom integration

ide-haskell for Atom supports stylish-haskell.

atom-beautify for Atom supports Haskell using stylish-haskell.

Using with Continuous Integration

You can quickly grab the latest binary and run stylish-haskell like so:

curl -sL https://raw.github.com/jaspervdj/stylish-haskell/master/scripts/latest.sh | sh -s .

Where the . can be replaced with the arguments you pass to stylish-haskell.

Credits

Written and maintained by Jasper Van der Jeugt.

Contributors:

  • Chris Done
  • Hiromi Ishii
  • Leonid Onokhov
  • Michael Snoyman
  • Mikhail Glushenkov

Changes

# CHANGELOG

- 0.9.2.0 (2018-05-01)
* Support alignment of case expressions with a single guard
* Add a new step to squash multiple spaces between some elements (by Martin
Huschenbett)

- 0.9.1.1 (2018-04-26)
* Bump `aeson` to 1.3 for tests as well

- 0.9.1.0 (2018-04-26)
* Support GHC 8.4.1 by adding instance SemiGroup ImportPortion (by George
Wilson)
* Bump `aeson` to 1.3

- 0.9.0.2 (2018-01-03)
* Bump lower bound of `directory` to `1.2.3` for `getXdgDirectory`

- 0.9.0.1 (2017-12-29)
* Fix missing Extra-source-file in cabal file

- 0.9.0.0 (2017-12-26)
* Embed the default configuration
* Add platform-specific configuration paths (by Jan Tojnar)
* Bump `haskell-src-exts` to 0.20
* Avoid unpaired parenthesis when import doesn't specify any items (by
Matthew Kennerly)
* Remove shebang lines at the beginning of file (by Vaibhav Sagar)

- 0.8.1.0 (2017-06-19)
* Add `pad_module_names` option (by Yuriy Syrovetskiy)
* Add `space_surround` option to import styling (by Linus Arver)
* Bump `optparse-applicative` to 0.14

- 0.8.0.0
* Remove `MagicHash` from whitelisted language extensions, since it was
causing parsing errors (by Artyom Kazak)
* Don't leave a `#-}` hanging on the next line when `language_pragmas`
is set to `compact` and the `#-}` doesn't fit into character limit
(by Artyom Kazak)
* Deduplicate import specs (i.e. `import Foo (a, a, b)` becomes
`import Foo (a, b)`) (by Artyom Kazak)
* Take package imports into account when prettifying imports
(by Artyom Kazak)
* Bump `aeson` to 1.2
* Bump `syb` to 0.7
* Bump `HUnit` to 1.6

- 0.7.1.0
* Keep `safe` and `{-# SOURCE #-}` import annotations (by Moritz Drexl)

- 0.7.0.0
* If there's parse errors, show these and exit with code 1
* Bump `aeson` to 1.1
* Bump `directory` to 1.3
* Bump `haskell-src-exts` to 1.19

- 0.6.5.0
* Fix issue with unit records (by Mizunashi Mana)
* Bump `HUnit` to 1.5

- 0.6.4.0
* Remove `XmlSyntax` from whitelisted language extensions, since it was
causing parsing errors

- 0.6.3.0
* Bump `optparse-applicative` to 0.13.0.0
* Export Import options & add a default
* Add `list_padding: module_name` option (by Oleg Grenrus)
* Bump `aeson` to 1.0 (by Oleg Grenrus)
* Special setting for empty import lists (by Oleg Grenrus)

- 0.6.2.0
* Bump `haskell-src-exts` to 1.18

- 0.6.1.0
* Fix line patching issue in Editor

- 0.6.0.0
* Add a `simple_align` step
* Move `records` step into `simple_align`
* Use a set of default language extensions for parsing (by Langston Barrett)
* Add a newline format option (by Svyatolslav Gryaznov)
* Add more symbols from UnicodeSyntax (by Langston Barrett)
* Add a `--version` option (by Ondra Pelech)

- 0.5.17.0
* Remove shebang from input before attempting to extract pragmas
* Set stdin and stdout encoding to UTF-8 by default

- 0.5.16.0
* Fail if the default configuration file is not found.

- 0.5.15.2
* Bump `aeson` to 0.11

- 0.5.15.1
* Fix error that caused haddock to bail on this package

- 0.5.15.0
* Add new options for import list alignment (by Ondřej Janošík)

- 0.5.14.4
* Bump `stylish-haskell` to 1.17.0

- 0.5.14.3
* Bump `HUnit` to 1.13

- 0.5.14.2
* Bump `aeson` to 0.10
* Bump `syb` to 0.6

- 0.5.14.1
* Bump `aeson` to 0.9

- 0.5.14.0
* Bump `syb` to 0.5
* Slight refactoring in align code

- 0.5.13.0
* Fix issue with shebang code

- 0.5.12.0
* Add support for shebang at start of file

- 0.5.11.2
* Bump `filepath` dependency to 1.5

- 0.5.11.1
* Fix -Wall compilation with GHC 7.10

- 0.5.11.0
* Bump `haskell-src-exts` dependency to 1.16

- 0.5.10.2
* Bump `mtl` dependency to 2.2

- 0.5.10.1
* Bump `aeson` dependency to 0.8

- 0.5.10.0
* Bump `haskell-src-exts` dependency to 1.15
* Fix test which was not run before

- `0.5.9.0`
* Add `compact_line` setting for Language Pragma styling
comments powered byDisqus