用Sublime Text写Haskell

基础准备

安装插件

SublimeHaskell

在Package Control中安装SublimeHaskell。
插件安装完成后,重启Sublime报错,提示需要安装hsdev cabal package。

1
2
3
4
5
SublimeHaskell: hsdev executable couldn't be found!
It's used in most features of SublimeHaskell
Check if it's installed and in PATH
If it's not installed, run 'cabal install hsdev' to install hsdev
You may also want to adjust 'add_to_PATH' setting

用cabal安装之:

1
$ cabal install hsdev

然后是cabal运行过程中报错,提示缺少happy cabal package。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ cabal install hsdev
Resolving dependencies...
Configuring haskell-src-exts-1.17.1...
Failed to install haskell-src-exts-1.17.1
Build log ( /Users/mist/.cabal/logs/haskell-src-exts-1.17.1.log ):
cabal: Entering directory '/var/folders/4f/s20y12v152dgfl1rqbs3ztfc0000gn/T/cabal-tmp-245/haskell-src-exts-1.17.1'
Configuring haskell-src-exts-1.17.1...
cabal.real: The program 'happy' version >=1.19 is required but it could not be
found.
cabal: Leaving directory '/var/folders/4f/s20y12v152dgfl1rqbs3ztfc0000gn/T/cabal-tmp-245/haskell-src-exts-1.17.1'
cabal: Error: some packages failed to install:
haskell-src-exts-1.17.1 failed during the configure step. The exception was:
ExitFailure 1
hlint-1.9.35 depends on haskell-src-exts-1.17.1 which failed to install.
hsdev-0.2.0.0 depends on haskell-src-exts-1.17.1 which failed to install.

遂先装happy,再来装hsdev。
(PS: 这个hsdev包安装过程中的各种build超超超慢)

1
2
$ cabal install happy
$ cabal install hsdev

等了好久终于装好了,结果打开Sublime依然报错,还需要手动配置SublimeHaskell。
菜单栏选Preferences -> Package Settings -> SublimeHaskell -> Settings - User进行配置,文件内容如下:

1
2
3
4
5
6
7
8
9
{
"add_to_PATH":
[
"/Users/mist/Library/Haskell/bin"
],
"enable_ghc_mod": false,
"enable_hdevtools": false
}

注意:文件中的路径应当修改为你自己的路径,可以用cat ~/.cabal/where-is-my-stuff.txt查看。

再次重启sublime,这次没有报错。

SublimeREPL

在Package Control中安装SublimeREPL。
这个插件可以直接新建一个tab用来跑GHCi,配合View -> Layout -> Coloum: 2使用,方便开发与调试。

stylish-haskell

有待研究。

测试一下

自带Build System可以使用

Tools -> Build System -> Automatic 或 Haskell
直接运行当前文件。
sublime build system

SublimeHaskell常用命令

显示声明位置:f12
详细声明文档:ctrl+k ctrl+i
显示类型:ctrl+k ctrl+h ctrl+t
插入类型:ctrl+k ctrl+i ctrl+i
当然,也可以用command+shift+p调用这些命令。

SublimeREPL

不用额外配置,通过Tools -> SublimeREPL -> Haskell可以直接打开GHCi。

总结

生命不息,折腾不止,终于可以愉快地敲haskell啦~
主要的坑都在cabal上,cabal这个包管理比起 pip npm 啥的实在太弱了,连uninstall都没有!(敲cabal uninstall会告诉你以后会加上这个功能的。。。)

参考

  1. SublimeHaskell GitHub repo
  2. Configuring your Haskell environment
  3. Sublime Text & Haskell
  4. stylish-haskell GitHub repo