• Stars
    star
    986
  • Rank 46,429 (Top 1.0 %)
  • Language
    Haskell
  • License
    Other
  • Created over 12 years ago
  • Updated 3 months ago

Reviews

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

Repository Details

Haskell code prettifier

stylish-haskell

Stack Build Status Cabal 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)
  • Format data constructors and fields in records.

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

Editor integration

Haskell Language Server

Haskell Language Server(HLS) includes a plugin for stylish-haskell. By changing the formatting provider option (haskell.formattingProvider) to stylish-haskell as described in HLS options, any editors that support Language Server Protocol can use stylish-haskell for formatting.

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/haskell/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
  • Beatrice Vergani
  • PaweΕ‚ Szulc
  • Łukasz GoΕ‚Δ™biewski
  • Felix Mulder

More Repositories

1

haskell-language-server

Official haskell ide support via language server (LSP). Successor of ghcide & haskell-ide-engine.
Haskell
2,709
star
2

haskell-ide-engine

The engine for haskell ide-integration. Not an IDE
Haskell
2,383
star
3

cabal

Official upstream development repository for Cabal and cabal-install
Haskell
1,623
star
4

haskell-mode

Emacs mode for Haskell
Emacs Lisp
1,329
star
5

aeson

A fast Haskell JSON library
Haskell
1,254
star
6

parsec

A monadic parser combinator library
Haskell
844
star
7

ghcide

A library for building Haskell IDE tooling
Haskell
584
star
8

vscode-haskell

VS Code extension for Haskell, powered by haskell-language-server
TypeScript
561
star
9

attoparsec

A fast Haskell library for parsing ByteStrings
Haskell
514
star
10

criterion

A powerful but simple library for measuring the performance of Haskell code.
Haskell
501
star
11

hackage-server

Hackage-Server: A Haskell Package Repository
Haskell
415
star
12

text

Haskell library for space- and time-efficient operations over Unicode text.
Haskell
406
star
13

haskell-platform

Distribution of Haskell with batteries included
Haskell
381
star
14

wreq

Haskell
379
star
15

lsp

Haskell library for the Microsoft Language Server Protocol
Haskell
366
star
16

mtl

The Monad Transformer Library
Haskell
366
star
17

vector

An efficient implementation of Int-indexed arrays (both mutable and immutable), with a powerful loop optimisation framework .
Haskell
363
star
18

haddock

Haskell Documentation Tool
HTML
361
star
19

network

Low-level networking interface
Haskell
326
star
20

containers

Assorted concrete container types
Haskell
315
star
21

statistics

A fast, high quality library for computing with statistics in Haskell.
Haskell
300
star
22

alex

A lexical analyser generator for Haskell
Haskell
298
star
23

bytestring

An efficient compact, immutable byte string type (both strict and lazy) suitable for binary or 8-bit character data.
Haskell
291
star
24

happy

The Happy parser generator for Haskell
Haskell
287
star
25

ghcup-hs

Haskell
284
star
26

ghcup

DEPRECATED IN FAVOR OF haskell/ghcup-hs
Shell
263
star
27

haskeline

A Haskell library for line input in command-line programs.
Haskell
222
star
28

c2hs

c2hs is a pre-processor for Haskell FFI bindings to C libraries
Haskell
198
star
29

fgl

A Functional Graph Library for Haskell
Haskell
184
star
30

hie-bios

Set up a GHC API session for various Haskell Projects
Haskell
180
star
31

HTTP

Haskell HTTP package
Haskell
177
star
32

ThreadScope

A graphical tool for profiling parallel Haskell programs
Haskell
153
star
33

actions

Github actions for Haskell CI
TypeScript
147
star
34

critbit

A Haskell implementation of crit-bit trees.
Haskell
138
star
35

play-haskell

Haskell Playground
Haskell
129
star
36

time

A time library
Haskell
119
star
37

primitive

This package provides various primitive memory-related operations.
Haskell
114
star
38

unix

POSIX functionality
Haskell
107
star
39

binary

Efficient, pure binary serialisation using ByteStrings in Haskell.
Haskell
106
star
40

rfcs

