hindent
Extensible Haskell pretty printer
https://github.com/mihaimaruseac/hindent
Version on this page: | 5.3.4 |
LTS Haskell 23.0: | 6.2.1 |
Stackage Nightly 2024-12-09: | 6.2.1 |
Latest on Hackage: | 6.2.1 |
hindent-5.3.4@sha256:dc781ea6e6166126404ad6ecefd81f4f0414a0a766c0f417514f299aca313ec4,3601
Module documentation for 5.3.4
hindent
Haskell pretty printer
Install
$ stack install hindent
Usage
$ hindent --help
hindent --version --help --style STYLE --line-length <...> --indent-size <...> --no-force-newline [-X<...>]* [<FILENAME>]
Version 5.1.1
Default --indent-size is 2. Specify --indent-size 4 if you prefer that.
-X to pass extensions e.g. -XMagicHash etc.
The --style option is now ignored, but preserved for backwards-compatibility.
Johan Tibell is the default and only style.
hindent is used in a pipeline style
$ cat path/to/sourcefile.hs | hindent
The default indentation size is 2
spaces. Configure indentation size with --indent-size
:
$ echo 'example = case x of Just p -> foo bar' | hindent --indent-size 2; echo
example =
case x of
Just p -> foo bar
$ echo 'example = case x of Just p -> foo bar' | hindent --indent-size 4; echo
example =
case x of
Just p -> foo bar
Customization
Create a .hindent.yaml
file in your project directory or in your
~/
home directory. The following fields are accepted and are the
default:
indent-size: 2
line-length: 80
force-trailing-newline: true
line-breaks: [":>", ":<|>"]
extensions:
- DataKinds
- GADTs
- TypeApplications
By default, hindent preserves the newline or lack of newline in your input. With force-trailing-newline
, it will make sure there is always a trailing newline.
hindent can be forced to insert a newline before specific operators and tokens with line-breaks
. This is especially useful when utilizing libraries like servant
which use long type aliases.
Using extensions
, hindent can be made aware of valid syntactic compiler extensions that would normally be considered invalid syntax.
It is also possible to specify which extensions HIndent runs
with in your .hindent.yaml
:
extensions:
- MagicHash
- RecursiveDo
Emacs
In
elisp/hindent.el
there is hindent-mode
, which provides keybindings to reindent parts of the
buffer:
M-q
reformats the current declaration. When inside a comment, it fills the current paragraph instead, like the standardM-q
.C-M-\
reformats the current region.
To enable it, add the following to your init file:
(add-to-list 'load-path "/path/to/hindent/elisp")
(require 'hindent)
(add-hook 'haskell-mode-hook #'hindent-mode)
Vim
The 'formatprg'
option lets you use an external program (like
hindent) to format your text. Put the following line into
~/.vim/ftplugin/haskell.vim to set this option for Haskell files:
setlocal formatprg=hindent
Then you can format with hindent using gq
. Read :help gq
and help 'formatprg'
for more details.
Note that unlike in emacs you have to take care of selecting a sensible buffer region as input to hindent yourself. If that is too much trouble you can try vim-textobj-haskell which provides a text object for top level bindings.
In order to format an entire source file execute:
:%!hindent
Alternatively you could use the vim-hindent plugin which runs hindent automatically when a Haskell file is saved.
Atom
Fortunately, you can use https://atom.io/packages/ide-haskell with the path to hindent specified instead of that to stylish-haskell. Works like a charm that way!
IntelliJ / other JetBrains IDEs
- Install the “HaskForce” Haskell plugin (this is so we get the language type recognized in the file watcher)
- Install the “File Watchers” plugin under “Browse Repositories”
- Add a File Watcher with
- File type: Haskell Language
- Program:
/path/to/hindent
- Arguments:
$FilePath$
- Immediate file synchronization: off
- Show console: Error
Now whenever you save a file, hindent
should autoformat it.
Changes
5.3.0: * Handle multiple deriving clauses in a DerivingStrategies scenario * Ignore non-files in findCabalFiles * Allow batch processing of multiple files * Prevent hindent from trying to open non-files when searching for .cabal files * Specify default extensions in configuration file * Fix bad output for [p|Foo|] pattern quasi-quotes * Parse C preprocessor line continuations * Fix pretty printing of ’(:) * Add parens around symbols (:|) when required * Support $p pattern splices * Fix associated type families * Non-dependent record constructor formatting
5.2.7: * Fix -X option bug
5.2.6: * Switch to optparse-applicative
5.2.5:
* Support get extensions from `.cabal` file
* Improve indention with record constructions and updates
* Fix `let ... in` bug
* Fix top-level lambda expressions in TemplateHaskell slices
* Update to haskell-src-exts dependency to version `>= 1.20.0`
5.2.4:
* Pretty print imports
* Fix pretty print for string literals for `DataKinds`
* Support `--validate` option for checking the format without reformatting
* Support parse `#include`, `#error`, `#warning` directives
* Support read `LANGUAGE` pragma and parse the declared extensions from source
* Treat `TypeApplications` extension as 'badExtensions' due to the `@` symbol
* Improve pretty print for unboxed tuples
* Fix many issues related to infix operators, includes TH name quotes,
`INLINE`/`NOINLINE` pragmas, infix type operator and infix constructor
* Fix pretty print for operators in `INLINE`/`NOINLINE` pragmas
* Support for `EmptyCases` extension
* Fix TH name quotes on operator names
* Optimize pretty print for many fundeps
* Fix extra linebreaks after short identifiers
5.2.3:
* Sort explicit import lists
* Report the `SrcLoc` when there's a parse error
* Improve long type signatures pretty printing
* Support custom line-break operators, add `--line-breaks` argument
* Fix infix data constructor
* Disable `RecursiveDo` and `DoRec` extensions by default
* Add RecStmt support
* Improve GADT records, data declaration records
* Complicated type alias and type signatures pretty printing
* Fix quasi-quoter names
5.2.2:
* Parallel list comprehensions
* Leave do, lambda, lambda-case on previous line of $
* Misc fixes
5.2.1:
* Fix hanging on large constraints
* Render multi-line comments
* Rename --tab-size to --indent-size
* Don't add a spurious space for comments at the end of the file
* Don't add trailing whitespace on <-
* Disable PatternSynonyms
* Put a newline before the closing bracket on a list
5.2.0:
* Default tab-width is now 2
* Supports .hindent.yaml file to specify alt tab-width and max
column
* Put last paren of export list on a new line
* Implement tab-size support in Emacs Lisp
5.1.1:
* Preserve spaces between groups of imports (fixes #200)
* Support shebangs (closes #208)
* Output filename for parse errors (fixes #179)
* Input with newline ends with newline (closes #211)
* Document -X (closes #212)
* Fix explicit forall in instances (closes #218)
* Put last paren of export list on a new line #227
5.1.0:
* Rewrote comment association, more reliable
* Added --tab-size flag for indentation spaces
* Fixed some miscellaneous bugs
5.0.1:
* Re-implement using bytestring instead of text
* Made compatible with GHC 7.8 through to GHC 8.0
* Added test suite and benchmarks in TESTS.md and BENCHMARKS.md
5.0.0:
* Drop support for styles
4.6.4
* Copy/delete file instead of renaming
4.4.6
* Fix whole module printer
* Accept a filename to reformat
4.4.5
* Fix bug in infix patterns
4.4.2
* Bunch of Gibiansky style fixes.
* Support CPP.
* Tibell style fixes.
4.3.8
* Fixed: bug in printing operators in statements.
4.5.4
* Improvements to Tibell style.
* 6x speed up on rendering operators.