summaryrefslogtreecommitdiffstats
path: root/site/posts/NeoVimOcamlInterfacesAndLSP.org
blob: a8fcc3f8b7e00bfe63a26c9f79a377f22c40b220 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#+TITLE: Neovim, OCaml Interfaces, Tree-Sitter and LSP

Can we all agree that witnessing syntax highlighting being absolutely off is
probably the most annoying thing that can happen to anybody?

I mean, just look at this horror.

#+CAPTION: Syntax highlighting being absolutely wrong
#+NAME:    fig:wrong-highlingting
[[../img/wrong-highlighting.png]]

What you are looking at is the result of trying to enable ~tree-sitter~ for
OCaml hacking and calling it a day. In a nutshell, OCaml ~mli~ files are
quickly turning into a random mess of nonsensical colors, and I didn’t know
why. I tried to blame
[[https://github.com/tree-sitter/tree-sitter-ocaml/issues/72][~tree-sitter-ocaml~]],
but of course I was wrong.

The issue is subtle, and to be honest, I don’t know if I totally grasp it. But
from my rough understanding, it breaks down as follows.

- ~tree-sitter-ocaml~ defines two grammars: ~ocaml~ for the ~ml~ files, and
  ~ocaml_interface~ (but ~ocamlinterface~ also works) for the ~mli~ files
- By default, neovim uses the filetype ~ocaml~ for ~mli~ files, so the incorrect
  parser is being used for syntax highlighting. This explains the root issue
- Bonus: ~ocamllsp~ does not recognize the ~ocamlinterface~ filetype by
  default (but somehow use the ~ocaml.interface~ id for ~mli~ files…[fn::There
  is probably something to be done here.])

So, in order to have both ~tree-sitter~ and ~ocamllsp~ working at the same time,
I had to tweak my configuration a little bit.

#+begin_src lua
lspconfig.ocamllsp.setup({
  cmd = { 'ocamllsp', '--fallback-read-dot-merlin' },
  -- Necessary to get tree sitter to work with .mli files
  filetypes = vim.list_extend(
    require('lspconfig.server_configurations.ocamllsp')
      .default_config
      .filetypes,
    { 'ocamlinterface' }
  ),
})

vim.cmd([[au! BufNewFile,BufRead *.mli setfiletype ocamlinterface]])
#+end_src

And now, I am blessed with a consistent syntax highlighting for my ~mli~ files.

#+CAPTION: Syntax highlighting being absolutely wrong
#+NAME:    fig:wrong-highlingting
[[../img/good-highlighting.png]]