• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

mihaimaruseac/hindent: Haskell pretty printer

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

mihaimaruseac/hindent

开源软件地址(OpenSource Url):

https://github.com/mihaimaruseac/hindent

开源编程语言(OpenSource Language):

Haskell 87.4%

开源软件介绍(OpenSource Introduction):

hindent Hackage CI (Stack) CI (Cabal)

Haskell pretty printer

Examples

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 standard M-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

  1. Install the "HaskForce" Haskell plugin (this is so we get the language type recognized in the file watcher)
  2. Install the "File Watchers" plugin under "Browse Repositories"
  3. Add a File Watcher with
    1. File type: Haskell Language
    2. Program: /path/to/hindent
    3. Arguments: $FilePath$
    4. Immediate file synchronization: off
    5. Show console: Error

Now whenever you save a file, hindent should autoformat it.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap