blog-static/content/blog/haskell_language_server_again.md

4.6 KiB

title date tags
Using GHC IDE for Haskell Error Checking and Autocompletion 2020-01-06T17:07:25-08:00
Haskell
Language Server Protocol

Last year, when I took Oregon State University's CS 381 class, I ended up setting up my editor with the Haskell IDE engine. This made it possible to detect errors, view types, and have good autocompletion within the editor itself. Recently, I've found that GHC IDE works better for my projects, so instead of butchering the original article, I'll just quickly write an updated version here, referencing the old one when necessary.

By the end of the article, your editor should be able to detect errors and properly autocomplete Haskell code, somewhat like in the below screenshot:

Imgur

Downloading and Installing GHC IDE

GHC IDE is a Haskell-based program that uses the {{< sidenote "right" "lsp-note" "language server protocol" >}} You don't really need to know what the language server protocol (LSP) is to use GHC IDE. If you are nonetheless interested, I wrote a little bit about it <a href="{{< relref "/blog/haskell_language_server" >}}#prelude-language-server-protocol">in the previous iteration of this post. If you want more information, check out the official Microsoft page on LSP. {{< /sidenote >}} to communicate with any editor that supports it. Editors with support the the LSP include Atom, Visual Studio Code, Emacs, and Vim. Thus, You can get a good Haskell development environment without tying yourself to one application or service.

We first want to download the GHC IDE. To do this, you need to have Git installed. Once you have that, in your Git bash (on Windows) or in your terminal (maxOS, Linux), type the command:

git clone https://github.com/digital-asset/ghcide.git

To install GHC IDE, you can use either cabal (which is typically the cabal-install package, and is required normally for this class) or stack (a build tool). For cabal:

cabal install

And for stack:

stack install

This will create an executable in your ~/.local/bin directory. By default, this is not usable from other programs, such as Vim, so you should add this directory to your path. On Linux and macOS, this is done by adding the following line to your .bashrc file (or equivalent):

export PATH=$PATH:/home/<yourusername>/.local/bin

On Windows, this is done by {{< sidenote "right" "path-note" "editing your PATH variable." >}} If you need to know how to change your PATH, I wrote about it briefly in the <a href="{{< relref "/blog/haskell_language_server" >}} #installation-of-v0-5-0-0-windows-systems">previous iteration of this post. {{< /sidenote >}} I don't run Windows, so I don't know where cabal install will place the executable, but I do know where the executable will appear if you use stack install - in the directory given by:

stack path --local-bin

Adding that to your path should be sufficient to use GHC IDE.

Setting up Your Editor

This is where the paths diverge. I personally use (Neo)vim, but for the sake of completeness, I'll go over installation for Atom and VSCode (I'm not including Emacs because I know nothing about configuring Emacs).

Atom

There appears to be an Atom extension specifically for GHC IDE: ide-haskell-ghcide. It doesn't have a lot of configuration options, and will certainly require GHC IDE to be in your path. However, since both GHC IDE and the Haskell IDE engine use the Language Server Protocol, the more mature ide-haskell-hie extension may work, as well. In fact, since ide-haskell-ghcide is so young, I'd recommend trying ide-haskell-hie first, configuring the settings (found under Settings > Packages > (Search ide-haskell-hie) > Settings) to use the following full path:

<output of stack path --local-bin>/ghcide

VSCode

The team behind GHC IDE maintains an official VSCode extension found here. Installing it, when you have GHC IDE also installed, should be sufficient to get VSCode to autocomplete and error check.

(Neo)vim

My original recommendations for (neo)vim remain unchanged, with the exception of using ghcide instead of hie in the serverCommands variable. You can find the original instructions here.

Conclusion

I hope that using GHC IDE, you'll be able to have a significantly more pleasant Haskell experience in CS 381. Enjoy!