From 1dff057cb0f245c899f95fc97957364343fa2899 Mon Sep 17 00:00:00 2001 From: Thomas Letan Date: Mon, 1 May 2023 17:30:13 +0200 Subject: Blogpost and OCaml, tree-sitter and LSP in neovim --- .gitignore | 3 ++ site/img/good-highlighting.png | Bin 0 -> 54642 bytes site/img/wrong-highlighting.png | Bin 0 -> 54066 bytes site/posts/NeoVimOcamlInterfacesAndLSP.org | 52 +++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 site/img/good-highlighting.png create mode 100644 site/img/wrong-highlighting.png create mode 100644 site/posts/NeoVimOcamlInterfacesAndLSP.org diff --git a/.gitignore b/.gitignore index dabfd7b..015567f 100644 --- a/.gitignore +++ b/.gitignore @@ -46,6 +46,7 @@ logs/site--posts--index.html.stderr logs/site--posts--Ltac.html.stderr logs/site--posts--meta.html.stderr logs/site--posts--miscellaneous.html.stderr +logs/site--posts--NeoVimOcamlInterfacesAndLSP.html.stderr logs/site--posts--RankNTypesInOCaml.html.stderr logs/site--posts--StronglySpecifiedFunctions.html.stderr logs/site--posts--Thanks.html.stderr @@ -85,6 +86,7 @@ logs/site--posts--index.html.stdout logs/site--posts--Ltac.html.stdout logs/site--posts--meta.html.stdout logs/site--posts--miscellaneous.html.stdout +logs/site--posts--NeoVimOcamlInterfacesAndLSP.html.stdout logs/site--posts--RankNTypesInOCaml.html.stdout logs/site--posts--StronglySpecifiedFunctions.html.stdout logs/site--posts--Thanks.html.stdout @@ -149,6 +151,7 @@ site/posts/MixingLtacAndGallina.html site/posts/MixingLtacAndGallina.vo site/posts/MixingLtacAndGallina.vok site/posts/MixingLtacAndGallina.vos +site/posts/NeoVimOcamlInterfacesAndLSP.html site/posts/RankNTypesInOCaml.html site/posts/RewritingInCoq.glob site/posts/RewritingInCoq.html diff --git a/site/img/good-highlighting.png b/site/img/good-highlighting.png new file mode 100644 index 0000000..d05dbd5 Binary files /dev/null and b/site/img/good-highlighting.png differ diff --git a/site/img/wrong-highlighting.png b/site/img/wrong-highlighting.png new file mode 100644 index 0000000..3926ead Binary files /dev/null and b/site/img/wrong-highlighting.png differ diff --git a/site/posts/NeoVimOcamlInterfacesAndLSP.org b/site/posts/NeoVimOcamlInterfacesAndLSP.org new file mode 100644 index 0000000..a8fcc3f --- /dev/null +++ b/site/posts/NeoVimOcamlInterfacesAndLSP.org @@ -0,0 +1,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]] -- cgit v1.2.3