This repo is archived, consider using https://github.com/ghc-proposals/ghc-proposals instead
TeX
98
star
41

win32

Haskell support for the Win32 API
Haskell
98
star
42

stm

Software Transactional Memory
Haskell
97
star
43

core-libraries-committee

95
star
44

haskell-report

Haskell Language Report
TeX
91
star
45

parallel

a library for parallel programming
Haskell
91
star
46

process

Library for dealing with system processes
Haskell
87
star
47

hoopl

Higher-order optimization library
Haskell
73
star
48

error-messages

72
star
49

pretty

Haskell Pretty-printer library
Haskell
69
star
50

filepath

Haskell FilePath core library
Haskell
66
star
51

docker-haskell

Dockerfile
63
star
52

directory

Platform-independent library for basic file system operations
Haskell
58
star
53

hackage-security

Hackage security framework based on TUF (The Update Framework)
Haskell
56
star
54

mwc-random

A very fast Haskell library for generating high quality pseudo-random numbers.
Haskell
55
star
55

random

Random number library
Haskell
53
star
56

ecosystem-proposals

Proposals for the Haskell Ecosystem
51
star
57

text-icu

This package provides the Haskell Data.Text.ICU library, for performing complex manipulation of Unicode text.
Haskell
47
star
58

base64-bytestring

Fast base64 encoding and decoding for Haskell.
Haskell
45
star
59

security-advisories

Haskell
44
star
60

deepseq

Deep evaluation of data structures
Haskell
41
star
61

tar

Reading, writing and manipulating ".tar" archive files.
Haskell
40
star
62

math-functions

Special mathematical functions
Haskell
40
star
63

hsc2hs

Pre-processor for .hsc files
Haskell
38
star
64

pvp

Haskell Package Version Policy (PVP)
CSS
38
star
65

zlib

Compression and decompression in the gzip and zlib formats
C
35
star
66

ghc-events

Library and tool for parsing .eventlog files from GHC
Haskell
33
star
67

text-format

A Haskell text formatting library optimized for ease of use and high performance.
Haskell
32
star
68

ghcup-metadata

GHCup metadata repository
Haskell
32
star
69

cabal-userguide

A handy user guide for the Cabal build tool
Nix
28
star
70

base16-bytestring

Fast base16 (hexadecimal) encoding and decoding for Haskell bytestrings.
Haskell
27
star
71

network-uri

URI manipulation facilities
Haskell
25
star
72

entropy

Easy entropy source for Haskell users.
Haskell
23
star
73

winghci

Simple Windows GUI for GHCi.
C
17
star
74

double-conversion

A fast Haskell library for converting between double precision floating point numbers and text strings. It is implemented as a binding to the V8-derived C++ double-conversion library.
C++
15
star
75

file-io

File IO (read/write/open) for OsPath API
Haskell
11
star
76

terminfo

Haskell bindings to the terminfo API.
Haskell
10
star
77

array

Haskell
10
star
78

xhtml

XHTML combinator library
Haskell
9
star
79

old-time

This package provides the old time library.
Haskell
7
star
80

meta

A place for discussing & documenting the github.com/haskell organization
7
star
81

blog.haskell.org

Repository of the Haskell Blog
JavaScript
7
star
82

ghc-builder

ghc builder bot
Haskell
6
star
83

cabal-website

The http://www.haskell.org/cabal/ website
HTML
4
star
84

network-bsd

POSIX network database (<netdb.h>) API
Haskell
4
star
85

haskell-wiki-configuration

Issue tracking for Haskell Wiki
PHP
4
star
86

clc-stackage

Meta-package to facilitate impact assessment for CLC proposals
Nix
3
star
87

hiw

Haskell Implementors Workshop
TeX
3
star
88

text-test-data

Test data for the Haskell text project
Text
3
star
89

old-locale

This package provides the ability to adapt to locale conventions such as date and time formats.
Haskell
3
star
90

tokenize

Simple tokenizer for English text
Haskell
3
star
91

bzlib

Compression and decompression in the bzip2 format
Haskell
2
star
92

os-string

Haskell
2
star
93

hpc

1
star