vim and ocaml (workaround for Dark)

Vim has always been hard to set up, and more so with OCaml.

Here, I'll show you a workaround on how to configure vim for it, and as a bonus, how to prepare it to contribute to Dark.

Lexicon

A New Beginning

For some reason, I had it working on my old laptop, but getting the exact same OCaml version and vimrc on my new one gets me a bunch of errors, hence the workaround: I am confident that I'll make it work someday, but for now we'll do a new vimrc and put back stuff from the old one.

  1. Back up your current vimrc and delete it
    $ mv ~/.vimrc ~/.vimrc.bak

  2. Install OPAM
    $ sh <(curl -sL https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh)>

  3. Init OPAM
    $ opam init

  4. (optional) Create a switch for the OCaml version used in Dark
    $ eval $(opam env)
    $ opam switch create 4.06.1
    $ eval $(opam env)

  5. (Mac only) Install XQuartz to have X11

  6. Install OCaml basic dependencies
    $ opam update
    $ opam upgrade
    $ opam install base utop
    $ opam install core async yojson core_extended core_bench cohttp-async async_graphics cryptokit menhir ocamlformat ocp-indent ocp-index user-setup
    $ opam install merlin

  7. (Workaround) If ocamlformat build crashed
    This happens with OCaml 4.06.1, install an older version
    $ opam install ocamlformat.0.12

  8. (For VSCode) Install ocaml-lsp
    $ opam pin add ocaml-lsp-server https://github.com/ocaml/ocaml-lsp.git
    $ opam install ocaml-lsp-server

  9. Now, let's create the vimrc with the user-setup tool
    $ opam user-setup install
    This creates a basic vimrc with tpope's vim-sensible defaults and the configuration for ocp-index, merlin and ocp-indent

  10. Add documentation to vim (run in vim)
    :execute "helptags " . substitute(system('opam config var share'),'\n$',',''') . "/merlin/vim/doc"

So now, basic autocompletion is done (ctrl+x ctrl+n in insert mode to try it).

Set Up Linting and Fixing

Install vim-plug

$ curl -fLo ~/.vim/autoload/plug.vim --create-dirs
  \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

Now in your vimrc, before " ## added by OPAM user-setup for vim / base ## 93ee63e278bdfc07d1139a748ed3fff2 ## you can edit, but keep this line:

call plug#begin('~/.vim/plugged')  
  Plug 'dense-analysis/ale'  
call plug#end()  

In vim, run :PlugInstall, and now you should get linting errors when you open a OCaml file.

Below this code, add

let g:ale_fixers = {
\   '*': ['remove_trailing_lines', 'trim_whitespace'],
\   'ocaml': ['ocamlformat', 'ocp-indent'],
\}

let g:ale_fix_on_save = 1