Skip to content

Instantly share code, notes, and snippets.

@arnarthor
Last active April 9, 2020 16:31
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnarthor/8e76a449c3db3c050b56700edc26deaa to your computer and use it in GitHub Desktop.
Save arnarthor/8e76a449c3db3c050b56700edc26deaa to your computer and use it in GitHub Desktop.

Reason

I have heard a lot of complaints about lack of editor integration or flaky editor tooling in the Reason community. At my previous job we had ~3000 modules and ~80k LOC. I'll admit that we were not very vigilant with .rei files or keeping our inter module dependencies really clean so this slowed both bsb and especially reason-language-server down a lot.

In fact so much that people couldn't use reason-language-server anymore on 15" MacBook Pros.

I myself have always used a Merlin based setup since I joined the Reason eco system before reason-language-server, and in our project my setup had never failed. Therefor I went on a quest of setting up 3 of the major editors (Emacs, Vim and VSCode) and documenting how to get them working with merlin to make sure that everyone could still work on our project despite it's size.

The editor setups listed below have all been tested in the last 6 months, but I am basing some of the instructions on old documentation that I had and my memory. So if something doesn't work listed here I might have forgotten some steps.

I do know that Emacs with use-package works from this document, since that is the setup that I use day to day.

Editor setup

Requirements

  • brew
  • opam - The OCaml Package Manager
    • brew install opam
    • opam init
  • Switch to correct OCaml compiler
    • opam switch create 4.06.1 (latest bucklescript uses 4.06, for Bucklescript <= 5.x use 4.02.3)
  • Install merlin - Context sensitive completion for OCaml in Vim and Emacs
    • opam install merlin
  • Install reason
    • opam install reason
  • Eval the opam setup
    • eval $(opam env)
    • For convenience put this into your .zshrc or .bashrc or .bashprofile based on what you use.

VSCode

Spacemacs

  • Install emacs-plus
  • Make sure to checkout develop branch of spacemacs
  • Install reason layer by adding (reasonml :variables reason-auto-refmt t) to dotspacemacs-configuration-layers

Emacs using use-package

  • Prevent backticks from being auto closed (useful for polymorphic variants since they don't have a closing backtick)

    • (eval-after-load 'smartparens
      '(progn
         (sp-pair "`" nil :actions :rem)))
  • Set project version of refmt (since opam version is not updated as frequently)

    • (defun shell-cmd (cmd)
        "Returns the stdout output of a shell command or nil if the command returned
      an error"
        (car (ignore-errors (apply 'process-lines (split-string cmd)))))
      
      (let* ((refmt-bin (or (shell-cmd "which bsrefmt")
                          (shell-cmd "which refmt"))))
      (when refmt-bin
        (setq refmt-command refmt-bin)))
  • Add merlin support

  (use-package merlin)
  • Add reason-mode with format on save
(use-package reason-mode
    :config
    (add-hook 'reason-mode-hook (lambda ()
				(add-hook 'before-save-hook 'refmt-before-save)
				(merlin-mode)))

Neovim

  • Install coc (instructions here)
  • Install ocaml-lsp or ocaml-language-server
  • Follow instructions here on how to configure your chosen language server.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment