ghcide: The core of an IDE

[ apache, development, library ] [ Propose Tags ]

A library for building Haskell IDE's on top of the GHC API.


[Skip to Readme]

Flags

Manual Flags

NameDescriptionDefault
ghc-lib

build against ghc-lib instead of the ghc package

Disabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Versions [RSS] 0.0.1, 0.0.2, 0.0.3, 0.0.4, 0.0.5, 0.0.6, 0.1.0, 0.2.0, 0.3.0, 0.4.0, 0.5.0, 0.6.0, 0.6.0.1, 0.6.0.2, 0.7.0.0, 0.7.1.0, 0.7.2.0, 0.7.3.0, 0.7.4.0, 0.7.5.0, 1.0.0.0, 1.1.0.0, 1.2.0.0, 1.2.0.1, 1.2.0.2, 1.3.0.0, 1.4.0.1, 1.4.0.2, 1.4.0.3, 1.4.1.0, 1.4.2.0, 1.4.2.1, 1.4.2.2, 1.4.2.3, 1.5.0, 1.5.0.1, 1.6.0.0, 1.6.0.1, 1.7.0.0, 1.8.0.0, 1.9.0.0, 1.9.1.0, 1.10.0.0, 2.0.0.0, 2.0.0.1, 2.1.0.0, 2.2.0.0, 2.3.0.0, 2.4.0.0, 2.5.0.0, 2.6.0.0, 2.7.0.0 (info)
Change log CHANGELOG.md
Dependencies aeson, async, base (>=4 && <5), binary, bytestring, containers, data-default, deepseq, directory, extra, filepath, ghc (>=8.4), ghc-boot, ghc-boot-th, ghc-paths, ghcide, hashable, haskell-lsp (>=0.17 && <0.18), haskell-lsp-types (>=0.17 && <0.18), hie-bios (>=0.2 && <0.3), hslogger, mtl, network-uri, optparse-applicative, prettyprinter, prettyprinter-ansi-terminal, rope-utf16-splay, safe-exceptions, shake (>=0.17.5 && <0.18.4), sorted-list, stm, syb, text, time, transformers, unix, unordered-containers, utf8-string [details]
License Apache-2.0
Copyright Digital Asset 2018-2019
Author Digital Asset
Maintainer Digital Asset
Revised Revision 2 made by cocreature at 2019-12-18T10:16:34Z
Category Development
Home page https://github.com/digital-asset/ghcide#readme
Bug tracker https://github.com/digital-asset/ghcide/issues
Source repo head: git clone https://github.com/digital-asset/ghcide.git
Uploaded by cocreature at 2019-10-20T21:59:36Z
Distributions Arch:1.9.1.0, NixOS:2.6.0.0
Reverse Dependencies 39 direct, 0 indirect [details]
Executables ghcide
Downloads 21325 total (385 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2019-10-20 [all 1 reports]

Readme for ghcide-0.0.4

[back to package description]

ghcide - A library for building Haskell IDE tooling

Note: ghcide was previously called hie-core.

Our vision is that you should build an IDE by combining:

vscode

  • hie-bios for determining where your files are, what are their dependencies, what extensions are enabled and so on;
  • ghcide (i.e. this library) for defining how to type check, when to type check, and producing diagnostic messages;
  • A bunch of plugins that haven't yet been written, e.g. hie-hlint and hie-ormolu, to choose which features you want;
  • haskell-lsp for sending those messages to a Language Server Protocol (LSP) server;
  • An extension for your editor. We provide a VS Code extension as extension in this directory, although the components work in other LSP editors too (see below for instructions using Emacs).

There are more details about our approach in this blog post.

Features

ghcide already exports the following features via the lsp protocol:

Feature LSP name
Display error messages (parse errors, typecheck errors, etc.) and enabled warnings. diagnostics
Go to definition in local package definition
Display type and source module of values hover
Remove redundant imports, replace suggested typos for values and module imports, fill type holes, insert missing type signatures, add suggested ghc extensions codeAction (quickfix)
Organize imports codeAction (source.organizeImports)

Using it

Install ghcide

With Nix

See ghcide-nix repository

With Cabal or Stack

First install the ghcide binary using stack or cabal, e.g.

  1. git clone https://github.com/digital-asset/ghcide.git
  2. cd ghcide
  3. cabal install or stack install (and make sure ~/.local/bin is on your $PATH)

It's important that ghcide is compiled with the same compiler you use to build your projects.

Test ghcide

Next, check that ghcide is capable of loading your code. Change to the project directory and run ghcide, which will try and load everything using the same code as the IDE, but in a way that's much easier to understand. For example, taking the example of shake, running ghcide gives some error messages and warnings before reporting at the end:

Files that failed:
 * .\model\Main.hs
 * .\model\Model.hs
 * .\model\Test.hs
 * .\model\Util.hs
 * .\output\docs\Main.hs
 * .\output\docs\Part_Architecture_md.hs
Completed (152 worked, 6 failed)

Of the 158 files in Shake, as of this moment, 152 can be loaded by the IDE, but 6 can't (error messages for the reasons they can't be loaded are given earlier). The failing files are all prototype work or test output, meaning I can confidently use Shake.

The ghcide executable mostly relies on hie-bios to do the difficult work of setting up your GHC environment. If it doesn't work, see the hie-bios manual to get it working. My default fallback is to figure it out by hand and create a direct style hie.yaml listing the command line arguments to load the project.

If you can't get ghcide working outside the editor, see this setup troubleshooting guide. Once you have got ghcide working outside the editor, the next step is to pick which editor to integrate with.

Using with VS Code

You can install the VSCode extension from the VSCode marketplace.

Using with Emacs

If you don't already have MELPA package installation configured, visit MELPA getting started page to get set up. Then, install use-package.

Now you have a choice of two different Emacs packages which can be used to communicate with the ghcide LSP server:

  • lsp-ui
  • eglot

In each case, you can enable support by adding the shown lines to your .emacs:

lsp-ui

;; LSP
(use-package flycheck
  :ensure t
  :init
  (global-flycheck-mode t))
(use-package yasnippet
  :ensure t)
(use-package lsp-mode
  :ensure t
  :hook (haskell-mode . lsp)
  :commands lsp)
(use-package lsp-ui
  :ensure t
  :commands lsp-ui-mode)
(use-package lsp-haskell
 :ensure t
 :config
 (setq lsp-haskell-process-path-hie "ghcide")
 (setq lsp-haskell-process-args-hie '())
 ;; Comment/uncomment this line to see interactions between lsp client/server.
 ;;(setq lsp-log-io t)
)

eglot

(use-package eglot
  :ensure t
  :config
  (add-to-list 'eglot-server-programs '(haskell-mode . ("ghcide" "--lsp"))))

Using with Vim/Neovim

LanguageClient-neovim

Install LanguageClient-neovim

Add this to your vim config:

let g:LanguageClient_rootMarkers = ['*.cabal', 'stack.yaml']
let g:LanguageClient_serverCommands = {
    \ 'rust': ['rls'],
    \ 'haskell': ['ghcide', '--lsp'],
    \ }

Refer to :he LanguageClient for more details on usage and configuration.

vim-lsp

Install vim-lsp.

Add this to your vim config:

au User lsp_setup call lsp#register_server({
    \ 'name': 'ghcide',
    \ 'cmd': {server_info->['/your/path/to/ghcide', '--lsp']},
    \ 'whitelist': ['haskell'],
    \ })

To verify it works move your cursor over a symbol and run :LspHover.

coc.nvim

Install coc.nvim

Add this to your coc-settings.json (which you can edit with :CocConfig):

{
  "languageserver": {
    "haskell": {
      "command": "ghcide",
      "args": [
        "--lsp"
      ],
      "rootPatterns": [
        ".stack.yaml",
        ".hie-bios",
        "BUILD.bazel",
        "cabal.config",
        "package.yaml"
      ],
      "filetypes": [
        "hs",
        "lhs",
        "haskell"
      ]
    }
  }
}

Here's a nice article on setting up neovim and coc: Vim and Haskell in 2019

Hacking on ghcide

To build and work on ghcide itself, you can use Stack or cabal, e.g., running stack test will execute the test suite.

Building the extension

For development, you can also the VSCode extension from this repository (see https://code.visualstudio.com/docs/setup/mac for details on adding code to your $PATH):

  1. cd extension/
  2. npm ci
  3. npm run vscepackage
  4. code --install-extension ghcide-0.0.1.vsix

Now opening a .hs file should work with ghcide.

History and relationship to other Haskell IDE's

The code behind ghcide was originally developed by Digital Asset as part of the DAML programming language. DAML is a smart contract language targeting distributed-ledger runtimes, based on GHC with custom language extensions. The DAML programming language has an IDE, and work was done to separate off a reusable Haskell-only IDE (what is now ghcide) which the DAML IDE then builds upon. Since that time, there have been various non-Digital Asset contributors, in addition to continued investment by Digital Asset. All contributions require a Contributor License Agreement that states you license the code under the Apache License.

The Haskell community has various IDE choices, but the one that has been gathering momentum is haskell-ide-engine. Our project owes a debt of gratitude to the haskell-ide-engine. We reuse libraries from their ecosystem, including hie-bios (a likely future environment setup layer in haskell-ide-engine), haskell-lsp and lsp-test (the haskell-ide-engine LSP protocol pieces). We make heavy use of their contributions to GHC itself, in particular the work to make GHC take string buffers rather than files. While ghcide is not a part of haskell-ide-engine, we feel it could form the core of a future version - but such decisions are up to the haskell-ide-engine contributors.

The best summary of the architecture of ghcide is available this talk (slides), given at MuniHac 2019. However, since that talk the project has renamed from hie-core to ghcide, and the repo has moved to this location.