是否有Haskell代码格式化程序?

Len*_*low 42 haskell code-formatting

我曾经写过

data A = A {
      a :: Double
    }
    deriving(Eq, Show)
Run Code Online (Sandbox Code Playgroud)

但现在我更喜欢

data A = A {
      a :: Double
    } deriving(Eq, Show)
Run Code Online (Sandbox Code Playgroud)

我认为答案是否定的,但无论如何我都会问:Haskell是否有代码格式化程序?

Chr*_*one 54

新答案

我现在写的是hindent,它是用haskell-src- exts编写的.它具有Emacs和Vim支持.


老答案

哈斯克尔-SRC-EXTS将分析你的代码,它有专门用于打印的AST为一个字符串一个漂亮的打印模块.例如

import Language.Haskell.Exts

main = interact codeFormat

codeFormat = check . fmap reformat . parseModuleWithComments where
  reformat = prettyPrint
  check r = case r of
              ParseOk a -> a
              ParseFailed loc err -> error $ show (loc,err)
Run Code Online (Sandbox Code Playgroud)

例:

?> putStrLn $ codeFormat "module X where x = 1 where { y 1 = 2; y _ = 2 }"
module X where
x = 1
  where y 1 = 2
        y _ = 2
Run Code Online (Sandbox Code Playgroud)

或者你可以自己写一个漂亮的打印机(如果你只是想要专业化的话,基于上面的那个),然后你可以拥有你想要的任何风格.替换prettyPrint为您自己的.AST很直接.

然后,您可以将其与Emacs连接,以便在每次点击保存或其他内容时重新格式化.

  • 把它放在~./ ghci :: set prompt"λ>" (8认同)

use*_*558 11

时尚的haskell,可以做到你想要的.