stylish-haskell: Haskell code prettifier

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

A Haskell code prettifier. For more information, see:

https://github.com/jaspervdj/stylish-haskell/blob/master/README.markdown


[Skip to Readme]

Properties

Versions 0.1.0.0, 0.1.1.0, 0.2.0.0, 0.2.0.1, 0.2.0.2, 0.3.0.0, 0.3.1.0, 0.3.2.0, 0.4.0.0, 0.5.0.0, 0.5.0.1, 0.5.0.2, 0.5.1.0, 0.5.2.0, 0.5.3.0, 0.5.4.0, 0.5.5.0, 0.5.5.1, 0.5.5.2, 0.5.6.0, 0.5.6.1, 0.5.7.0, 0.5.8.0, 0.5.9.0, 0.5.9.1, 0.5.10.0, 0.5.10.1, 0.5.10.2, 0.5.11.0, 0.5.11.1, 0.5.11.2, 0.5.12.0, 0.5.13.0, 0.5.14.0, 0.5.14.1, 0.5.14.2, 0.5.14.3, 0.5.14.4, 0.5.15.0, 0.5.15.1, 0.5.15.2, 0.5.16.0, 0.5.17.0, 0.6.0.0, 0.6.1.0, 0.6.2.0, 0.6.3.0, 0.6.4.0, 0.6.5.0, 0.7.0.0, 0.7.1.0, 0.8.0.0, 0.8.1.0, 0.9.0.0, 0.9.0.1, 0.9.0.2, 0.9.1.0, 0.9.1.1, 0.9.2.0, 0.9.2.1, 0.9.2.2, 0.9.3.0, 0.9.4.0, 0.9.4.3, 0.9.4.4, 0.10.0.0, 0.11.0.0, 0.11.0.3, 0.12.0.0, 0.12.1.0, 0.12.2.0, 0.12.2.0, 0.13.0.0, 0.14.0.0, 0.14.0.1, 0.14.1.0, 0.14.2.0, 0.14.3.0, 0.14.4.0, 0.14.5.0, 0.14.6.0
Change log CHANGELOG
Dependencies aeson (>=0.6 && <1.6), base (>=4.8 && <5), bytestring (>=0.9 && <0.11), Cabal (>=2.4 && <3.3), containers (>=0.3 && <0.7), directory (>=1.2.3 && <1.4), file-embed (>=0.0.10 && <0.1), filepath (>=1.1 && <1.5), ghc-lib-parser (>=8.10 && <8.12), HsYAML (>=0.2.0 && <0.3), HsYAML-aeson (>=0.2.0 && <0.3), mtl (>=2.0 && <2.3), optparse-applicative (>=0.12 && <0.16), semigroups (>=0.18 && <0.20), strict (>=0.3 && <0.5), stylish-haskell, syb (>=0.3 && <0.8), text (>=1.2 && <1.3) [details]
License BSD-3-Clause
Copyright 2012 Jasper Van der Jeugt
Author Jasper Van der Jeugt <m@jaspervdj.be>
Maintainer Jasper Van der Jeugt <m@jaspervdj.be>
Category Language
Home page https://github.com/jaspervdj/stylish-haskell
Source repo head: git clone https://github.com/jaspervdj/stylish-haskell
Uploaded by JasperVanDerJeugt at 2020-10-08T12:49:06Z

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for stylish-haskell-0.12.2.0

[back to package description]

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:

Features

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.

Record formatting

Basically, stylish-haskell supports 4 different styles of records, controlled by records in the config file.

Here's an example of all four styles:

-- equals: "indent 2", "first_field": "indent 2"
data Foo a
  = Foo
      { a :: Int
      , a2 :: String
        -- ^ some haddock
      }
  | Bar
      { b :: a
      }
  deriving (Eq, Show)
  deriving (ToJSON) via Bar Foo

-- equals: "same_line", "first_field": "indent 2"
data Foo a = Foo
               { a :: Int
               , a2 :: String
                 -- ^ some haddock
               }
           | Bar
               { b :: a
               }
  deriving (Eq, Show)
  deriving (ToJSON) via Bar Foo

-- equals: "same_line", "first_field": "same_line"
data Foo a = Foo { a :: Int
                 , a2 :: String
                   -- ^ some haddock
                 }
           | Bar { b :: a
                 }
  deriving (Eq, Show)
  deriving (ToJSON) via Bar Foo

-- equals: "indent 2", first_field: "same_line"
data Foo a
  = Foo { a :: Int
        , a2 :: String
          -- ^ some haddock
        }
  | Bar { b :: a
        }
  deriving (Eq, Show)
  deriving (ToJSON) via Bar Foo

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.

Alternatively, [vim-autoformat] supports stylish-haskell. To have it automatically reformat the files on save, add to your vimrc:

autocmd BufWrite *.hs :Autoformat
" Don't automatically indent on save, since vim's autoindent for haskell is buggy
autocmd FileType haskell let b:autoformat_autoindent=0

There are also plugins that run 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.

Visual Studio Code integration

stylish-haskell-vscode for VSCode supports 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: