|
This page describes how to easily setup [Visual Studio Code](https://code.visualstudio.com/) (VSCode) for GHC development. This includes [Language Server Protocol](https://microsoft.github.io/language-server-protocol/) (LSP) support for Haskell and C files. The former is accomplished by using [`ghcide`](https://github.com/digital-asset/ghcide), the latter by [`ccls`](https://github.com/MaskRay/ccls).
|
|
## Haskell
|
|
|
|
|
|
The whole procedure is very similar to that described for [Spacemacs](spacemacs). If you miss somethings here, it might be on that other page. However, this page should be self-contained (with some links to other pages to reduce duplication in the wiki).
|
|
If all dependencies to build GHC (with Hadrian) are installed, the [Haskell plugin](https://marketplace.visualstudio.com/items?itemName=haskell.haskell) works out of the box. It installs the required [Haskell Language Server](https://github.com/haskell/haskell-language-server) automatically.
|
|
|
|
|
|
Some animations to demonstrate what you can expect:
|
|
## C
|
|
|
|
|
|
![Haskell - Hover](uploads/8d364182c4feec961cbbf0eeb7a4e2ca/peek_vscode_haskell_hover.gif)
|
|
I (Sven) got the best results with the [clangd plugin](https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd). It requires `clangd` to be installed and a `compile_commands.json` file to be in place.
|
|
|
|
|
|
![Haskell - Quickfix](uploads/49faab281021bcc257e9ba490df8cccc/peek_vscode_haskell_quickfix.gif)
|
|
`compile_commands.json` contains all build parameters for all C files. It can easily be generated with [bear](https://github.com/rizsotto/Bear). You just have to prefix your build command with `bear`, e.g.
|
|
|
|
|
|
![C - Hover & Error](uploads/160b19edec2d0b40789afd23f65e1cb4/peek_vscode_c_hover_error.gif)
|
|
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
**:warning:** Currently `ghcide` (ghc.nix argument: `withIde = true;`) is not supported by `ghc.nix`: https://github.com/alpmestan/ghc.nix#using-ghcide-%EF%B8%8F-currently-unavailable . Most of this page still applies, but you have to provide your own `ghcide` and `bear` binaries. `ghcide` must have been built with exactly the same GHC version you use to build your GHC project.
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
# Table of contents
|
|
|
|
[[_TOC_]]
|
|
|
|
|
|
|
|
# Install [`ghc.nix`](https://github.com/alpmestan/ghc.nix)
|
|
|
|
|
|
|
|
If you haven't already installed `ghc.nix`, please refer to the corresponding section on the [Spacemacs](spacemacs#ghcnix) page.
|
|
|
|
|
|
|
|
## Configure [`ghc.nix`](https://github.com/alpmestan/ghc.nix) with a `shell.nix` file
|
|
|
|
|
|
|
|
Create a `shell.nix` file your ghc source folder, that looks like this:
|
|
|
|
|
|
|
|
```nix
|
|
|
|
import ./ghc.nix/default.nix {
|
|
|
|
bootghc = "ghc865";
|
|
|
|
withIde = true;
|
|
|
|
withHadrianDeps = true;
|
|
|
|
cores = 8;
|
|
|
|
withDocs = false;
|
|
|
|
}
|
|
|
|
```
|
|
```
|
|
|
|
bear make [...]
|
|
For the context of this page, only the argument `withIde = true;` matters. It makes sure that the resulting Nix environments contain the right `ghcide` executable.
|
|
|
|
|
|
|
|
## Build GHC with `ghc.nix`
|
|
|
|
|
|
|
|
:exclamation: The first time you call `nix-shell` in the ghc source folder, all dependencies are downloaded and (if needed) built. Building `ghcide` takes much time. Consider to enable `cachix`, a binary cache for Nix packages: [spacemacs/cachix](spacemacs#cachix)
|
|
|
|
|
|
|
|
Some source code files are generated during build.
|
|
|
|
|
|
|
|
Build GHC in the context defined by `ghc.nix` and `shell.nix`, e.g.
|
|
|
|
```bash
|
|
|
|
nix-shell --command 'bear hadrian/build.sh -c -j12 --docs=none --flavour=Devel2'
|
|
|
|
```
|
|
```
|
|
|
|
|
|
### What is `bear`?
|
|
```
|
|
|
|
bear hadrian/build [...]
|
|
`ccls` needs a [`compile_commands.json`](https://clang.llvm.org/docs/JSONCompilationDatabase.html) (*JSON Compilcation Database*) file as configuration. This file is mostly a mapping of `*.c` filenames to C compiler arguments.
|
|
|
|
|
|
|
|
`compile_commands.json` is generated by [`bear`](https://github.com/rizsotto/Bear), a tool that intercepts all calls to the C compiler. That means, you have to trigger a full (re-)build with `bear`, even if you already got a fully compiled GHC project.
|
|
|
|
|
|
|
|
# Start and configure VSCode
|
|
|
|
## Start VSCode in a ghc.nix/nix-shell environment
|
|
|
|
|
|
|
|
```bash
|
|
|
|
nix-shell -p vscode ccls
|
|
|
|
```
|
|
```
|
|
|
|
|
|
## Install `ghcide` and `ccls` plugins
|
|
## Nix
|
|
Install the `ghcide` plugin in VSCode. It's listed as an extension in the VSCode Market Place: https://marketplace.visualstudio.com/items?itemName=DigitalAssetHoldingsLLC.ghcide
|
|
|
|
|
|
|
|
Some goes for the `ccls` plugin: https://marketplace.visualstudio.com/items?itemName=ccls-project.ccls
|
|
|
|
|
|
|
|
## Create workspaces
|
|
|
|
|
|
|
|
Create two distinct workspaces for the GHC compiler and Hadrian.
|
|
|
|
|
|
|
|
The simplest way is to:
|
|
|
|
- `File -> New Window`
|
|
|
|
- `File -> Open Folder...` (in the new window)
|
|
|
|
- `File -> Save Workspace As...` (in the new window)
|
|
|
|
|
|
|
|
The folder of your GHC compiler workspace is the source root of the cloned GHC repository. The Hadrian folder, is `hadrian/` in that previously described folder.
|
|
|
|
|
|
|
|
So, e.g. if your GHC source folder has the path `/home/sven/src/ghc`, then the two folders are:
|
|
|
|
- **`/home/sven/src/ghc`** for GHC
|
|
|
|
- **`/home/sven/src/ghc/hadrian`** for Hadrian
|
|
|
|
|
|
|
|
## Plugin configuration
|
|
|
|
|
|
|
|
### `ccls`
|
|
|
|
|
|
|
|
Works out of the box! It automatically finds the `compile_commands.json` file and uses it as configuration.
|
|
|
|
|
|
|
|
### `ghcide`
|
|
|
|
|
|
|
|
The are the arguments to the `ghcide` plugin: `Hic: Arguments` (`hic.arguments`) and `Hic: Executable Path` (`hic.executablePath`).
|
|
|
|
You have to set them per workspace, i.e. `hic.arguments` differs for the GHC compiler and the Hadrian workspace.
|
|
|
|
|
|
|
|
| Workspace | `hic.arguments` | `hic.executablePath` |
|
|
|
|
|--------------|------------------|----------------------|
|
|
|
|
| GHC Compiler | `--lsp compiler` | `ghcide` |
|
|
|
|
| Hadrian | `--lsp .` | `ghcide` |
|
|
|
|
|
|
|
|
![vscode_ghcide_config_compiler](uploads/444828858073add12000ae9f524eb041/vscode_ghcide_config_compiler.png)
|
|
|
|
|
|
|
|
# Troubleshooting
|
|
If you're using [ghc.nix](https://github.com/alpmestan/ghc.nix), you'll very likely want use it the provide the dependencies for the previously described plugins.
|
|
|
|
|
|
Please have a look at [spacemacs/Troubleshooting](spacemacs#troubleshooting). The first analysis steps are editor-agnostic. |
|
This can easily be done with the [Nix Environment Selector](https://marketplace.visualstudio.com/items?itemName=arrterian.nix-env-selector) plugin. |
|
\ No newline at end of file |
|
\ No newline at end of file |